/** * 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) { $conditions = null; if (array_key_exists('conditional_display', $group) && array_key_exists('conditions', $group['conditional_display'])) { $conditions = $group['conditional_display']; } else { $conditions = get_post_meta($group['id'], '_wpcf_conditional_display', true); } if (!empty($conditions['conditions'])) { $meta_box_id = "wpcf-group-{$group['slug']}"; $prefix = 'wpcf-'; $suffix = ''; $cond = array(); if (isset($post->ID)) { $cond_values = get_post_custom($post->ID); } else { $cond_values = array(); } $_cond_values = array(); foreach ($cond_values as $k => $v) { $v = maybe_unserialize($v[0]); $_cond_values[$k . $suffix] = is_array($v) ? strval(array_shift($v)) : $v; } unset($cond_values); $cond = array(); if (!empty($conditions['custom_use'])) { if (!empty($conditions['custom'])) { $custom = WPToolset_Types::getCustomConditional($conditions['custom']); $passed = WPToolset_Forms_Conditional::evaluateCustom($custom['custom'], $_cond_values); $cond = array('custom' => $custom['custom'], 'custom_use' => true); } } else { $cond = array('relation' => $conditions['relation'], 'conditions' => array(), 'values' => $_cond_values); foreach ($conditions['conditions'] as $d) { $c_field = types_get_field($d['field']); if (!empty($c_field)) { $_c = array('id' => wpcf_types_get_meta_prefix($c_field) . $d['field'] . $suffix, 'type' => $c_field['type'], 'operator' => $d['operation'], 'args' => array($d['value'])); $cond['conditions'][] = $_c; } } $passed = wptoolset_form_conditional_check(array('conditional' => $cond)); } $data = array('id' => $meta_box_id, 'conditional' => $cond); wptoolset_form_add_conditional('post', $data); if (!$passed) { $group['_conditional_display'] = 'failed'; } else { $group['_conditional_display'] = 'passed'; } } } return $groups; }
/** * @return string Proper display name suitable for direct displaying to the user. * * Handles string translation and adds an asterisk if the field is required. * * @since 1.9 */ public function get_display_name() { // Try to translate through standard toolset-forms method $string_name = sprintf('field %s name', $this->get_slug()); $display_name = WPToolset_Types::translate($string_name, $this->get_name()); // Add an asterisk if the field is required if ($this->get_is_required() && !empty($display_name)) { $display_name .= '*'; } return $display_name; }
/** * Filters conditional. * * We'll just handle this as a custom conditional * * Custom conditional * Main properties: * [custom] - custom statement made by user, note that $xxxx should match * IDs of fields that passed this filter. * [values] - same as for regular conditional * * [conditional] => Array( [custom] => ($wpcf-my-date = DATE(01,02,2014)) OR ($wpcf-my-date > DATE(07,02,2014)) [values] => Array( [wpcf-my-date] => 32508691200 ) ) * * @param array|string $field settings array (as stored in DB) or field ID * @param int $post_id Post or user ID to fetch meta data to check against * @return array */ public static function filterConditional($if, $post_id) { $data = WPToolset_Types::getCustomConditional($if, '', WPToolset_Types::getConditionalValues($post_id)); return $data; }
public function filterTypesField($field, $post_id = null, $_post_wpcf = array()) { require_once WPTOOLSET_FORMS_ABSPATH . '/classes/class.types.php'; return WPToolset_Types::filterField($field, $post_id, $_post_wpcf); }
/** * New validation using API calls from toolset-forms. * * Uses API cals * @uses wptoolset_form_validate_field() * @uses wptoolset_form_conditional_check() * * @todo Make it work with other fields (generic) * * @param type $post_id * @param type $values * @return boolean */ function validate($post_id, $values) { $form_id = $this->form_id; $valid = true; // Loop over fields $form_source_data = $this->_formData->getForm()->post_content; preg_match_all("/\\[cred_show_group.*cred_show_group\\]/Uism", $form_source_data, $res); $conditional_group_fields = array(); if (count($res[0]) > 0) { for ($i = 0, $res_limit = count($res[0]); $i < $res_limit; $i++) { preg_match_all("/field=\"([^\"]+)\"/Uism", $res[0][$i], $parsed_fields); if (count($parsed_fields[1]) > 0) { for ($j = 0, $count_parsed_fields = count($parsed_fields); $j < $count_parsed_fields; $j++) { if (!empty($parsed_fields[1][$j])) { $conditional_group_fields[] = trim($parsed_fields[1][$j]); } } } } } foreach ($this->form_properties['fields'] as $field) { if (in_array(str_replace('wpcf-', '', $field['name']), $conditional_group_fields)) { continue; } // If Types field if (isset($field['plugin_type']) && $field['plugin_type'] == 'types') { $field_name = $field['name']; if (!isset($_POST[$field_name])) { continue; } /* // Adjust field ID $field['id'] = strpos( $field['name'], 'wpcf-' ) === 0 ? substr( $field['name'], 5 ) : $field['name']; // CRED have synonym 'text' for textfield if ( $field['type'] == 'text' ) { $field['type'] = 'textfield'; } */ $field = wpcf_fields_get_field_by_slug(str_replace('wpcf-', '', $field['name'])); if (empty($field)) { continue; } // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } // Set field config $config = wptoolset_form_filter_types_field($field, $post_id); //Add custom colorpicer hexadecimal backend validator //Fix https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/193712665/comments //Todo in future add hexadecimal check in types if ($field['type'] == 'colorpicker') { $config['validation']['hexadecimal'] = array('args' => array('$value', '1'), 'message' => 'Enter valid hexadecimal value'); } if (isset($config['conditional']['values'])) { foreach ($config['conditional']['values'] as $post_key => $post_value) { if (isset($this->form_properties['fields'][$post_key])) { $config['conditional']['values'][$post_key] = $this->form_properties['fields'][$post_key]['value']; } } } // Set values to loop over $_values = !empty($values[$field_name]) ? $values[$field_name]['value'] : null; if (empty($config['repetitive'])) { $_values = array($_values); } // Loop over each value if (is_array($_values)) { foreach ($_values as $value) { $validation = wptoolset_form_validate_field($form_id, $config, $value); $conditional = wptoolset_form_conditional_check($config); /** * add form_errors messages */ if (is_wp_error($validation) && $conditional) { $error_data = $validation->get_error_data(); if (isset($error_data[0])) { $this->add_top_message($error_data[0], $config['id']); } else { $this->add_top_message($validation->get_error_message(), $config['id']); } $valid = false; if (empty($ret_validation)) { continue; } // foreach( $errors as $error ) { // $error = explode( ' ', $error ); // $key = array_shift($error); // $error = implode( ' ', $error ); // $this->form_errors[$key] = $error; // $this->form_messages[$key] = $validation->get_error_message(); // } } } } } elseif (!isset($field['plugin_type']) && isset($field['validation'])) { if (!isset($_POST[$field['name']])) { continue; } $config = array('id' => $field['name'], 'type' => $field['type'], 'slug' => $field['name'], 'title' => $field['name'], 'description' => '', 'name' => $field['name'], 'repetitive' => $field['repetitive'], 'validation' => $field['validation'], 'conditional' => array()); $value = $field['value']; require_once WPTOOLSET_FORMS_ABSPATH . '/classes/class.types.php'; $validation = array(); foreach ($field['validation'] as $rule => $settings) { if ($settings['active']) { $id = $config['slug']; $validation[$rule] = array('args' => isset($settings['args']) ? array_unshift($value, $settings['args']) : array($value, true), 'message' => WPToolset_Types::translate('field ' . $id . ' validation message ' . $rule, $settings['message'])); } } $config['validation'] = $validation; $validation = wptoolset_form_validate_field($form_id, $config, $value); if (is_wp_error($validation)) { $error_data = $validation->get_error_data(); //TODO: replace id with name if (isset($error_data[0])) { $this->add_top_message($error_data[0], $config['id']); } else { $this->add_top_message($validation->get_error_message(), $config['id']); } $valid = false; if (empty($ret_validation)) { continue; } // foreach( $errors as $error ) { // $error = explode( ' ', $error ); // $key = array_shift($error); // $error = implode( ' ', $error ); // $this->form_errors[$key] = $error; // $this->form_messages[$key] = $validation->get_error_message(); // } } } } return $valid; }
public static function parse_conditional( $post, $condition, $debug = false, $attr, $id, $has_post ){ $logging_string = "####################\nwpv-conditional attributes\n####################\n" . print_r( $attr, true ) . "\n####################\nDebug information\n####################" . "\n--------------------\nOriginal expression: " . $condition . "\n--------------------"; if (!defined('WPTOOLSET_COMMON_PATH')) { define('WPTOOLSET_COMMON_PATH', WPV_PATH_EMBEDDED . '/common'); } require_once WPV_PATH_EMBEDDED . '/common/toolset-forms/classes/class.types.php'; require_once WPV_PATH_EMBEDDED . '/common/toolset-forms/classes/class.cred.php'; $data = WPToolset_Types::getCustomConditional($condition, '', WPToolset_Types::getConditionalValues($id)); $evaluate = $data['custom']; $values = $data['values']; if (strpos($evaluate, "REGEX") === false) { $evaluate = trim(stripslashes($evaluate)); // Check dates $evaluate = wpv_filter_parse_date($evaluate); $evaluate = self::handle_user_function($evaluate); } $fields = self::extractFields($evaluate); $evaluate = apply_filters( 'wpv-extra-condition-filters', $evaluate); $temp = self::extractVariables( $evaluate, $attr, $has_post, $id ); $evaluate = $temp[0]; $logging_string .= $temp[1]; $passed = self::evaluateCustom($evaluate); if ( $passed ){ return array ( 'debug' => $logging_string, 'passed'=>$passed ); } $evaluate = self::_update_values_in_expression($evaluate, $fields, $values); $logging_string .= "\n--------------------\nConverted expression: " . $evaluate . "\n--------------------"; $passed = self::evaluateCustom($evaluate); return array ( 'debug' => $logging_string, 'passed'=>$passed ); }