function wpv_apply_user_function_date_compare($value) { $parsed = wpv_filter_parse_date($value); if ($parsed) { $value = $parsed; } return $value; }
/** * wpv_render_datepicker * * Renders the datepicker for date based frontend filters * * @param $url_param (string) the URL parameter used on the frontend filter * @param $date_format (string) the date format to use when displaying the selected date besides the datepicker * @param $default_date (string) the default date to be used when there is no value passed by the URL parameter - special case NONE * * @return (string) containing the needed inputs * * @since unknown * * @note $default_date default value was changed in 1.7 from NOW() to NONE; empty will mean NONE too * * @todo add an attribute for themes http://rtsinani.github.io/jquery-datepicker-skins/ * OR better an option in the Views settings, because we can not have two different styles for two datepickers */ function wpv_render_datepicker( $url_param, $date_format, $default_date = '' ) { static $support_loaded = false; $display_date = $datepicker_date = ''; // TODO we need to enqueue this style in the right way. We are loading it twice! And it can not be unregistered... if ( !$support_loaded && 1 === 0 ) { ?> <script type="text/javascript"> jQuery(document).ready(function () { jQuery('head').append('<link rel="stylesheet" href="<?php echo WPV_URL_EMBEDDED . '/common/toolset-forms/css/wpt-jquery-ui/datepicker.css';?>" type="text/css" />'); }); </script> <?php $support_loaded = true; } if ( $date_format == '' ) { $date_format = get_option( 'date_format' ); } $clear_button_style = ''; $date = ''; if( isset( $_GET[$url_param] ) ) { if ( $_GET[$url_param] == '' || $_GET[$url_param] == '0' ) { $date = ''; } else { $date = $_GET[$url_param]; } } else { if ( $default_date == '' || $default_date == 'NONE' ) { $date = ''; } else { $date = wpv_filter_parse_date( $default_date ); } } if ( is_numeric( $date ) ) { if ( $date < -12219292800 || $date > 32535215940 ) { $date = ''; } } else { $date = ''; } if ( $date != '' ) { $display_date = adodb_date( $date_format, $date ); } else { $clear_button_style = ' style="display:none"'; } $out = ''; $out .= '<span class="wpv_date_input js-wpv-date-param-' . $url_param . ' js-wpv-date-display" data-param="' . $url_param . '">' . $display_date . '</span> '; $out .= '<input type="hidden" class="js-wpv-date-param-' . $url_param . '-value js-wpv-filter-trigger" name="' . $url_param . '" value="' . $date . '" />'; $out .= '<input type="hidden" class="js-wpv-date-param-' . $url_param . '-format" name="' . $url_param . '-format" value="' . $date_format . '" />'; if ( $date != '' ) { $datepicker_date = adodb_date( 'dmY', $date ); } $out .= '<input type="hidden" data-param="' . $url_param . '" class="wpv-date-front-end js-wpv-frontend-datepicker js-wpv-date-front-end-' . $url_param . '" value="' . $datepicker_date . '"/>'; $delete_date_image = WPV_URL_EMBEDDED_FRONTEND . '/res/img/delete.png'; $delete_date_image = apply_filters( 'wpv_filter_wpv_delete_date_image', $delete_date_image ); $delete_date_image = apply_filters( 'wptoolset_filter_wptoolset_delete_date_image', $delete_date_image ); $out .= '<img src="' . $delete_date_image . '" title="' . esc_attr( __( 'Clear date', 'wpv-views' ) ) . '" alt="' . esc_attr( __( 'Clear date', 'wpv-views' ) ) . '" class="wpv-date-front-end-clear js-wpv-date-front-end-clear js-wpv-date-front-end-clear-' . $url_param . '" data-param="' . $url_param . '"' . $clear_button_style . ' />'; return $out; }
function wpv_add_time_functions($value) { return wpv_filter_parse_date($value); }
/** * Filters conditional display value for built-in Types Conditinal check. * * @param type $value * @param type $field * @param type $operation * @param type $conditional_field * @param type $post * @return type */ function wpcf_fields_date_conditional_value_filter($value, $field, $operation, $field_compared, $post) { $field = wpcf_admin_fields_get_field($field); if (!empty($field) && $field['type'] == 'date') { /* * * * * * Here we need to determine data */ $_field = new WPCF_Field(); $_field->set($post, $field); return wpcf_fields_date_value_get_filter($value, $_field, 'timestamp', 'skip_hour_and_minute'); // TODO Date revise why needed. // Check dates $value = wpv_filter_parse_date($value); } return $value; }
/** * Extracts fields from custom conditional statement. * * @param type $evaluate * @return type */ public static function extractFields($evaluate) { //############################################################################################### //https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/193583580/comments //Fix REGEX conditions that contains \ that is stripped out if (strpos($evaluate, "REGEX") === false) { $evaluate = trim(stripslashes($evaluate)); // Check dates $evaluate = wpv_filter_parse_date($evaluate); $evaluate = self::handle_user_function($evaluate); } // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } // if new version $(field-value) use this regex if (preg_match('/\\$\\(([^()]+)\\)/', $evaluate)) { preg_match_all('/\\$\\(([^()]+)\\)/', $evaluate, $matches); } else { preg_match_all('/\\$([^\\s]*)/', $evaluate, $matches); } $fields = array(); if (!empty($matches)) { foreach ($matches[1] as $field_name) { $fields[trim($field_name, '()')] = trim($field_name, '()'); } } return $fields; }
/** * Filters conditional display value. * * @param type $value * @param type $field * @param type $operation * @param type $conditional_field * @param type $post * @return type */ function wpcf_fields_date_conditional_display_value_filter($value, $field, $operation, $field_compared, $post) { $field = wpcf_admin_fields_get_field($field); if (!empty($field) && $field['type'] == 'date') { $time = strtotime($value); if ($time) { return $time; } else { // Check dates $value = wpv_filter_parse_date($value); } } return $value; }
function wpv_render_datepicker($url_param, $date_format, $default_date = 'NOW()') { static $support_loaded = false; $display_date = $datepicker_date = ''; if (!$support_loaded) { ?> <script type="text/javascript"> jQuery(document).ready(function () { jQuery('head').append('<link rel="stylesheet" href="<?php echo WPV_URL_EMBEDDED . '/res/css/datepicker.css'; ?> " type="text/css" />'); }); </script> <?php $support_loaded = true; } if ($date_format == '') { $date_format = get_option('date_format'); } if (isset($_GET[$url_param]) && $_GET[$url_param] != '' && $_GET[$url_param] != '0') { $date = (int) $_GET[$url_param]; } else { //$date = time(); if ($default_date == '') { //If default date not set, date = now() $date = wpv_filter_parse_date('NOW()'); } elseif ($default_date == 'NONE') { // Empty Date $date = ''; } else { $date = wpv_filter_parse_date($default_date); } } //if ( $default_date != 'NONE' ){ if ($date != '') { $display_date = date_i18n($date_format, intval($date)); } $out = ''; $out .= '<span class="wpv_date_input js-wpv-date-param-' . $url_param . ' js-wpv-date-display" data-param="' . $url_param . '">' . $display_date . '</span> '; $out .= '<input type="hidden" class="js-wpv-date-param-' . $url_param . '-value" name="' . $url_param . '" value="' . $date . '" />'; $out .= '<input type="hidden" class="js-wpv-date-param-' . $url_param . '-format" name="' . $url_param . '-format" value="' . $date_format . '" />'; //if ( $default_date != 'NONE' ){ if ($date != '') { $datepicker_date = date('dmy', intval($date)); } $out .= '<input type="hidden" data-param="' . $url_param . '" class="wpv-date-front-end js-wpv-date-front-end-' . $url_param . '" value="' . $datepicker_date . '"/>'; return $out; }
/** * Main conditinal evaluation function. * * This is important break-point. * * @since 1.2 * @version 0.1 * @param type $o * @return boolean */ function evaluate($o) { // Set vars $post = $o->post; $field = $o->cf; /* * * Since Types 1.2 * We force initial value to be FALSE. * Better to have restricted than allowed because of sensitive data. * If conditional is set on field and it goes wrong - better to abort * so user can report bug without exposing his content. */ $passed = false; if (empty($post->ID)) { /* * * Keep all forbidden if post is not saved. */ $passed = false; /* * * * * * * * * * * VIEWS * * Custom call uses Views code * wpv_filter_parse_date() * wpv_condition() */ } else { if (isset($field['data']['conditional_display']['custom_use'])) { /* * * * More malformed forbids */ if (empty($field['data']['conditional_display']['custom'])) { return false; } /* * * * Filter meta values (switch them with $_POST values) * Used by Views, Types do not need it. */ add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); /* * * Set statement */ $evaluate = trim(stripslashes($field['data']['conditional_display']['custom'])); // Check dates $evaluate = wpv_filter_parse_date($evaluate); // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } preg_match_all('/\\$([^\\s]*)/', $field['data']['conditional_display']['custom'], $matches); if (empty($matches)) { /* * * If statement false */ $passed = false; } else { /* * * * If statement right, check condition */ $fields = array(); foreach ($matches[1] as $field_name) { /* * * * This field value is checked */ $f = wpcf_admin_fields_get_field(trim(strval($field_name))); if (empty($f)) { return false; } $c = new WPCF_Field(); $c->set($post, $f); // Set field $fields[$field_name] = $c->slug; } $fields['evaluate'] = $evaluate; $check = wpv_condition($fields); /* * * * Views return string malformed, * boolean if call completed. */ if (!is_bool($check)) { $passed = false; } else { $passed = $check; } } /* * * * Remove filter meta values */ remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); } else { /* * * * * * * * * * TYPES * * If not custom code, use Types built-in check. * wpcf_cd_admin_compare() */ $passed_all = true; $passed_one = false; // Basic check if (empty($field['data']['conditional_display']['conditions'])) { return false; } // Keep count to see if OR/AND relation needed $count = count($field['data']['conditional_display']['conditions']); foreach ($field['data']['conditional_display']['conditions'] as $condition) { /* * * * Malformed condition and should be treated as forbidden */ if (!isset($condition['field']) || !isset($condition['operation']) || !isset($condition['value'])) { $passed_one = false; continue; } /* * * * This field value is checked */ $f = wpcf_admin_fields_get_field(trim(strval($condition['field']))); if (empty($f)) { return false; } $c = new WPCF_Field(); $c->set($post, $f); /* * * Since Types 1.2 * meta is property of WPCF_Field::$__meta * * BREAKPOINT * This is where values for evaluation are set. * Please do not allow other places - use hooks. */ $value = defined('DOING_AJAX') ? $c->_get_meta('POST') : $c->__meta; /* * * Apply filters */ $value = apply_filters('wpcf_conditional_display_compare_meta_value', $value, $c->cf['id'], $condition['operation'], $c->slug, $post); $condition['value'] = apply_filters('wpcf_conditional_display_compare_condition_value', $condition['value'], $c->cf['id'], $condition['operation'], $c->slug, $post); /* * * * Call built-in Types compare func */ $passed = wpcf_cd_admin_compare($condition['operation'], $value, $condition['value']); if (!$passed) { $passed_all = false; } else { $passed_one = true; } } /* * * * Check OR/AND relation */ if ($count > 1) { if (!$passed_all && $field['data']['conditional_display']['relation'] == 'AND') { $passed = false; } if (!$passed_one && $field['data']['conditional_display']['relation'] == 'OR') { $passed = false; } } } } return (bool) $passed; }
/** * Checks if there is conditional display. * * @param type $element * @param type $field * @param type $post * @return type */ function wpcf_cd_post_edit_field_filter($element, $field, $post, $context = 'group') { if (defined('DOING_AJAX') && $context == 'repetitive') { return $element; } if (!empty($field['data']['conditional_display']['conditions'])) { add_action('admin_head', 'wpcf_cd_add_field_js'); $passed = true; if (empty($post->ID)) { $passed = false; } else { if (isset($field['data']['conditional_display']['custom_use'])) { if (empty($field['data']['conditional_display']['custom'])) { return array(); } $evaluate = trim(stripslashes($field['data']['conditional_display']['custom'])); // Check dates $evaluate = wpv_filter_parse_date($evaluate); // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } preg_match_all('/\\$([^\\s]*)/', $field['data']['conditional_display']['custom'], $matches); if (empty($matches)) { $passed = false; } else { $fields = array(); foreach ($matches[1] as $key => $field_name) { $fields[$field_name] = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_name)) . $field_name; } $fields['evaluate'] = $evaluate; $check = wpv_condition($fields); if (!is_bool($check)) { $passed = false; } else { $passed = $check; } } } else { $passed_all = true; $passed_one = false; foreach ($field['data']['conditional_display']['conditions'] as $condition) { // This is malformed condition and should be treated as passed // @TODO Approve it if (!isset($condition['field']) || !isset($condition['operation']) || !isset($condition['value'])) { $passed_one = true; continue; } $value = get_post_meta($post->ID, wpcf_types_get_meta_prefix($condition['field']) . $condition['field'], true); $value = apply_filters('wpcf_conditional_display_compare_meta_value', $value, $condition['field'], $condition['operation'], $field['slug'], $post); $condition['value'] = apply_filters('wpcf_conditional_display_compare_condition_value', $condition['value'], $condition['field'], $condition['operation'], $field['slug'], $post); $check = wpcf_cd_admin_compare($condition['operation'], $value, $condition['value']); if (!$check) { $passed_all = false; } else { $passed_one = true; } } if (!$passed_all && $field['data']['conditional_display']['relation'] == 'AND') { $passed = false; } if (!$passed_one && $field['data']['conditional_display']['relation'] == 'OR') { $passed = false; } } } if (!$passed) { $wrap = '<div class="wpcf-cd wpcf-cd-failed" style="display:none;">'; $element['__wpcf_cd_status'] = 'failed'; } else { $wrap = '<div class="wpcf-cd wpcf-cd-passed">'; $element['__wpcf_cd_status'] = 'passed'; } if (isset($element['#before'])) { $element['#before'] = $wrap . $element['#before']; } else { $element['#before'] = $wrap; } if (isset($element['#after'])) { $element['#after'] = $element['#after'] . '</div>'; } else { $element['#after'] = '</div>'; } } return $element; }
/** * Filters groups on post edit page. * * @param type $groups * @param type $post * @return type */ function wpcf_cd_post_groups_filter($groups, $post, $context) { if ($context != 'group') { return $groups; } foreach ($groups as $key => &$group) { $meta_conditional = !isset($group['conditional_display']) ? get_post_meta($group['id'], '_wpcf_conditional_display', true) : $group['conditional_display']; if (!empty($meta_conditional['conditions'])) { $group['conditional_display'] = $meta_conditional; add_action('admin_head', 'wpcf_cd_add_group_js'); if (empty($post->ID)) { $group['_conditional_display'] = 'failed'; continue; } $passed = true; if (isset($group['conditional_display']['custom_use'])) { if (empty($group['conditional_display']['custom'])) { $group['_conditional_display'] = 'failed'; continue; } $evaluate = trim(stripslashes($group['conditional_display']['custom'])); // Check dates $evaluate = wpv_filter_parse_date($evaluate); // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } preg_match_all('/\\$([^\\s]*)/', $group['conditional_display']['custom'], $matches); if (empty($matches)) { $group['_conditional_display'] = 'failed'; continue; } $fields = array(); foreach ($matches[1] as $key => $field_name) { $fields[$field_name] = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_name)) . $field_name; wpcf_cd_add_group_js('add', $field_name, '', '', $group['id']); } $fields['evaluate'] = $evaluate; $check = wpv_condition($fields); $passed = $check; if (!is_bool($check)) { $passed = false; $group['_conditional_display'] = 'failed'; } else { if ($check) { $group['_conditional_display'] = 'passed'; } else { $group['_conditional_display'] = 'failed'; } } } else { $passed_all = true; $passed_one = false; foreach ($group['conditional_display']['conditions'] as $condition) { // Load field $field = wpcf_admin_fields_get_field($condition['field']); wpcf_fields_type_action($field['type']); wpcf_cd_add_group_js('add', $condition['field'], $condition['value'], $condition['operation'], $group['id']); $value = get_post_meta($post->ID, wpcf_types_get_meta_prefix($condition['field']) . $condition['field'], true); $value = apply_filters('wpcf_conditional_display_compare_meta_value', $value, $condition['field'], $condition['operation'], $key, $post); $condition['value'] = apply_filters('wpcf_conditional_display_compare_condition_value', $condition['value'], $condition['field'], $condition['operation'], $key, $post); $check = wpcf_cd_admin_compare($condition['operation'], $value, $condition['value']); if (!$check) { $passed_all = false; } else { $passed_one = true; } } if (!$passed_all && $group['conditional_display']['relation'] == 'AND') { $passed = false; } if (!$passed_one && $group['conditional_display']['relation'] == 'OR') { $passed = false; } } if (!$passed) { $group['_conditional_display'] = 'failed'; } else { $group['_conditional_display'] = 'passed'; } } } return $groups; }
function wpv_add_time_functions_to_conditionals( $value ) { return wpv_filter_parse_date( $value ); }
/** * Extracts fields from custom conditional statement. * * @param type $evaluate * @return type */ public static function extractFields($evaluate) { $evaluate = trim(stripslashes($evaluate)); // Check dates $evaluate = wpv_filter_parse_date($evaluate); $evaluate = self::handle_user_function($evaluate); // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } // if new version $(field-value) use this regex if (preg_match('/\\$\\(([^()]+)\\)/', $evaluate)) { preg_match_all('/\\$\\(([^()]+)\\)/', $evaluate, $matches); } else { preg_match_all('/\\$([^\\s]*)/', $evaluate, $matches); } $fields = array(); if (!empty($matches)) { foreach ($matches[1] as $field_name) { $fields[trim($field_name, '()')] = trim($field_name, '()'); } } return $fields; }
/** * Extracts fields from custom conditional statement. * * @param type $evaluate * @return type */ protected static function _extractFields($evaluate) { $evaluate = trim(stripslashes($evaluate)); // Check dates $evaluate = wpv_filter_parse_date($evaluate); // Add quotes = > < >= <= === <> !== $strings_count = preg_match_all('/[=|==|===|<=|<==|<===|>=|>==|>===|\\!===|\\!==|\\!=|<>]\\s(?!\\$)(\\w*)[\\)|\\$|\\W]/', $evaluate, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $temp_match) { $temp_replace = is_numeric($temp_match) ? $temp_match : '\'' . $temp_match . '\''; $evaluate = str_replace(' ' . $temp_match . ')', ' ' . $temp_replace . ')', $evaluate); } } preg_match_all('/\\$([^\\s]*)/', $evaluate, $matches); $fields = array(); if (!empty($matches)) { foreach ($matches[1] as $field_name) { $fields[$field_name] = $field_name; } } return $fields; }