Esempio n. 1
0
/**
 * Condition function to evaluate and display given block based on expressions
 * 'args' => arguments for evaluation fields
 * 
 * Supported actions and symbols:
 * 
 * Integer and floating-point numbers
 * Math operators: +, -, *, /
 * Comparison operators: <, >, =, <=, >=, !=
 * Boolean operators: AND, OR, NOT
 * Nested expressions - several levels of brackets
 * Variables defined as shortcode parameters starting with a dollar sign
 * empty() function that checks for blank or non-existing fields
 * 
 * 
 * @note As of 1.9, this is not used in Views anymore, seems to be used on the toolset-forms library
 */
function wpv_condition($atts, $post_to_check = null)
{
    extract(shortcode_atts(array('evaluate' => FALSE), $atts));
    // Do not overwrite global post
    //    global $post;
    // if in admin, get the post from the URL
    if (is_admin()) {
        if (empty($post_to_check->ID)) {
            // Get post
            if (isset($_GET['post'])) {
                $post_id = (int) $_GET['post'];
            } else {
                if (isset($_POST['post_ID'])) {
                    $post_id = (int) $_POST['post_ID'];
                } else {
                    $post_id = 0;
                }
            }
            if ($post_id) {
                $post = get_post($post_id);
            }
        } else {
            $post = $post_to_check;
        }
    }
    if (empty($post->ID)) {
        global $post;
    }
    $has_post = true;
    if (empty($post->ID)) {
        // Will not execute any condition that involves custom fields
        $has_post = false;
    }
    global $wplogger;
    if ($has_post) {
        do_action('wpv_condition', $post);
    }
    $logging_string = "Original expression: " . $evaluate;
    add_filter('wpv-extra-condition-filters', 'wpv_add_time_functions');
    $evaluate = apply_filters('wpv-extra-condition-filters', $evaluate);
    $logging_string .= "; After extra conditions: " . $evaluate;
    // evaluate empty() statements for variables
    if ($has_post) {
        $empties = preg_match_all("/empty\\(\\s*\\\$(\\w+)\\s*\\)/", $evaluate, $matches);
        if ($empties && $empties > 0) {
            for ($i = 0; $i < $empties; $i++) {
                $match_var = get_post_meta($post->ID, $atts[$matches[1][$i]], true);
                $is_empty = '1=0';
                // mark as empty only nulls and ""
                //            if ( is_null( $match_var ) || strlen( $match_var ) == 0 ) {
                if (is_null($match_var) || is_string($match_var) && strlen($match_var) == 0 || is_array($match_var) && empty($match_var)) {
                    $is_empty = '1=1';
                }
                $evaluate = str_replace($matches[0][$i], $is_empty, $evaluate);
                $logging_string .= "; After empty: " . $evaluate;
            }
        }
    }
    // find variables that are to be used as strings.
    // eg '$f1'
    // will replace $f1 with the actual field value
    if ($has_post) {
        $strings_count = preg_match_all('/(\'[\\$\\w^\']*\')/', $evaluate, $matches);
        if ($strings_count && $strings_count > 0) {
            for ($i = 0; $i < $strings_count; $i++) {
                $string = $matches[1][$i];
                // remove single quotes from string literals to get value only
                $string = strpos($string, '\'') === 0 ? substr($string, 1, strlen($string) - 2) : $string;
                if (strpos($string, '$') === 0) {
                    $variable_name = substr($string, 1);
                    // omit dollar sign
                    if (isset($atts[$variable_name])) {
                        $string = get_post_meta($post->ID, $atts[$variable_name], true);
                        $evaluate = str_replace($matches[1][$i], "'" . $string . "'", $evaluate);
                        $logging_string .= "; After variables I: " . $evaluate;
                    }
                }
            }
        }
    }
    // find string variables and evaluate
    $strings_count = preg_match_all('/((\\$\\w+)|(\'[^\']*\'))\\s*([\\!<>\\=]+)\\s*((\\$\\w+)|(\'[^\']*\'))/', $evaluate, $matches);
    // get all string comparisons - with variables and/or literals
    if ($strings_count && $strings_count > 0) {
        for ($i = 0; $i < $strings_count; $i++) {
            // get both sides and sign
            $first_string = $matches[1][$i];
            $second_string = $matches[5][$i];
            $math_sign = $matches[4][$i];
            // remove single quotes from string literals to get value only
            $first_string = strpos($first_string, '\'') === 0 ? substr($first_string, 1, strlen($first_string) - 2) : $first_string;
            $second_string = strpos($second_string, '\'') === 0 ? substr($second_string, 1, strlen($second_string) - 2) : $second_string;
            // replace variables with text representation
            if (strpos($first_string, '$') === 0 && $has_post) {
                $variable_name = substr($first_string, 1);
                // omit dollar sign
                if (isset($atts[$variable_name])) {
                    $first_string = get_post_meta($post->ID, $atts[$variable_name], true);
                } else {
                    $first_string = '';
                }
            }
            if (strpos($second_string, '$') === 0 && $has_post) {
                $variable_name = substr($second_string, 1);
                if (isset($atts[$variable_name])) {
                    $second_string = get_post_meta($post->ID, $atts[$variable_name], true);
                } else {
                    $second_string = '';
                }
            }
            // don't do string comparison if variables are numbers
            if (!(is_numeric($first_string) && is_numeric($second_string))) {
                // compare string and return true or false
                $compared_str_result = wpv_compare_strings($first_string, $second_string, $math_sign);
                if ($compared_str_result) {
                    $evaluate = str_replace($matches[0][$i], '1=1', $evaluate);
                } else {
                    $evaluate = str_replace($matches[0][$i], '1=0', $evaluate);
                }
            } else {
                $evaluate = str_replace($matches[1][$i], $first_string, $evaluate);
                $evaluate = str_replace($matches[5][$i], $second_string, $evaluate);
            }
            $logging_string .= "; After variables II: " . $evaluate;
        }
    }
    // find remaining strings that maybe numeric values.
    // This handles 1='1'
    $strings_count = preg_match_all('/(\'[^\']*\')/', $evaluate, $matches);
    if ($strings_count && $strings_count > 0) {
        for ($i = 0; $i < $strings_count; $i++) {
            $string = $matches[1][$i];
            // remove single quotes from string literals to get value only
            $string = strpos($string, '\'') === 0 ? substr($string, 1, strlen($string) - 2) : $string;
            if (is_numeric($string)) {
                $evaluate = str_replace($matches[1][$i], $string, $evaluate);
                $logging_string .= "; After variables III: " . $evaluate;
            }
        }
    }
    // find all variable placeholders in expression
    if ($has_post) {
        $count = preg_match_all('/\\$(\\w+)/', $evaluate, $matches);
        $logging_string .= "; Variable placeholders: " . var_export($matches[1], true);
        // replace all variables with their values listed as shortcode parameters
        if ($count && $count > 0) {
            // sort array by length desc, fix str_replace incorrect replacement
            $matches[1] = wpv_sort_matches_by_length($matches[1]);
            foreach ($matches[1] as $match) {
                if (isset($atts[$match])) {
                    $meta = get_post_meta($post->ID, $atts[$match], true);
                    if (empty($meta)) {
                        $meta = "0";
                    }
                } else {
                    $meta = "0";
                }
                $evaluate = str_replace('$' . $match, $meta, $evaluate);
                $logging_string .= "; After variables IV: " . $evaluate;
            }
        }
    }
    $logging_string .= "; End evaluated expression: " . $evaluate;
    $wplogger->log($logging_string, WPLOG_DEBUG);
    // evaluate the prepared expression using the custom eval script
    $result = wpv_evaluate_expression($evaluate);
    if ($has_post) {
        do_action('wpv_condition_end', $post);
    }
    // return true, false or error string to the conditional caller
    return $result;
}
Esempio n. 2
0
/**
 * Condition function to evaluate and display given block based on expressions
 * 'args' => arguments for evaluation fields
 * 
 * Supported actions and symbols:
 * 
 * Integer and floating-point numbers
 * Math operators: +, -, *, /
 * Comparison operators: &lt;, &gt;, =, &lt;=, &gt;=, !=
 * Boolean operators: AND, OR, NOT
 * Nested expressions - several levels of brackets
 * Variables defined as shortcode parameters starting with a dollar sign
 * empty() function that checks for blank or non-existing fields
 * 
 * 
 */
function wpv_condition($atts)
{
    extract(shortcode_atts(array('evaluate' => FALSE), $atts));
    global $post;
    // if in admin, get the post from the URL
    if (is_admin()) {
        // Get post
        if (isset($_GET['post'])) {
            $post_id = (int) $_GET['post'];
        } else {
            if (isset($_POST['post_ID'])) {
                $post_id = (int) $_POST['post_ID'];
            } else {
                $post_id = 0;
            }
        }
        if ($post_id) {
            $post = get_post($post_id);
        }
    }
    global $wplogger;
    $logging_string = "Original expression: " . $evaluate;
    // evaluate empty() statements for variables
    $empties = preg_match_all("/empty\\(\\s*\\\$(\\w+)\\s*\\)/", $evaluate, $matches);
    if ($empties && $empties > 0) {
        for ($i = 0; $i < $empties; $i++) {
            $match_var = get_post_meta($post->ID, $atts[$matches[1][$i]], true);
            $is_empty = '1=0';
            // mark as empty only nulls and ""
            if (is_null($match_var) || strlen($match_var) == 0) {
                $is_empty = '1=1';
            }
            $evaluate = str_replace($matches[0][$i], $is_empty, $evaluate);
        }
    }
    // find string variables and evaluate
    $strings_count = preg_match_all('/((\\$\\w+)|(\'[^\']*\'))\\s*([\\!<>\\=]+)\\s*((\\$\\w+)|(\'[^\']*\'))/', $evaluate, $matches);
    // get all string comparisons - with variables and/or literals
    if ($strings_count && $strings_count > 0) {
        for ($i = 0; $i < $strings_count; $i++) {
            // get both sides and sign
            $first_string = $matches[1][$i];
            $second_string = $matches[5][$i];
            $math_sign = $matches[4][$i];
            // replace variables with text representation
            if (strpos($first_string, '$') === 0) {
                $variable_name = substr($first_string, 1);
                // omit dollar sign
                $first_string = get_post_meta($post->ID, $atts[$variable_name], true);
            }
            if (strpos($second_string, '$') === 0) {
                $variable_name = substr($second_string, 1);
                $second_string = get_post_meta($post->ID, $atts[$variable_name], true);
            }
            // remove single quotes from string literals to get value only
            $first_string = strpos($first_string, '\'') === 0 ? substr($first_string, 1, strlen($first_string) - 2) : $first_string;
            $second_string = strpos($second_string, '\'') === 0 ? substr($second_string, 1, strlen($second_string) - 2) : $second_string;
            // don't do string comparison if variables are numbers
            if (!(is_numeric($first_string) && is_numeric($second_string))) {
                // compare string and return true or false
                $compared_str_result = wpv_compare_strings($first_string, $second_string, $math_sign);
                if ($compared_str_result) {
                    $evaluate = str_replace($matches[0][$i], '1=1', $evaluate);
                } else {
                    $evaluate = str_replace($matches[0][$i], '1=0', $evaluate);
                }
            }
        }
    }
    // find all variable placeholders in expression
    $count = preg_match_all('/\\$(\\w+)/', $evaluate, $matches);
    $logging_string .= "; Variable placeholders: " . var_export($matches[1], true);
    // replace all variables with their values listed as shortcode parameters
    if ($count && $count > 0) {
        // sort array by length desc, fix str_replace incorrect replacement
        wpv_sort_matches_by_length(&$matches[1]);
        foreach ($matches[1] as $match) {
            $meta = get_post_meta($post->ID, $atts[$match], true);
            if (empty($meta)) {
                $meta = "0";
            }
            $evaluate = str_replace('$' . $match, $meta, $evaluate);
        }
    }
    $logging_string .= "; End evaluated expression: " . $evaluate;
    $wplogger->log($logging_string, WPLOG_DEBUG);
    // evaluate the prepared expression using the custom eval script
    $result = wpv_evaluate_expression($evaluate);
    // return true, false or error string to the conditional caller
    return $result;
}
 /**
  * Condition function to evaluate and display given block based on expressions
  * 
  * Supported actions and symbols:
  * 
  * Integer and floating-point numbers
  * Math operators: +, -, *, /
  * Comparison operators: &lt;, &gt;, =, &lt;=, &gt;=, !=
  * Boolean operators: AND, OR, NOT
  * Nested expressions - several levels of brackets
  * Variables defined as shortcode parameters starting with a dollar sign
  * empty() function that checks for blank or non-existing fields
  * 
  * @global type $wplogger
  * @param type $evaluate
  * @param type $fields
  * @return type
  */
 protected function _wpv_condition($evaluate, $fields)
 {
     global $wplogger;
     $logging_string = "Original expression: " . $evaluate;
     add_filter('wpv-extra-condition-filters', 'wpv_add_time_functions');
     $evaluate = apply_filters('wpv-extra-condition-filters', $evaluate);
     // evaluate empty() statements for variables
     //        $empties = preg_match_all( "/empty\(\s*\\$(\w+)\s*\)/", $evaluate,
     //                $matches );
     $empties = preg_match_all("/empty\\(\\s*\\\$([\\w_-]+)\\s*\\)/", $evaluate, $matches);
     if ($empties && $empties > 0) {
         for ($i = 0; $i < $empties; $i++) {
             $match_var = $this->_getValue($fields[$matches[1][$i]]);
             $is_empty = '1=0';
             // mark as empty only nulls and ""
             //            if ( is_null( $match_var ) || strlen( $match_var ) == 0 ) {
             if (is_null($match_var) || is_string($match_var) && strlen($match_var) == 0 || is_array($match_var) && empty($match_var)) {
                 $is_empty = '1=1';
             }
             $evaluate = str_replace($matches[0][$i], $is_empty, $evaluate);
         }
     }
     // find variables that are to be used as strings.
     // eg '$f1'
     // will replace $f1 with the actual field value
     $strings_count = preg_match_all('/(\'[\\$\\w^\']*\')/', $evaluate, $matches);
     if ($strings_count && $strings_count > 0) {
         for ($i = 0; $i < $strings_count; $i++) {
             $string = $matches[1][$i];
             // remove single quotes from string literals to get value only
             $string = strpos($string, '\'') === 0 ? substr($string, 1, strlen($string) - 2) : $string;
             if (strpos($string, '$') === 0) {
                 $variable_name = substr($string, 1);
                 // omit dollar sign
                 if (isset($fields[$variable_name])) {
                     $string = $this->_getValue($fields[$variable_name]);
                     $evaluate = str_replace($matches[1][$i], "'" . $string . "'", $evaluate);
                 }
             }
         }
     }
     // find string variables and evaluate
     //        $strings_count = preg_match_all( '/((\$\w+)|(\'[^\']*\'))\s*([\!<>\=]+)\s*((\$\w+)|(\'[^\']*\'))/',
     //                $evaluate, $matches );
     $strings_count = preg_match_all('/((\\$[\\w_-]+)|([\'\\d][^\']*[\'\\d]))\\s*([\\!<>\\=]+)\\s*((\\$[\\w_-]+)|([\'\\d][^\']*[\'\\d]))/', $evaluate, $matches);
     // get all string comparisons - with variables and/or literals
     if ($strings_count && $strings_count > 0) {
         for ($i = 0; $i < $strings_count; $i++) {
             // get both sides and sign
             $first_string = $matches[1][$i];
             $second_string = $matches[5][$i];
             $math_sign = $matches[4][$i];
             // remove single quotes from string literals to get value only
             $first_string = strpos($first_string, '\'') === 0 ? substr($first_string, 1, strlen($first_string) - 2) : $first_string;
             $second_string = strpos($second_string, '\'') === 0 ? substr($second_string, 1, strlen($second_string) - 2) : $second_string;
             // replace variables with text representation
             if (strpos($first_string, '$') === 0) {
                 $variable_name = substr($first_string, 1);
                 // omit dollar sign
                 if (isset($fields[$variable_name])) {
                     $first_string = $this->_getValue($fields[$variable_name]);
                 } else {
                     $first_string = '';
                 }
             }
             if (strpos($second_string, '$') === 0) {
                 $variable_name = substr($second_string, 1);
                 if (isset($fields[$variable_name])) {
                     $second_string = $this->_getValue($fields[$variable_name]);
                 } else {
                     $second_string = '';
                 }
             }
             // don't do string comparison if variables are numbers
             if (is_numeric($first_string) && !is_numeric($second_string) || !is_numeric($first_string) && is_numeric($second_string)) {
                 $evaluate = str_replace($matches[0][$i], '1=0', $evaluate);
             } else {
                 if (!(is_numeric($first_string) && is_numeric($second_string))) {
                     // compare string and return true or false
                     $compared_str_result = wpv_compare_strings($first_string, $second_string, $math_sign);
                     if ($compared_str_result) {
                         $evaluate = str_replace($matches[0][$i], '1=1', $evaluate);
                     } else {
                         $evaluate = str_replace($matches[0][$i], '1=0', $evaluate);
                     }
                 } else {
                     $evaluate = str_replace($matches[1][$i], $first_string, $evaluate);
                     $evaluate = str_replace($matches[5][$i], $second_string, $evaluate);
                 }
             }
         }
     }
     // find remaining strings that maybe numeric values.
     // This handles 1='1'
     $strings_count = preg_match_all('/(\'[^\']*\')/', $evaluate, $matches);
     if ($strings_count && $strings_count > 0) {
         for ($i = 0; $i < $strings_count; $i++) {
             $string = $matches[1][$i];
             // remove single quotes from string literals to get value only
             $string = strpos($string, '\'') === 0 ? substr($string, 1, strlen($string) - 2) : $string;
             if (is_numeric($string)) {
                 $evaluate = str_replace($matches[1][$i], $string, $evaluate);
             }
         }
     }
     // find all variable placeholders in expression
     //        $count = preg_match_all( '/\$(\w+)/', $evaluate, $matches );
     $count = preg_match_all('/\\$([\\w-_]+)/', $evaluate, $matches);
     $logging_string .= "; Variable placeholders: " . var_export($matches[1], true);
     // replace all variables with their values listed as shortcode parameters
     if ($count && $count > 0) {
         // sort array by length desc, fix str_replace incorrect replacement
         $matches[1] = wpv_sort_matches_by_length($matches[1]);
         foreach ($matches[1] as $match) {
             if (isset($fields[$match])) {
                 $meta = $this->_getValue($fields[$match]);
                 if (empty($meta)) {
                     $meta = "0";
                 }
             } else {
                 $meta = "0";
             }
             $evaluate = str_replace('$' . $match, $meta, $evaluate);
         }
     }
     $logging_string .= "; End evaluated expression: " . $evaluate;
     $wplogger->log($logging_string, WPLOG_DEBUG);
     //         evaluate the prepared expression using the custom eval script
     $result = wpv_evaluate_expression($evaluate);
     // return true, false or error string to the conditional caller
     return $result;
 }
Esempio n. 4
0
/**
 * Condition function to evaluate and display given block based on expressions
 * 'args' => arguments for evaluation fields
 * 
 * Supported actions and symbols:
 * 
 * Integer and floating-point numbers
 * Math operators: +, -, *, /
 * Comparison operators: &lt;, &gt;, =, &lt;=, &gt;=, !=
 * Boolean operators: AND, OR, NOT
 * Nested expressions - several levels of brackets
 * Variables defined as shortcode parameters starting with a dollar sign
 * empty() function that checks for blank or non-existing fields
 * 
 * 
 */
function wpv_condition_manage_and_evaluate( $atts, $post_to_check = null ) {
	extract(
        shortcode_atts( 
			array(
				'evaluate' => false, 
				'debug' => false, 
				'condition' => true
			), 
			$atts
		)
    );
	
	$condition = ( $condition == 'true' || $condition === TRUE ) ? true : false;

    // Do not overwrite global post
//    global $post;

    // if in admin, get the post from the URL
    if ( is_admin() ) {
        if ( empty( $post_to_check->ID ) ) {
            // Get post
            if ( isset( $_GET['post'] ) ) {
                $post_id = (int) $_GET['post'];
            } else if ( isset( $_POST['post_ID'] ) ) {
                $post_id = (int) $_POST['post_ID'];
            } else {
                $post_id = 0;
            }
            if ( $post_id ) {
                $post = get_post( $post_id );
            }
        } else {
            $post = $post_to_check;
        }
    }
    if ( empty( $post->ID ) ) {
        global $post;
    }
	$has_post = true;
    if ( empty( $post->ID ) ) {
        // Will not execute any condition that involves custom fields
        $has_post = false;
    }

    global $wplogger;
    
	if ( $has_post ) {
		do_action( 'wpv_condition', $post );
	}

	$logging_string = "####################\nwpv-if attributes\n####################\n" 
		. print_r( $atts, true ) 
		. "\n####################\nDebug information\n####################"
		. "\n--------------------\nOriginal expression: " 
		. $evaluate 
		. "\n--------------------";
		
	$evaluate = str_replace( " NEQ ", " != ", $evaluate );
	$evaluate = str_replace( " neq ", " != ", $evaluate );
	$evaluate = str_replace( " EQ ", " = ", $evaluate );
	$evaluate = str_replace( " eq ", " = ", $evaluate );
	$evaluate = str_replace( " NE ", " != ", $evaluate );
	$evaluate = str_replace( " ne ", " != ", $evaluate );
	
	$evaluate = str_replace( " LT ", " < ", $evaluate );
	$evaluate = str_replace( " lt ", " < ", $evaluate );
	$evaluate = str_replace( " LTE ", " <= ", $evaluate );
	$evaluate = str_replace( " lte ", " <= ", $evaluate );
	$evaluate = str_replace( " GT ", " > ", $evaluate );
	$evaluate = str_replace( " gt ", " > ", $evaluate );
	$evaluate = str_replace( " GTE ", " >= ", $evaluate );
	$evaluate = str_replace( " gte ", " >= ", $evaluate );

    $evaluate = apply_filters( 'wpv-extra-condition-filters', $evaluate );
	
	$logging_string .= "\nAfter expanding custom functions and date expressions: " . $evaluate;

    // Evaluate empty($field) where $field is a custom field key
	if ( $has_post ) {
		$empties = preg_match_all( "/empty\(\s*\\$(\w+)\s*\)/", $evaluate, $matches );
		if ( 
			$empties 
			&& $empties > 0 
		) {
			for ( $i = 0; $i < $empties; $i++ ) {
				$is_empty = '1=0';
				$is_empty_logging_extra = '';
				if ( isset( $atts[$matches[1][$i]] ) ) {
					$match_var = get_post_meta( $post->ID, $atts[$matches[1][$i]], true );
					if ( 
						is_null( $match_var )
						|| ( 
							is_string( $match_var ) 
							&& strlen( $match_var ) == 0 
						)
						|| ( 
							is_array( $match_var ) 
							&& empty( $match_var ) 
						) 
					) {
						$is_empty = '1=1';
						$is_empty_logging_extra = "\n\tField " . $atts[$matches[1][$i]] . " is empty";
					} else {
						$is_empty_logging_extra = "\n\tField " . $atts[$matches[1][$i]] . " is not empty";
					}
				} else {
					$is_empty_logging_extra = "\n\tERROR: Key '" . $matches[1][$i] . "' does not point to a valid attribute in the wpv-if shortcode";
				}
				$evaluate = str_replace( $matches[0][$i], $is_empty, $evaluate );
				$logging_string .= "\nAfter checking " . ( $i + 1 ) . " empty statements: " . $evaluate . $is_empty_logging_extra;
			}
		}
	}
    
    // Evaluate quoted variables that are to be used as strings
    // '$f1' will replace $f1 with the custom field value
	if ( $has_post ) {
		$strings_count = preg_match_all( '/(\'[\$\w^\']*\')/', $evaluate, $matches );
		if ( 
			$strings_count 
			&& $strings_count > 0 
		) {
			for ( $i = 0; $i < $strings_count; $i++ ) {
				$string = $matches[1][$i];
				// remove single quotes from string literals to get value only
				$string = (strpos( $string, '\'' ) === 0) ? substr( $string, 1, strlen( $string ) - 2 ) : $string;
				if ( strpos( $string, '$' ) === 0 ) {
					$quoted_variables_logging_extra = '';
					$variable_name = substr( $string, 1 ); // omit dollar sign
					if ( isset( $atts[$variable_name] ) ) {
						$string = get_post_meta( $post->ID, $atts[$variable_name], true );
						$evaluate = str_replace( $matches[1][$i], "'" . $string . "'", $evaluate );
					} else {
						$evaluate = str_replace( $matches[1][$i], "", $evaluate );
						$quoted_variables_logging_extra = "\n\tERROR: Key " . $matches[1][$i] . " does not point to a valid attribute in the wpv-if shortcode: expect parsing errors";
					}
					$logging_string .= "\nAfter replacing " . ( $i + 1 ) . " quoted variables: " . $evaluate . $quoted_variables_logging_extra;
				}
			}
		}
	}

    // Evaluate non-quoted variables, by de-quoting the quoted ones if needed
    $strings_count = preg_match_all( '/((\$\w+)|(\'[^\']*\'))\s*([\!<>\=]+)\s*((\$\w+)|(\'[^\']*\'))/',
            $evaluate, $matches );

    // get all string comparisons - with variables and/or literals
    if ( 
		$strings_count 
		&& $strings_count > 0 
	) {
        for ( $i = 0; $i < $strings_count; $i++ ) {

            // get both sides and sign
            $first_string = $matches[1][$i];
            $second_string = $matches[5][$i];
            $math_sign = $matches[4][$i];
			
			$general_variables_logging_extra = '';

            // remove single quotes from string literals to get value only
            $first_string = ( strpos( $first_string, '\'' ) === 0 ) ? substr( $first_string, 1, strlen( $first_string ) - 2 ) : $first_string;
            $second_string = ( strpos( $second_string, '\'' ) === 0 ) ? substr( $second_string, 1, strlen( $second_string ) - 2 ) : $second_string;
			$general_variables_logging_extra .= "\n\tComparing " . $first_string . " to " . $second_string;

            // replace variables with text representation
            if ( 
				strpos( $first_string, '$' ) === 0 
				&& $has_post 
			) {
                $variable_name = substr( $first_string, 1 ); // omit dollar sign
                if ( isset( $atts[$variable_name] ) ) {
                    $first_string = get_post_meta( $post->ID, $atts[$variable_name], true );
                } else {
                    $first_string = '';
					$general_variables_logging_extra .= "\n\tERROR: Key " . $variable_name . " does not point to a valid attribute in the wpv-if shortcode";
                }
            }
            if ( strpos( $second_string, '$' ) === 0 && $has_post ) {
                $variable_name = substr( $second_string, 1 );
                if ( isset( $atts[$variable_name] ) ) {
                    $second_string = get_post_meta( $post->ID, $atts[$variable_name], true );
                } else {
                    $second_string = '';
					$general_variables_logging_extra .= "\n\tERROR: Key " . $variable_name . " does not point to a valid attribute in the wpv-if shortcode";
                }
            }

            // don't do string comparison if variables are numbers 
            if ( 
				! ( 
					is_numeric( $first_string ) 
					&& is_numeric( $second_string ) 
				) 
			) {
                // compare string and return true or false
                $compared_str_result = wpv_condition_compare_strings( $first_string, $second_string, $math_sign );

                if ( $compared_str_result ) {
                    $evaluate = str_replace( $matches[0][$i], '1=1', $evaluate );
                } else {
                    $evaluate = str_replace( $matches[0][$i], '1=0', $evaluate );
                }
            } else {
                $evaluate = str_replace( $matches[1][$i], $first_string, $evaluate );
                $evaluate = str_replace( $matches[5][$i], $second_string, $evaluate );
            }
			$logging_string .= "\nAfter replacing " . ( $i + 1 ) . " general variables and comparing strings: " . $evaluate . $general_variables_logging_extra;
        }
    }

    // Evaluate comparisons when at least one of them is numeric
    $strings_count = preg_match_all( '/(\'[^\']*\')/', $evaluate, $matches );
    if ( 
		$strings_count 
		&& $strings_count > 0 
	) {
        for ( $i = 0; $i < $strings_count; $i++ ) {
            $string = $matches[1][$i];
            // remove single quotes from string literals to get value only
            $string = ( strpos( $string, '\'' ) === 0 ) ? substr( $string, 1, strlen( $string ) - 2 ) : $string;
            if ( is_numeric( $string ) ) {
                $evaluate = str_replace( $matches[1][$i], $string, $evaluate );
				$logging_string .= "\nAfter matching " . ( $i + 1 ) . " numeric strings into real numbers: " . $evaluate;
				$logging_string .= "\n\tMatched " . $matches[1][$i] . " to " . $string;
            }
        }
    }


    // Evaluate all remaining variables
	if ( $has_post ) {
		$count = preg_match_all( '/\$(\w+)/', $evaluate, $matches );

		// replace all variables with their values listed as shortcode parameters
		if ( 
			$count 
			&& $count > 0 
		) {
			$logging_string .= "\nRemaining variables: " . var_export( $matches[1], true );
			// sort array by length desc, fix str_replace incorrect replacement
			// wpv_sort_matches_by_length belongs to common/functions.php
			$matches[1] = wpv_sort_matches_by_length( $matches[1] );

			foreach ( $matches[1] as $match ) {
				if ( isset( $atts[$match] ) ) {
					$meta = get_post_meta( $post->ID, $atts[$match], true );
					if ( empty( $meta ) ) {
						$meta = "0";
					}
				} else {
					$meta = "0";
				}
				$evaluate = str_replace( '$' . $match, $meta, $evaluate );
				$logging_string .= "\nAfter replacing remaining variables: " . $evaluate;
			}
		}
	}

    $logging_string .= "\n--------------------\nEnd evaluated expression: " 
		. $evaluate 
		. "\n--------------------";

    $wplogger->log( $logging_string, WPLOG_DEBUG );
    // evaluate the prepared expression using the custom eval script
	// wpv_condition_evaluate_expression
    $result = wpv_condition_evaluate_expression( $evaluate );
    
	if ( $has_post ) {
		do_action( 'wpv_condition_end', $post );
	}
	
	$return = array();
	if ( is_bool( $result ) ) {
		$return['result'] = $result;
		$return['debug'] = $logging_string;
	} else {
		$return['result'] = ! $condition;
		$return['debug'] = $logging_string . "\n" . $result;
	}

    return $return;
}
Esempio n. 5
0
    public static function extractVariables( $evaluate, $atts, $has_post, $id ){
        $logging_string = '';
        // Evaluate quoted variables that are to be used as strings
        // '$f1' will replace $f1 with the custom field value

            $strings_count = preg_match_all( '/(\'[\$\w^\']*\')/', $evaluate, $matches );
            if (
                $strings_count
                && $strings_count > 0
            ) {
                for ( $i = 0; $i < $strings_count; $i++ ) {
                    $string = $matches[1][$i];
                    // remove single quotes from string literals to get value only
                    $string = (strpos( $string, '\'' ) === 0) ? substr( $string, 1, strlen( $string ) - 2 ) : $string;
                    if ( strpos( $string, '$' ) === 0 ) {
                        $quoted_variables_logging_extra = '';
                        $variable_name = substr( $string, 1 ); // omit dollar sign
                        if ( isset( $atts[$variable_name] ) ) {
                            $string = get_post_meta( $id, $atts[$variable_name], true );
                            $evaluate = str_replace( $matches[1][$i], "'" . $string . "'", $evaluate );
                        } else {
                            $evaluate = str_replace( $matches[1][$i], "", $evaluate );
                            $quoted_variables_logging_extra = "\n\tERROR: Key " . $matches[1][$i] . " does not point to a valid attribute in the wpv-if shortcode: expect parsing errors";
                        }
                        $logging_string .= "\nAfter replacing " . ( $i + 1 ) . " quoted variables: " . $evaluate . $quoted_variables_logging_extra;
                    }
                }
            }

            // Evaluate non-quoted variables, by de-quoting the quoted ones if needed


        $strings_count = preg_match_all( '/((\$\w+)|(\'[^\']*\'))\s*([\!<>\=|lt|lte|eq|ne|gt|gte]+)\s*((\$\w+)|(\'[^\']*\'))/',
            $evaluate, $matches );

        // get all string comparisons - with variables and/or literals
        if (
            $strings_count
            && $strings_count > 0
        ) {
            for ( $i = 0; $i < $strings_count; $i++ ) {

                // get both sides and sign
                $first_string = $matches[1][$i];
                $second_string = $matches[5][$i];
                $math_sign = $matches[4][$i];

                $general_variables_logging_extra = '';

                // remove single quotes from string literals to get value only
                $first_string = ( strpos( $first_string, '\'' ) === 0 ) ? substr( $first_string, 1, strlen( $first_string ) - 2 ) : $first_string;
                $second_string = ( strpos( $second_string, '\'' ) === 0 ) ? substr( $second_string, 1, strlen( $second_string ) - 2 ) : $second_string;
                $general_variables_logging_extra .= "\n\tComparing " . $first_string . " to " . $second_string;

                // replace variables with text representation
                if (
                    strpos( $first_string, '$' ) === 0
                    && $has_post
                ) {
                    $variable_name = substr( $first_string, 1 ); // omit dollar sign
                    if ( isset( $atts[$variable_name] ) ) {
                        $first_string = get_post_meta( $id, $atts[$variable_name], true );
                    } else {
                        $first_string = '';
                        $general_variables_logging_extra .= "\n\tERROR: Key " . $variable_name . " does not point to a valid attribute in the wpv-if shortcode";
                    }
                }
                if ( strpos( $second_string, '$' ) === 0 && $has_post ) {
                    $variable_name = substr( $second_string, 1 );
                    if ( isset( $atts[$variable_name] ) ) {
                        $second_string = get_post_meta( $id, $atts[$variable_name], true );
                    } else {
                        $second_string = '';
                        $general_variables_logging_extra .= "\n\tERROR: Key " . $variable_name . " does not point to a valid attribute in the wpv-if shortcode";
                    }
                }


                $evaluate = (is_numeric( $first_string )  ? str_replace( $matches[1][$i], $first_string, $evaluate ) : str_replace( $matches[1][$i], "'$first_string'", $evaluate ));
                $evaluate = (is_numeric( $first_string )  ? str_replace( $matches[5][$i], $second_string, $evaluate ) : str_replace( $matches[5][$i], "'$second_string'", $evaluate ));
                $logging_string .= "\nAfter replacing " . ( $i + 1 ) . " general variables and comparing strings: " . $evaluate . $general_variables_logging_extra;
            }
        }
        // Evaluate comparisons when at least one of them is numeric
		$strings_count = preg_match_all( '/(\'[^\']*\')/', $evaluate, $matches );
		if (
			$strings_count
			&& $strings_count > 0
		) {
			for ( $i = 0; $i < $strings_count; $i++ ) {
				$string = $matches[1][$i];
				// remove single quotes from string literals to get value only
				$string = ( strpos( $string, '\'' ) === 0 ) ? substr( $string, 1, strlen( $string ) - 2 ) : $string;
				if ( is_numeric( $string ) ) {
					$evaluate = str_replace( $matches[1][$i], $string, $evaluate );
					$logging_string .= "\nAfter matching " . ( $i + 1 ) . " numeric strings into real numbers: " . $evaluate;
					$logging_string .= "\n\tMatched " . $matches[1][$i] . " to " . $string;
				}
			}
		}

		// Evaluate all remaining variables
		if ( $has_post ) {
			$count = preg_match_all( '/\$(\w+)/', $evaluate, $matches );

			// replace all variables with their values listed as shortcode parameters
			if (
				$count
				&& $count > 0
			) {
				$logging_string .= "\nRemaining variables: " . var_export( $matches[1], true );
				// sort array by length desc, fix str_replace incorrect replacement
				// wpv_sort_matches_by_length belongs to common/functions.php
				$matches[1] = wpv_sort_matches_by_length( $matches[1] );

				foreach ( $matches[1] as $match ) {
					if ( isset( $atts[$match] ) ) {
						$meta = get_post_meta( $id, $atts[$match], true );
						if ( empty( $meta ) ) {
							$meta = "''";
						}
					} else {
						$meta = "0";
					}
					$evaluate = str_replace( '$' . $match, $meta, $evaluate );
					$logging_string .= "\nAfter replacing remaining variables: " . $evaluate;
				}
			}
		}
        return array( $evaluate, $logging_string);
    }