/** * Submitted Bulk actions. */ function wpcf_admin_custom_fields_control_bulk_actions($action = '') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_fields_control_bulk')) { return; } if ($action == 'wpcf-deactivate-bulk') { $fields = wpcf_admin_fields_get_fields(false, true); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name'])); } } wpcf_admin_fields_save_fields($fields); } else { if ($action == 'wpcf-activate-bulk') { $fields = wpcf_admin_fields_get_fields(false, true); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields'])); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id)); } wpcf_admin_fields_save_fields($fields); } else { if ($action == 'wpcf-delete-bulk') { require_once WPCF_INC_ABSPATH . '/fields.php'; $failed = array(); $success = array(); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); $response = wpcf_admin_fields_delete_field($field_id); if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success))); } if (!empty($failed)) { wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)), 'error'); } } } } $url = add_query_arg(array('page' => 'wpcf-custom-fields-control', 'display_all' => isset($_REQUEST['display_all']) ? 1 : 0), admin_url('admin.php')); wp_redirect($url); die; }
/** * Actions for outside fields control. * * @param type $action */ function wpcf_types_cf_under_control($action = 'add', $args = array(), $post_type = 'wp-types-group', $meta_name = 'wpcf-fields') { global $wpcf_types_under_control; $wpcf_types_under_control['errors'] = array(); switch ($action) { case 'add': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); foreach ($args['fields'] as $field_id) { $field_type = !empty($args['type']) ? $args['type'] : 'textfield'; if (strpos($field_id, md5('wpcf_not_controlled')) !== false) { $field_id_name = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); $field_id_add = preg_replace('/^wpcf\\-/', '', $field_id_name); $adding_field_with_wpcf_prefix = $field_id_add != $field_id_name; // Activating field that previously existed in Types if (array_key_exists($field_id_add, $fields)) { $fields[$field_id_add]['data']['disabled'] = 0; } else { // Adding from outside $fields[$field_id_add]['id'] = $field_id_add; $fields[$field_id_add]['type'] = $field_type; if ($adding_field_with_wpcf_prefix) { $fields[$field_id_add]['name'] = $field_id_add; $fields[$field_id_add]['slug'] = $field_id_add; } else { $fields[$field_id_add]['name'] = $field_id_name; $fields[$field_id_add]['slug'] = $field_id_name; } $fields[$field_id_add]['description'] = ''; $fields[$field_id_add]['data'] = array(); if ($adding_field_with_wpcf_prefix) { // This was most probably a previous Types field // let's take full control $fields[$field_id_add]['data']['controlled'] = 0; } else { // @TODO WATCH THIS! MUST NOT BE DROPPED IN ANY CASE $fields[$field_id_add]['data']['controlled'] = 1; } } $unset_key = array_search($field_id, $args['fields']); if ($unset_key !== false) { unset($args['fields'][$unset_key]); $args['fields'][$unset_key] = $field_id_add; } } } wpcf_admin_fields_save_fields($fields, true, $meta_name); return $args['fields']; break; case 'check_exists': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); $field = $args; if (array_key_exists($field, $fields) && empty($fields[$field]['data']['disabled'])) { return true; } return false; break; case 'check_outsider': $fields = wpcf_admin_fields_get_fields(false, true, false, $meta_name, false); $field = $args; if (array_key_exists($field, $fields) && !empty($fields[$field]['data']['controlled'])) { return true; } return false; break; default: break; } }
/** * Submitted Bulk actions. */ function wpcf_admin_custom_fields_control_bulk_actions($action = '') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_fields_control_bulk')) { return; } switch ($action) { case 'wpcf-change-type-bulk': if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) { wpcf_admin_custom_fields_change_type($_POST['fields'], $_POST['wpcf-id'], TYPES_CUSTOM_FIELD_GROUP_CPT_NAME); } break; case 'wpcf-deactivate-bulk': $fields = wpcf_admin_fields_get_fields(false, true); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name'])); } } wpcf_admin_fields_save_fields($fields); break; case 'wpcf-activate-bulk': $fields = wpcf_admin_fields_get_fields(false, true); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields'])); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id)); } wpcf_admin_fields_save_fields($fields); break; case 'wpcf-delete-bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; $failed = array(); $success = array(); foreach ($_POST['fields'] as $field_id) { $field_id = sanitize_text_field($field_id); $response = wpcf_admin_fields_delete_field($field_id); if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success))); } if (!empty($failed)) { wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed)), 'error'); } break; case 'wpcf-add-to-group-bulk': case 'wpcf-remove-from-group-bulk': if (true && isset($_POST['wpcf-id']) && isset($_POST['fields'])) { $fields = array_values((array) $_POST['fields']); if (!empty($fields)) { $groups = explode(',', $_POST['wpcf-id']); foreach ($groups as $group_id) { switch ($action) { case 'wpcf-add-to-group-bulk': wpcf_admin_fields_save_group_fields($group_id, $fields, true); break; case 'wpcf-remove-from-group-bulk': wpcf_admin_fields_remove_field_from_group_bulk($group_id, $fields); break; } } } } break; } wp_safe_redirect(esc_url_raw(add_query_arg(array('page' => 'wpcf-custom-fields-control'), admin_url('admin.php')))); die; }
/** * All AJAX calls go here. */ function wpcf_ajax() { if (!current_user_can('manage_options') || (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } switch ($_REQUEST['wpcf_action']) { case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group($_GET['group_id'], $_GET['field_id']); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_activation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_deactivation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 1; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_activation_link(esc_attr($_GET['wpcf-post-type'])) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 0; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_deactivation_link($_GET['wpcf-post-type']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option('wpcf-custom-types', array()); unset($custom_types[$_GET['wpcf-post-type']]); update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 1; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_activation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 0; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_deactivation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); unset($custom_taxonomies[$_GET['wpcf-tax']]); update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_radio_option': require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $element = wpcf_fields_radio_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'jQuery("#wpcf-form-groups-radio-ajax-response-' . urldecode($_GET['wpcf_ajax_update_add']) . '").append(\'' . trim(str_replace("\r\n", '', wpcf_form_simple($element_txt))) . '\');', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = $_GET['group_id']; $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'group_update_post_types': require_once WPCF_INC_ABSPATH . '/fields.php'; $post_types = empty($_GET['wpcf']['group']['supports']) ? array() : $_GET['wpcf']['group']['supports']; if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_post_types($_GET['group_id'], $post_types); $output = array(); foreach ($post_types as $post_type) { $post_type = get_post_type_object($post_type); if (!empty($post_type->label)) { $output[] = $post_type->label; } } if (empty($post_types)) { $output[] = __('No post types associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No post types associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'group_update_taxonomies': require_once WPCF_INC_ABSPATH . '/fields.php'; $taxonomies_post = empty($_GET['wpcf']['group']['taxonomies']) ? array() : $_GET['wpcf']['group']['taxonomies']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_terms($_GET['group_id'], $terms); $output = array(); foreach ($taxonomies_post as $taxonomy => $terms) { $taxonomy = get_taxonomy($taxonomy); if (!empty($taxonomy)) { $title = $taxonomy->label . ': '; foreach ($terms as $term_id) { $term = get_term($term_id, $taxonomy->name); $output[] = $title . $term->name; $title = ''; } } } if (empty($output)) { $output[] = __('No taxonomies associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No taxonomies associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'custom_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; wpcf_admin_custom_fields_control_bulk_ajax(); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field($_GET['field_id']); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field($_GET['field']); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; default: break; } die; }
/** * Submitted Bulk actions. */ function wpcf_admin_custom_fields_control_bulk_actions($action = '') { if ($action == 'wpcf-deactivate-bulk') { $fields = wpcf_admin_fields_get_fields(); foreach ($_POST['fields'] as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message_store(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name'])); } } wpcf_admin_fields_save_fields($fields); } else { if ($action == 'wpcf-activate-bulk') { $fields = wpcf_admin_fields_get_fields(); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $_POST['fields'])); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id]) && empty($fields[$field_id]['data']['disabled_by_type'])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message_store(sprintf(__('Added to Types control: %s', 'wpcf'), $field_id)); } wpcf_admin_fields_save_fields($fields); } else { if ($action == 'wpcf-delete-bulk') { require_once WPCF_INC_ABSPATH . '/fields.php'; $failed = array(); $success = array(); foreach ($_POST['fields'] as $field_id) { $response = wpcf_admin_fields_delete_field($field_id); if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message_store(sprintf(__('Fields %s have been deleted.', 'wpcf'), implode(', ', $success))); } if (!empty($failed)) { wpcf_admin_message_store(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), implode(', ', $failed))); } } } } wp_redirect($_SERVER['REQUEST_URI']); die; }
/** * Auto-register the computed values as Types fields. * This method automatically creates the Woocommerce Views filter custom fields and group. * Hooked to wp_loaded * @access public * @return void */ public function wpv_register_typesfields_func() { //Define WC Views canonical custom field array $wc_views_custom_fields_array = array('views_woo_price', 'views_woo_on_sale', 'views_woo_in_stock'); //Preparation to Types control $wc_views_fields_array = array(); $string_wpcf_not_controlled = md5('wpcf_not_controlled'); foreach ($wc_views_custom_fields_array as $key => $value) { $wc_views_fields_array[] = $value . '_' . $string_wpcf_not_controlled; } if (defined('WPCF_INC_ABSPATH')) { //First, check if WC Views Types Group field does not exist if (!$this->check_if_types_group_exist('WooCommerce Views filter fields')) { require_once WPCF_INC_ABSPATH . '/fields.php'; //Part 1: Assign to Types Control //Get Fields $fields = wpcf_admin_fields_get_fields(false, true); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $wc_views_fields_array)); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } } //Save fields wpcf_admin_fields_save_fields($fields); //Retrieve updated fields $fields = wpcf_admin_fields_get_fields(false, false); //Assign names foreach ($fields as $key => $value) { if ($key == 'views_woo_price') { $fields['views_woo_price']['name'] = 'WooCommerce Product Price'; } elseif ($key == 'views_woo_on_sale') { $fields['views_woo_on_sale']['name'] = 'Product On Sale Status'; } elseif ($key == 'views_woo_in_stock') { $fields['views_woo_in_stock']['name'] = 'Product In Stock Status'; } } //Save fields wpcf_admin_fields_save_fields($fields); //Define group $group = array('name' => 'WooCommerce Views filter fields', 'description' => '', 'filters_association' => 'any', 'conditional_display' => array('relation' => 'AND', 'custom' => ''), 'preview' => 'edit_mode', 'admin_html_preview' => '', 'admin_styles' => '', 'slug' => 'wc-views-types-groups-fields'); //Save group $group_id = wpcf_admin_fields_save_group($group); //Save group fields wpcf_admin_fields_save_group_fields($group_id, $fields_bulk); } } }
/** * All AJAX calls go here. * * @global object $wpdb * */ function wpcf_ajax() { if (!current_user_can('manage_options') || (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } switch ($_REQUEST['wpcf_action']) { /* User meta actions*/ case 'user_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; require_once WPCF_INC_ABSPATH . '/usermeta-control.php'; wpcf_admin_user_fields_control_bulk_ajax(); break; case 'usermeta_delete': case 'delete_usermeta': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { $field_id = sanitize_text_field($_GET['field_id']); wpcf_admin_fields_delete_field($field_id, 'wp-types-user-group', 'wpcf-usermeta'); } if (isset($_GET['field'])) { $field = sanitize_text_field($_GET['field']); wpcf_admin_fields_delete_field($field, 'wp-types-user-group', 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'remove_from_history2': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta'); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true, 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'deactivate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_usermeta_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id']), 'wp-types-user-group'); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'usermeta_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; require_once WPCF_INC_ABSPATH . '/usermeta-form.php'; wpcf_usermeta_insert_existing_ajax(); wpcf_form_render_js_validation(); break; /* End Usertmeta actions*/ /* End Usertmeta actions*/ case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group(intval($_GET['group_id']), sanitize_text_field($_GET['field_id'])); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'deactivate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 1; $custom_types[$_GET['wpcf-post-type']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post type deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { unset($custom_types[$_GET['wpcf-post-type']]['disabled']); $custom_types[$_GET['wpcf-post-type']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post type activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $custom_type = strval($_GET['wpcf-post-type']); /** * Delete relation between custom posts types * * Filter allow to delete all custom fields used to make * a relation between posts. * * @since 1.6.4 * * @param bool $delete True or false flag to delete relationships. * @param string $var Currently deleted custom post type. */ if (apply_filters('wpcf_delete_relation_meta', false, $custom_type)) { global $wpdb; $wpdb->delete($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $custom_type)), array('%s')); } unset($custom_types[$custom_type]); /** * remove post relation */ foreach (array_keys($custom_types) as $post_type) { if (array_key_exists('post_relationship', $custom_types[$post_type])) { /** * remove "has" relation */ if (array_key_exists('has', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['has'])) { unset($custom_types[$post_type]['post_relationship']['has'][$custom_type]); $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); } /** * remove "belongs" relation */ if (array_key_exists('belongs', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['belongs'])) { unset($custom_types[$post_type]['post_relationship']['belongs'][$custom_type]); $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); wpcf_admin_deactivate_content('post_type', $custom_type); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'duplicate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $custom_type = esc_html(strval($_GET['wpcf-post-type'])); if (isset($custom_types[$custom_type])) { $i = 0; $key = false; do { $key = sprintf($custom_type . '-%d', ++$i); } while (isset($custom_types[$key])); if ($key) { /** * duplicate custom post type */ $custom_types[$key] = $custom_types[$custom_type]; /** * update some options */ $custom_types[$key]['labels']['name'] .= sprintf(' (%d)', $i); $custom_types[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $custom_types[$key]['slug'] = $key; $custom_types[$key]['__types_id'] = $key; /** * update custom post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); /** * update custom taxonomies too */ $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); foreach ($custom_taxonomies as $taxonomy_key => $taxonomy_data) { if (isset($taxonomy_data['supports']) && isset($taxonomy_data['supports'][$custom_type])) { $custom_taxonomies[$taxonomy_key]['supports'][$key] = 1; } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('execute' => 'location.reload();')); } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'taxonomy_duplicate': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomy = esc_html(strval($_GET['wpcf-tax'])); require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$custom_taxonomy])) { $i = 0; $key = false; do { $key = sprintf($custom_taxonomy . '-%d', ++$i); } while (isset($custom_taxonomies[$key])); if ($key) { /** * duplicate custom taxonomies */ $custom_taxonomies[$key] = $custom_taxonomies[$custom_taxonomy]; /** * update some options */ $custom_taxonomies[$key]['labels']['name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['slug'] = $key; $custom_taxonomies[$key]['id'] = $key; $custom_taxonomies[$key]['__types_id'] = $key; /** * update custom taxonomies */ update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); /** * update post types */ if (isset($custom_taxonomies[$key]['supports']) && is_array($custom_taxonomies[$key]['supports']) && !empty($custom_taxonomies[$key]['supports'])) { $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); foreach (array_keys($custom_taxonomies[$key]['supports']) as $custom_type) { /** * avoid to create fake CPT from old data */ if (!isset($custom_types[$custom_type])) { continue; } if (!isset($custom_types[$custom_type]['taxonomies'])) { $custom_types[$custom_type]['taxonomies'] = array(); } $custom_types[$custom_type]['taxonomies'][$key] = 1; } /** * update custom post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); } echo json_encode(array('execute' => 'location.reload();')); } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'deactivate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 1; $custom_taxonomies[$_GET['wpcf-tax']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 0; $custom_taxonomies[$_GET['wpcf-tax']][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'location.reload();')); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $custom_taxonomy = strval($_GET['wpcf-tax']); unset($custom_taxonomies[$custom_taxonomy]); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); wpcf_admin_deactivate_content('taxonomy', $custom_taxonomy); echo json_encode(array('output' => '', 'execute' => 'location.reload();')); break; case 'add_radio_option': require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $element = wpcf_fields_radio_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'jQuery("#wpcf-form-groups-radio-ajax-response-' . urldecode($_GET['wpcf_ajax_update_add']) . '").append(\'' . trim(str_replace("\r\n", '', wpcf_form_simple($element_txt))) . '\');', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'add_checkboxes_option': require_once WPCF_INC_ABSPATH . '/fields/checkboxes.php'; $element = wpcf_fields_checkboxes_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_checkboxes_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = sanitize_text_field($_GET['group_id']); $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'group_update_post_types': require_once WPCF_INC_ABSPATH . '/fields.php'; // @todo Sanitize this $post_types $post_types = empty($_GET['wpcf']['group']['supports']) ? array() : $_GET['wpcf']['group']['supports']; if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_post_types(sanitize_text_field($_GET['group_id']), $post_types); $output = array(); foreach ($post_types as $post_type) { $post_type = get_post_type_object($post_type); if (!empty($post_type->label)) { $output[] = $post_type->label; } } if (empty($post_types)) { $output[] = __('No post types associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No post types associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'group_update_taxonomies': require_once WPCF_INC_ABSPATH . '/fields.php'; $taxonomies_post = empty($_GET['wpcf']['group']['taxonomies']) ? array() : $_GET['wpcf']['group']['taxonomies']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_terms(sanitize_text_field($_GET['group_id']), $terms); $output = array(); foreach ($taxonomies_post as $taxonomy => $terms) { $taxonomy = get_taxonomy($taxonomy); if (!empty($taxonomy)) { $title = $taxonomy->label . ': '; foreach ($terms as $term_id) { $term = get_term($term_id, $taxonomy->name); $output[] = $title . $term->name; $title = ''; } } } if (empty($output)) { $output[] = __('No taxonomies associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No taxonomies associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'custom_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; wpcf_admin_custom_fields_control_bulk_ajax(); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field_id'])); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field'])); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; case 'add_condition': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_ABSPATH . '/includes/conditional-display.php'; if (!empty($_GET['field']) || !empty($_GET['group'])) { $data = array(); if (isset($_GET['group'])) { $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter(array(), array(), null, true)); echo json_encode(array('output' => $output)); } else { $data['id'] = str_replace('_conditional_display', '', sanitize_text_field($_GET['field'])); $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter($data, array(), null, false)); if (!empty($data['id'])) { echo json_encode(array('output' => $output)); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'pt_edit_fields': if (!empty($_GET['parent']) && !empty($_GET['child'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/post-relationship.php'; wpcf_pr_admin_edit_fields(sanitize_text_field($_GET['parent']), sanitize_text_field($_GET['child'])); } break; case 'toggle': $option = get_option('wpcf_toggle', array()); $hidden = isset($_GET['hidden']) ? (bool) $_GET['hidden'] : 1; $_GET['div'] = strval($_GET['div']); if (!$hidden) { unset($option[$_GET['div']]); } else { $option[$_GET['div']] = 1; } update_option('wpcf_toggle', $option); break; case 'cb_save_empty_migrate': $output = sprintf('<span style="color:red;">%s</div>', __('Migration process is not yet finished - please save group first, then change settings of this field.', 'wpcf')); if (isset($_GET['field']) && isset($_GET['subaction'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; $option = $_GET['meta_type'] == 'usermeta' ? 'wpcf-usermeta' : 'wpcf-fields'; $meta_type = sanitize_text_field($_GET['meta_type']); $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field']), false, false, false, $option); $_txt_updates = $meta_type == 'usermeta' ? __('%d users require update', 'wpcf') : __('%d posts require update', 'wpcf'); $_txt_no_updates = $meta_type == 'usermeta' ? __('No users require update', 'wpcf') : __('No posts require update', 'wpcf'); $_txt_updated = $meta_type == 'usermeta' ? __('Users updated', 'wpcf') : __('Posts updated', 'wpcf'); if (!empty($field)) { if ($_GET['subaction'] == 'save_check' || $_GET['subaction'] == 'do_not_save_check') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty_check($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty_check($field, $_GET['subaction']); } } if (!empty($posts)) { $output = '<div class="message updated"><p>' . sprintf($_txt_updates, count($posts)) . ' ' . '<a href="javascript:void(0);" class="button-primary" onclick="' . 'wpcfCbSaveEmptyMigrate(jQuery(this).parent().parent().parent(), \'' . sanitize_text_field($_GET['field']) . '\', ' . count($posts) . ', \'' . wp_create_nonce('cb_save_empty_migrate') . '\', \''; $output .= $_GET['subaction'] == 'save_check' ? 'save' : 'do_not_save'; $output .= '\', \'' . $meta_type . '\');' . '">' . __('Update') . '</a>' . '</p></div>'; } else { $output = '<div class="message updated"><p><em>' . $_txt_no_updates . '</em></p></div>'; } } else { if ($_GET['subaction'] == 'save' || $_GET['subaction'] == 'do_not_save') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty($field, $_GET['subaction']); } } if (isset($posts['offset'])) { if (!isset($_GET['total'])) { $output = '<span style="color:red;">' . __('Error occured', 'wpcf') . '</div>'; } else { $output = '<script type="text/javascript">wpcfCbMigrateStep(' . intval($_GET['total']) . ',' . $posts['offset'] . ',' . '\'' . sanitize_text_field($_GET['field']) . '\',' . '\'' . wp_create_nonce('cb_save_empty_migrate') . '\', \'' . $meta_type . '\');</script>' . number_format($posts['offset']) . '/' . number_format(intval($_GET['total'])) . '<div class="wpcf-ajax-loading-small"></div>'; } } else { $output = '<div class="message updated"><p>' . $_txt_updated . '</p></div>'; } } } } } echo json_encode(array('output' => $output)); break; default: break; } die; }
/** * Deletes field. * * @param type $field_id */ function wpcf_admin_fields_delete_field($field_id) { global $wpdb; $fields = get_option('wpcf-fields', array()); if (isset($fields[$field_id])) { // Remove from groups $groups = wpcf_admin_fields_get_groups(); foreach ($groups as $key => $group) { wpcf_admin_fields_remove_field_from_group($group['id'], $field_id); } // Remove from posts if (!wpcf_types_cf_under_control('check_outsider', $field_id)) { $results = $wpdb->get_results("SELECT post_id, meta_key FROM {$wpdb->postmeta} WHERE meta_key = '" . wpcf_types_get_meta_prefix($fields[$field_id]) . strval($field_id) . "'"); foreach ($results as $result) { delete_post_meta($result->post_id, $result->meta_key); } } unset($fields[$field_id]); wpcf_admin_fields_save_fields($fields, true); return true; } else { return false; } }
private function process_bulk_action() { $action = $this->current_action(); if (false == $action) { return; } if (!wp_verify_nonce(wpcf_getpost('_wpnonce'), WPCF_Page_Control_Termmeta::BULK_ACTION_NONCE)) { wp_die(__('Invalid nonce.', 'wpcf')); } $selected_field_definitions = wpcf_getpost(self::INPUT_SLUGS, array()); if (is_string($selected_field_definitions)) { $selected_field_definitions = array($selected_field_definitions); } if (!is_array($selected_field_definitions) || empty($selected_field_definitions)) { // Nothing to do here return; } $factory = WPCF_Field_Term_Definition_Factory::get_instance(); switch ($action) { case self::BULK_ACTION_ADD_TO_GROUP: $group_ids = $this->read_group_ids(); foreach ($group_ids as $group_id) { wpcf_admin_fields_save_group_fields($group_id, $selected_field_definitions, true, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); } break; case self::BULK_ACTION_REMOVE_FROM_TO_GROUP: $group_ids = $this->read_group_ids(); foreach ($group_ids as $group_id) { wpcf_admin_fields_remove_field_from_group_bulk($group_id, $selected_field_definitions); } break; case self::BULK_ACTION_CHANGE_TYPE: $field_type_slug = wpcf_getpost('wpcf-id'); if (!empty($field_type_slug)) { wpcf_admin_custom_fields_change_type($selected_field_definitions, $field_type_slug, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); } break; case self::BULK_ACTION_ACTIVATE: $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $selected_field_definitions), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message(sprintf(__('Added to Types control: %s', 'wpcf'), esc_html($field_id)), 'updated', 'echo'); } wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); break; case self::BULK_ACTION_DEACTIVATE: $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); foreach ($selected_field_definitions as $field_id) { $field_id = sanitize_text_field($field_id); if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']), 'updated', 'echo'); } } wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); break; case self::BULK_ACTION_DELETE: $failed = array(); $success = array(); foreach ($selected_field_definitions as $field_id) { $field_id = sanitize_text_field($field_id); // Permanently single field definition and field data. $field_definition = $factory->load_field_definition($field_id); if (null == $field_definition) { $response = false; } else { $response = $factory->delete_definition($field_definition); } if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message(sprintf(__('Fields %s have been deleted.', 'wpcf'), esc_html(implode(', ', $success))), 'updated', 'echo'); } if (!empty($failed)) { wpcf_admin_message(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), esc_html(implode(', ', $failed))), 'error', 'echo'); } break; } // We made changes to field definitions and now the listing table is going to be rendered. $factory->clear_definition_storage(); }
/** * Actions for outside fields control. * * @param type $action */ function wpcf_types_cf_under_control($action = 'add', $args = array()) { global $wpcf_types_under_control; $wpcf_types_under_control['errors'] = array(); switch ($action) { case 'add': $fields = wpcf_admin_fields_get_fields(); foreach ($args['fields'] as $field_id) { $field_type = !empty($args['type']) ? $args['type'] : 'textfield'; if (strpos($field_id, md5('wpcf_not_controlled')) !== false) { $field_id_add = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); // Activating field that previously existed in Types if (array_key_exists($field_id_add, $fields)) { $fields[$field_id_add]['data']['disabled'] = 0; } else { // Adding from outside $fields[$field_id_add]['id'] = $field_id_add; $fields[$field_id_add]['type'] = $field_type; $fields[$field_id_add]['name'] = $field_id_add; $fields[$field_id_add]['slug'] = $field_id_add; $fields[$field_id_add]['description'] = ''; $fields[$field_id_add]['data'] = array(); // @TODO WATCH THIS! MUST NOT BE DROPPED IN ANY CASE $fields[$field_id_add]['data']['controlled'] = 1; } $unset_key = array_search($field_id, $args['fields']); if ($unset_key !== false) { unset($args['fields'][$unset_key]); $args['fields'][$unset_key] = $field_id_add; } } } wpcf_admin_fields_save_fields($fields, true); return $args['fields']; break; case 'check_exists': $fields = wpcf_admin_fields_get_fields(); $field = $args; if (array_key_exists($field, $fields) && empty($fields[$field]['data']['disabled'])) { return true; } return false; break; case 'check_outsider': $fields = wpcf_admin_fields_get_fields(); $field = $args; if (array_key_exists($field, $fields) && !empty($fields[$field]['data']['controlled'])) { return true; } return false; break; default: break; } }
/** * Deletes field. * Modified by Gen, 13.02.2013 * * @global object $wpdb * * @param type $field_id */ function wpcf_admin_fields_delete_field($field_id, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME, $meta_name = 'wpcf-fields') { global $wpdb; $fields = get_option($meta_name, array()); if (isset($fields[$field_id])) { // Remove from groups $groups = wpcf_admin_fields_get_groups($post_type); foreach ($groups as $key => $group) { wpcf_admin_fields_remove_field_from_group($group['id'], $field_id); } // Remove from posts if (!wpcf_types_cf_under_control('check_outsider', $field_id, $post_type, $meta_name)) { $results = $wpdb->get_results($wpdb->prepare("SELECT post_id, meta_key FROM {$wpdb->postmeta} WHERE meta_key = %s", wpcf_types_get_meta_prefix($fields[$field_id]) . strval($field_id))); foreach ($results as $result) { delete_post_meta($result->post_id, $result->meta_key); } } unset($fields[$field_id]); wpcf_admin_fields_save_fields($fields, true, $meta_name); return true; } else { return false; } }
/** * Completely erase field definition from options and clear cache. * * @param string $field_slug */ private function erase_field_definition_from_options($field_slug) { $fields_from_options = $this->get_fields_from_options(); unset($fields_from_options[$field_slug]); wpcf_admin_fields_save_fields($fields_from_options, true, $this->get_option_name()); $this->clear_definition_storage($field_slug); }
/** * All AJAX calls go here. * * @global object $wpdb * */ function wpcf_ajax() { /** * check nounce */ if (!(isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } require_once WPCF_INC_ABSPATH . '/classes/class.wpcf.roles.php'; /** * check permissions */ switch ($_REQUEST['wpcf_action']) { case 'deactivate_post_type': case 'activate_post_type': case 'delete_post_type': case 'duplicate_post_type': $post_type = wpcf_ajax_helper_get_post_type(); if (empty($post_type)) { wpcf_ajax_helper_print_error_and_die(); } if (!WPCF_Roles::user_can_edit_custom_post_by_slug($post_type)) { wpcf_ajax_helper_verification_failed_and_die(); } break; case 'taxonomy_duplicate': case 'deactivate_taxonomy': case 'activate_taxonomy': case 'delete_taxonomy': $custom_taxonomy = wpcf_ajax_helper_get_taxonomy(); if (empty($custom_taxonomy)) { wpcf_ajax_helper_print_error_and_die(); } if (!WPCF_Roles::user_can_edit_custom_taxonomy_by_slug($custom_taxonomy)) { wpcf_ajax_helper_verification_failed_and_die(); } break; case 'deactivate_group': case 'activate_group': case 'delete_group': if (!isset($_GET['group_id']) || empty($_GET['group_id'])) { wpcf_ajax_helper_print_error_and_die(); } if (!WPCF_Roles::user_can_edit_custom_field_group_by_id($_GET['group_id'])) { wpcf_ajax_helper_verification_failed_and_die(); } break; case 'deactivate_user_group': case 'activate_user_group': case 'delete_usermeta_group': if (!isset($_GET['group_id']) || empty($_GET['group_id'])) { wpcf_ajax_helper_print_error_and_die(); } if (!WPCF_Roles::user_can_edit_usermeta_field_group_by_id($_GET['group_id'])) { wpcf_ajax_helper_verification_failed_and_die(); } break; case 'deactivate_term_group': case 'activate_term_group': case 'delete_term_group': if (!isset($_GET['group_id']) || empty($_GET['group_id'])) { wpcf_ajax_helper_print_error_and_die(); } if (!WPCF_Roles::user_can_edit_term_field_group_by_id($_GET['group_id'])) { wpcf_ajax_helper_verification_failed_and_die(); } break; case 'usermeta_delete': case 'delete_usermeta': case 'remove_from_history2': case 'usermeta_insert_existing': case 'fields_insert': case 'fields_insert_existing': case 'remove_field_from_group': case 'add_radio_option': case 'add_select_option': case 'add_checkboxes_option': case 'group_form_collapsed': case 'form_fieldset_toggle': case 'fields_delete': case 'delete_field': case 'remove_from_history': case 'add_condition': case 'pt_edit_fields': case 'toggle': case 'cb_save_empty_migrate': if (!current_user_can('manage_options')) { wpcf_ajax_helper_verification_failed_and_die(); } /** * do not check actions from other places */ break; default: return; } /** * do actions */ switch ($_REQUEST['wpcf_action']) { /* User meta actions*/ case 'usermeta_delete': case 'delete_usermeta': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { $field_id = sanitize_text_field($_GET['field_id']); wpcf_admin_fields_delete_field($field_id, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta'); } if (isset($_GET['field'])) { $field = sanitize_text_field($_GET['field']); wpcf_admin_fields_delete_field($field, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'remove_from_history2': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta'); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true, 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'deactivate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id']), TYPES_USER_META_FIELD_GROUP_CPT_NAME); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); die; } break; case 'activate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id']), TYPES_USER_META_FIELD_GROUP_CPT_NAME); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); die; } break; case 'delete_usermeta_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $redirect = wpcf_ajax_group_delete_redirect(); wpcf_admin_fields_delete_group(intval($_GET['group_id']), TYPES_USER_META_FIELD_GROUP_CPT_NAME); echo json_encode($redirect); break; case 'usermeta_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; require_once WPCF_INC_ABSPATH . '/usermeta-form.php'; wpcf_usermeta_insert_existing_ajax(); wpcf_form_render_js_validation(); break; /* End Usertmeta actions*/ /* End Usertmeta actions*/ case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group(intval($_GET['group_id']), sanitize_text_field($_GET['field_id'])); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $redirect = wpcf_ajax_group_delete_redirect(); wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode($redirect); break; case 'deactivate_term_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id']), Types_Field_Group_Term::POST_TYPE); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); } break; case 'activate_term_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id']), Types_Field_Group_Term::POST_TYPE); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { wpcf_ajax_helper_print_error_and_die(); } break; case 'delete_term_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $redirect = wpcf_ajax_group_delete_redirect(); wpcf_admin_fields_delete_group(intval($_GET['group_id']), Types_Field_Group_Term::POST_TYPE); echo json_encode($redirect); break; case 'deactivate_post_type': $post_type = wpcf_ajax_helper_get_post_type(); if (empty($post_type)) { wpcf_ajax_helper_print_error_and_die(); } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $custom_types[$post_type]['disabled'] = 1; $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post Type deactivated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'activate_post_type': $post_type = wpcf_ajax_helper_get_post_type(); if (empty($post_type)) { wpcf_ajax_helper_print_error_and_die(); } $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); unset($custom_types[$post_type]['disabled']); $custom_types[$post_type][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); echo json_encode(array('output' => __('Post Type activated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'delete_post_type': $post_type = wpcf_ajax_helper_get_post_type(); if (empty($post_type)) { wpcf_ajax_helper_print_error_and_die(); } $post_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); /** * Delete relation between custom posts types * * Filter allow to delete all custom fields used to make * a relation between posts. * * @since 1.6.4 * * @param bool $delete True or false flag to delete relationships. * @param string $var Currently deleted post type. */ if (apply_filters('wpcf_delete_relation_meta', false, $post_type)) { global $wpdb; $wpdb->delete($wpdb->postmeta, array('meta_key' => sprintf('_wpcf_belongs_%s_id', $post_type)), array('%s')); } unset($post_types[$post_type]); /** * remove post relation */ foreach (array_keys($post_types) as $post_type) { if (array_key_exists('post_relationship', $post_types[$post_type])) { /** * remove "has" relation */ if (array_key_exists('has', $post_types[$post_type]['post_relationship']) && array_key_exists($post_type, $post_types[$post_type]['post_relationship']['has'])) { unset($post_types[$post_type]['post_relationship']['has'][$post_type]); $post_types[$post_type][TOOLSET_EDIT_LAST] = time(); } /** * remove "belongs" relation */ if (array_key_exists('belongs', $post_types[$post_type]['post_relationship']) && array_key_exists($post_type, $post_types[$post_type]['post_relationship']['belongs'])) { unset($post_types[$post_type]['post_relationship']['belongs'][$post_type]); $post_types[$post_type][TOOLSET_EDIT_LAST] = time(); } } } update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $post_types); wpcf_admin_deactivate_content('post_type', $post_type); echo json_encode(array('output' => '', 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'duplicate_post_type': $post_type = wpcf_ajax_helper_get_post_type(); if (empty($post_type)) { wpcf_ajax_helper_print_error_and_die(); } $post_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); $i = 0; $key = false; do { $key = sprintf($post_type . '-%d', ++$i); } while (isset($post_types[$key])); if ($key) { /** * duplicate post type */ $post_types[$key] = $post_types[$post_type]; /** * update some options */ $post_types[$key]['labels']['name'] .= sprintf(' (%d)', $i); $post_types[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $post_types[$key]['slug'] = $key; $post_types[$key]['__types_id'] = $key; /** * update post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $post_types); /** * update custom taxonomies too */ $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); foreach ($custom_taxonomies as $taxonomy_key => $taxonomy_data) { if (isset($taxonomy_data['supports']) && isset($taxonomy_data['supports'][$post_type])) { $custom_taxonomies[$taxonomy_key]['supports'][$key] = 1; } } update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } break; case 'taxonomy_duplicate': $custom_taxonomy = wpcf_ajax_helper_get_taxonomy(); if (empty($custom_taxonomy)) { wpcf_ajax_helper_print_error_and_die(); } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); $i = 0; $key = false; do { $key = sprintf($custom_taxonomy . '-%d', ++$i); } while (isset($custom_taxonomies[$key])); if ($key) { /** * duplicate custom taxonomies */ $custom_taxonomies[$key] = $custom_taxonomies[$custom_taxonomy]; /** * update some options */ $custom_taxonomies[$key]['labels']['name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['labels']['singular_name'] .= sprintf(' (%d)', $i); $custom_taxonomies[$key]['slug'] = $key; $custom_taxonomies[$key]['id'] = $key; $custom_taxonomies[$key]['__types_id'] = $key; /** * update custom taxonomies */ update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); /** * update post types */ if (isset($custom_taxonomies[$key]['supports']) && is_array($custom_taxonomies[$key]['supports']) && !empty($custom_taxonomies[$key]['supports'])) { $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array()); foreach (array_keys($custom_taxonomies[$key]['supports']) as $custom_type) { /** * avoid to create fake CPT from old data */ if (!isset($custom_types[$custom_type])) { continue; } if (!isset($custom_types[$custom_type]['taxonomies'])) { $custom_types[$custom_type]['taxonomies'] = array(); } $custom_types[$custom_type]['taxonomies'][$key] = 1; } /** * update post types */ update_option(WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types); } echo json_encode(array('execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } break; case 'deactivate_taxonomy': $custom_taxonomy = wpcf_ajax_helper_get_taxonomy(); if (empty($custom_taxonomy)) { wpcf_ajax_helper_print_error_and_die(); } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (!isset($custom_taxonomies[$custom_taxonomy]) || !isset($custom_taxonomies[$custom_taxonomy]['supports'])) { $taxonomy = get_taxonomy($custom_taxonomy); $supports = array(); if (is_object($taxonomy)) { if (!empty($taxonomy->object_type)) { foreach ($taxonomy->object_type as $support_post) { $supports[$support_post] = 1; } } $custom_taxonomies[$custom_taxonomy] = array('slug' => $taxonomy->name, 'labels' => array('singular_name' => $taxonomy->labels->singular_name, 'name' => $taxonomy->labels->name), 'supports' => $supports, 'type' => '_builtin', '_builtin' => true); } else { wpcf_ajax_helper_print_error_and_die(); } } if (wpcf_is_builtin_taxonomy($custom_taxonomy)) { $custom_taxonomies[$custom_taxonomy]['type'] = '_builtin'; $custom_taxonomies[$custom_taxonomy]['_builtin'] = true; } $custom_taxonomies[$custom_taxonomy]['disabled'] = 1; $custom_taxonomies[$custom_taxonomy][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'activate_taxonomy': $custom_taxonomy = wpcf_ajax_helper_get_taxonomy(); if (empty($custom_taxonomy)) { wpcf_ajax_helper_print_error_and_die(); } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); if (!isset($custom_taxonomies[$custom_taxonomy])) { $taxonomy = get_taxonomy($custom_taxonomy); if (is_object($taxonomy)) { $custom_taxonomies[$custom_taxonomy] = array('slug' => $taxonomy->name, 'labels' => array('singular_name' => $taxonomy->labels->singular_name), 'type' => '_builtin', '_builtin' => true); } else { wpcf_ajax_helper_print_error_and_die(); } } if (wpcf_is_builtin_taxonomy($custom_taxonomy)) { $custom_taxonomies[$custom_taxonomy]['type'] = '_builtin'; $custom_taxonomies[$custom_taxonomy]['_builtin'] = true; } $custom_taxonomies[$custom_taxonomy]['disabled'] = 0; $custom_taxonomies[$custom_taxonomy][TOOLSET_EDIT_LAST] = time(); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'delete_taxonomy': $custom_taxonomy = wpcf_ajax_helper_get_taxonomy(); if (empty($custom_taxonomy)) { wpcf_ajax_helper_print_error_and_die(); } $custom_taxonomies = get_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array()); unset($custom_taxonomies[$custom_taxonomy]); update_option(WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $custom_taxonomies); wpcf_admin_deactivate_content('taxonomy', $custom_taxonomy); // on group edit page if (isset($_GET['wpcf_ref'])) { $redirect = $_GET['wpcf_ref'] == 'dashboard' ? admin_url('admin.php?page=toolset-dashboard') : admin_url('admin.php?page=wpcf-ctt'); echo json_encode(array('output' => '', 'execute' => 'redirect', 'wpcf_redirect' => $redirect, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); // on listing page } else { echo json_encode(array('output' => '', 'execute' => 'reload', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } break; case 'add_radio_option': /** * check required data */ if (!isset($_GET['parent_name']) || !isset($_GET['wpcf_ajax_update_add'])) { wpcf_ajax_helper_print_error_and_die(); } require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $value = esc_html(urldecode($_GET['parent_name'])); $element = wpcf_fields_radio_get_option($value); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, $value); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'append', 'append_target' => '#wpcf-form-groups-radio-ajax-response-' . esc_html(urldecode($_GET['wpcf_ajax_update_add'])), 'append_value' => trim(preg_replace('/[\\r\\n]+/', '', wpcf_form_simple($element_txt))), 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': /** * check required data */ if (!isset($_GET['parent_name'])) { wpcf_ajax_helper_print_error_and_die(); } require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(esc_html(urldecode($_GET['parent_name']))); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'add_checkboxes_option': /** * check required data */ if (!isset($_GET['parent_name'])) { wpcf_ajax_helper_print_error_and_die(); } require_once WPCF_INC_ABSPATH . '/fields/checkboxes.php'; $value = esc_html(urldecode($_GET['parent_name'])); $element = wpcf_fields_checkboxes_get_option($value); $id = array_shift($element); $element_txt = wpcf_fields_checkboxes_get_option_alt_text($id, $value); echo json_encode(array('output' => wpcf_form_simple($element), 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = sanitize_text_field($_GET['group_id']); $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = sanitize_text_field($_GET['toggle']); $fieldset = sanitize_text_field($_GET['id']); wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field_id'])); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field(sanitize_text_field($_GET['field'])); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; case 'add_condition': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_ABSPATH . '/includes/conditional-display.php'; if (!empty($_GET['field']) || !empty($_GET['group'])) { $data = array(); if (isset($_GET['group'])) { $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter(array(), array(), null, true)); echo json_encode(array('output' => $output)); } else { $data['id'] = str_replace('_conditional_display', '', sanitize_text_field($_GET['field'])); $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter($data, array(), null, false)); if (!empty($data['id'])) { echo json_encode(array('output' => $output)); } else { wpcf_ajax_helper_print_error_and_die(); } } } else { wpcf_ajax_helper_print_error_and_die(); } break; case 'pt_edit_fields': if (!empty($_GET['parent']) && !empty($_GET['child'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/post-relationship.php'; wpcf_pr_admin_edit_fields(sanitize_text_field($_GET['parent']), sanitize_text_field($_GET['child'])); } break; case 'toggle': $option = get_option('wpcf_toggle', array()); $hidden = isset($_GET['hidden']) ? (bool) $_GET['hidden'] : 1; $div = esc_html(strval($_GET['div'])); if (!$hidden) { unset($option[$div]); } else { $option[$div] = 1; } update_option('wpcf_toggle', $option); break; case 'cb_save_empty_migrate': $output = sprintf('<span style="color:red;">%s</div>', __('Migration process is not yet finished - please save group first, then change settings of this field.', 'wpcf')); if (isset($_GET['field']) && isset($_GET['subaction'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; $option = $_GET['meta_type'] == 'usermeta' ? 'wpcf-usermeta' : 'wpcf-fields'; $meta_type = sanitize_text_field($_GET['meta_type']); $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field']), false, false, false, $option); $_txt_updates = $meta_type == 'usermeta' ? __('%d users require update', 'wpcf') : __('%d posts require update', 'wpcf'); $_txt_no_updates = $meta_type == 'usermeta' ? __('No users require update', 'wpcf') : __('No posts require update', 'wpcf'); $_txt_updated = $meta_type == 'usermeta' ? __('Users updated', 'wpcf') : __('Posts updated', 'wpcf'); if (!empty($field)) { if ($_GET['subaction'] == 'save_check' || $_GET['subaction'] == 'do_not_save_check') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty_check($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty_check($field, $_GET['subaction']); } } if (!empty($posts)) { $output = '<div class="message updated"><p>' . sprintf($_txt_updates, count($posts)) . ' ' . '<a href="javascript:void(0);" class="button-primary" onclick="' . 'wpcfCbSaveEmptyMigrate(jQuery(this).parent().parent().parent(), \'' . sanitize_text_field($_GET['field']) . '\', ' . count($posts) . ', \'' . wp_create_nonce('cb_save_empty_migrate') . '\', \''; $output .= $_GET['subaction'] == 'save_check' ? 'save' : 'do_not_save'; $output .= '\', \'' . $meta_type . '\');' . '">' . __('Update', 'wpcf') . '</a>' . '</p></div>'; } else { $output = '<div class="message updated"><p><em>' . $_txt_no_updates . '</em></p></div>'; } } else { if ($_GET['subaction'] == 'save' || $_GET['subaction'] == 'do_not_save') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty($field, $_GET['subaction']); } } if (isset($posts['offset'])) { if (!isset($_GET['total'])) { $output = '<span style="color:red;">' . __('Error occured', 'wpcf') . '</div>'; } else { $output = '<script type="text/javascript">wpcfCbMigrateStep(' . intval($_GET['total']) . ',' . $posts['offset'] . ',' . '\'' . sanitize_text_field($_GET['field']) . '\',' . '\'' . wp_create_nonce('cb_save_empty_migrate') . '\', \'' . $meta_type . '\');</script>' . number_format($posts['offset']) . '/' . number_format(intval($_GET['total'])) . '<div class="wpcf-ajax-loading-small"></div>'; } } else { $output = sprintf('<div class="message updated"><p>%s</p></div>', $_txt_updated); } } } } } echo json_encode(array('output' => $output)); break; default: break; } die; }
/** * All AJAX calls go here. */ function wpcf_ajax() { if (!current_user_can('manage_options') || (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action']))) { die; } switch ($_REQUEST['wpcf_action']) { /* User meta actions*/ case 'user_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; require_once WPCF_INC_ABSPATH . '/usermeta-control.php'; wpcf_admin_user_fields_control_bulk_ajax(); break; case 'usermeta_delete': case 'delete_usermeta': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field($_GET['field_id'], 'wp-types-user-group', 'wpcf-usermeta'); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field($_GET['field'], 'wp-types-user-group', 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'remove_from_history2': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(true, true, false, 'wpcf-usermeta'); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true, 'wpcf-usermeta'); } echo json_encode(array('output' => '')); break; case 'deactivate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_usermeta_get_ajax_activation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_user_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id']), 'wp-types-user-group'); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_usermeta_get_ajax_deactivation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_usermeta_group': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/usermeta.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id']), 'wp-types-user-group'); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'usermeta_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; require_once WPCF_INC_ABSPATH . '/usermeta-form.php'; wpcf_usermeta_insert_existing_ajax(); wpcf_form_render_js_validation(); break; /* End Usertmeta actions*/ /* End Usertmeta actions*/ case 'fields_insert': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_ajax(); wpcf_form_render_js_validation(); break; case 'fields_insert_existing': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-form.php'; wpcf_fields_insert_existing_ajax(); wpcf_form_render_js_validation(); break; case 'remove_field_from_group': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['group_id']) && isset($_GET['field_id'])) { wpcf_admin_fields_remove_field_from_group($_GET['group_id'], $_GET['field_id']); } break; case 'deactivate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_deactivate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_activation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_group': require_once WPCF_INC_ABSPATH . '/fields.php'; $success = wpcf_admin_fields_activate_group(intval($_GET['group_id'])); if ($success) { echo json_encode(array('output' => __('Group activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").replaceWith(\'' . wpcf_admin_fields_get_ajax_deactivation_link(intval($_GET['group_id'])) . '\');jQuery(".wpcf-table-column-active-' . intval($_GET['group_id']) . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_group': require_once WPCF_INC_ABSPATH . '/fields.php'; wpcf_admin_fields_delete_group(intval($_GET['group_id'])); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . intval($_GET['group_id']) . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 1; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_activation_link(esc_attr($_GET['wpcf-post-type'])) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-types.php'; $custom_types = get_option('wpcf-custom-types', array()); if (isset($custom_types[$_GET['wpcf-post-type']])) { $custom_types[$_GET['wpcf-post-type']]['disabled'] = 0; update_option('wpcf-custom-types', $custom_types); echo json_encode(array('output' => __('Post type activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").replaceWith(\'' . wpcf_admin_custom_types_get_ajax_deactivation_link($_GET['wpcf-post-type']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-post-type'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_post_type': if (!isset($_GET['wpcf-post-type'])) { die; } $custom_types = get_option('wpcf-custom-types', array()); $custom_type = strval($_GET['wpcf-post-type']); unset($custom_types[$custom_type]); /** * remove post relation */ foreach (array_keys($custom_types) as $post_type) { if (array_key_exists('post_relationship', $custom_types[$post_type])) { /** * remove "has" relation */ if (array_key_exists('has', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['has'])) { unset($custom_types[$post_type]['post_relationship']['has'][$custom_type]); } /** * remove "belongs" relation */ if (array_key_exists('belongs', $custom_types[$post_type]['post_relationship']) && array_key_exists($custom_type, $custom_types[$post_type]['post_relationship']['belongs'])) { unset($custom_types[$post_type]['post_relationship']['belongs'][$custom_type]); } } } update_option('wpcf-custom-types', $custom_types); wpcf_admin_deactivate_content('post_type', $custom_type); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-post-type'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'deactivate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 1; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy deactivated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_activation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('No', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'activate_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } require_once WPCF_INC_ABSPATH . '/custom-taxonomies.php'; $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); if (isset($custom_taxonomies[$_GET['wpcf-tax']])) { $custom_taxonomies[$_GET['wpcf-tax']]['disabled'] = 0; update_option('wpcf-custom-taxonomies', $custom_taxonomies); echo json_encode(array('output' => __('Taxonomy activated', 'wpcf'), 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").replaceWith(\'' . wpcf_admin_custom_taxonomies_get_ajax_deactivation_link($_GET['wpcf-tax']) . '\');jQuery(".wpcf-table-column-active-' . $_GET['wpcf-tax'] . '").html("' . __('Yes', 'wpcf') . '");', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'delete_taxonomy': if (!isset($_GET['wpcf-tax'])) { die; } $custom_taxonomies = get_option('wpcf-custom-taxonomies', array()); $custom_taxonomy = strval($_GET['wpcf-tax']); unset($custom_taxonomies[$custom_taxonomy]); update_option('wpcf-custom-taxonomies', $custom_taxonomies); wpcf_admin_deactivate_content('taxonomy', $custom_taxonomy); echo json_encode(array('output' => '', 'execute' => 'jQuery("#wpcf-list-activate-' . $_GET['wpcf-tax'] . '").parents("tr").css("background-color", "#FF0000").fadeOut();', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_radio_option': require_once WPCF_INC_ABSPATH . '/fields/radio.php'; $element = wpcf_fields_radio_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_radio_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'execute' => 'jQuery("#wpcf-form-groups-radio-ajax-response-' . urldecode($_GET['wpcf_ajax_update_add']) . '").append(\'' . trim(str_replace("\r\n", '', wpcf_form_simple($element_txt))) . '\');', 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'add_select_option': require_once WPCF_INC_ABSPATH . '/fields/select.php'; $element = wpcf_fields_select_get_option(urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element))); break; case 'add_checkboxes_option': require_once WPCF_INC_ABSPATH . '/fields/checkboxes.php'; $element = wpcf_fields_checkboxes_get_option(urldecode($_GET['parent_name'])); $id = array_shift($element); $element_txt = wpcf_fields_checkboxes_get_option_alt_text($id, urldecode($_GET['parent_name'])); echo json_encode(array('output' => wpcf_form_simple($element), 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute'))); break; case 'group_form_collapsed': require_once WPCF_INC_ABSPATH . '/fields-form.php'; $group_id = $_GET['group_id']; $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_fields_form_save_open_fieldset($action, $fieldset, $group_id); break; case 'form_fieldset_toggle': $action = $_GET['toggle']; $fieldset = $_GET['id']; wpcf_admin_form_fieldset_save_toggle($action, $fieldset); break; case 'group_update_post_types': require_once WPCF_INC_ABSPATH . '/fields.php'; $post_types = empty($_GET['wpcf']['group']['supports']) ? array() : $_GET['wpcf']['group']['supports']; if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_post_types($_GET['group_id'], $post_types); $output = array(); foreach ($post_types as $post_type) { $post_type = get_post_type_object($post_type); if (!empty($post_type->label)) { $output[] = $post_type->label; } } if (empty($post_types)) { $output[] = __('No post types associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No post types associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'group_update_taxonomies': require_once WPCF_INC_ABSPATH . '/fields.php'; $taxonomies_post = empty($_GET['wpcf']['group']['taxonomies']) ? array() : $_GET['wpcf']['group']['taxonomies']; $terms = array(); foreach ($taxonomies_post as $taxonomy) { foreach ($taxonomy as $tax => $term) { $terms[] = $term; } } if (!empty($_GET['group_id'])) { wpcf_admin_fields_save_group_terms($_GET['group_id'], $terms); $output = array(); foreach ($taxonomies_post as $taxonomy => $terms) { $taxonomy = get_taxonomy($taxonomy); if (!empty($taxonomy)) { $title = $taxonomy->label . ': '; foreach ($terms as $term_id) { $term = get_term($term_id, $taxonomy->name); $output[] = $title . $term->name; $title = ''; } } } if (empty($output)) { $output[] = __('No taxonomies associated', 'wpcf'); } $output = implode(', ', $output); } else { $output = __('No taxonomies associated', 'wpcf'); } echo json_encode(array('output' => $output)); break; case 'custom_fields_control_bulk': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/fields-control.php'; wpcf_admin_custom_fields_control_bulk_ajax(); break; case 'fields_delete': case 'delete_field': require_once WPCF_INC_ABSPATH . '/fields.php'; if (isset($_GET['field_id'])) { wpcf_admin_fields_delete_field($_GET['field_id']); } if (isset($_GET['field'])) { wpcf_admin_fields_delete_field($_GET['field']); } echo json_encode(array('output' => '')); break; case 'remove_from_history': require_once WPCF_INC_ABSPATH . '/fields.php'; $fields = wpcf_admin_fields_get_fields(); if (isset($_GET['field_id']) && isset($fields[$_GET['field_id']])) { $fields[$_GET['field_id']]['data']['removed_from_history'] = 1; wpcf_admin_fields_save_fields($fields, true); } echo json_encode(array('output' => '')); break; case 'add_condition': require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_ABSPATH . '/includes/conditional-display.php'; if (!empty($_GET['field']) || !empty($_GET['group'])) { $data = array(); if (isset($_GET['group'])) { $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter(array(), array(), null, true)); echo json_encode(array('output' => $output)); } else { $data['id'] = str_replace('_conditional_display', '', $_GET['field']); $output = wpcf_form_simple(wpcf_cd_admin_form_single_filter($data, array(), null, false)); if (!empty($data['id'])) { echo json_encode(array('output' => $output)); } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } } } else { echo json_encode(array('output' => __('Error occured', 'wpcf'))); } break; case 'pt_edit_fields': if (!empty($_GET['parent']) && !empty($_GET['child'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/post-relationship.php'; wpcf_pr_admin_edit_fields($_GET['parent'], $_GET['child']); } break; case 'toggle': $option = get_option('wpcf_toggle', array()); $hidden = isset($_GET['hidden']) ? (bool) $_GET['hidden'] : 1; $_GET['div'] = strval($_GET['div']); if (!$hidden) { unset($option[$_GET['div']]); } else { $option[$_GET['div']] = 1; } update_option('wpcf_toggle', $option); break; case 'cb_save_empty_migrate': $output = '<span style="color:red;">' . __('Wrong field specified', 'wpcf') . '</div>'; if (isset($_GET['field']) && isset($_GET['subaction'])) { require_once WPCF_INC_ABSPATH . '/fields.php'; $option = $_GET['meta_type'] == 'usermeta' ? 'wpcf-usermeta' : 'wpcf-fields'; $meta_type = $_GET['meta_type']; $field = wpcf_admin_fields_get_field($_GET['field'], false, false, false, $option); $_txt_updates = $meta_type == 'usermeta' ? __('%d users require update', 'wpcf') : __('%d posts require update', 'wpcf'); $_txt_no_updates = $meta_type == 'usermeta' ? __('No users require update', 'wpcf') : __('No posts require update', 'wpcf'); $_txt_updated = $meta_type == 'usermeta' ? __('Users updated', 'wpcf') : __('Posts updated', 'wpcf'); if (!empty($field)) { if ($_GET['subaction'] == 'save_check' || $_GET['subaction'] == 'do_not_save_check') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty_check($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty_check($field, $_GET['subaction']); } } if (!empty($posts)) { $output = '<div class="message updated"><p>' . sprintf($_txt_updates, count($posts)) . ' ' . '<a href="javascript:void(0);" class="button-primary" onclick="' . 'wpcfCbSaveEmptyMigrate(jQuery(this).parent().parent().parent(), \'' . $_GET['field'] . '\', ' . count($posts) . ', \'' . wp_create_nonce('cb_save_empty_migrate') . '\', \''; $output .= $_GET['subaction'] == 'save_check' ? 'save' : 'do_not_save'; $output .= '\', \'' . $meta_type . '\');' . '">' . __('Update') . '</a>' . '</p></div>'; } else { $output = '<div class="message updated"><p><em>' . $_txt_no_updates . '</em></p></div>'; } } else { if ($_GET['subaction'] == 'save' || $_GET['subaction'] == 'do_not_save') { if ($field['type'] == 'checkbox') { $posts = wpcf_admin_fields_checkbox_migrate_empty($field, $_GET['subaction']); } else { if ($field['type'] == 'checkboxes') { $posts = wpcf_admin_fields_checkboxes_migrate_empty($field, $_GET['subaction']); } } if (isset($posts['offset'])) { if (!isset($_GET['total'])) { $output = '<span style="color:red;">' . __('Error occured', 'wpcf') . '</div>'; } else { $output = '<script type="text/javascript">wpcfCbMigrateStep(' . intval($_GET['total']) . ',' . $posts['offset'] . ',' . '\'' . $_GET['field'] . '\',' . '\'' . wp_create_nonce('cb_save_empty_migrate') . '\', \'' . $meta_type . '\');</script>' . number_format($posts['offset']) . '/' . number_format(intval($_GET['total'])) . '<div class="wpcf-ajax-loading-small"></div>'; } } else { $output = '<div class="message updated"><p>' . $_txt_updated . '</p></div>'; } } } } } echo json_encode(array('output' => $output)); break; default: break; } die; }