/** * 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); }
/** * * Only for attachments, only default checkboxes! * * @internal breakpoint * @param type $post_ID * @param type $post */ function wpcf_admin_post_add_attachment_hook($post_ID, $post) { global $wpcf; /** * Basic check: only attachment */ if ('attachment' != $post->post_type) { 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; } /** * Process all checkbox fields */ foreach ($all_fields as $field) { /** * only checkbox */ if (!isset($field['#type']) || 'checkbox' != $field['#type']) { continue; } $field_data = wpcf_admin_fields_get_field($field['wpcf-id']); /** * check is checked for new! */ $checked = array_key_exists('checked', $field_data['data']) && $field_data['data']['checked']; /** * do not save empty and not checked? fine, go away... */ if ('no' == $field_data['data']['save_empty'] && !$checked) { continue; } /** * all other just save... */ $update_data = 0; if ($checked) { $update_data = $field_data['data']['set_value']; } add_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data); } do_action('wpcf_attachement_add', $post_ID); }
/** * All AJAX calls go here. */ function wpcf_ajax_embedded() { if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) { die('Verification failed'); } switch ($_REQUEST['wpcf_action']) { case 'editor_insert_date': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/date.php'; wpcf_fields_date_editor_form(); break; case 'insert_skype_button': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php'; wpcf_fields_skype_meta_box_ajax(); break; case 'editor_callback': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $field = wpcf_admin_fields_get_field($_GET['field_id']); if (!empty($field)) { $function = 'wpcf_fields_' . $field['type'] . '_editor_callback'; if (function_exists($function)) { call_user_func($function); } } break; case 'dismiss_message': if (isset($_GET['id'])) { $messages = get_option('wpcf_dismissed_messages', array()); $messages[] = $_GET['id']; update_option('wpcf_dismissed_messages', $messages); } break; case 'pr_add_child_post': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) { $relationships = get_option('wpcf_post_relationship', array()); $post = get_post($_GET['post_id']); $post_type = $_GET['post_type_child']; $parent_post_type = $_GET['post_type_parent']; $data = $relationships[$parent_post_type][$post_type]; $output = wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, false); } echo json_encode(array('output' => $output)); break; case 'pr_save_child_post': ob_start(); // Try to catch any errors require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = array(); if (isset($_GET['post_id']) && isset($_GET['post_type_child'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type_child']; $output = wpcf_pr_admin_save_post_hook($_GET['post_id']); } $errors = ob_get_clean(); echo json_encode(array('output' => $output, 'errors' => $errors)); break; case 'pr_delete_child_post': require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id'])) { $output = wpcf_pr_admin_delete_child_item($_GET['post_id']); } echo json_encode(array('output' => $output)); break; case 'pr-update-belongs': require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_POST['post_id']) && isset($_POST['wpcf_pr_belongs'])) { $output = wpcf_pr_admin_update_belongs($_POST['post_id'], $_POST['wpcf_pr_belongs']); } echo json_encode(array('output' => $output)); break; case 'pr_pagination': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_sort': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_sort_parent': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = 'Passed wrong parameters'; if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) { $post = get_post($_GET['post_id']); $post_type = $_GET['post_type']; $has = wpcf_pr_admin_get_has($post->post_type); $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type); } echo json_encode(array('output' => $output)); break; case 'pr_save_all': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php'; $output = array(); if (isset($_POST['post_id']) && isset($_POST['wpcf_post_relationship'])) { $output = wpcf_pr_admin_save_post_hook($_POST['post_id']); } echo json_encode(array('output' => $output)); break; case 'repetitive_add': if (isset($_GET['field_id'])) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; $field = wpcf_admin_fields_get_field($_GET['field_id']); // Pass as normal unset($field['data']['repetitive']); $fields = array($_GET['field_id'] => $field); $element = wpcf_admin_post_process_fields(false, $fields, false, false, 'repetitive'); if ($field['type'] == 'skype') { $key = key($element); unset($element[$key]['#title']); echo json_encode(array('output' => wpcf_form_simple($element) . wpcf_form_render_js_validation('#post', false))); } else { $element = array_shift($element); if (!in_array($field['type'], array('checkbox'))) { unset($element['#title']); } echo json_encode(array('output' => wpcf_form_simple(array('repetitive' => $element)) . wpcf_form_render_js_validation('#post', false))); } } else { echo json_encode(array('output' => 'params missing')); } break; case 'repetitive_delete': if (isset($_POST['post_id']) && isset($_POST['field_id']) && isset($_POST['old_value'])) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; $field = wpcf_admin_fields_get_field($_POST['field_id']); if (!empty($field)) { if ($field['type'] == 'date') { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], strtotime(base64_decode($_POST['old_value']))); } else { if ($field['type'] == 'skype') { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], unserialize(base64_decode($_POST['old_value']))); } else { delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], base64_decode($_POST['old_value'])); } } echo json_encode(array('output' => 'deleted')); } else { echo json_encode(array('output' => 'field not found')); } } else { echo json_encode(array('output' => 'params missing')); } break; case 'cd_verify': if (!is_array($_POST['wpcf'])) { die; } require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $passed_fields = array(); $failed_fields = array(); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); foreach ($_POST['wpcf'] as $field_id => $field_value) { $element = array(); $field = wpcf_admin_fields_get_field($field_id); if (!empty($field['data']['conditional_display']['conditions'])) { $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group'); if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') { $passed_fields[] = 'wpcf[' . $field['id'] . ']'; } else { $failed_fields[] = 'wpcf[' . $field['id'] . ']'; } } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); if (!empty($passed_fields) || !empty($failed_fields)) { $execute = ''; foreach ($passed_fields as $field_name) { $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " "; } foreach ($failed_fields as $field_name) { $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " "; } echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } die; break; case 'cd_group_verify': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $group = wpcf_admin_fields_get_group($_POST['group_id']); if (empty($group)) { echo json_encode(array('output' => '')); die; } $execute = ''; $group['conditional_display'] = get_post_meta($group['id'], '_wpcf_conditional_display', true); // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } if (!empty($group['conditional_display']['conditions'])) { $result = wpcf_cd_post_groups_filter(array(0 => $group), $post, 'group'); if (!empty($result)) { $result = array_shift($result); $passed = $result['_conditional_display'] == 'passed' ? true : false; } else { $passed = false; } if (!$passed) { $execute = 'jQuery("#' . $group['slug'] . '").slideUp().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-failed\').removeClass(\'wpcf-cd-group-passed\').hide();'; } else { $execute = 'jQuery("#' . $group['slug'] . '").show().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-passed\').removeClass(\'wpcf-cd-group-failed\').slideDown();'; } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4); echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'pr_verify': require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php'; $passed_fields = array(); $failed_fields = array(); $post = false; if (isset($_SERVER['HTTP_REFERER'])) { $split = explode('?', $_SERVER['HTTP_REFERER']); if (isset($split[1])) { parse_str($split[1], $vars); if (isset($vars['post'])) { $_POST['post_ID'] = $vars['post']; $post = get_post($vars['post']); } } } // Dummy post if (!$post) { $post = new stdClass(); $post->ID = 1; } // Filter meta values (switch them with $_POST values) add_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4); if (isset($_POST['wpcf_post_relationship'])) { $child_post_id = key($_POST['wpcf_post_relationship']); $data = $_POST['wpcf_post_relationship'] = array_shift($_POST['wpcf_post_relationship']); foreach ($data as $field_id => $field_value) { $element = array(); $field = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $field_id)); if (!empty($field['data']['conditional_display']['conditions'])) { $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group'); if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') { $passed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id']; } else { $failed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id']; } } } } // Remove filter meta values (switch them with $_POST values) remove_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4); if (!empty($passed_fields) || !empty($failed_fields)) { $execute = ''; foreach ($passed_fields as $field_name) { $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " "; } foreach ($failed_fields as $field_name) { $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " "; } echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } die; break; default: break; } if (function_exists('wpcf_ajax')) { wpcf_ajax(); } die; }
/** * deprecated */ private function add_admin_style($form) { $admin_styles_value = $preview_profile = $edit_profile = ''; if (isset($this->update['admin_styles'])) { $admin_styles_value = $this->update['admin_styles']; } $temp = ''; if ($this->update) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; // require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php'; //Get sample post $post = query_posts('posts_per_page=1'); if (!empty($post) && count($post) != '') { $post = $post[0]; } $preview_profile = wpcf_admin_post_meta_box_preview($post, $this->update, 1); $group = $this->update; $group['fields'] = wpcf_admin_post_process_fields($post, $group['fields'], true, false); $edit_profile = wpcf_admin_post_meta_box($post, $group, 1, true); add_action('admin_enqueue_scripts', 'wpcf_admin_fields_form_fix_styles', PHP_INT_MAX); } $temp[] = array('#type' => 'radio', '#suffix' => '<br />', '#value' => 'edit_mode', '#title' => 'Edit mode', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#before' => '<div class="wpcf-admin-css-preview-style-edit">', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'editmode\')', 'checked' => 'checked')); $temp[] = array('#type' => 'radio', '#title' => 'Read Only', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#after' => '</div>', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'readonly\')')); $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_html_preview]', '#inline' => true, '#id' => 'wpcf-form-groups-admin-html-preview', '#before' => '<h3>Field group HTML</h3>'); $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_styles]', '#inline' => true, '#value' => $admin_styles_value, '#default_value' => '', '#id' => 'wpcf-form-groups-css-fields-editor', '#after' => ' <div class="wpcf-update-preview-btn"><input type="button" value="Update preview" onclick="wpcfPreviewHtml()" style="float:right;" class="button-secondary"></div> <h3>' . __('Field group preview', 'wpcf') . '</h3> <div id="wpcf-update-preview-div">Preview here</div> <script type="text/javascript"> var wpcfReadOnly = ' . json_encode(base64_encode($preview_profile)) . '; var wpcfEditMode = ' . json_encode(base64_encode($edit_profile)) . '; var wpcfDefaultCss = ' . json_encode(base64_encode($admin_styles_value)) . '; </script> ', '#before' => sprintf('<h3>%s</h3>', __('Your CSS', 'wpcf'))); $admin_styles = _wpcf_filter_wrap('admin_styles', __('Admin styles for fields:', 'wpcf'), '', '', $temp, __('Open style editor', 'wpcf')); $form['p_wrap_1_' . wpcf_unique_id(serialize($admin_styles))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">'); $form = $form + $admin_styles; $form['adminstyles-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />'); return $form; }
/** * Generates form data. */ function wpcf_admin_fields_form() { wpcf_admin_add_js_settings('wpcf_nonce_toggle_group', '\'' . wp_create_nonce('group_form_collapsed') . '\''); wpcf_admin_add_js_settings('wpcf_nonce_toggle_fieldset', '\'' . wp_create_nonce('form_fieldset_toggle') . '\''); $default = array(); global $wpcf_button_style; global $wpcf_button_style30; global $wpcf; // If it's update, get data $update = false; if (isset($_REQUEST['group_id'])) { $update = wpcf_admin_fields_get_group(intval($_REQUEST['group_id'])); if (empty($update)) { $update = false; wpcf_admin_message(sprintf(__("Group with ID %d do not exist", 'wpcf'), intval($_REQUEST['group_id']))); } else { $update['fields'] = wpcf_admin_fields_get_fields_by_group(sanitize_text_field($_REQUEST['group_id']), 'slug', false, true); $update['post_types'] = wpcf_admin_get_post_types_by_group(sanitize_text_field($_REQUEST['group_id'])); $update['taxonomies'] = wpcf_admin_get_taxonomies_by_group(sanitize_text_field($_REQUEST['group_id'])); $update['templates'] = wpcf_admin_get_templates_by_group(sanitize_text_field($_REQUEST['group_id'])); $update['admin_styles'] = wpcf_admin_get_groups_admin_styles_by_group(sanitize_text_field($_REQUEST['group_id'])); } } $form = array(); $form['#form']['callback'] = array('wpcf_admin_save_fields_groups_submit'); // Form sidebars $form['open-sidebar'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-form-fields-align-right">'); // Set help icon $form['help-icon'] = array('#type' => 'markup', '#markup' => '<div class="wpcf-admin-fields-help"><img src="' . WPCF_EMBEDDED_RELPATH . '/common/res/images/question.png" style="position:relative;top:2px;" /> <a href="http://wp-types.com/documentation/user-guides/using-custom-fields/?utm_source=typesplugin&utm_medium=help&utm_term=fields-help&utm_content=fields-editor&utm_campaign=types" target="_blank">' . __('Custom fields help', 'wpcf') . '</a></div>'); $form['submit2'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit')); $form['fields'] = array('#type' => 'fieldset', '#title' => __('Available fields', 'wpcf')); // Get field types $fields_registered = wpcf_admin_fields_get_available_types(); foreach ($fields_registered as $filename => $data) { $form['fields'][basename($filename, '.php')] = array('#type' => 'markup', '#markup' => '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax&wpcf_action=fields_insert' . '&field=' . basename($filename, '.php') . '&page=wpcf-edit') . '&_wpnonce=' . wp_create_nonce('fields_insert') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary">' . $data['title'] . '</a> '); // Process JS if (!empty($data['group_form_js'])) { foreach ($data['group_form_js'] as $handle => $script) { if (isset($script['inline'])) { add_action('admin_footer', $script['inline']); continue; } $deps = !empty($script['deps']) ? $script['deps'] : array(); $in_footer = !empty($script['in_footer']) ? $script['in_footer'] : false; wp_register_script($handle, $script['src'], $deps, WPCF_VERSION, $in_footer); wp_enqueue_script($handle); } } // Process CSS if (!empty($data['group_form_css'])) { foreach ($data['group_form_css'] as $handle => $script) { if (isset($script['src'])) { $deps = !empty($script['deps']) ? $script['deps'] : array(); wp_enqueue_style($handle, $script['src'], $deps, WPCF_VERSION); } else { if (isset($script['inline'])) { add_action('admin_head', $script['inline']); } } } } } // Get fields created by user $fields = wpcf_admin_fields_get_fields(true, true); if (!empty($fields)) { $form['fields-existing'] = array('#type' => 'fieldset', '#title' => __('User created fields', 'wpcf'), '#id' => 'wpcf-form-groups-user-fields'); foreach ($fields as $key => $field) { if (isset($update['fields']) && array_key_exists($key, $update['fields'])) { continue; } if (!empty($field['data']['removed_from_history'])) { continue; } $form['fields-existing'][$key] = array('#type' => 'markup', '#markup' => '<div id="wpcf-user-created-fields-wrapper-' . $field['id'] . '" style="float:left; margin-right: 10px;"><a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&wpcf_action=fields_insert_existing' . '&page=wpcf-edit' . '&field=' . $field['id']) . '&_wpnonce=' . wp_create_nonce('fields_insert_existing') . '" ' . 'class="wpcf-fields-add-ajax-link button-secondary" onclick="jQuery(this).parent().fadeOut();" ' . ' data-slug="' . $field['id'] . '">' . htmlspecialchars(stripslashes($field['name'])) . '</a>' . '<a href="' . admin_url('admin-ajax.php' . '?action=wpcf_ajax' . '&wpcf_action=remove_from_history' . '&field_id=' . $field['id']) . '&_wpnonce=' . wp_create_nonce('remove_from_history') . '&wpcf_warning=' . sprintf(__('Are you sure that you want to remove field %s from history?', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '&wpcf_ajax_update=wpcf-user-created-fields-wrapper-' . $field['id'] . '" title="' . sprintf(__('Remove field %s', 'wpcf'), htmlspecialchars(stripslashes($field['name']))) . '" class="wpcf-ajax-link"><img src="' . WPCF_RES_RELPATH . '/images/delete-2.png" style="postion:absolute;margin-top:5px;margin-left:-4px;" /></a></div>'); } } $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>'); // Group data $form['open-main'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-form-fields-main">'); $form['title'] = array('#type' => 'textfield', '#name' => 'wpcf[group][name]', '#id' => 'wpcf-group-name', '#value' => $update ? $update['name'] : '', '#inline' => true, '#attributes' => array('style' => 'width:100%;margin-bottom:10px;', 'placeholder' => __('Enter group title', 'wpcf')), '#validate' => array('required' => array('value' => true))); $form['description'] = array('#type' => 'textarea', '#id' => 'wpcf-group-description', '#name' => 'wpcf[group][description]', '#value' => $update ? $update['description'] : '', '#attributes' => array('placeholder' => __('Enter a description for this group', 'wpcf'))); /** * * FILTER BOX * Since Types 1.2 we moved JS to /embedded/resources/js/custom-fields-form-filter.js * */ // Support post types and taxonomies $post_types = get_post_types('', 'objects'); $options = array(); $post_types_currently_supported = array(); $form_types = array(); foreach ($post_types as $post_type_slug => $post_type) { if (in_array($post_type_slug, $wpcf->excluded_post_types) || !$post_type->show_ui) { continue; } $options[$post_type_slug]['#name'] = 'wpcf[group][supports][' . $post_type_slug . ']'; $options[$post_type_slug]['#title'] = $post_type->label; $options[$post_type_slug]['#default_value'] = $update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types']) ? 1 : 0; $options[$post_type_slug]['#value'] = $post_type_slug; $options[$post_type_slug]['#inline'] = TRUE; $options[$post_type_slug]['#suffix'] = '<br />'; $options[$post_type_slug]['#id'] = 'wpcf-form-groups-support-post-type-' . $post_type_slug; $options[$post_type_slug]['#attributes'] = array('class' => 'wpcf-form-groups-support-post-type'); if ($update && !empty($update['post_types']) && in_array($post_type_slug, $update['post_types'])) { $post_types_currently_supported[] = $post_type->label; } } if (empty($post_types_currently_supported)) { $post_types_currently_supported[] = __('Displayed on all content types', 'wpcf'); } /** * POST TYPE FILTER */ $temp = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][supports]', '#inline' => true); /** * Here we use unique function for all filters * Since Types 1.2 */ $form_types = _wpcf_filter_wrap('custom_post_types', __('Post Types:', 'wpcf'), implode(',', $post_types_currently_supported), __('Displayed on all content types', 'wpcf'), $temp); /** * TAXONOMIES FILTER QUERY */ $taxonomies = apply_filters('wpcf_group_form_filter_taxonomies', get_taxonomies('', 'objects')); $options = array(); $tax_currently_supported = array(); $form_tax = array(); $form_tax_single = array(); /** * Filter toxonomies */ foreach ($taxonomies as $category_slug => $category) { if ($category_slug == 'nav_menu' || $category_slug == 'link_category' || $category_slug == 'post_format') { continue; } $terms = apply_filters('wpcf_group_form_filter_terms', get_terms($category_slug, array('hide_empty' => false))); if (!empty($terms)) { $options = array(); $add_title = '<div class="taxonomy-title">' . $category->labels->name . '</div>'; $title = ''; foreach ($terms as $term) { $checked = 0; if ($update && !empty($update['taxonomies']) && array_key_exists($category_slug, $update['taxonomies'])) { if (array_key_exists($term->term_taxonomy_id, $update['taxonomies'][$category_slug])) { $checked = 1; $tax_currently_supported[$term->term_taxonomy_id] = $term->name; $title = ''; } } $options[$term->term_taxonomy_id]['#name'] = 'wpcf[group][taxonomies][' . $category_slug . '][' . $term->term_taxonomy_id . ']'; $options[$term->term_taxonomy_id]['#title'] = $term->name; $options[$term->term_taxonomy_id]['#default_value'] = $checked; $options[$term->term_taxonomy_id]['#value'] = $term->term_taxonomy_id; $options[$term->term_taxonomy_id]['#inline'] = true; $options[$term->term_taxonomy_id]['#prefix'] = $add_title; $options[$term->term_taxonomy_id]['#suffix'] = '<br />'; $options[$term->term_taxonomy_id]['#id'] = 'wpcf-form-groups-support-tax-' . $term->term_taxonomy_id; $options[$term->term_taxonomy_id]['#attributes'] = array('class' => 'wpcf-form-groups-support-tax'); $add_title = ''; } $form_tax_single['taxonomies-' . $category_slug] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'wpcf[group][taxonomies][' . $category_slug . ']', '#suffix' => '<br />', '#inline' => true); } } if (empty($tax_currently_supported)) { $tax_currently_supported[] = __('Not Selected', 'wpcf'); } /** * Since Types 1.2 we use unique function */ $form_tax = _wpcf_filter_wrap('custom_taxonomies', __('Terms:', 'wpcf'), implode(', ', array_values($tax_currently_supported)), __('Not Selected', 'wpcf'), $form_tax_single); /** * TEMPLATES */ // Choose templates $templates = get_page_templates(); $templates_views = get_posts('post_type=view-template&numberposts=-1&status=publish'); $options = array(); $options['default-template'] = array('#title' => __('Default Template'), '#default_value' => !empty($update['templates']) && in_array('default', $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => 'default', '#inline' => true, '#after' => '<br />'); foreach ($templates as $template_name => $template_filename) { $options[$template_filename] = array('#title' => $template_name, '#default_value' => !empty($update['templates']) && in_array($template_filename, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_filename, '#inline' => true, '#after' => '<br />'); } foreach ($templates_views as $template_view) { $options[$template_view->post_name] = array('#title' => 'View Template ' . $template_view->post_title, '#default_value' => !empty($update['templates']) && in_array($template_view->ID, $update['templates']), '#name' => 'wpcf[group][templates][]', '#value' => $template_view->ID, '#inline' => true, '#after' => '<br />'); $templates_view_list_text[$template_view->ID] = $template_view->post_title; } $text = ''; if (!empty($update['templates'])) { $text = array(); $templates = array_flip($templates); foreach ($update['templates'] as $template) { if ($template == 'default') { $template = __('Default Template'); } else { if (strpos($template, '.php') !== false) { $template = $templates[$template]; } else { $template = 'View Template ' . $templates_view_list_text[$template]; } } $text[] = $template; } $text = implode(', ', $text); } else { $text = __('Not Selected', 'wpcf'); } // Add class foreach ($options as $_k => $_option) { $options[$_k]['#attributes'] = array('class' => 'wpcf-form-groups-support-templates'); } $form_templates = array('#type' => 'checkboxes', '#name' => 'wpcf[group][templates]', '#options' => $options, '#inline' => true); /** * Since Types 1.2 we use unique function */ $form_templates = _wpcf_filter_wrap('templates', __('Templates:', 'wpcf'), $text, __('Not Selected', 'wpcf'), $form_templates); /** * Now starting form */ $form['supports-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat"><thead><tr><th>' . __('Where to display this group', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Each custom fields group can display on different content types or different taxonomy.', 'wpcf') . '</p>'); /** * Join filter forms */ // Types $form['p_wrap_1_' . wpcf_unique_id(serialize($form_types))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">'); $form = $form + $form_types; // Terms $form['p_wrap_2_' . wpcf_unique_id(serialize($form_tax))] = array('#type' => 'markup', '#markup' => '</p><p class="wpcf-filter-wrap">'); $form = $form + $form_tax; // Templates $form['p_wrap_3_' . wpcf_unique_id(serialize($form_templates))] = array('#type' => 'markup', '#markup' => '</p><p class="wpcf-filter-wrap">'); $form = $form + $form_templates; $form['p_wrap_4_' . wpcf_unique_id(serialize($form_templates))] = array('#type' => 'markup', '#markup' => '</p>'); /** * TODO Code from now on should be revised */ $count = 0; $count += !empty($update['post_types']) ? 1 : 0; $count += !empty($update['taxonomies']) ? 1 : 0; $count += !empty($update['templates']) ? 1 : 0; $display = $count > 1 ? '' : ' style="display:none;"'; $form['filters_association'] = array('#type' => 'radios', '#name' => 'wpcf[group][filters_association]', '#id' => 'wpcf-fields-form-filters-association', '#options' => array(__('Display this group when ANY of the above conditions is met', 'wpcf') => 'any', __('Display this group when ALL the above conditions is met', 'wpcf') => 'all'), '#default_value' => !empty($update['filters_association']) ? $update['filters_association'] : 'any', '#inline' => true, '#before' => '<div id="wpcf-fields-form-filters-association-form"' . $display . '>', '#after' => '<div id="wpcf-fields-form-filters-association-summary" style="margin-top:10px;font-style:italic;margin-bottom:15px;"></div></div>'); wpcf_admin_add_js_settings('wpcf_filters_association_or', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% or View Template is: %vt%', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcf_filters_association_and', '\'' . __('This group will appear on %pt% edit pages where content belongs to taxonomy: %tx% and View Template is: %vt%', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcf_filters_association_all_pages', '\'' . __('all', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcf_filters_association_all_taxonomies', '\'' . __('any', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcf_filters_association_all_templates', '\'' . __('any', 'wpcf') . '\''); $additional_filters = apply_filters('wpcf_fields_form_additional_filters', array(), $update); $form = $form + $additional_filters; $form['supports-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />'); /** Admin styles* */ $form['adminstyles-table-open'] = array('#type' => 'markup', '#markup' => '<table class="widefat" id="wpcf-admin-styles-box"><thead><tr><th>' . __('Styling Editor', 'wpcf') . '</th></tr></thead><tbody><tr><td>' . '<p>' . __('Customize Fields for admin panel.', 'wpcf') . '</p>'); $admin_styles_value = $preview_profile = $edit_profile = ''; if (isset($update['admin_styles'])) { $admin_styles_value = $update['admin_styles']; } $temp = ''; if ($update) { require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php'; //Get sample post $post = query_posts('posts_per_page=1'); if (!empty($post) && count($post) != '') { $post = $post[0]; } $preview_profile = wpcf_admin_post_meta_box_preview($post, $update, 1); $group = $update; $group['fields'] = wpcf_admin_post_process_fields($post, $group['fields'], true, false); $edit_profile = wpcf_admin_post_meta_box($post, $group, 1, true); add_action('admin_enqueue_scripts', 'wpcf_admin_fields_form_fix_styles', PHP_INT_MAX); } $temp[] = array('#type' => 'radio', '#suffix' => '<br />', '#value' => 'edit_mode', '#title' => 'Edit mode', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#before' => '<div class="wpcf-admin-css-preview-style-edit">', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'editmode\')', 'checked' => 'checked')); $temp[] = array('#type' => 'radio', '#title' => 'Read Only', '#name' => 'wpcf[group][preview]', '#default_value' => '', '#after' => '</div>', '#inline' => true, '#attributes' => array('onclick' => 'changePreviewHtml(\'readonly\')')); $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_html_preview]', '#inline' => true, '#id' => 'wpcf-form-groups-admin-html-preview', '#before' => '<h3>Field group HTML</h3>'); $temp[] = array('#type' => 'textarea', '#name' => 'wpcf[group][admin_styles]', '#inline' => true, '#value' => $admin_styles_value, '#default_value' => '', '#id' => 'wpcf-form-groups-css-fields-editor', '#after' => ' <div class="wpcf-update-preview-btn"><input type="button" value="Update preview" onclick="wpcfPreviewHtml()" style="float:right;" class="button-secondary"></div> <h3>Field group preview</h3> <div id="wpcf-update-preview-div">Preview here</div> <script type="text/javascript"> var wpcfReadOnly = ' . json_encode($preview_profile) . '; var wpcfEditMode = ' . json_encode($edit_profile) . '; var wpcfDefaultCss = ' . json_encode($admin_styles_value) . '; </script> ', '#before' => '<h3>Your CSS</h3>'); $admin_styles = _wpcf_filter_wrap('admin_styles', __('Admin styles for fields:', 'wpcf'), '', '', $temp, __('Open style editor', 'wpcf')); $form['p_wrap_1_' . wpcf_unique_id(serialize($admin_styles))] = array('#type' => 'markup', '#markup' => '<p class="wpcf-filter-wrap">'); $form = $form + $admin_styles; $form['adminstyles-table-close'] = array('#type' => 'markup', '#markup' => '</td></tr></tbody></table><br />'); /** End admin Styles * */ // Group fields $form['fields_title'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields', 'wpcf') . '</h2>'); $show_under_title = true; $form['ajax-response-open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-fields-sortable" class="ui-sortable">'); // If it's update, display existing fields $existing_fields = array(); if ($update && isset($update['fields'])) { foreach ($update['fields'] as $slug => $field) { $field['submitted_key'] = $slug; $field['group_id'] = $update['id']; $form_field = wpcf_fields_get_field_form_data($field['type'], $field); if (is_array($form_field)) { $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">'); $form = $form + $form_field; $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>'); } $existing_fields[] = $slug; $show_under_title = false; } } // Any new fields submitted but failed? (Don't double it) if (!empty($_POST['wpcf']['fields'])) { foreach ($_POST['wpcf']['fields'] as $key => $field) { if (in_array($key, $existing_fields)) { continue; } $field['submitted_key'] = $key; $form_field = wpcf_fields_get_field_form_data($field['type'], $field); if (is_array($form_field)) { $form['draggable-open-' . rand()] = array('#type' => 'markup', '#markup' => '<div class="ui-draggable">'); $form = $form + $form_field; $form['draggable-close-' . rand()] = array('#type' => 'markup', '#markup' => '</div>'); } } $show_under_title = false; } $form['ajax-response-close'] = array('#type' => 'markup', '#markup' => '</div>' . '<div id="wpcf-ajax-response"></div>'); if ($show_under_title) { $form['fields_title']['#markup'] = $form['fields_title']['#markup'] . '<div id="wpcf-fields-under-title">' . __('There are no fields in this group. To add a field, click on the field buttons at the right.', 'wpcf') . '</div>'; } // If update, create ID field if ($update) { $form['group_id'] = array('#type' => 'hidden', '#name' => 'group_id', '#value' => $update['id'], '#forced_value' => true); } $form['submit'] = array('#type' => 'submit', '#name' => 'save', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary wpcf-disabled-on-submit')); // Close main div $form['close-sidebar'] = array('#type' => 'markup', '#markup' => '</div>'); $form = apply_filters('wpcf_form_fields', $form, $update); // Add JS settings wpcf_admin_add_js_settings('wpcfFormUniqueValuesCheckText', '\'' . __('Warning: same values selected', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcfFormUniqueNamesCheckText', '\'' . __('Warning: field name already used', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcfFormUniqueSlugsCheckText', '\'' . __('Warning: field slug already used', 'wpcf') . '\''); wpcf_admin_add_js_settings('wpcfFormAlertOnlyPreview', sprintf("'%s'", __('Sorry, but this is only preview!', 'wpcf'))); return $form; }
/** * 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']); } } } }
/** * 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'))) { // 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 // Fix duplicates for repetitive fields foreach ($group['fields'] as $temp_field_id => $temp_field_data) { if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) { if (count($_POST['wpcf'][$temp_field_id]) < 2) { continue; } $temp_check_duplicates_old_value = array(); $temp_check_duplicates_new_value = array(); foreach ($_POST['wpcf'][$temp_field_id] as $posted_field_key => $posted_field_value) { $temp_old_value = base64_decode($posted_field_value['old_value']); $temp_new_value = isset($posted_field_value['new_value']) ? $posted_field_value['new_value'] : serialize($posted_field_value); $temp_field_key = wpcf_types_get_meta_prefix($temp_field_data) . $temp_field_id; // Check new values if (in_array(md5($temp_new_value), $temp_check_duplicates_new_value)) { unset($_POST['wpcf'][$temp_field_id][$posted_field_key]); if ($temp_old_value != '__wpcf_repetitive_new_field') { // Rebuild delete_post_meta($post_ID, $temp_field_key, $temp_old_value); update_post_meta($post_ID, $temp_field_key, $temp_old_value); } } else { if ($temp_old_value == '__wpcf_repetitive_new_field') { $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key]; unset($_POST['wpcf'][$temp_field_id][$posted_field_key]); continue; } $temp_check_duplicates_new_value[] = md5($temp_new_value); } // Check old values // if ($temp_old_value != '__wpcf_repetitive_new_field') { // if (in_array(md5($temp_old_value), // $temp_check_duplicates_old_value)) { // unset($_POST['wpcf'][$temp_field_id][$posted_field_key]); // if ($temp_old_value != '__wpcf_repetitive_new_field') { // // Rebuild // delete_post_meta($post_ID, $temp_field_key, // $temp_old_value); // update_post_meta($post_ID, $temp_field_key, // $temp_old_value); // } // } else { // if ($temp_old_value == '__wpcf_repetitive_new_field') { // $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key]; // unset($_POST['wpcf'][$temp_field_id][$posted_field_key]); // continue; // } // $temp_check_duplicates_old_value[] = md5($temp_old_value); // } // } } } } $_REQUEST['wpcf'] = $_POST['wpcf']; // Fix indexes for repetitive foreach ($group['fields'] as $temp_field_id => $temp_field_data) { if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) { $temp_count = 1; $temp_data = $_REQUEST['wpcf'][$temp_field_id]; unset($_REQUEST['wpcf'][$temp_field_id]); $_REQUEST['wpcf'][$temp_field_id] = array(); foreach ($temp_data as $temp_post_key => $temp_post_data) { $_REQUEST['wpcf'][$temp_field_id][$temp_count] = $temp_post_data; $temp_count += 1; } } } $_POST['wpcf'] = $_REQUEST['wpcf']; $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) { // Repetitive fields if (isset($_POST['wpcf_repetitive'][$field_slug])) { foreach ($field_value as $temp_id => $repetitive_data) { $field = wpcf_fields_get_field_by_slug($field_slug); // Skype specific if ($field['type'] == 'skype') { wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data, base64_decode($repetitive_data['old_value'])); } else { wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data['new_value'], base64_decode($repetitive_data['old_value'])); } } } else { wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $field_value); } } } } }
/** * Has form table row. * * @param type $post * @param type $post_type * @param type $data * @param type $parent_post_type * @param stdClass $item * @return string */ function wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, $item) { $new_item = false; $date_trigger = false; // Set item if (empty($item)) { $item = new stdClass(); $item->ID = 'new_' . mt_rand(); $item->post_title = ''; $item->post_content = ''; $item->post_type = $post_type; $new_item = true; } // Cleanup data if (empty($data['fields_setting'])) { $data['fields_setting'] = 'all_cf'; } $item_parents = isset($data['fields']['_wpcf_pr_parents']) ? $data['fields']['_wpcf_pr_parents'] : array(); unset($data['fields']['_wpcf_pr_parents']); $row_data = array(); $wpcf_fields = wpcf_admin_fields_get_fields(); $row_data[] = wpcf_form_simple(array('field' => array('#type' => 'textfield', '#id' => 'wpcf_post_relationship_' . $item->ID . '_wp_title', '#name' => 'wpcf_post_relationship[' . $item->ID . '][_wp_title]', '#value' => $item->post_title, '#inline' => true, '#attributes' => $new_item || $data['fields_setting'] == 'all_cf_standard' || isset($data['fields']['_wp_title']) ? array() : array('readonly' => 'readonly')))); if ($data['fields_setting'] == 'specific' && !empty($data['fields'])) { foreach ($data['fields'] as $field_key => $true) { if ($field_key == '_wp_title') { continue; } else { if ($field_key == '_wp_body') { $value = wp_trim_words($item->post_content, 10, null); $element = wpcf_form_simple(array('field' => array('#type' => 'textarea', '#id' => 'wpcf_post_relationship_' . $item->ID . '_' . $field_key, '#name' => 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']', '#value' => $item->post_content, '#attributes' => array('style' => 'width:300px;height:100px;'), '#inline' => true))); } else { $wpcf_key = str_replace(WPCF_META_PREFIX, '', $field_key); if (strpos($field_key, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[$wpcf_key])) { // Date trigger if ($wpcf_fields[$wpcf_key]['type'] == 'date') { $date_trigger = true; } // Get WPCF form $element = wpcf_admin_post_process_fields($item, array('field' => $wpcf_fields[$wpcf_key]), false, false, 'post_relationship'); $element = array_shift($element); // TODO There may still be problem with IDs if (!in_array($wpcf_fields[$wpcf_key]['type'], array('image', 'file'))) { $element['#id'] = 'wpcf_post_relationship_' . $item->ID . '_' . $wpcf_key; } $element['#name'] = 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']'; $element['#inline'] = true; unset($element['#title'], $element['#description']); if (in_array($wpcf_fields[$wpcf_key]['type'], array('wysiwyg'))) { $element['#type'] = 'textarea'; $element['#attributes'] = array('style' => 'width:300px;height:100px;'); } if (in_array($wpcf_fields[$wpcf_key]['type'], array('checkbox'))) { $element['#suffix'] = '<input type="hidden" name="wpcf_post_relationship_checkbox[' . $item->ID . '][' . $wpcf_key . ']" value="1" />'; } $value = get_post_meta($item->ID, $field_key, true); $element = wpcf_form_simple(array('field' => $element)); } else { // Just render textfield $value = get_post_meta($item->ID, $field_key, true); $element = wpcf_form_simple(array('field' => array('#type' => 'textfield', '#id' => 'wpcf_post_relationship_' . $item->ID . '_' . $field_key, '#name' => 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']', '#value' => $value, '#inline' => true))); } } } $row_data[] = $element; } // Get other parents foreach ($item_parents as $parent => $temp_data) { if ($parent == $parent_post_type) { continue; } $meta = get_post_meta($item->ID, '_wpcf_belongs_' . $parent . '_id', true); $meta = empty($meta) ? 0 : $meta; $belongs_data = array('belongs' => array($parent => $meta)); $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($item, $parent, $belongs_data); unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']); $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $item->ID . '][parents][' . $parent . ']'; $row_data[] = wpcf_form_simple($temp_form); } } else { $groups = wpcf_admin_post_get_post_groups_fields($item, 'post_relationships'); if ($data['fields_setting'] == 'all_cf_standard') { $element = wpcf_form_simple(array('field' => array('#type' => 'textarea', '#id' => 'wpcf_post_relationship_' . $item->ID . '_wp_body', '#name' => 'wpcf_post_relationship[' . $item->ID . '][_wp_body]', '#value' => $item->post_content, '#attributes' => array('style' => 'width:300px;height:100px;'), '#inline' => true))); $row_data[] = $element; } foreach ($groups as $group) { foreach ($group['fields'] as $field) { // Date trigger if ($field['type'] == 'date') { $date_trigger = true; } // Get WPCF form $element_org = wpcf_admin_post_process_fields($item, array('field' => $field), false, false, 'post_relationship'); $element = array_shift($element_org); // @todo Check if this is needed for skype only // if ($field['type'] == 'skype') { // $temp_second_element = array_pop($element_org); // if (!empty($temp_second_element['#validate'])) { // $element['#validate'] = $temp_second_element['#validate']; // } // // } // TODO Monitor if may still be problem with IDs if (!in_array($field['type'], array('image', 'file'))) { $element['#id'] = 'wpcf_post_relationship_' . $item->ID . '_' . $field['id']; } $element['#name'] = 'wpcf_post_relationship[' . $item->ID . '][' . wpcf_types_get_meta_prefix($field) . $field['slug'] . ']'; $element['#inline'] = true; unset($element['#title'], $element['#description']); if (in_array($field['type'], array('wysiwyg'))) { $element['#type'] = 'textarea'; $element['#attributes'] = array('style' => 'width:300px;height:100px;'); } if (in_array($field['type'], array('checkbox'))) { $element['#suffix'] = '<input type="hidden" name="wpcf_post_relationship_checkbox[' . $item->ID . '][' . wpcf_types_get_meta_prefix($field) . $field['slug'] . ']" value="1" />'; } $value = get_post_meta($item->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true); $element = array('field' => $element); $element = wpcf_form_simple($element); $row_data[] = $element; } } // Get all parents $item_parents = wpcf_pr_admin_get_belongs($post_type); if ($item_parents) { foreach ($item_parents as $parent => $temp_data) { if ($parent == $parent_post_type) { continue; } $meta = get_post_meta($item->ID, '_wpcf_belongs_' . $parent . '_id', true); $meta = empty($meta) ? 0 : $meta; $belongs_data = array('belongs' => array($parent => $meta)); $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($item, $parent, $belongs_data); unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']); $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $item->ID . '][parents][' . $parent . ']'; $row_data[] = wpcf_form_simple($temp_form); } } } if (!empty($row_data)) { $output = ''; $output .= '<tr><td>' . implode('</td><td>', $row_data) . '<input type="hidden" name="wpcf_post_relationship[' . $item->ID . '][post_type]" value="' . $post_type . '" /></td><td class="actions">' . '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&' . 'wpcf_action=pr_save_child_post&post_type_parent=' . $parent_post_type . '&post_id=' . $post->ID . '&post_type_child=' . $post_type . '&_wpnonce=' . wp_create_nonce('pr_save_child_post')) . '" class="wpcf-pr-save-ajax button-secondary">' . __('Save') . '</a>'; $output .= strpos($item->ID, 'new_') === false ? ' <a href="' . get_edit_post_link($item->ID) . '" class="button-secondary">' . __('Edit') . '</a>' : ''; $output .= strpos($item->ID, 'new_') === false ? ' <a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&' . 'wpcf_action=pr_delete_child_post' . '&post_id=' . $item->ID . '&_wpnonce=' . wp_create_nonce('pr_delete_child_post')) . '" class="wpcf-pr-delete-ajax button-secondary">' . __('Delete') . '</a>' : ''; if ($date_trigger) { $output .= '<script type="text/javascript"> //<![CDATA[ jQuery(document).ready(function(){ wpcfFieldsDateInit("#wpcf-post-relationship"); }); //]]> </script>'; } $output .= wpcf_form_render_js_validation('#post', false) . '</td></tr>'; return $output; } return $output = '<tr><td><span style="color:Red;">' . __('Error occured', 'wpcf') . '</span></td></tr>'; }
/** * 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); } } } }