/** * View function. * * @param type $params */ function wpcf_fields_select_view($params) { $field = wpcf_fields_get_field_by_slug($params['field']['slug']); $output = ''; if (!empty($field['data']['options'])) { $field_value = $params['field_value']; foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && $option['value'] == $params['field_value']) { $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' title', $option['title']); } } $output = $field_value; } return $output; }
/** * Get the TYPES field label from the custom field name. * * @param $custom_field_name * * @return mixed */ public function get_field_label($custom_field_name) { $result = $custom_field_name; if (!isset($this->_options['display_types_label_on_facet']) || !(self::CONST_TYPES_FIELD_PREFIX === substr($custom_field_name, 0, strlen(self::CONST_TYPES_FIELD_PREFIX)))) { // No need to replace custom field name by types field label return $result; } $custom_field_name_without_prefix = substr($custom_field_name, strlen(self::CONST_TYPES_FIELD_PREFIX)); $field = wpcf_fields_get_field_by_slug($custom_field_name_without_prefix); // Retrieve field among TYPES fields if (isset($field)) { $result = $field['name']; } return $result; }
/** * View function. * * @param type $params */ function wpcf_fields_select_view($params) { if (isset($params['usermeta']) && !empty($params['usermeta'])) { $field = wpcf_fields_get_field_by_slug($params['field']['slug'], 'wpcf-usermeta'); } else { $field = wpcf_fields_get_field_by_slug($params['field']['slug']); } $output = ''; if (!empty($field['data']['options'])) { $field_value = $params['field_value']; foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && $option['value'] == $params['field_value']) { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' title', $option['title']); } } $output = $field_value; } return $output; }
/** * View function. * * @param type $params */ function wpcf_fields_checkbox_view($params) { $output = ''; $option_name = 'wpcf-fields'; if (isset($params['usermeta']) && !empty($params['usermeta'])) { $option_name = 'wpcf-usermeta'; } if (isset($params['option_name'])) { $option_name = $params['option_name']; } if (isset($params['state']) && $params['state'] == 'unchecked' && empty($params['field_value'])) { if (empty($params['#content'])) { return '__wpcf_skip_empty'; } return htmlspecialchars_decode($params['#content']); } elseif (isset($params['state']) && $params['state'] == 'unchecked') { return '__wpcf_skip_empty'; } if (isset($params['state']) && $params['state'] == 'checked' && !empty($params['field_value'])) { if (empty($params['#content'])) { return '__wpcf_skip_empty'; } return htmlspecialchars_decode($params['#content']); } elseif (isset($params['state']) && $params['state'] == 'checked') { return '__wpcf_skip_empty'; } if (!empty($params['#content']) && !empty($params['field_value'])) { return htmlspecialchars_decode($params['#content']); } // Check if 'save_empty' is yes and if value is 0 - set value to empty string if (isset($params['field']['data']['save_empty']) && $params['field']['data']['save_empty'] == 'yes' && $params['field_value'] == '0' && 'db' != $params['field']['data']['display']) { $params['field_value'] = ''; } if ('db' == $params['field']['data']['display'] && $params['field_value'] != '') { $output = $params['field_value']; // Show the translated value if we have one. $field = wpcf_fields_get_field_by_slug($params['field']['slug'], $option_name); $output = wpcf_translate('field ' . $field['id'] . ' checkbox value', $output); } elseif ($params['field']['data']['display'] == 'value' && $params['field_value'] != '') { if (!empty($params['field']['data']['display_value_selected'])) { $output = $params['field']['data']['display_value_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value selected', $output); } } elseif ($params['field']['data']['display'] == 'value' && !empty($params['field']['data']['display_value_not_selected'])) { $output = $params['field']['data']['display_value_not_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value not selected', $output); } else { return '__wpcf_skip_empty'; } return $output; }
/** * Important save_post hook. * * Core function. Works and stable. Do not move or change. * If required, add hooks only. * * @internal breakpoint * @param type $post_ID * @param type $post */ function wpcf_admin_post_save_post_hook($post_ID, $post) { global $wpcf; // Basic cheks /* * Allow this hook to be triggered only if Types form is submitted */ if (!isset($_POST['_wpcf_post_wpnonce']) || !wp_verify_nonce($_POST['_wpcf_post_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) { return false; } /* * Do not save post if is type of: * revision * attachment * wp-types-group * view * view-template * cred-form */ if (in_array($post->post_type, $wpcf->excluded_post_types)) { return false; } /* * * * Get all groups connected to this $post */ $groups = wpcf_admin_post_get_post_groups_fields($post); if (empty($groups)) { return false; } $all_fields = array(); $_not_valid = array(); $_error = false; /* * * * Loop over each group * * TODO Document this * Connect 'wpcf-invalid-fields' with all fields */ foreach ($groups as $group) { // Process fields $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation'); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; // Collect all not valid fields if ($form->isError()) { $_error = true; // Set error only to true $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid()); } } // Set fields foreach ($all_fields as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Field(); $_temp->set($wpcf->post, $v['wpcf-id']); $all_fields[$k]['_field'] = $_temp; } foreach ($_not_valid as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Field(); $_temp->set($wpcf->post, $v['wpcf-id']); $_not_valid[$k]['_field'] = $_temp; } /* * * Allow interaction here. * Conditional will set $error to false if field is conditional * and not submitted. */ $error = apply_filters('wpcf_post_form_error', $_error, $_not_valid, $all_fields); $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields); // Notify user about error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } /* * Save invalid elements so user can be informed after redirect. */ if (!empty($not_valid)) { update_post_meta($post_ID, 'wpcf-invalid-fields', $not_valid); } /* * * * * * Save meta fields */ if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } // Set field $wpcf->field->set($post_ID, $field); // Skip copied fields // CHECKPOINT if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } // Don't save invalid // CHECKPOINT if (isset($not_valid[$field['slug']])) { continue; } /* * * * Saving fields * @since 1.2 * * We changed way repetitive fields are saved. * On each save fields are rewritten and order is saved in * '_$slug-sort-order' meta field. */ /* * * We marked fields as repetitive in POST['__wpcf_repetitive'] * Without this check we won't save any. * @see WPCF_Repeater::get_fields_form() */ if (isset($_POST['__wpcf_repetitive'][$wpcf->field->slug])) { /* * Use here WPCF_Repeater class. * WPCF_Repeater::set() - switches to current post * WPCF_Repeater::save() - saves repetitive field */ $wpcf->repeater->set($post_ID, $field); $wpcf->repeater->save(); } else { /* * Use WPCF_Field::save() */ $wpcf->field->save(); } do_action('wpcf_post_field_saved', $post_ID, $field); } } /* * Process checkboxes * * TODO Revise and remove * Since Types 1.1.5 we moved this check to embedded/includes/checkbox.php * checkbox.php added permanently to bootstrap. */ foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } // if ( $field['#type'] == 'checkbox' // && !isset( $_POST['wpcf'][$field['wpcf-slug']] ) ) { // $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] ); // if ( isset( $field_data['data']['save_empty'] ) // && $field_data['data']['save_empty'] == 'yes' ) { // update_post_meta( $post_ID, // wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'], // 0 ); // } else { // delete_post_meta( $post_ID, // wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'] ); // } // } if ($field['#type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id']); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id]; } } update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } } } do_action('wpcf_post_saved', $post_ID); }
/** * 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; }
/** * Unified save child function. * * @param int $parent_id * @param int $child_id * @param array $save_fields * @return bool|WP_Error */ function save_child($parent_id, $child_id, $save_fields = array()) { $parent = get_post(intval($parent_id)); $child = get_post(intval($child_id)); $post_data = array(); if (empty($parent) || empty($child)) { return new WP_Error('wpcf-relationship-save-child', 'no parent/child post'); } // Save relationship update_post_meta($child->ID, '_wpcf_belongs_' . $parent->post_type . '_id', $parent->ID); // Check if added via AJAX $check = get_post_meta($child->ID, '_wpcf_relationship_new', true); $new = !empty($check); delete_post_meta($child->ID, '_wpcf_relationship_new'); // Set post data $post_data['ID'] = $child->ID; // Title needs to be checked if submitted at all if (!isset($save_fields['_wp_title'])) { // If not submitted that means it is not offered to be edited if (!empty($child->post_title)) { $post_title = $child->post_title; } else { // DO NOT LET IT BE EMPTY $post_title = $child->post_type . ' ' . $child->ID; } } else { $post_title = $save_fields['_wp_title']; } $post_data['post_title'] = $post_title; $post_data['post_content'] = isset($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $child->post_content; $post_data['post_type'] = $child->post_type; // Check post status - if new, convert to 'publish' else keep remaining if ($new) { $post_data['post_status'] = 'publish'; } else { $post_data['post_status'] = get_post_status($child->ID); } /* * * * * * * * UPDATE POST */ $cf = new WPCF_Field(); if (isset($_POST['wpcf_post_relationship'][$parent_id]) && isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) { $_POST['wpcf'] = array(); foreach ($_POST['wpcf_post_relationship'][$parent_id][$child_id] as $slug => $value) { $_POST['wpcf'][$cf->__get_slug_no_prefix($slug)] = $value; $_POST['wpcf'][$slug] = $value; } } unset($cf); /** * avoid filters for children * / global $wp_filter; $save_post = $wp_filter['save_post']; $wp_filter['save_post'] = array(); */ $updated_id = wp_update_post($post_data); /* $wp_filter['save_post'] = $save_post; */ unset($save_post); if (empty($updated_id)) { return new WP_Error('relationship-update-post-failed', 'Updating post failed'); } // Save parents if (!empty($save_fields['parents'])) { foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) { update_post_meta($child->ID, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id); } } // Update taxonomies if (!empty($save_fields['taxonomies']) && is_array($save_fields['taxonomies'])) { $_save_data = array(); foreach ($save_fields['taxonomies'] as $taxonomy => $t) { if (!is_taxonomy_hierarchical($taxonomy)) { $_save_data[$taxonomy] = strval($t); continue; } foreach ($t as $term_id) { if ($term_id != '-1') { $term = get_term($term_id, $taxonomy); if (empty($term)) { continue; } $_save_data[$taxonomy][] = $term_id; } } } wp_delete_object_term_relationships($child->ID, array_keys($save_fields['taxonomies'])); foreach ($_save_data as $_taxonomy => $_terms) { wp_set_post_terms($child->ID, $_terms, $_taxonomy, $append = false); } } // Unset non-types unset($save_fields['_wp_title'], $save_fields['_wp_body'], $save_fields['parents'], $save_fields['taxonomies']); /* * * * * * * * UPDATE Loop over fields */ foreach ($save_fields as $slug => $value) { if (defined('WPTOOLSET_FORMS_VERSION')) { // Get field by slug $field = wpcf_fields_get_field_by_slug(str_replace(WPCF_META_PREFIX, '', $slug)); if (empty($field)) { continue; } // Set config $config = wptoolset_form_filter_types_field($field, $child->ID); // Check if valid $valid = wptoolset_form_validate_field('post', $config, $value); if (is_wp_error($valid)) { $errors = $valid->get_error_data(); $msg = sprintf(__('Child post "%s" field "%s" not updated:', 'wpcf'), $child->post_title, $field['name']); wpcf_admin_message_store($msg . ' ' . implode(', ', $errors), 'error'); continue; } } $this->cf->set($child, $field); $this->cf->context = 'post_relationship'; $this->cf->save($value); } do_action('wpcf_relationship_save_child', $child, $parent); clean_post_cache($parent->ID); clean_post_cache($child->ID); // Added because of caching meta 1.5.4 wp_cache_flush(); return true; }
/** * Remove all conditions. * * @param $id */ public function remove_conditions($id, $group_conditions = false) { if ($group_conditions) { delete_post_meta($id, '_wpcf_conditional_display'); } else { $field = wpcf_fields_get_field_by_slug($id); if (empty($field)) { return; } $all_types_fields = get_option('wpcf-fields', array()); $all_types_fields[$id]['data']['conditional_display'] = array(); update_option('wpcf-fields', $all_types_fields); } }
function wpcf_admin_userprofilesave_init($user_id) { if (defined('WPTOOLSET_FORMS_VERSION')) { global $wpcf; $errors = false; /** * check checkbox type fields to delete or save empty if needed */ $groups = wpcf_admin_usermeta_get_groups_fields(); foreach ($groups as $group) { if (!array_key_exists('fields', $group) || empty($group['fields'])) { continue; } foreach ($group['fields'] as $field) { switch ($field['type']) { case 'checkboxes': if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) { delete_user_meta($user_id, $field['meta_key']); } break; case 'checkbox': if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) { if ('yes' == $field['data']['save_empty']) { $_POST['wpcf'][$field['slug']] = 0; } else { delete_user_meta($user_id, $field['meta_key']); } } break; } } } // Save meta fields if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta'); if (empty($field)) { continue; } // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value; // Set config $config = wptoolset_form_filter_types_field($field, $user_id); foreach ($_field_value as $_k => $_val) { // Check if valid $valid = wptoolset_form_validate_field('your-profile', $config, $_val); if (is_wp_error($valid)) { $errors = true; $_errors = $valid->get_error_data(); $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']); wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error'); if (types_is_repetitive($field)) { unset($field_value[$_k]); } else { break; } } } // Save field if (types_is_repetitive($field)) { $wpcf->usermeta_repeater->set($user_id, $field); $wpcf->usermeta_repeater->save($field_value); } else { $wpcf->usermeta_field->set($user_id, $field); $wpcf->usermeta_field->usermeta_save($field_value); } do_action('wpcf_user_field_saved', $user_id, $field); // TODO Move to checkboxes if ($field['type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['id'], false, false, false, 'wpcf-usermeta'); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['id']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id]; } } update_user_meta($user_id, $field['meta_key'], $update_data); } } } } if ($errors) { update_post_meta($user_id, '__wpcf-invalid-fields', true); } do_action('wpcf_user_saved', $user_id); return; } global $wpcf; $all_fields = array(); $_not_valid = array(); $_error = false; $error = ''; $groups = $groups = wpcf_admin_usermeta_get_groups_fields(); if (empty($groups)) { return false; } foreach ($groups as $group) { // Process fields $fields = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false, 'validation'); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; // Collect all not valid fields if ($form->isError()) { $_error = true; // Set error only to true $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid()); } } // Set fields foreach ($all_fields as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Usermeta_Field(); $_temp->set($user_id, $v['wpcf-id']); $all_fields[$k]['_field'] = $_temp; } foreach ($_not_valid as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Usermeta_Field(); $_temp->set($user_id, $v['wpcf-id']); $_not_valid[$k]['_field'] = $_temp; } $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields); // Notify user about error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } /* * Save invalid elements so user can be informed after redirect. */ if (!empty($not_valid)) { update_user_meta($user_id, 'wpcf-invalid-fields', $not_valid); } if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta'); if (empty($field)) { continue; } $wpcf->usermeta_field->set($user_id, $field); if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } if (isset($_POST['__wpcf_repetitive'][$wpcf->usermeta_field->slug])) { $wpcf->usermeta_repeater->set($user_id, $field); $wpcf->usermeta_repeater->save(); } else { $wpcf->usermeta_field->usermeta_save(); } do_action('wpcf_post_field_saved', '', $field); } //end foreach } //end if foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } if ($field['#type'] == 'checkbox') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta'); if (!isset($_POST['wpcf'][$field['wpcf-slug']])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0); } else { delete_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']); } } } if ($field['#type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta'); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id]; } } update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } } } }
function short_code_menu_callback($index, $cf_key, $function_name, $menu, $shortcode) { global $link_layout_number, $wpdb; static $fields_started = false; static $templates_started = false; if ($menu == __('View template', 'wpv-views') && !$templates_started) { $templates_started = true; echo '</tr><td> </td><tr>'; echo '</tr><tr><td><strong>' . __('View templates', 'wpv-views') . '</strong></td>'; echo '</tr><tr>'; $link_layout_number = 0; } if ($menu != '' && $menu != __('View template', 'wpv-views') && !$fields_started) { $fields_started = true; echo '</tr><td> </td><tr>'; echo '</tr><tr><td><strong>' . __('Fields', 'wpv-views') . '</strong></td>'; echo '</tr><tr>'; $link_layout_number = 0; } if (!($link_layout_number % 2)) { if ($link_layout_number != 0) { echo '</tr><tr>'; } } if ($menu == __('View template', 'wpv-views')) { // get the View template title. $field_name = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE post_type='view-template' AND post_name=%s", $cf_key)); if (!$field_name) { $field_name = $cf_key; } } else { $field_name = $cf_key; if (function_exists('wpcf_types_get_meta_prefix')) { // we have types. // Get the field name for display $types_prefix = wpcf_types_get_meta_prefix(); if (strpos($cf_key, $types_prefix) === 0) { $field_info = wpcf_fields_get_field_by_slug(substr($cf_key, strlen($types_prefix))); if (isset($field_info['name'])) { $field_name = $field_info['name']; } } } } echo '<td><a style="cursor: pointer" onclick="on_add_field_wpv(\'' . $menu . '\', \'' . esc_js($cf_key) . '\', \'' . base64_encode($field_name) . '\')">'; echo $field_name; echo '</a></td>'; $link_layout_number++; }
/** * The save_post hook. * * @param int $post_ID * @param WP_Post $post * @since unknown */ function wpcf_admin_post_save_post_hook($post_ID, $post) { // Apparently, some things are *slightly* different when saving a child post in the Post relationships metabox. // Ugly hack that will go away with m2m. // // Note: The types_updating_child_post filter is needed for a situation when user clicks on the Update button // for the parent post. In that case we don't get enough information to determine if a child post is being updated. $is_child_post_update = 'wpcf_ajax' == wpcf_getget('action') && 'pr_save_child_post' == wpcf_getget('wpcf_action') || apply_filters('types_updating_child_post', false); global $wpcf; $errors = false; // Do not save post if is type of if (in_array($post->post_type, array('revision', 'view', 'view-template', 'cred-form', 'cred-user-form', 'nav_menu_item', 'mediapage'))) { return; } $_post_wpcf = array(); if (!empty($_POST['wpcf'])) { $_post_wpcf = $_POST['wpcf']; } // handle checkbox if (array_key_exists('_wptoolset_checkbox', $_POST) && is_array($_POST['_wptoolset_checkbox'])) { foreach ($_POST['_wptoolset_checkbox'] as $key => $field_value) { $field_slug = preg_replace('/^wpcf\\-/', '', $key); if (array_key_exists($field_slug, $_post_wpcf)) { continue; } $_post_wpcf[$field_slug] = false; } } // handle radios if (array_key_exists('_wptoolset_radios', $_POST) && is_array($_POST['_wptoolset_radios'])) { foreach ($_POST['_wptoolset_radios'] as $key => $field_value) { $field_slug = preg_replace('/^wpcf\\-/', '', $key); if (array_key_exists($field_slug, $_post_wpcf)) { continue; } $_post_wpcf[$field_slug] = false; } } if (count($_post_wpcf)) { $add_error_message = true; if (isset($_POST['post_id']) && $_POST['post_id'] != $post_ID) { $add_error_message = false; } // check some attachment to delete $delete_attachments = apply_filters('wpcf_delete_attachments', true); $images_to_delete = array(); if ($delete_attachments && isset($_POST['wpcf']) && array_key_exists('delete-image', $_POST['wpcf']) && is_array($_POST['wpcf']['delete-image'])) { foreach ($_POST['wpcf']['delete-image'] as $image) { $images_to_delete[$image] = 1; } } foreach ($_post_wpcf as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } // This is (apparently) expected for repetitive fields... if ($is_child_post_update && types_is_repetitive($field)) { $field_value = array($field_value); } $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value; // Set config $config = wptoolset_form_filter_types_field($field, $post_ID, $_post_wpcf); // remove from images_to_delete if user add again if ($delete_attachments && 'image' == $config['type']) { $images = $_field_value; if (!is_array($images)) { $images = array($images); } foreach ($images as $image) { if (array_key_exists($image, $images_to_delete)) { unset($images_to_delete[$image]); } } } // add filter to remove field name from error message // This filter is toolset-common/toolset-forms/classes/class.validation.php add_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1); foreach ($_field_value as $_k => $_val) { // Check if valid $validation = wptoolset_form_validate_field('post', $config, $_val); $conditional = wptoolset_form_conditional_check($config); $not_valid = is_wp_error($validation) || !$conditional; if ($add_error_message && is_wp_error($validation)) { $errors = true; $_errors = $validation->get_error_data(); $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']); wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error'); } if ($not_valid) { if (types_is_repetitive($field)) { unset($field_value[$_k]); } else { break; } } } remove_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1); // Save field if (types_is_repetitive($field)) { $wpcf->repeater->set($post_ID, $field); $wpcf->repeater->save($field_value); } else { if (!$not_valid) { $wpcf->field->set($post_ID, $field); $wpcf->field->save($field_value); } } do_action('wpcf_post_field_saved', $post_ID, $field); // TODO Move to checkboxes if ($field['type'] == 'checkboxes') { if (!empty($field['data']['options'])) { $update_data = array(); foreach ($field['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['id']][$option_id])) { if (isset($field['data']['save_empty']) && $field['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id]; } } update_post_meta($post_ID, $field['meta_key'], $update_data); } } } // delete images if ($delete_attachments && !empty($images_to_delete)) { $args = array('post_parent' => $post_ID, 'posts_per_page' => -1); $children_array = get_children($args); foreach ($children_array as $child) { if (!array_key_exists($child->guid, $images_to_delete)) { continue; } wp_delete_attachment($child->ID); } } } if ($errors) { update_post_meta($post_ID, '__wpcf-invalid-fields', true); } do_action('wpcf_post_saved', $post_ID); return; }
/** * Calls view function for specific field type. * * @param type $field * @param type $atts * @return type */ function types_render_field($field, $params, $content = null, $code = '') { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; // Count fields (if there are duplicates) static $count = array(); // Get field $field = wpcf_fields_get_field_by_slug($field); if (empty($field)) { return ''; } // Count it if (!isset($count[$field['slug']])) { $count[$field['slug']] = 1; } else { $count[$field['slug']] += 1; } // Get post field value global $post; $value = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($value == '' && $field['type'] != 'checkbox') { return ''; } // Load type $type = wpcf_fields_type_action($field['type']); // Apply filters to field value $value = apply_filters('wpcf_fields_value_display', $value); $value = apply_filters('wpcf_fields_slug_' . $field['slug'] . '_value_display', $value); $value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_display', $value); // To make sure if (is_string($value)) { $value = addslashes(stripslashes($value)); } // Set values $field['name'] = wpcf_translate('field ' . $field['id'] . ' name', $field['name']); $params['field'] = $field; $params['post'] = $post; $params['field_value'] = $value; // Get output $params['#content'] = htmlspecialchars($content); $params['#code'] = $code; $output = wpcf_fields_type_action($field['type'], 'view', $params); // Convert to string if (!empty($output)) { $output = strval($output); } // @todo Reconsider if ever changing this (works fine now) // If no output or 'raw' return default if (($params['raw'] == 'true' || empty($output)) && !empty($value)) { $field_name = ''; if ($params['show_name'] == 'true') { $field_name = wpcf_frontend_wrap_field_name($field, $field['name'], $params); } $field_value = wpcf_frontend_wrap_field_value($field, $value, $params); $output = wpcf_frontend_wrap_field($field, $field_name . $field_value); } // Apply filters $output = strval(apply_filters('types_view', $output, $value, $field['type'], $field['slug'], $field['name'], $params)); // Add count if (isset($count[$field['slug']]) && intval($count[$field['slug']]) > 1) { $add = '-' . intval($count[$field['slug']]); $output = str_replace('id="wpcf-field-' . $field['slug'] . '"', 'id="wpcf-field-' . $field['slug'] . $add . '"', $output); } return htmlspecialchars_decode(stripslashes($output)); }
/** * View function. * * @param type $params */ function wpcf_fields_checkbox_view($params) { $output = ''; $option_name = 'wpcf-fields'; if (isset($params['usermeta']) && !empty($params['usermeta'])) { $option_name = 'wpcf-usermeta'; } else { if (isset($params['termmeta']) && !empty($params['termmeta'])) { $option_name = 'wpcf-termmeta'; } } if (isset($params['option_name'])) { $option_name = $params['option_name']; } if (isset($params['state']) && $params['state'] == 'unchecked' && empty($params['field_value'])) { if (empty($params['#content'])) { return '__wpcf_skip_empty'; } return htmlspecialchars_decode($params['#content']); } elseif (isset($params['state']) && $params['state'] == 'unchecked') { return '__wpcf_skip_empty'; } if (isset($params['state']) && $params['state'] == 'checked' && !empty($params['field_value'])) { if (empty($params['#content'])) { return '__wpcf_skip_empty'; } return htmlspecialchars_decode($params['#content']); } elseif (isset($params['state']) && $params['state'] == 'checked') { return '__wpcf_skip_empty'; } if (!empty($params['#content']) && !empty($params['field_value'])) { return htmlspecialchars_decode($params['#content']); } // Check if 'save_empty' is yes and if value is 0 - set value to empty string if (isset($params['field']['data']['save_empty']) && $params['field']['data']['save_empty'] == 'yes' && $params['field_value'] == '0' && 'db' != $params['field']['data']['display']) { $params['field_value'] = ''; } if ('db' == $params['field']['data']['display'] && $params['field_value'] != '') { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $output = $params['field_value']; // Show the translated value if we have one. $field = wpcf_fields_get_field_by_slug($params['field']['slug'], $option_name); if (is_array($field) && isset($field['id'])) { $output = wpcf_translate('field ' . $field['id'] . ' checkbox value', $output); } } elseif ($params['field']['data']['display'] == 'value' && $params['field_value'] != '') { if (!empty($params['field']['data']['display_value_selected'])) { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $output = $params['field']['data']['display_value_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value selected', $output); } } elseif ($params['field']['data']['display'] == 'value' && !empty($params['field']['data']['display_value_not_selected'])) { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $output = $params['field']['data']['display_value_not_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value not selected', $output); } else { return '__wpcf_skip_empty'; } return $output; }
</p> <!-- TYPES TIP: Custom code to load Types function and render a REPEATING custom field "House Photos" but with each output thumbnail image also linking to the full-size image file (for lightbox effect, for example). --> <p><strong>Property Photos</strong></p> <?php // Do nothing if we don't have Types. if (apply_filters('types_is_active', false)) { $output = ''; // ID of the current post $post_id = get_the_ID(); // Slug of a Types repeating image field, without the "wpcf-" prefix. $field_slug = 'house-photos'; // TODO set the field slug you want to display // Parameters that define the field $field_definition = wpcf_fields_get_field_by_slug($field_slug); if (!empty($field_definition)) { // Get the raw field data. $images = get_post_meta($post_id, "wpcf-{$field_slug}"); foreach ($images as $image_index => $image) { // Parameters for the Types field rendering mechanism. $image_parameters = array('proportional' => 'true', 'url' => 'true', 'field_value' => $image); // Get an image of specific (maximum) proportions. // NOTE: Update image size to your needs $thumbnail_parameters = array_merge($image_parameters, array('width' => '200', 'height' => '200')); $thumbnail_url = types_render_field_single($field_definition, $thumbnail_parameters, null, '', $image_index); // Get the image in full size. $fullsize_parameters = array_merge($image_parameters, array('size' => 'full')); $fullsize_url = types_render_field_single($field_definition, $fullsize_parameters, null, '', $image_index); // Append the markup (a thumbnail linking to the full image) to existing content. // NOTE: Customize the output to your needs
/** * save_post hook. * * @param type $post_ID * @param type $post */ function wpcf_admin_post_save_post_hook($post_ID, $post) { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) { return false; } if (in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) { return false; } // Get groups $groups = wpcf_admin_post_get_post_groups_fields($post); if (empty($groups)) { return false; } $all_fields = array(); foreach ($groups as $group) { // Process fields $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation'); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; $error = $form->isError(); // Trigger form error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } } // Save invalid elements so user can be informed after redirect if (!empty($all_fields)) { update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields); } // Save meta fields if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field_slug])) { continue; } $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } $meta_key = wpcf_types_get_meta_prefix($field) . $field_slug; // Don't save invalid if (isset($all_fields[$meta_key]['#error'])) { continue; } // Repetitive fields if (isset($_POST['wpcf_repetitive'][$field_slug])) { delete_post_meta($post_ID, $meta_key); foreach ($field_value as $temp_id => $repetitive_data) { // Skype specific if ($field['type'] == 'skype') { unset($repetitive_data['old_value']); wpcf_admin_post_save_field($post_ID, $meta_key, $field, $repetitive_data, true); } else { wpcf_admin_post_save_field($post_ID, $meta_key, $field, $repetitive_data['new_value'], true); } } } else { wpcf_admin_post_save_field($post_ID, $meta_key, $field, $field_value); } } } // Process checkboxes foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } if ($field['#type'] == 'checkbox' && !isset($_POST['wpcf'][$field['wpcf-slug']])) { $field_data = wpcf_admin_fields_get_field($field['wpcf-id']); if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0); } else { delete_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']); } } if ($field['#type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id']); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id]; } } update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } } } }
/** * View function. * * @param type $params */ function wpcf_fields_radio_view($params) { if ($params['style'] == 'raw') { return ''; } $field = wpcf_fields_get_field_by_slug($params['field']['slug']); $output = ''; // See if user specified output for each field if (isset($params['option'])) { foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && $option['value'] == $params['field_value'] && $option_key == $params['option']) { return htmlspecialchars_decode($params['#content']); } } return ' '; } if (!empty($field['data']['options'])) { $field_value = $params['field_value']; foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && $option['value'] == $params['field_value']) { $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' title', $option['title']); if (isset($params['field']['data']['display']) && $params['field']['data']['display'] != 'db' && !empty($option['display_value'])) { $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' display value', $option['display_value']); } } } $field_value = wpcf_frontend_wrap_field_value($params['field'], $field_value, $params); $output = wpcf_frontend_wrap_field($params['field'], $field_value); } return $output; }
/** * Calls view function for specific field type. * * @global object $wpdb * * @param type $field * @param type $atts * @return type */ function types_render_usermeta_field($field_id, $params, $content = null, $code = '') { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; global $wpcf, $post, $wpdb; // HTML var holds actual output $html = ''; // Set post ID $post_id = $post->ID; if (isset($params['post_id']) && !empty($params['post_id'])) { $post_id = $params['post_id']; } // Get field $field = wpcf_fields_get_field_by_slug($field_id, 'wpcf-usermeta'); // If field not found return empty string if (empty($field)) { // Log if (!function_exists('wplogger')) { require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php'; } global $wplogger; $wplogger->log('types_render_usermeta_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG); return ''; } //Get user By ID if (isset($params['user_id'])) { $user_id = $params['user_id']; } else { if (isset($params['user_name'])) { //Get user by login $user_id = $wpdb->get_var($wpdb->prepare("SELECT * FROM " . $wpdb->users . " WHERE user_login = %s", $params['user_name'])); } else { if (isset($params['user_is_author'])) { //Get Post author $user_id = $post->post_author; } else { if (isset($params['user_current'])) { //Get current logged user $user_id = wpcf_usermeta_get_user(); } else { //If empty get post author, if no post, return empty if (!empty($post_id)) { $user_id = $post->post_author; } else { return; } } } } } if (empty($user_id)) { return; } // Set field $wpcf->usermeta_field->set($user_id, $field); // See if repetitive if (wpcf_admin_is_repetitive($field)) { $wpcf->usermeta_repeater->set($user_id, $field); $_meta = $wpcf->usermeta_repeater->_get_meta(); $meta = $_meta['custom_order']; // $meta = get_post_meta( $post_id, // wpcf_types_get_meta_prefix( $field ) . $field['slug'], false ); // Sometimes if meta is empty - array(0 => '') is returned if (count($meta) == 1) { $meta_id = key($meta); $_temp = array_shift($meta); if (strval($_temp) == '') { return ''; } else { $params['field_value'] = $_temp; return types_render_field_single($field, $params, $content, $code, $meta_id); } } else { if (!empty($meta)) { $output = ''; if (isset($params['index'])) { $index = $params['index']; } else { $index = ''; } // Allow wpv-for-each shortcode to set the index $index = apply_filters('wpv-for-each-index', $index); if ($index === '') { $output = array(); foreach ($meta as $temp_key => $temp_value) { $params['field_value'] = $temp_value; $temp_output = types_render_field_single($field, $params, $content, $code, $temp_key); if (!empty($temp_output)) { $output[] = $temp_output; } } if (!empty($output) && isset($params['separator'])) { $output = implode(html_entity_decode($params['separator']), $output); } else { if (!empty($output)) { $output = implode('', $output); } else { return ''; } } } else { // Make sure indexed right $_index = 0; foreach ($meta as $temp_key => $temp_value) { if ($_index == $index) { $params['field_value'] = $temp_value; return types_render_field_single($field, $params, $content, $code, $temp_key); } $_index++; } // If missed index return ''; } $html = $output; } else { return ''; } } } else { $params['field_value'] = get_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($params['field_value'] == '' && $field['type'] != 'checkbox') { return ''; } $html = types_render_field_single($field, $params, $content, $code, $wpcf->usermeta_field->meta_object->umeta_id); } // API filter // $wpcf->usermeta_field->set( $user_id, $field ); return $wpcf->usermeta_field->html($html, $params); }
/** * AJAX save child item call. * * @param type $parent_post * @param type $post_type * @param type $data * @return string */ function wpcf_pr_admin_save_child_item($parent_post, $post_type, $data) { if (!empty($data['wpcf_post_relationship'])) { $post_id = key($data['wpcf_post_relationship']); $save_fields = array_shift($data['wpcf_post_relationship']); if (strpos($post_id, 'new_') !== false) { $post_data['post_title'] = !empty($save_fields['_wp_title']) ? $save_fields['_wp_title'] : 'post ' . $post_id; $post_data['post_content'] = !empty($save_fields['_wp_body']) ? $save_fields['_wp_body'] : ''; $post_data['post_type'] = $post_type; $post_data['post_status'] = 'publish'; } else { $post_data = (array) get_post($post_id); $post_data['post_title'] = !empty($save_fields['_wp_title']) ? $save_fields['_wp_title'] : $post_data['post_title']; $post_data['post_content'] = !empty($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $post_data['post_content']; } unset($save_fields['_wp_title'], $save_fields['_wp_body']); $post_id = wp_insert_post($post_data); // Save other parents if (isset($save_fields['parents'])) { foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) { update_post_meta($post_id, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id); } unset($save_fields['parents']); } foreach ($save_fields as $meta_key => $field_value) { // Process fields $field_slug = str_replace(WPCF_META_PREFIX, '', $meta_key); $field = wpcf_fields_get_field_by_slug($field_slug); if (!empty($field)) { // Apply filters $field_value = apply_filters('wpcf_fields_value_save', $field_value, $field['type'], $field_slug); $field_value = apply_filters('wpcf_fields_slug_' . $field_slug . '_value_save', $field_value); $field_value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_save', $field_value); $field_value = apply_filters('wpcf_pr_fields_type_' . $field['type'] . '_value_save', $field_value, $meta_key, $post_id); do_action('wpcf_fields_slug_' . $field_slug . '_save', $field_value); do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value); } update_post_meta($post_id, $meta_key, $field_value); } // Process checkboxes if (isset($data['wpcf_post_relationship_checkbox'][$post_id])) { $check_meta = key($data['wpcf_post_relationship_checkbox'][$post_id]); if (!isset($save_fields[$check_meta])) { delete_post_meta($post_id, $check_meta); } } update_post_meta($post_id, '_wpcf_belongs_' . $parent_post->post_type . '_id', $parent_post->ID); $item = get_post($post_id); $relationships = get_option('wpcf_post_relationship', array()); if (!isset($relationships[$parent_post->post_type][$item->post_type])) { return __('Post not updated. Relationship data missing.', 'wpcf'); } $data = $relationships[$parent_post->post_type][$item->post_type]; $output = wpcf_pr_admin_post_meta_box_has_row($parent_post, $item->post_type, $data, $parent_post->post_type, $item); return $output; } return __('Post not updated. Relationship data missing.', 'wpcf'); }
/** * Filters conditional. * * There are two types of conditionals: * 1. Regular conditionals created using Types GUI * 2. Custom onditionals (user entered manually) * * 1. Regular conditional * * Main properties: * [relation] - AND|OR evaluate as true if all or any condition is TRUE * [conditions] - array of conditions * [values] - values to check against (used only by PHP), evaluate method * should not be aware if it's checking post meta or user meta, * instead array of needed values (or missing) are passed to method. * Types use filteres get_post_meta() and get_user_meta(). * * [conditions] * Each conditions is array with properties: * id: ID of trigger field (this field value is checked) to evaluate * this field as TRUE or FALSE (corresponds to main IDs set here) * type: type of trigger field. JS and PHP needs at least this information * when processing condtitional evaluation * operator: which operation to perform (=|>|<|>=|<=|!=|between) * args: arguments passed to checking functions * * Example of reguar conditional * * [conditional] => Array( [relation] => OR [conditions] => Array( [0] => Array( [id] => wpcf-my-date [type] => date [operator] => = [args] => Array( [0] => 02/01/2014 ) ) [1] => Array( [id] => wpcf-my-date [type] => date [operator] => between [args] => Array( [0] => 02/07/2014 [1] => 02/10/2014 ) ) ) [values] => Array( [wpcf-my-date] => 32508691200 ) ) * * * 2. 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($field, $post_id, $_post_wpcf = array()) { $field = self::getConfig($field); if (is_null($field)) { return array(); } // Caching static $cache = array(); $cache_key = md5(serialize($field) . $post_id); if (isset($cache[$cache_key])) { return $cache[$cache_key]; } /* Suffix - used to construct ID of this field and other fields connected * to it via conditionals. * * @see at bottom 'General field settings' * * Reason to use it - Types have child posts forms inside same form as * main fields. It's like having same sets of fields inside same form. * Main post fields do not have suffix. * * Example main field: * ID: wpcf-text * conditional: '$wpcf-date > DATE(01,02,2014)' * * Example child field: * ID: wpcf-text-123 * conditional: '$wpcf-date-123 > DATE(01,02,2014)' * Suffix is child post ID (wpcf-text-$child_post_id). * * That way right triggers and conditional fields are mapped. */ $suffix = isset($field['suffix']) ? $field['suffix'] : ''; $cond = array(); if (empty($field['meta_type'])) { $field['meta_type'] = 'postmeta'; } // Get [values] $cond_values = self::getConditionalValues($post_id, $field['meta_type']); if (function_exists('wpcf_fields_get_field_by_slug')) { // Update the conditional values according to what's being saved. foreach ($_post_wpcf as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } $field_value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_save', $field_value, $field, null); $cond_values[$field['meta_key']] = $field_value; } } // Set custom conditional if (!empty($field['data']['conditional_display']['custom_use'])) { require_once 'class.conditional.php'; $custom = $field['data']['conditional_display']['custom']; // Extract field values ('${$field_name}') $cond = self::getCustomConditional($custom, $suffix, $cond_values); // Regular conditional } elseif (!empty($field['data']['conditional_display']['conditions'])) { $cond = array('relation' => $field['data']['conditional_display']['relation'], 'conditions' => array(), 'values' => array()); // Loop over conditions and collect settings foreach ($field['data']['conditional_display']['conditions'] as $d) { // Get field settings $c_field = self::getConfig($d['field']); // If it's Types field if (!empty($c_field)) { $_c = array('id' => self::getPrefix($c_field) . $d['field'] . $suffix, 'type' => $c_field['type'], 'operator' => $d['operation'], 'args' => array($d['value'])); $cond['conditions'][] = $_c; // Apply filters from field (that is why we set 'type' property) wptoolset_form_field_add_filters($field['type']); $key = $c_field['meta_key']; if (isset($cond_values[$key])) { $cond['values'][$key . $suffix] = apply_filters('wptoolset_conditional_value_php', $cond_values[$key], $c_field['type']); } // Otherwise do nothing add [values] // That allows checking for non-Types field // TODO REVIEW THIS } elseif (isset($cond_values[$d['field']])) { $cond['values'][$d['field'] . $suffix] = $cond_values[$d['field']]; } } } unset($cond_values, $c_values, $c_field); return $cache[$cache_key] = $cond; }
/** * View function. * * @param type $params */ function wpcf_fields_checkbox_view($params) { $output = ''; if (isset($params['state']) && $params['state'] == 'unchecked' && empty($params['field_value'])) { return htmlspecialchars_decode($params['#content']); } else { if (isset($params['state']) && $params['state'] == 'unchecked') { return '__wpcf_skip_empty'; } } if (isset($params['state']) && $params['state'] == 'checked' && !empty($params['field_value'])) { return htmlspecialchars_decode($params['#content']); } else { if (isset($params['state']) && $params['state'] == 'checked') { return '__wpcf_skip_empty'; } } if (!empty($params['#content'])) { return htmlspecialchars_decode($params['#content']); } if ($params['field']['data']['display'] == 'db' && $params['field_value'] != '') { $field = wpcf_fields_get_field_by_slug($params['field']['slug']); $output = $field['data']['set_value']; // Show the translated value if we have one. $output = wpcf_translate('field ' . $field['id'] . ' checkbox value', $output); } else { if ($params['field']['data']['display'] == 'value' && $params['field_value'] != '') { if (!empty($params['field']['data']['display_value_selected'])) { $output = $params['field']['data']['display_value_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value selected', $output); } } else { if ($params['field']['data']['display'] == 'value') { if (!empty($params['field']['data']['display_value_not_selected'])) { $output = $params['field']['data']['display_value_not_selected']; $output = wpcf_translate('field ' . $params['field']['id'] . ' checkbox value not selected', $output); } } } } return $output; }
/** * View function. * * @param type $params */ function wpcf_fields_radio_view($params) { if (isset($params['style']) && $params['style'] == 'raw') { return ''; } if (isset($params['termmeta']) && !empty($params['termmeta'])) { $field = wpcf_fields_get_field_by_slug($params['field']['slug'], 'wpcf-termmeta'); } else { if (isset($params['usermeta']) && !empty($params['usermeta'])) { $field = wpcf_fields_get_field_by_slug($params['field']['slug'], 'wpcf-usermeta'); } else { $field = wpcf_fields_get_field_by_slug($params['field']['slug']); } } $output = ''; // See if user specified output for each field if (isset($params['option'])) { foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value'])) { $test_val = stripslashes(strval($option['value'])); if ($test_val == $params['field_value'] && $option_key == $params['option']) { return htmlspecialchars_decode($params['#content']); } } } // return ' '; return '__wpcf_skip_empty'; } if (!empty($field['data']['options'])) { $field_value = $params['field_value']; foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && stripslashes($option['value']) == stripslashes($params['field_value'])) { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' title', $option['title']); if (isset($params['field']['data']['display']) && $params['field']['data']['display'] != 'db' && !empty($option['display_value'])) { // We need to translate here because the stored value is on the original language // When updaing the value in the Field group, we might have problems $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' display value', $option['display_value']); } } } $output = $field_value; } return $output; }
/** * Saves single field. * * @param type $post_ID * @param type $all_fields * @param type $field_slug * @param type $field_value * @param string $old_value * @return boolean */ function wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $field_value, $old_value = '_wpcf_dummy') { // Don't save invalid if (isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]) && isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]['#error'])) { return false; } // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (!empty($field)) { // Skype specific if (wpcf_admin_is_repetitive($field) && $field['type'] == 'skype') { if ($old_value != '__wpcf_repetitive_new_field') { $old_value = unserialize(base64_decode($field_value['old_value'])); } unset($field_value['old_value']); } // Apply filters $field_value = apply_filters('wpcf_fields_value_save', $field_value, $field['type'], $field_slug); $field_value = apply_filters('wpcf_fields_slug_' . $field_slug . '_value_save', $field_value); $field_value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_save', $field_value); // Save field if ($old_value == '_wpcf_dummy') { update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field_slug, $field_value); } else { if ($old_value == '__wpcf_repetitive_new_field') { add_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field_slug, $old_value); } update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field_slug, $field_value, $old_value); } do_action('wpcf_fields_slug_' . $field_slug . '_save', $field_value); do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value); } // Process checkboxes foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } if ($field['#type'] == 'checkbox' && !isset($_POST['wpcf'][$field['wpcf-slug']])) { if (is_null($old_value) || $old_value == '_wpcf_dummy') { delete_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']); } else { delete_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $old_value); } } if ($field['#type'] == 'checkboxes' && !isset($_POST['wpcf'][$field['wpcf-slug']])) { if (is_null($old_value) || $old_value == '_wpcf_dummy') { update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], array()); } else { update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], array(), $old_value); } } } }
/** * Important save_post hook. * * Core function. Works and stable. Do not move or change. * If required, add hooks only. * * @internal breakpoint * @param type $post_ID * @param type $post */ function wpcf_admin_post_save_post_hook($post_ID, $post) { if (defined('WPTOOLSET_FORMS_VERSION')) { global $wpcf; $errors = false; // Do not save post if is type of if (in_array($post->post_type, array('revision', 'view', 'view-template', 'cred-form', 'nav_menu_item', 'mediapage'))) { return false; } $_post_wpcf = array(); if (!empty($_POST['wpcf'])) { $_post_wpcf = $_POST['wpcf']; } /** * handle checkbox */ if (array_key_exists('_wptoolset_checkbox', $_POST) && is_array($_POST['_wptoolset_checkbox'])) { foreach ($_POST['_wptoolset_checkbox'] as $key => $field_value) { $field_slug = preg_replace('/^wpcf\\-/', '', $key); if (array_key_exists($field_slug, $_post_wpcf)) { continue; } $_post_wpcf[$field_slug] = false; } } if (count($_post_wpcf)) { /** * check some attachment to delete */ $delete_attachments = apply_filters('wpcf_delete_attachments', true); $images_to_delete = array(); if ($delete_attachments && isset($_POST['wpcf']) && array_key_exists('delete-image', $_POST['wpcf']) && is_array($_POST['wpcf']['delete-image'])) { foreach ($_POST['wpcf']['delete-image'] as $image) { $images_to_delete[$image] = 1; } } foreach ($_post_wpcf as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } // Skip copied fields if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value; // Set config $config = wptoolset_form_filter_types_field($field, $post_ID, $_post_wpcf); /** * remove from images_to_delete if user add again */ if ($delete_attachments && 'image' == $config['type']) { $images = $_field_value; if (!is_array($images)) { $images = array($images); } foreach ($images as $image) { if (array_key_exists($image, $images_to_delete)) { unset($images_to_delete[$image]); } } } foreach ($_field_value as $_k => $_val) { // Check if valid $validation = wptoolset_form_validate_field('post', $config, $_val); $conditional = wptoolset_form_conditional_check($config); $not_valid = is_wp_error($validation) || !$conditional; if (is_wp_error($validation)) { $errors = true; $_errors = $validation->get_error_data(); $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']); wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error'); } if ($not_valid) { if (types_is_repetitive($field)) { unset($field_value[$_k]); } else { break; } } } // Save field if (types_is_repetitive($field)) { $wpcf->repeater->set($post_ID, $field); $wpcf->repeater->save($field_value); } else { if (!$not_valid) { $wpcf->field->set($post_ID, $field); $wpcf->field->save($field_value); } } do_action('wpcf_post_field_saved', $post_ID, $field); // TODO Move to checkboxes if ($field['type'] == 'checkboxes') { if (!empty($field['data']['options'])) { $update_data = array(); foreach ($field['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['id']][$option_id])) { if (isset($field['data']['save_empty']) && $field['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id]; } } update_post_meta($post_ID, $field['meta_key'], $update_data); } } } /** * delete images */ if ($delete_attachments && !empty($images_to_delete)) { $args = array('post_parent' => $post_ID, 'posts_per_page' => -1); $children_array = get_children($args); foreach ($children_array as $child) { if (!array_key_exists($child->guid, $images_to_delete)) { continue; } wp_delete_attachment($child->ID); } } } if ($errors) { update_post_meta($post_ID, '__wpcf-invalid-fields', true); } do_action('wpcf_post_saved', $post_ID); return; } // OLD global $wpcf; // Basic cheks /* * Allow this hook to be triggered only if Types form is submitted */ // if ( !isset( $_POST['_wpcf_post_wpnonce'] ) || !wp_verify_nonce( $_POST['_wpcf_post_wpnonce'], // 'update-' . $post->post_type . '_' . $post_ID ) ) { // return false; // } /* * Do not save post if is type of: * revision * attachment * wp-types-group * view * view-template * cred-form */ if (in_array($post->post_type, $wpcf->excluded_post_types)) { return false; } /* * * * Get all groups connected to this $post */ $groups = wpcf_admin_post_get_post_groups_fields($post); if (empty($groups)) { return false; } $all_fields = array(); $_not_valid = array(); $_error = false; /* * * * Loop over each group * * TODO Document this * Connect 'wpcf-invalid-fields' with all fields */ foreach ($groups as $group) { if (isset($group['fields'])) { // Process fields $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation'); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; // Collect all not valid fields if ($form->isError()) { $_error = true; // Set error only to true $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid()); } } } // Set fields foreach ($all_fields as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Field(); $_temp->set($wpcf->post, $v['wpcf-id']); $all_fields[$k]['_field'] = $_temp; } foreach ($_not_valid as $k => $v) { // only Types field if (empty($v['wpcf-id'])) { continue; } $_temp = new WPCF_Field(); $_temp->set($wpcf->post, $v['wpcf-id']); $_not_valid[$k]['_field'] = $_temp; } /* * * Allow interaction here. * Conditional will set $error to false if field is conditional * and not submitted. */ $error = apply_filters('wpcf_post_form_error', $_error, $_not_valid, $all_fields); $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields); // Notify user about error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } /* * Save invalid elements so user can be informed after redirect. */ if (!empty($not_valid)) { update_post_meta($post_ID, 'wpcf-invalid-fields', $not_valid); } /* * * * * * Save meta fields */ if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (empty($field)) { continue; } // Set field $wpcf->field->set($post_ID, $field); // Skip copied fields // CHECKPOINT if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) { continue; } // Don't save invalid // CHECKPOINT if (isset($not_valid[$field['slug']])) { continue; } /* * * * Saving fields * @since 1.2 * * We changed way repetitive fields are saved. * On each save fields are rewritten and order is saved in * '_$slug-sort-order' meta field. */ /* * * We marked fields as repetitive in POST['__wpcf_repetitive'] * Without this check we won't save any. * @see WPCF_Repeater::get_fields_form() */ if (isset($_POST['__wpcf_repetitive'][$wpcf->field->slug])) { /* * Use here WPCF_Repeater class. * WPCF_Repeater::set() - switches to current post * WPCF_Repeater::save() - saves repetitive field */ $wpcf->repeater->set($post_ID, $field); $wpcf->repeater->save(); } else { /* * Use WPCF_Field::save() */ $wpcf->field->save(); } do_action('wpcf_post_field_saved', $post_ID, $field); } } /* * Process checkboxes * * TODO Revise and remove * Since Types 1.1.5 we moved this check to embedded/includes/checkbox.php * checkbox.php added permanently to bootstrap. */ foreach ($all_fields as $field) { if (!isset($field['#type'])) { continue; } // if ( $field['#type'] == 'checkbox' // && !isset( $_POST['wpcf'][$field['wpcf-slug']] ) ) { // $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] ); // if ( isset( $field_data['data']['save_empty'] ) // && $field_data['data']['save_empty'] == 'yes' ) { // update_post_meta( $post_ID, // wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'], // 0 ); // } else { // delete_post_meta( $post_ID, // wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'] ); // } // } if ($field['#type'] == 'checkboxes') { $field_data = wpcf_admin_fields_get_field($field['wpcf-id']); if (!empty($field_data['data']['options'])) { $update_data = array(); foreach ($field_data['data']['options'] as $option_id => $option_data) { if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) { if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') { $update_data[$option_id] = 0; } } else { $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id]; } } update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } } } do_action('wpcf_post_saved', $post_ID); }
/** * Calls view function for specific field type. * * @param type $field * @param type $atts * @return type */ function types_render_field($field_id, $params, $content = null, $code = '') { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; global $post; // Get field $field = wpcf_fields_get_field_by_slug($field_id); if (empty($field)) { if (!function_exists('wplogger')) { require_once WPCF_EMBEDDED_ABSPATH . '/common/wplogger.php'; } global $wplogger; $wplogger->log('types_render_field call for missing field \'' . $field_id . '\'', WPLOG_DEBUG); return ''; } // See if repetitive if (wpcf_admin_is_repetitive($field)) { $meta = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], false); if (!empty($meta)) { $output = ''; if (isset($params['index'])) { $index = $params['index']; } else { $index = ''; } // Allow wpv-for-each shortcode to set the index $index = apply_filters('wpv-for-each-index', $index); if ($index === '') { $output = array(); foreach ($meta as $temp_key => $temp_value) { $params['field_value'] = $temp_value; $temp_output = types_render_field_single($field, $params, $content, $code); if (!empty($temp_output)) { $output[] = $temp_output; } } if (!empty($output) && isset($params['separator'])) { $output = implode($params['separator'], $output); } else { if (!empty($output)) { $output = implode('', $output); } else { return ''; } } } else { if (isset($meta[$index])) { $params['field_value'] = $meta[$index]; return types_render_field_single($field, $params, $content, $code); } else { return ''; } } return $output; } else { return ''; } } else { $params['field_value'] = get_post_meta($post->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); if ($params['field_value'] == '' && $field['type'] != 'checkbox') { return ''; } return types_render_field_single($field, $params, $content, $code); } }
/** * View function. * * @param type $params */ function wpcf_fields_radio_view($params) { if (isset($params['style']) && $params['style'] == 'raw') { return ''; } if (isset($params['usermeta']) && !empty($params['usermeta'])) { $field = wpcf_fields_get_field_by_slug($params['field']['slug'], 'wpcf-usermeta'); } else { $field = wpcf_fields_get_field_by_slug($params['field']['slug']); } $output = ''; // See if user specified output for each field if (isset($params['option'])) { foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value'])) { $test_val = stripslashes(strval($option['value'])); if ($test_val == $params['field_value'] && $option_key == $params['option']) { return htmlspecialchars_decode($params['#content']); } } } // return ' '; return '__wpcf_skip_empty'; } if (!empty($field['data']['options'])) { $field_value = $params['field_value']; foreach ($field['data']['options'] as $option_key => $option) { if (isset($option['value']) && stripslashes($option['value']) == stripslashes($params['field_value'])) { $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' title', $option['title']); if (isset($params['field']['data']['display']) && $params['field']['data']['display'] != 'db' && !empty($option['display_value'])) { $field_value = wpcf_translate('field ' . $params['field']['id'] . ' option ' . $option_key . ' display value', $option['display_value']); } } } $output = $field_value; } return $output; }
/** * save_post hook. * * @param type $post_ID * @param type $post */ function wpcf_admin_post_save_post_hook($post_ID, $post) { // TODO Check if this prevents saving from outside of post form if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) { return false; } if (!in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) { // Get groups $groups = wpcf_admin_post_get_post_groups_fields($post); if (empty($groups)) { return false; } $all_fields = array(); foreach ($groups as $group) { // Process fields $fields = wpcf_admin_post_process_fields($post, $group['fields'], true); // Validate fields $form = wpcf_form_simple_validate($fields); $all_fields = $all_fields + $fields; $error = $form->isError(); // Trigger form error if ($error) { wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error'); } } // Save invalid elements so user can be informed after redirect if (!empty($all_fields)) { update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields); } // Save meta fields if (!empty($_POST['wpcf'])) { foreach ($_POST['wpcf'] as $field_slug => $field_value) { // Don't save invalid if (isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]) && isset($all_fields[wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug)) . $field_slug]['#error'])) { continue; } // Get field by slug $field = wpcf_fields_get_field_by_slug($field_slug); if (!empty($field)) { // Apply filters $field_value = apply_filters('wpcf_fields_value_save', $field_value, $field['type'], $field_slug); $field_value = apply_filters('wpcf_fields_slug_' . $field_slug . '_value_save', $field_value); $field_value = apply_filters('wpcf_fields_type_' . $field['type'] . '_value_save', $field_value); // Save field update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field_slug, $field_value); do_action('wpcf_fields_slug_' . $field_slug . '_save', $field_value); do_action('wpcf_fields_type_' . $field['type'] . '_save', $field_value); } } } // Process checkboxes foreach ($all_fields as $field) { if ($field['#type'] == 'checkbox' && !isset($_POST['wpcf'][$field['wpcf-slug']])) { delete_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']); } } } }