Using (array() in custom conditional functions - php

How can I create an array of multiple endpoint for the below custom conditional code?
function is_single_with_endpoint( $endpoint ) {
global $wp_query;
if ( ! isset( $endpoint ) && ! empty( $endpoint ) ) {
return false;
}
if ( array_key_exists( $endpoint, $wp_query->query_vars ) ) {
return true;
} else {
return false;
}
}
This will work is I use
if ( ! is_single_with_endpoint('overview') && ! is_single_with_endpoint('analytics') ) {
but not as
if ( ! is_single_with_endpoint(array('overview','analytics')) ){

We can just step through the array of inputs and check each one for a failure. If none of them produce failure, then it passes at the end.
function is_single_with_endpoint($endpoints) {
global $wp_query;
// Iterate through set (can be set of one)
foreach ((array)$endpoints AS $endpoint) {
if (empty($endpoint)) {
return false;
}
if (!array_key_exists($endpoint, $wp_query->query_vars)) {
return false;
}
}
// Didn't fail, so it passes
return true;
}

I would do this, instead:
function is_single_with_endpoint($endpoint = null){
global $wp_query;
if($endpoint === null){
return false;
}
if(is_array($endpoint)){
foreach($endpoint as $k => $v){
if(!array_key_exists($k, $wp_query->query_vars)){
return false;
}
}
}
else{
if(!array_key_exists($endpoint)){
return false;
}
}
return true;
}

Related

DOMNode::isEqualNode(): Not yet implemented

I made this function in php to check if a parent XML has already got a xml to insert it or not :
function has_xml_node( \DOMElement $needle, \DOMElement $node ) {
if( $node->hasChildNodes() ){
foreach ( $node->childNodes as $childNode ) {
if( $needle->isEqualNode( $childNode ) ){
return true;
}
}
}
return false;
}
I got this php error :
DOMNode::isEqualNode(): Not yet implemented in ...
How can I fix this ?
I found the solution here.
Use $needle->isSameNode( $childNode );
BUT it seems to check 'baseURI' as well so if you have two same tags but from different xml files, $needle->isSameNode( $childNode ); return false.
I made a function to work around this problem.
function is_same_node( \DOMNode $node_to_compare, \DOMNode $node_refered, $with_attributes = true ) {
if( $node_to_compare->tagName !== $node_refered->tagName )
return false;
if( $node_to_compare->nodeValue !== $node_refered->nodeValue )
return false;
if( $node_to_compare->prefix !== $node_refered->prefix )
return false;
if( $with_attributes ){
if( !$node_to_compare->hasAttributes() && $node_refered->hasAttributes() ){
return false;
}else{
if( !$node_refered->hasAttributes() && $node_to_compare->hasAttributes() ){
return false;
}else{
foreach ( $node_to_compare->attributes as $attribute ){
$find_attribute_with_same_value = false;
foreach ( $node_refered->attributes as $attribute_refered ){
if(
$attribute_refered->name === $attribute->name
&& ( $attribute_refered->value === $attribute->value )
){
$find_attribute_with_same_value = true;
}
}
if( !$find_attribute_with_same_value )
return false;
}
}
}
}
return true;
}

Pre-populate Gravity Form with multiple values from ACF

Here is a simple example of what I am trying to achieve:
I have a gravity form with different sections that will be shown conditionally by pre-populating dynamically. The thing is that I can't seem to populate them based on my ACF data (which are checkboxes as well).
If I put the values into the code it works like this:
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
if( 'mychoice' === $choice['value'] || 'anotherchoice' === $choice['value'] ) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}
To get it to be populated dynamically I was trying something like this which didn't work (not that good with php):
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
$addons = get_field('addons');
if( $addons === $choice['value'] ) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}
It's not working and I know I am missing something here but can't figure out what it is:/ Any help or pointers would be highly appreciated! I tried keeping it precise but if any more information is required please let me know and I will update the post accordingly.
Figured it out with some help:) In case anybody needs this functionality, here is how it works:
add_filter( 'gform_pre_render_2', 'my_populate_checkbox' );
function my_populate_checkbox( $form ) {
global $post;
$adfields = get_field( 'addons', get_the_ID() );
foreach( $form['fields'] as &$field ) {
if( 11 === $field->id ) {
foreach( $field->choices as &$choice ) {
if( in_array( $choice['value'] ,$adfields )) {
$choice['isSelected'] = true;
}
}
}
}
return $form;
}

Order properties should not be accessed directly - WooCommerce 3.0

I've just upgraded my local WooCommerce website to 3.0. Everything works perfectly as normal, but I've noticed with debugging turned on that I'm getting hundreds of the following notices:
[05-Apr-2017 12:25:00 UTC] PHP Notice: id was called <strong>incorrectly</strong>. Order properties should not be accessed directly. Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in C:\xampp\htdocs\dev\wp-includes\functions.php on line 4137
So it looks like WooCommerce are pulling back being able to directly call order data. One example this code is being triggered by is this function in my functions.php file:
function eden_woocommerce_order_number($original, $order)
{
return 'EDN-' . str_pad($order->id, 10, 0, STR_PAD_LEFT);
}
This function simply adds "EDN" to the start of the order ID and pads it by 10 characters, but WooCommerce doesn't like how I'm calling $order - what would be the best way to rewrite such a function that 3.0 is happy with?
it says "id was called incorrectly. Order properties should not be accessed directly."
Try $order->get_id()
Maybe its helpful for others too. Here's the some stuff regarding to all the functions of directly accessed values through the magic function.
This function is from Woocommerce 3.0
if ( 'completed_date' === $key ) {
return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
} elseif ( 'paid_date' === $key ) {
return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
} elseif ( 'modified_date' === $key ) {
return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
} elseif ( 'order_date' === $key ) {
return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
} elseif ( 'id' === $key ) {
return $this->get_id();
} elseif ( 'post' === $key ) {
return get_post( $this->get_id() );
} elseif ( 'status' === $key ) {
return $this->get_status();
} elseif ( 'post_status' === $key ) {
return get_post_status( $this->get_id() );
} elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
return $this->get_customer_note();
} elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
return $this->get_customer_id();
} elseif ( 'tax_display_cart' === $key ) {
return get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_totals_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'display_cart_ex_tax' === $key ) {
return 'excl' === get_option( 'woocommerce_tax_display_cart' );
} elseif ( 'cart_discount' === $key ) {
return $this->get_total_discount();
} elseif ( 'cart_discount_tax' === $key ) {
return $this->get_discount_tax();
} elseif ( 'order_tax' === $key ) {
return $this->get_cart_tax();
} elseif ( 'order_shipping_tax' === $key ) {
return $this->get_shipping_tax();
} elseif ( 'order_shipping' === $key ) {
return $this->get_shipping_total();
} elseif ( 'order_total' === $key ) {
return $this->get_total();
} elseif ( 'order_type' === $key ) {
return $this->get_type();
} elseif ( 'order_currency' === $key ) {
return $this->get_currency();
} elseif ( 'order_version' === $key ) {
return $this->get_version();
} elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
return $this->{"get_{$key}"}();
} else {
return get_post_meta( $this->get_id(), '_' . $key, true );
}
You should call the woo get function. add get_ ()
For example, change:
$order->status to $order->get_status()

Redux WP Framework. Cannot disable 'dev_mode' with if statment

I have a problem.
I want to disable 'dev_mode' if 'opt_name' not redux_demo, using the if statment, but it did not work ... where does the fault ??
I found the code on Here
and I turn it into like this
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
if ( $redux->args['dev_mode'] == true ) {
$redux->args['dev_mode'] = false;
}
} else {
if ( $redux->args['dev_mode'] == false ) {
$redux->args['dev_mode'] = true;
}
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
the above code, I input in config.php
Thanks B4 and sorry my english is not good. :D
You can also filter the args JUST for your opt_name using this:
apply_filters( "redux/args/{$this->args['opt_name']}", $this->args );
if ( ! function_exists( 'redux_disable_dev_mode_plugin' ) ) {
function redux_disable_dev_mode_plugin( $redux ) {
if ( $redux->args['opt_name'] != 'redux_demo' ) {
$redux->args['dev_mode'] = false;
}
}
add_action( 'redux/construct', 'redux_disable_dev_mode_plugin' );
}
In framework.php (for me it's from line 1281). It worked for me after making those two attr false.
// Force dev_mode on WP_DEBUG = true and if it's a local server
if ( Redux_Helpers::isLocalHost() || ( Redux_Helpers::isWpDebug() ) ) {
if ( $this->args['dev_mode'] != true ) {
$this->args['update_notice'] = false;
}
$this->dev_mode_forced = true; // make it false
$this->args['dev_mode'] = true; //make it false
}

CakePHP not completely decrypting virtual field

I'm running into an issue in my CakePHP (2.4.1) app where the virtual field in one of my models is not properly being decrypted using Cake's Security::rijndael(). In my User model, the display field is defined as
public $virtualFields = array( 'name' => 'CONCAT(User.first_name, " ", User.last_name)' );
Both User.first_name and User.last_name are encrypted, and User.name is the displayField for the model. In the AssignedShift related model (User hasMany AssignedShift), the User.name field is not being properly decrypted. Here is an example return:
Array(
[1] => Test|�d�F�3��������������S0�Dy=>�dJ���sP�4�F�n�؄s-#���7P���n�ʙ.�C�#���˷C�z��(;Eu)�
)
This is the only model that this happens on, so I'm pretty confident my afterFind method is working properly. In any case, below are the functions for encrypt/decrypt:
/*AppModel.php*/
function _afterFind($results, $primary) {
if( $primary ) {
foreach( $results as $key => $val) {
if( isset( $val[$this->alias] ) ) {
$results[$key][$this->alias] = $this->doAfterFind( $results[$key][$this->alias] );
}
}
} else {
if( isset( $results['id']) ) {
$results = $this->doAfterFind($results);
} else {
foreach( $results as $key => $val ) {
if( isset( $val[$this->alias] ) ) {
if( isset( $val[$this->alias]['id'] ) ) {
$results[$key][$this->alias] = $this->doAfterFind( $results[$key][$this->alias] );
} else {
foreach( $results[$key][$this->alias] as $key2 => $val2 ) {
$results[$key][$this->alias][$key2] = $this->doAfterFind( $results[$key][$this->alias][$key2] );
}
}
}
}
}
}
return $results;
}
public function doAfterFind($data) {
foreach( $data as $key => $val) {
if( !empty( $val ) && strlen( $val ) >= 88) {
$data[$key] = Security::rijndael( base64_decode( $val ), Configure::read( 'Security.cipherSeed' ), 'decrypt' );
}
}
//My attempt at hacking it back together
if(array_key_exists('first_name', $data)) {
$data['name'] = $data['first_name'] ." ". $data['last_name'];
}
return $data;
}
/*AssignedShift.php*/
public function afterFind($results, $primary = false) {
return $this->_afterFind($results, $primary);
}
Both models were created from the console - User has been modified but I haven't touched the relationships, and AssignedShift is completely untouched. Any ideas? Thanks in advance.

Categories