/** * Set current post and field. * * @param type $post * @param type $cf */ function set($user_id, $cf) { global $wpcf; /* * * Check if $cf is string */ if (is_string($cf)) { WPCF_Loader::loadInclude('fields'); $cf = wpcf_admin_fields_get_field($this->__get_slug_no_prefix($cf)); if (empty($cf)) { $this->_reset(); return false; } } $this->currentUID = $user_id; $this->ID = $cf['id']; $this->cf = $cf; $this->slug = wpcf_types_get_meta_prefix($this->cf) . $this->cf['slug']; $this->meta = $this->_get_meta(); $this->config = $this->_get_config(); $this->unique_id = wpcf_unique_id(serialize((array) $this)); $this->cf['value'] = $this->meta; // Debug $wpcf->debug->fieds[$this->unique_id] = $this->cf; $wpcf->debug->meta[$this->slug][] = $this->meta; // Load files if (isset($this->cf['type'])) { $file = WPCF_EMBEDDED_INC_ABSPATH . '/fields/' . $this->cf['type'] . '.php'; if (file_exists($file)) { include_once $file; } if (defined('WPCF_INC_ABSPATH')) { $file = WPCF_INC_ABSPATH . '/fields/' . $this->cf['type'] . '.php'; if (file_exists($file)) { include_once $file; } } } }
/** * Gets posts that belongs to current post. * * @global type $post * @param type $post_type * @param type $args * @return type */ function types_child_posts($post_type, $args = array()) { static $cache = array(); if (isset($args['post_id'])) { $post = $args['post_id'] != '0' ? get_post($args['post_id']) : null; } else { global $post; } if (empty($post->ID)) { return array(); } $cache_key = md5($post->ID . serialize(func_get_args())); if (isset($cache[$cache_key])) { return $cache[$cache_key]; } global $wp_post_types; // WP allows querying inactive post types if (!isset($wp_post_types[$post_type]) || !$wp_post_types[$post_type]->publicly_queryable) { return array(); } $defaults = array('post_status' => array('publish')); $args = wp_parse_args($args, $defaults); WPCF_Loader::loadModel('relationship'); WPCF_Loader::loadInclude('fields-post'); $child_posts = WPCF_Relationship_Model::getChildrenByPostType($post, $post_type, array(), array(), $args); foreach ($child_posts as $child_post_key => $child_post) { $child_posts[$child_post_key]->fields = array(); $groups = wpcf_admin_post_get_post_groups_fields($child_post); foreach ($groups as $group) { if (!empty($group['fields'])) { // Process fields foreach ($group['fields'] as $k => $field) { $data = null; if (types_is_repetitive($field)) { $data = wpcf_get_post_meta($child_post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], false); // get all field instances } else { $data = wpcf_get_post_meta($child_post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); // get single field instance // handle checkboxes which are one value serialized if ($field['type'] == 'checkboxes' && !empty($data)) { $data = maybe_unserialize($data); } } if (!is_null($data)) { $child_posts[$child_post_key]->fields[$k] = $data; } } } } } $cache[$cache_key] = $child_posts; return $child_posts; }
/** * Set current post and field. * * @param type $post * @param type $cf */ function set($post, $cf) { global $wpcf; /* * * Check if $cf is string */ if (is_string($cf)) { WPCF_Loader::loadInclude('fields'); $_cf = wpcf_admin_fields_get_field($this->__get_slug_no_prefix($cf)); // Check if found without prefix if (empty($_cf)) { $_cf = wpcf_admin_fields_get_field($cf); } if (empty($_cf)) { /* * TODO Check what happens if field is not found */ $this->_reset(); return false; } $cf = $_cf; } $this->post = is_integer($post) ? get_post($post) : $post; // If empty post it is new if (empty($this->post->ID)) { $this->post = new stdClass(); $this->post->ID = 0; } $this->ID = $cf['id']; $this->cf = $cf; $this->slug = wpcf_types_get_meta_prefix($this->cf) . $this->cf['slug']; $this->meta = $this->_get_meta(); $this->config = $this->_get_config(); $this->unique_id = wpcf_unique_id(serialize((array) $this)); $this->cf['value'] = $this->meta; // Debug $wpcf->debug->fields[$this->unique_id] = $this->cf; $wpcf->debug->meta[$this->slug][] = $this->meta; // Load files $this->_include_file_by_field_type($this->cf['type']); if (defined('WPCF_INC_ABSPATH')) { $file = WPCF_INC_ABSPATH . '/fields/' . preg_replace('/[^\\w]+/', '', $this->cf['type']) . '.php'; if (file_exists($file)) { include_once $file; } } }
/** * Single condition form elements. * * @param type $data * @param type $condition * @param type $key * @return string */ function wpcf_cd_admin_form_single_filter($data, $condition, $key = null, $group = false, $force_multi = false) { global $wpcf; if ($group) { $name = 'wpcf[group][conditional_display]'; } else { $name = 'wpcf[fields][' . $data['id'] . '][conditional_display]'; } $group_id = isset($_GET['group_id']) ? intval($_GET['group_id']) : false; /* * * * TODO Review this allowing fields from same group as conditional (self loop) * I do not remember allowing fields from same group as conditional (self loop) * on Group Fields edit screen. */ // if ( $group_id && !$group ) {// Allow group to use other fields // $fields = wpcf_admin_fields_get_fields_by_group( $group_id ); // } else { $fields = wpcf_admin_fields_get_fields(true, false, true); ksort($fields, SORT_STRING); // } if ($group) { $_distinct = wpcf_admin_fields_get_fields_by_group($group_id); foreach ($_distinct as $_field_id => $_field) { if (isset($fields[$_field_id])) { unset($fields[$_field_id]); } } } $options = array(); $ignore_field_type_array = array('audio', 'checkboxes', 'embed', 'file', 'image', 'video', 'wysiwyg'); $flag_repetitive = false; foreach ($fields as $field_id => $field) { if (!$group && $data['id'] == $field_id) { continue; } // WE DO NOT ALLOW repetitive fields to be compared. if (wpcf_admin_is_repetitive($field)) { $flag_repetitive = true; continue; } /** * Skip some files */ if (in_array($field['type'], $ignore_field_type_array)) { continue; } /** * build options */ $options[$field_id] = array('#value' => $field_id, '#title' => stripslashes($field['name']), '#attributes' => array('class' => 'wpcf-conditional-select-' . $field['type'])); } /* * Special case * https://icanlocalize.basecamphq.com/projects/7393061-wp-views/todo_items/153565054/comments * * When field is new and only one diff field in list - that * means one field is saved but other not yet. */ $is_new = isset($data['id']) && isset($fields[$data['id']]) ? false : true; $special_stop = false; if ($is_new) { if (count($options) == 1) { $special_stop = true; } } /* * * This means all fields are repetitive and no one left to compare with. * WE DO NOT ALLOW repetitive fields to be compared. */ if (!$group && empty($options) && $flag_repetitive) { return array('cd' => array('#type' => 'markup', '#markup' => '<p class="wpcf-error">' . __('Conditional display is only working based on non-repeating fields. All fields in this group are repeating, so you cannot set their display based on other fields.', 'wpcf') . '</p>' . wpcf_conditional_disable_add_js($data['id']))); } else { if (!$group && (empty($options) || $special_stop)) { return array('cd' => array('#type' => 'markup', '#markup' => '<p>' . __('You will be able to set conditional field display when you save more fields.', 'wpcf') . '</p>')); } } $id = !is_null($key) ? $key : strval('condition_' . wpcf_unique_id(serialize($data) . serialize($condition) . $key . $group)); $form = array(); $before = '<div class="wpcf-cd-entry"><br />'; $form['cd']['field_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][field]', '#options' => $options, '#inline' => true, '#before' => $before, '#default_value' => isset($condition['field']) ? $condition['field'] : null, '#attributes' => array('class' => 'wpcf-cd-field')); $form['cd']['operation_' . $id] = array('#type' => 'select', '#name' => $name . '[conditions][' . $id . '][operation]', '#options' => array_flip(wpcf_cd_admin_operations()), '#inline' => true, '#default_value' => isset($condition['operation']) ? $condition['operation'] : null, '#attributes' => array('class' => 'wpcf-cd-operation')); $form['cd']['value_' . $id] = array('#type' => 'textfield', '#name' => $name . '[conditions][' . $id . '][value]', '#inline' => true, '#value' => isset($condition['value']) ? $condition['value'] : '', '#attributes' => array('class' => 'wpcf-cd-value')); /* * * Adjust for date */ if (!empty($condition['value'])) { WPCF_Loader::loadInclude('fields/date/functions.php'); $timestamp = wpcf_fields_date_convert_datepicker_to_timestamp($condition['value']); if ($timestamp !== false) { $date_value = adodb_date('d', $timestamp) . ',' . adodb_date('m', $timestamp) . ',' . adodb_date('Y', $timestamp); $date_function = 'date'; } else { if (wpcf_fields_date_timestamp_is_valid($condition['value'])) { $date_value = adodb_date('d', $condition['value']) . ',' . adodb_date('m', $condition['value']) . ',' . adodb_date('Y', $condition['value']); $date_function = 'date'; } } } if (empty($date_value)) { $date_value = ''; $date_function = false; } $form['cd']['value_date_' . $id] = array('#type' => 'markup', '#markup' => '<br />' . wpcf_conditional_add_date_controls($date_function, $date_value, $name . '[conditions][' . $id . ']'), '#attributes' => array('class' => 'wpcf-cd-value-date')); $form['cd']['remove_' . $id] = array('#type' => 'button', '#name' => 'remove', '#value' => __('Remove condition', 'wpcf'), '#attributes' => array('onclick' => 'wpcfCdRemoveCondition(jQuery(this));', 'class' => 'wpcf-add-condition'), '#after' => '</div><br />'); return $form['cd']; }
_e('Crop images, so that they fill the specified dimensions exactly.', 'wpcf'); ?> </label> </p> </div> </div> <p class="form-inline"> <input id="image-url" type="checkbox" name="url" value="1" data-bind="checked: imageUrl, click: imageUrlDisable" /> <label for="image-url"><?php _e('Output only the URL of the re-sized image instead of the img tag', 'wpcf'); ?> </label> </p> <!--<input id="image-onload" type="text" name="onload" value="<?php echo $data['onload']; ?> " /> <label for="image-onload"><?php _e('Onload callback', 'wpcf'); ?> </label>--> </script><!--END TYPES MODAL IMAGE--> <?php WPCF_Loader::loadInclude('fields/colorpicker'); wpcf_fields_colorpicker_enqueue_scripts(); wpcf_fields_colorpicker_render_js();
/** * Main conditinal evaluation function. * * @since 1.2 * @version 0.2 * @param type $o * @return boolean */ public static 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. */ if ($o->context == 'relationship') { add_filter('get_post_metadata', array('WPCF_Evaluate', 'relationship_custom_statement_meta_ajax_validation_filter'), 10, 4); } else { add_filter('get_post_metadata', array('WPCF_Evaluate', 'meta_ajax_validation_filter'), 10, 4); } do_action('types_custom_conditional_statement', $o); /* * * 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, $post); /* * * * Views return string malformed, * boolean if call completed. */ if (!is_bool($check)) { $passed = false; } else { $passed = $check; } } /* * * * Remove filter meta values */ if ($o->context == 'relationship') { remove_filter('get_post_metadata', array('WPCF_Evaluate', 'relationship_custom_statement_meta_ajax_validation_filter'), 10, 4); } else { remove_filter('get_post_metadata', array('WPCF_Evaluate', '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. * * TODO Monitor this * 1.3 Change use of $c->_get_meta( 'POST' ) * to $c->get_submitted_data() */ // $value = defined( 'DOING_AJAX' ) ? $c->_get_meta( 'POST' ) : $c->__meta; $value = defined('DOING_AJAX') ? $c->get_submitted_data() : $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 */ WPCF_Loader::loadInclude('conditional-display'); $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 ($field['data']['conditional_display']['relation'] == 'AND') { $passed = $passed_all; } else { if ($field['data']['conditional_display']['relation'] == 'OR') { $passed = $passed_one; } } } } } return (bool) $passed; }
/** * Process AJAX conditional verify. * * @global type $wpcf * @param type $data * @return boolean|string */ public static function conditionalVerify($data) { WPCF_Loader::loadInclude('fields'); WPCF_Loader::loadInclude('fields-post'); WPCF_Loader::loadInclude('conditional-display'); global $wpcf; $js_execute = ''; $_flag_relationship = false; /* * * Determine post. */ if (empty($data['wpcf']) && !empty($data['wpcf_post_relationship'])) { /* * Relationship case */ $_temp = $data['wpcf_post_relationship']; $parent_id = key($_temp); $_data = array_shift($_temp); $post_id = key($_data); $post = get_post($post_id); $posted_fields = $_data[$post_id]; $_flag_relationship = true; /* * * Regular submission */ } else { if (isset($data['wpcf_main_post_id'])) { $post_id = intval($data['wpcf_main_post_id']); $post = get_post($post_id); } } // No post if (empty($post->ID)) { return false; } // Get Groups (Fields) for current post $groups = wpcf_admin_post_get_post_groups_fields($post); $_processed = array(); foreach ($groups as $group) { if (!empty($group['fields'])) { foreach ($group['fields'] as $field_id => $field) { // Check if already processed if (isset($_processed[$field_id])) { continue; } if ($wpcf->conditional->is_conditional($field_id)) { if ($_flag_relationship) { // Process only submitted fields if (!isset($posted_fields[WPCF_META_PREFIX . $field_id])) { continue; } $wpcf->conditional->set($post, $field_id); $wpcf->conditional->context = 'relationship'; $_relationship_name = false; // Set name and other values processed by hooks $parent = get_post($parent_id); if (!empty($parent->ID)) { $wpcf->relationship->set($parent, $post); $wpcf->relationship->cf->set($post, $field_id); $_child = $wpcf->relationship->get_child(); $_child->form->cf->set($post, $field_id); $_relationship_name = $_child->form->alter_form_name('wpcf[' . $wpcf->conditional->cf['id'] . ']'); } if (!$_relationship_name) { continue; } /* * BREAKPOINT * Adds filtering regular evaluation (not wpv_conditional) */ add_filter('types_field_get_submitted_data', 'wpcf_relationship_ajax_data_filter', 10, 2); $name = $_relationship_name; } else { $wpcf->conditional->set($post, $field_id); $name = 'wpcf[' . $wpcf->conditional->cf['id'] . ']'; } // Evaluate $passed = $wpcf->conditional->evaluate(); if ($passed) { $js_execute .= 'jQuery(\'[name^="' . $name . '"]\').parents(\'.' . 'wpcf-conditional' . '\').show().removeClass(\'' . 'wpcf-conditional' . '-failed\').addClass(\'' . 'wpcf-conditional' . '-passed\');' . " "; $js_execute .= 'jQuery(\'[name^="' . $name . '"]\').parents(\'.wpcf-repetitive-wrapper\').show();'; } else { $js_execute .= 'jQuery(\'[name^="' . $name . '"]\').parents(\'.wpcf-repetitive-wrapper\').hide();'; $js_execute .= 'jQuery(\'[name^="' . $name . '"]\').parents(\'.' . 'wpcf-conditional' . '\').hide().addClass(\'' . 'wpcf-conditional' . '-failed\').removeClass(\'' . 'wpcf-conditional' . '-passed\');' . " "; } } $_processed[$field_id] = true; } } } return $js_execute; }