Пример #1
0
/**
 * Adds 3rd party created types, taxonomies and fields
 */
function wpcf_admin_migration_form_submit()
{
    $cfui_types = get_option('cpt_custom_post_types', array());
    $cfui_taxonomies = get_option('cpt_custom_tax_types', array());
    $wpcf_types = get_option('wpcf-custom-types', array());
    $wpcf_taxonomies = get_option('wpcf-custom-taxonomies', array());
    $redirect_page = 'wpcf-ctt';
    if (!empty($_POST['cfui']['types'])) {
        $data = array();
        foreach ($_POST['cfui']['types'] as $key => $types_slug) {
            if (array_key_exists(sanitize_title($types_slug), $wpcf_types)) {
                continue;
            }
            foreach ($cfui_types as $cfui_type) {
                if (sanitize_title($cfui_type['name']) == $types_slug) {
                    $data[$types_slug] = wpcf_admin_migrate_get_cfui_type_data($cfui_type);
                    wpcf_admin_message_store(sprintf(__("Post Type %s added", 'wpcf'), '<em>' . $cfui_type['name'] . '</em>'));
                }
            }
        }
        $wpcf_types = array_merge($wpcf_types, $data);
        update_option('wpcf-custom-types', $wpcf_types);
    }
    if (!empty($_POST['cfui']['tax'])) {
        $data = array();
        foreach ($_POST['cfui']['tax'] as $key => $tax_slug) {
            if (array_key_exists(sanitize_title($tax_slug), $wpcf_taxonomies)) {
                continue;
            }
            foreach ($cfui_taxonomies as $cfui_tax) {
                if (sanitize_title($cfui_tax['name']) == $tax_slug) {
                    $data[$tax_slug] = wpcf_admin_migrate_get_cfui_tax_data($cfui_tax);
                    wpcf_admin_message_store(sprintf(__("Taxonomy %s added", 'wpcf'), '<em>' . $cfui_tax['name'] . '</em>'));
                }
            }
        }
        $wpcf_taxonomies = array_merge($wpcf_taxonomies, $data);
        update_option('wpcf-custom-taxonomies', $wpcf_taxonomies);
    }
    // ACF
    if (!empty($_POST['acf_posts']['migrate_groups'])) {
        foreach ($_POST['acf_posts']['migrate_groups'] as $acf_group_id) {
            if (empty($_POST['acf_posts'][$acf_group_id])) {
                continue;
            }
            global $wpdb;
            $group = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts} WHERE post_title = %s AND post_type='wp-types-group'", $_POST['acf_posts'][$acf_group_id]['post_title']));
            if (empty($group)) {
                $group = array();
                $group['name'] = $_POST['acf_posts'][$acf_group_id]['post_title'];
                $group['description'] = $_POST['acf_posts'][$acf_group_id]['post_content'];
                $group_id = wpcf_admin_fields_save_group($group);
                wpcf_admin_message_store(sprintf(__("Group %s added", 'wpcf'), '<em>' . $group['name'] . '</em>'));
            } else {
                $group_id = $group->ID;
                wpcf_admin_message_store(sprintf(__("Group %s updated", 'wpcf'), '<em>' . $group->post_title . '</em>'));
            }
            $fields_to_add = array();
            if ($group_id && !empty($_POST['acf_posts'][$acf_group_id]['fields'])) {
                foreach ($_POST['acf_posts'][$acf_group_id]['fields'] as $field_id => $field) {
                    if (!in_array($field_id, $_POST['acf_posts'][$acf_group_id]['migrate_fields'])) {
                        continue;
                    }
                    if (!wpcf_types_cf_under_control('check_exists', $field['slug']) || wpcf_types_cf_under_control('check_outsider', $field['slug'])) {
                        // save field
                        $field['controlled'] = 1;
                        $temp = wpcf_admin_fields_save_field($field);
                        $fields_to_add[] = $temp;
                        wpcf_admin_message_store(sprintf(__("Field %s added", 'wpcf'), '<em>' . $temp . '</em>'));
                    }
                }
                wpcf_admin_fields_save_group_fields($group_id, $fields_to_add, false);
            }
            wpcf_admin_fields_save_group_post_types($group_id, array());
            wpcf_admin_fields_save_group_terms($group_id, array());
        }
        $redirect_page = 'wpcf';
    }
    flush_rewrite_rules();
    // Deactivate plugins
    if (!empty($_POST['deactivate-cfui'])) {
        $active_plugins = get_option('active_plugins', array());
        foreach ($active_plugins as $key => $file) {
            if (strpos($file, 'custom-post-type-ui.php') !== false) {
                unset($active_plugins[$key]);
            }
        }
        update_option('active_plugins', array_values($active_plugins));
    }
    wp_redirect(admin_url('admin.php?page=' . $redirect_page));
    die;
}
Пример #2
0
/**
 * Edit fields form.
 *
 * @param type $parent
 * @param type $child
 */
function wpcf_pr_admin_edit_fields($parent, $child)
{
    $post_type_parent = get_post_type_object($parent);
    $post_type_child = get_post_type_object($child);
    if (empty($post_type_parent) || empty($post_type_child)) {
        die(__('Wrong post types', 'wpcf'));
    }
    $relationships = get_option('wpcf_post_relationship', array());
    if (!isset($relationships[$parent][$child])) {
        die(__('Relationship do not exist', 'wpcf'));
    }
    $data = $relationships[$parent][$child];
    wp_enqueue_script('jquery');
    wp_enqueue_style('types');
    wpcf_admin_ajax_head('Edit fields', 'wpcf');
    // Process submit
    if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'pt_edit_fields')) {
        $relationships[$parent][$child]['fields_setting'] = sanitize_text_field($_POST['fields_setting']);
        // @todo Maybe sanitization here
        $relationships[$parent][$child]['fields'] = isset($_POST['fields']) ? $_POST['fields'] : array();
        update_option('wpcf_post_relationship', $relationships);
        ?>
        <script type="text/javascript">
            window.parent.jQuery('#TB_closeWindowButton').trigger('click');
            window.parent.location.reload();
        </script>
        <?php 
        die;
    }
    $groups = wpcf_admin_get_groups_by_post_type($child);
    $options_cf = array();
    $repetitive_warning = false;
    $repetitive_warning_markup = array();
    $repetitive_warning_txt = __('Repeating fields should not be used in child posts. Types will update all field values.', 'wpcf');
    foreach ($groups as $group) {
        $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
        foreach ($fields as $key => $cf) {
            $__key = wpcf_types_cf_under_control('check_outsider', $key) ? $key : WPCF_META_PREFIX . $key;
            $options_cf[$__key] = array();
            $options_cf[$__key]['#title'] = $cf['name'];
            $options_cf[$__key]['#name'] = 'fields[' . $__key . ']';
            $options_cf[$__key]['#default_value'] = isset($data['fields'][$__key]) ? 1 : 0;
            // Repetitive warning
            if (wpcf_admin_is_repetitive($cf)) {
                if (!$repetitive_warning) {
                    $repetitive_warning_markup = array('#type' => 'markup', '#markup' => '<div class="message error" style="display:none;" id="wpcf-repetitive-warning"><p>' . $repetitive_warning_txt . '</p></div>');
                }
                $repetitive_warning = true;
                $options_cf[$__key]['#after'] = !isset($data['fields'][$__key]) ? '<div class="message error" style="display:none;"><p>' : '<div class="message error"><p>';
                $options_cf[$__key]['#after'] .= $repetitive_warning_txt;
                $options_cf[$__key]['#after'] .= '</p></div>';
                $options_cf[$__key]['#attributes'] = array('onclick' => 'jQuery(this).parent().find(\'.message\').toggle();', 'disabled' => 'disabled');
            }
        }
    }
    $form = array();
    $form['repetitive_warning_markup'] = $repetitive_warning_markup;
    $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('Do not show management options for this post type', 'wpcf') => 'only_list', __('All fields, including the standard post fields', 'wpcf') => 'all_cf_standard', __('Specific fields', 'wpcf') => 'specific'), '#default_value' => empty($data['fields_setting']) ? 'all_cf' : $data['fields_setting']);
    /**
     * check default, to avoid missing configuration
     */
    if (!in_array($form['select']['#default_value'], $form['select']['#options'])) {
        $form['select']['#default_value'] = 'all_cf';
    }
    /**
     * build options for "Specific fields"
     */
    $options = array();
    /**
     * check and add build-in properites
     */
    $check_support = array('title' => array('name' => '_wp_title', 'title' => __('Post title', 'wpcf')), 'editor' => array('name' => '_wp_body', 'title' => __('Post body', 'wpcf')), 'excerpt' => array('name' => '_wp_excerpt', 'title' => __('Post excerpt', 'wpcf')), 'thumbnail' => array('name' => '_wp_featured_image', 'title' => __('Post featured image', 'wpcf')));
    foreach ($check_support as $child_field_key => $child_field_data) {
        if (!post_type_supports($child, $child_field_key)) {
            continue;
        }
        $options[$child_field_data['name']] = array('#title' => $child_field_data['title'], '#name' => sprintf('fields[%s]', $child_field_data['name']), '#default_value' => isset($data['fields'][$child_field_data['name']]) ? 1 : 0);
    }
    /**
     * add custom fields
     */
    $options = $options + $options_cf;
    $temp_belongs = wpcf_pr_admin_get_belongs($child);
    foreach ($temp_belongs as $temp_parent => $temp_data) {
        if ($temp_parent == $parent) {
            continue;
        }
        $temp_parent_type = get_post_type_object($temp_parent);
        $options[$temp_parent] = array();
        $options[$temp_parent]['#title'] = $temp_parent_type->label;
        $options[$temp_parent]['#name'] = 'fields[_wpcf_pr_parents][' . $temp_parent . ']';
        $options[$temp_parent]['#default_value'] = isset($data['fields']['_wpcf_pr_parents'][$temp_parent]) ? 1 : 0;
    }
    /**
     * remove "Specific fields" if there is no fields
     */
    if (empty($options)) {
        unset($form['select']['#options'][__('Specific fields', 'wpcf')]);
        if ('specific' == $form['select']['#default_value']) {
            $form['select']['#default_value'] = 'all_cf';
        }
    }
    // Taxonomies
    $taxonomies = get_object_taxonomies($post_type_child->name, 'objects');
    if (!empty($taxonomies)) {
        foreach ($taxonomies as $tax_id => $taxonomy) {
            $options[$tax_id] = array();
            $options[$tax_id]['#title'] = sprintf(__('Taxonomy - %s', 'wpcf'), $taxonomy->label);
            $options[$tax_id]['#name'] = 'fields[_wpcf_pr_taxonomies][' . $tax_id . ']';
            $options[$tax_id]['#default_value'] = isset($data['fields']['_wpcf_pr_taxonomies'][$tax_id]) ? 1 : 0;
        }
    }
    $form['specific'] = array('#type' => 'checkboxes', '#name' => 'fields', '#options' => $options, '#default_value' => isset($data['fields']), '#before' => '<div id="wpcf-specific" style="display:none;margin:10px 0 0 20px;">', '#after' => '</div>');
    $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
    echo '<form method="post" action="" class="types-select-child-fields">';
    echo wpcf_form_simple($form);
    echo wp_nonce_field('pt_edit_fields');
    echo '</form>';
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function(){
            if (jQuery('input[name="fields_setting"]:checked').val() == 'specific') {
                jQuery('#wpcf-specific').show();
            } else {
    <?php 
    if ($repetitive_warning && 'only_list' != $form['select']['#default_value']) {
        ?>
                    jQuery('#wpcf-repetitive-warning').show();
        <?php 
    }
    ?>
            }
            jQuery('input[name="fields_setting"]').change(function(){
                if (jQuery(this).val() == 'specific') {
                    jQuery('#wpcf-specific').slideDown();
                } else {
                    jQuery('#wpcf-specific').slideUp();
    <?php 
    if ($repetitive_warning) {
        ?>
                    if ( 'only_list' != jQuery('input[name="fields_setting"]:checked').val()) {
                        jQuery('#wpcf-repetitive-warning').show();
                    }
        <?php 
    }
    ?>
                }
            });
        });
    </script>
    <?php 
    wpcf_admin_ajax_footer();
}
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 public function prepare_field_select_screen()
 {
     /**
      * check nonce
      */
     if (0 || !isset($_REQUEST['_wpnonce']) || !isset($_REQUEST['parent']) || !isset($_REQUEST['child']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $this->get_nonce('child-post-fields', $_REQUEST['parent'], $_REQUEST['child']))) {
         $this->verification_failed_and_die();
     }
     $parent = $_REQUEST['parent'];
     $child = $_REQUEST['child'];
     $post_type_parent = get_post_type_object($parent);
     $post_type_child = get_post_type_object($child);
     if (empty($post_type_parent) || empty($post_type_child)) {
         die(__('Wrong post types', 'wpcf'));
     }
     $relationships = get_option('wpcf_post_relationship', array());
     if (!isset($relationships[$parent][$child])) {
         $this->print_notice_and_die(__('Please save Post Type first to edit these fields.', 'wpcf'));
     }
     $repetitive_warning_markup = array();
     $data = $relationships[$parent][$child];
     $form = array();
     $form['repetitive_warning_markup'] = $repetitive_warning_markup;
     $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('Do not show management options for this post type', 'wpcf') => 'only_list', __('All fields, including the standard post fields', 'wpcf') => 'all_cf_standard', __('Specific fields', 'wpcf') => 'specific'), '#attributes' => array('display' => 'ul'), '#default_value' => empty($data['fields_setting']) ? 'all_cf' : $data['fields_setting']);
     /**
      * check default, to avoid missing configuration
      */
     if (!in_array($form['select']['#default_value'], $form['select']['#options'])) {
         $form['select']['#default_value'] = 'all_cf';
     }
     /**
      * Specific options
      */
     $groups = wpcf_admin_get_groups_by_post_type($child);
     $options_cf = array();
     $repetitive_warning = false;
     $repetitive_warning_txt = __('Repeating fields should not be used in child posts. Types will update all field values.', 'wpcf');
     foreach ($groups as $group) {
         $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
         foreach ($fields as $key => $cf) {
             $__key = wpcf_types_cf_under_control('check_outsider', $key) ? $key : WPCF_META_PREFIX . $key;
             $options_cf[$__key] = array('#title' => $cf['name'], '#name' => 'fields[' . $__key . ']', '#default_value' => isset($data['fields'][$__key]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
             // Repetitive warning
             if (wpcf_admin_is_repetitive($cf)) {
                 if (!$repetitive_warning) {
                     $repetitive_warning_markup = array('#type' => 'markup', '#markup' => '<div class="message error" style="display:none;" id="wpcf-repetitive-warning"><p>' . $repetitive_warning_txt . '</p></div>');
                 }
                 $repetitive_warning = true;
                 $options_cf[$__key]['#after'] = !isset($data['fields'][$__key]) ? '<div class="message error" style="display:none;"><p>' : '<div class="message error"><p>';
                 $options_cf[$__key]['#after'] .= $repetitive_warning_txt;
                 $options_cf[$__key]['#after'] .= '</p></div></li>';
                 $options_cf[$__key]['#attributes'] = array('onclick' => 'jQuery(this).parent().find(\'.message\').toggle();', 'disabled' => 'disabled');
             }
         }
     }
     /**
      * build options for "Specific fields"
      */
     $options = array();
     /**
      * check and add built-in properites
      */
     require_once WPCF_INC_ABSPATH . '/post-relationship.php';
     $supports = wpcf_post_relationship_get_supported_fields_by_post_type($child);
     foreach ($supports as $child_field_key => $child_field_data) {
         $options[$child_field_data['name']] = array('#title' => $child_field_data['title'], '#name' => sprintf('fields[%s]', $child_field_data['name']), '#default_value' => isset($data['fields'][$child_field_data['name']]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
     }
     /**
      * add custom fields
      */
     $options = $options + $options_cf;
     $temp_belongs = wpcf_pr_admin_get_belongs($child);
     foreach ($temp_belongs as $temp_parent => $temp_data) {
         if ($temp_parent == $parent) {
             continue;
         }
         $temp_parent_type = get_post_type_object($temp_parent);
         $options[$temp_parent] = array('#title' => $temp_parent_type->label, '#name' => 'fields[_wpcf_pr_parents][' . $temp_parent . ']', '#default_value' => isset($data['fields']['_wpcf_pr_parents'][$temp_parent]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
     }
     /**
      * remove "Specific fields" if there is no fields
      */
     if (empty($options)) {
         unset($form['select']['#options'][__('Specific fields', 'wpcf')]);
         if ('specific' == $form['select']['#default_value']) {
             $form['select']['#default_value'] = 'all_cf';
         }
     }
     // Taxonomies
     $taxonomies = get_object_taxonomies($post_type_child->name, 'objects');
     if (!empty($taxonomies)) {
         foreach ($taxonomies as $tax_id => $taxonomy) {
             $options[$tax_id] = array('#title' => sprintf(__('Taxonomy - %s', 'wpcf'), $taxonomy->label), '#name' => 'fields[_wpcf_pr_taxonomies][' . $tax_id . ']', '#default_value' => isset($data['fields']['_wpcf_pr_taxonomies'][$tax_id]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
         }
     }
     $form['specific'] = array('#type' => 'checkboxes', '#name' => 'fields', '#options' => $options, '#default_value' => isset($data['fields']), '#before' => sprintf('<ul id="wpcf-specific" class="%s">', 'specific' == $form['select']['#default_value'] ? '' : 'hidden'), '#after' => '</ul>');
     $form['nonce'] = array('#type' => 'hidden', '#value' => wp_create_nonce($this->get_nonce('child-post-fields-save', $parent, $child)), '#name' => 'wpcf-fields-save-nonce', '#id' => 'wpcf-fields-save-nonce');
     $form['parent'] = array('#type' => 'hidden', '#value' => esc_attr($parent), '#name' => 'wpcf-parent', '#id' => 'wpcf-parent');
     $form['child'] = array('#type' => 'hidden', '#value' => esc_attr($child), '#name' => 'wpcf-child', '#id' => 'wpcf-child');
     echo wpcf_form_simple($form);
     die;
 }
Пример #4
0
/**
 * Saves user fields and groups.
 * 
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 * 
 * @return type 
 */
function wpcf_admin_save_usermeta_groups_submit($form)
{
    if (!isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    $_POST['wpcf']['group']['name'] = trim($_POST['wpcf']['group']['name']);
    $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
    global $wpdb;
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post($_REQUEST['group_id']);
        // Name changed
        if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) {
            // Check if already exists
            $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group');
            if (!empty($exists)) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error');
                return $form;
            }
        }
        if (empty($post) || $post->post_type != 'wp-types-user-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return $form;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
        // Check if already exists
        $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-user-group');
        if (!empty($exists)) {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), $_POST['wpcf']['group']['name']), 'error');
            return $form;
        }
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
        }
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            $field = apply_filters('wpcf_field_pre_save', $field);
            if (!empty($field['is_new'])) {
                // Check name and slug
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), 'wp-types-user-group', 'wpcf-usermeta')) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return $form;
                }
                if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), 'wp-types-user-group', 'wpcf-usermeta')) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                    return $form;
                }
            }
            // Field ID and slug are same thing
            $field_id = wpcf_admin_fields_save_field($field, 'wp-types-user-group', 'wpcf-usermeta');
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
        }
    }
    // Save group
    $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $admin_style = $_POST['wpcf']['group']['admin_styles'];
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = $_REQUEST['group_id'];
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], 'wp-types-user-group');
    $_POST['wpcf']['group']['id'] = $group_id;
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields, false, 'wp-types-user-group');
        wpcf_admin_fields_save_group_showfor($group_id, $roles);
        wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom');
        wp_redirect(admin_url('admin.php?page=wpcf-edit-usermeta&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
Пример #5
0
 function column_cb($item)
 {
     if (!wpcf_types_cf_under_control('check_exists', $item['id'], TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
         $item['id'] = $item['id'] . '_' . md5('wpcf_not_controlled');
     }
     return '<input type="checkbox" name="fields[]" value="' . $item['id'] . '" />';
 }
Пример #6
0
/**
 * Saves fields and groups.
 * 
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 * 
 * @return type 
 */
function wpcf_admin_save_fields_groups_submit($form)
{
    if (!isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    global $wpdb;
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post($_REQUEST['group_id']);
        if (empty($post) || $post->post_type != 'wp-types-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return false;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return false;
            }
        }
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (!empty($field['is_new'])) {
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return false;
                }
            }
            // Field ID and slug are same thing
            //            $slug = $_POST['wpcf']['fields'][$key]['slug'] = sanitize_title($field['name']);
            $field_id = wpcf_admin_fields_save_field($field);
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
            // WPML
            if (function_exists('wpml_cf_translation_preferences_store')) {
                $wpml_save_cf = wpml_cf_translation_preferences_store($key, wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id);
            }
        }
    }
    // Save group
    $post_types = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $taxonomies_post = isset($_POST['wpcf']['group']['taxonomies']) ? $_POST['wpcf']['group']['taxonomies'] : array();
    $terms = array();
    foreach ($taxonomies_post as $taxonomy) {
        foreach ($taxonomy as $tax => $term) {
            $terms[] = $term;
        }
    }
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = $_REQUEST['group_id'];
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group']);
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields);
        wpcf_admin_fields_save_group_post_types($group_id, $post_types);
        wpcf_admin_fields_save_group_terms($group_id, $terms);
        if (!empty($_POST['wpcf']['group']['templates'])) {
            wpcf_admin_fields_save_group_templates($group_id, $_POST['wpcf']['group']['templates']);
        }
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(__('Group saved', 'wpcf'));
        wp_redirect(admin_url('admin.php?page=wpcf-edit&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
Пример #7
0
 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();
 }
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  *
  * @return type Description.
  */
 private function save_group_fields($group_id)
 {
     if (empty($_POST['wpcf']['fields'])) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all fields
     foreach ($_POST['wpcf']['fields'] as $key => $field) {
         $field = wpcf_sanitize_field($field);
         $field = apply_filters('wpcf_field_pre_save', $field);
         if (!empty($field['is_new'])) {
             // Check name and slug
             if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                 return $form;
             }
             if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']))) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return $form;
             }
         }
         $field['submit-key'] = $key;
         // Field ID and slug are same thing
         $field_id = wpcf_admin_fields_save_field($field);
         if (is_wp_error($field_id)) {
             $this->triggerError();
             wpcf_admin_message($field_id->get_error_message(), 'error');
             return;
         }
         if (!empty($field_id)) {
             $fields[] = $field_id;
         }
         // WPML
         /** @var string $field_id */
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id;
                 wpml_cf_translation_preferences_store($key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields);
 }
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 public function save()
 {
     if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) {
         return false;
     }
     $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
     $group_name = wp_kses_post($_POST['wpcf']['group']['name']);
     require_once WPCF_EMBEDDED_ABSPATH . '/classes/forms.php';
     $form = new Enlimbo_Forms_Wpcf();
     if (empty($group_name)) {
         $form->triggerError();
         wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error');
         return $form;
     }
     $new_group = false;
     $group_slug = sanitize_title($group_name);
     // Basic check
     if (isset($_REQUEST[$this->get_id])) {
         // Check if group exists
         $post = get_post(intval($_REQUEST[$this->get_id]));
         // Name changed
         if (strtolower($group_name) != strtolower($post->post_title)) {
             // Check if already exists
             $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
             if (!empty($exists)) {
                 $form->triggerError();
                 wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
                 return $form;
             }
         }
         if (empty($post) || $post->post_type != TYPES_USER_META_FIELD_GROUP_CPT_NAME) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST[$this->get_id])), 'error');
             return $form;
         }
         $group_id = $post->ID;
     } else {
         $new_group = true;
         // Check if already exists
         $exists = get_page_by_title($group_name, 'OBJECT', TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         if (!empty($exists)) {
             $form->triggerError();
             wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), apply_filters('the_title', $exists->post_title)), 'error');
             return $form;
         }
     }
     // Save fields for future use
     $fields = array();
     if (!empty($_POST['wpcf']['fields'])) {
         foreach ($_POST['wpcf']['fields'] as $key => $field) {
             $field = wpcf_sanitize_field($field);
             $field = apply_filters('wpcf_field_pre_save', $field);
             if (!empty($field['is_new'])) {
                 // Check name and slug
                 if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                     return $form;
                 }
                 if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta')) {
                     $form->triggerError();
                     wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                     return $form;
                 }
             }
             // Field ID and slug are same thing
             $field_id = wpcf_admin_fields_save_field($field, TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'wpcf-usermeta');
             if (!empty($field_id)) {
                 $fields[] = $field_id;
             }
         }
     }
     // Save group
     $roles = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
     /**
      * Admin styles
      */
     if (isset($_POST['wpcf']['group']['admin_styles'])) {
         $admin_style = esc_html($_POST['wpcf']['group']['admin_styles']);
     }
     // Rename if needed
     if (isset($_REQUEST[$this->get_id])) {
         $_POST['wpcf']['group']['id'] = intval($_REQUEST[$this->get_id]);
     }
     $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group'], TYPES_USER_META_FIELD_GROUP_CPT_NAME, 'user');
     // Set open fieldsets
     if ($new_group && !empty($group_id)) {
         $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
         if (isset($open_fieldsets[-1])) {
             $open_fieldsets[$group_id] = $open_fieldsets[-1];
             unset($open_fieldsets[-1]);
             update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
         }
     }
     // Rest of processes
     if (!empty($group_id)) {
         wpcf_admin_fields_save_group_fields($group_id, $fields, false, TYPES_USER_META_FIELD_GROUP_CPT_NAME);
         $this->save_group_showfor($group_id, $roles);
         /**
          * Admin styles
          */
         if (defined('TYPES_USE_STYLING_EDITOR') && TYPES_USE_STYLING_EDITOR && isset($admin_style)) {
             wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
         }
         $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
         do_action('wpcf_save_group', $_POST['wpcf']['group']);
         // admin message
         if (!wpcf_is_client()) {
             wpcf_admin_message_store(apply_filters('types_message_usermeta_saved', __('Group saved', 'wpcf'), $group_name, $new_group ? false : true), 'custom');
         }
         wp_safe_redirect(admin_url(sprintf('admin.php?page=wpcf-edit-usermeta&group_id=%d', $group_id)));
         exit;
     } else {
         wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
     }
 }
Пример #10
0
/**
 * Saves group's fields.
 * 
 * @global type $wpdb
 * @param type $group_id
 * @param type $fields 
 */
function wpcf_admin_fields_save_group_fields($group_id, $fields, $add = false)
{
    $fields = wpcf_types_cf_under_control('add', array('fields' => $fields));
    if ($add) {
        $existing_fields = wpcf_admin_fields_get_fields_by_group($group_id);
        $order = array();
        if (!empty($existing_fields)) {
            foreach ($existing_fields as $field_id => $field) {
                if (in_array($field['id'], $fields)) {
                    continue;
                }
                $order[] = $field['id'];
            }
            foreach ($fields as $field) {
                $order[] = $field;
            }
            $fields = $order;
        }
    }
    if (empty($fields)) {
        delete_post_meta($group_id, '_wp_types_group_fields');
        return false;
    }
    $fields = ',' . implode(',', (array) $fields) . ',';
    update_post_meta($group_id, '_wp_types_group_fields', $fields);
}
Пример #11
0
/**
 * Saves fields and groups.
 *
 * If field name is changed in specific group - new one will be created,
 * otherwise old one will be updated and will appear in that way in other grups.
 *
 * @return type
 */
function wpcf_admin_save_fields_groups_submit($form)
{
    if (!isset($_POST['wpcf']) || !isset($_POST['wpcf']['group']) || !isset($_POST['wpcf']['group']['name'])) {
        return false;
    }
    // @todo maybe sanitize_text_field this too
    $_POST['wpcf']['group']['name'] = trim(strip_tags($_POST['wpcf']['group']['name']));
    $_POST['wpcf']['group'] = apply_filters('wpcf_group_pre_save', $_POST['wpcf']['group']);
    if (empty($_POST['wpcf']['group']['name'])) {
        $form->triggerError();
        wpcf_admin_message(__('Group name can not be empty.', 'wpcf'), 'error');
        return $form;
    }
    $new_group = false;
    $group_slug = $_POST['wpcf']['group']['slug'] = sanitize_title($_POST['wpcf']['group']['name']);
    // Basic check
    if (isset($_REQUEST['group_id'])) {
        // Check if group exists
        $post = get_post(intval($_REQUEST['group_id']));
        // Name changed
        if (strtolower($_POST['wpcf']['group']['name']) != strtolower($post->post_title)) {
            // Check if already exists
            $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group');
            if (!empty($exists)) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error');
                return $form;
            }
        }
        if (empty($post) || $post->post_type != 'wp-types-group') {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("Wrong group ID %d", 'wpcf'), intval($_REQUEST['group_id'])), 'error');
            return $form;
        }
        $group_id = $post->ID;
    } else {
        $new_group = true;
        // Check if already exists
        $exists = get_page_by_title($_POST['wpcf']['group']['name'], 'OBJECT', 'wp-types-group');
        if (!empty($exists)) {
            $form->triggerError();
            wpcf_admin_message(sprintf(__("A group by name <em>%s</em> already exists. Please use a different name and save again.", 'wpcf'), htmlspecialchars($_POST['wpcf']['group']['name'])), 'error');
            return $form;
        }
    }
    // Save fields for future use
    $fields = array();
    if (!empty($_POST['wpcf']['fields'])) {
        // Before anything - search unallowed characters
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            if (empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['name']) || !empty($field['slug']) && preg_match('#[^a-zA-Z0-9\\s\\_\\-]#', $field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field slugs cannot contain non-English characters. Please edit this field name %s and save again.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
            if (!empty($field['name']) && is_numeric($field['name']) || !empty($field['slug']) && is_numeric($field['slug'])) {
                $form->triggerError();
                wpcf_admin_message(sprintf(__('Field names or slugs cannot contain only numbers.', 'wpcf'), $field['name']), 'error');
                return $form;
            }
        }
        // First check all fields
        foreach ($_POST['wpcf']['fields'] as $key => $field) {
            $field = apply_filters('wpcf_field_pre_save', $field);
            if (!empty($field['is_new'])) {
                // Check name and slug
                if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                    return $form;
                }
                if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']))) {
                    $form->triggerError();
                    wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                    return $form;
                }
            }
            // Field ID and slug are same thing
            $field_id = wpcf_admin_fields_save_field($field);
            if (is_wp_error($field_id)) {
                $form->triggerError();
                wpcf_admin_message($field_id->get_error_message(), 'error');
                return $form;
            }
            if (!empty($field_id)) {
                $fields[] = $field_id;
            }
            // WPML
            if (function_exists('wpml_cf_translation_preferences_store')) {
                $wpml_save_cf = wpml_cf_translation_preferences_store($key, wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_id)) . $field_id);
            }
        }
    }
    // Save group
    $post_types = isset($_POST['wpcf']['group']['supports']) ? $_POST['wpcf']['group']['supports'] : array();
    $taxonomies_post = isset($_POST['wpcf']['group']['taxonomies']) ? $_POST['wpcf']['group']['taxonomies'] : array();
    $admin_style = $_POST['wpcf']['group']['admin_styles'];
    $terms = array();
    foreach ($taxonomies_post as $taxonomy) {
        foreach ($taxonomy as $tax => $term) {
            $terms[] = $term;
        }
    }
    // Rename if needed
    if (isset($_REQUEST['group_id'])) {
        $_POST['wpcf']['group']['id'] = intval($_REQUEST['group_id']);
    }
    $group_id = wpcf_admin_fields_save_group($_POST['wpcf']['group']);
    $_POST['wpcf']['group']['id'] = $group_id;
    // Set open fieldsets
    if ($new_group && !empty($group_id)) {
        $open_fieldsets = get_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', true);
        if (isset($open_fieldsets[-1])) {
            $open_fieldsets[$group_id] = $open_fieldsets[-1];
            unset($open_fieldsets[-1]);
            update_user_meta(get_current_user_id(), 'wpcf-group-form-toggle', $open_fieldsets);
        }
    }
    // Rest of processes
    if (!empty($group_id)) {
        wpcf_admin_fields_save_group_fields($group_id, $fields);
        wpcf_admin_fields_save_group_post_types($group_id, $post_types);
        wpcf_admin_fields_save_group_terms($group_id, $terms);
        wpcf_admin_fields_save_group_admin_styles($group_id, $admin_style);
        if (empty($_POST['wpcf']['group']['templates'])) {
            $_POST['wpcf']['group']['templates'] = array();
        }
        wpcf_admin_fields_save_group_templates($group_id, $_POST['wpcf']['group']['templates']);
        $_POST['wpcf']['group']['fields'] = isset($_POST['wpcf']['fields']) ? $_POST['wpcf']['fields'] : array();
        do_action('wpcf_save_group', $_POST['wpcf']['group']);
        wpcf_admin_message_store(apply_filters('types_message_custom_fields_saved', __('Group saved', 'wpcf'), $_POST['wpcf']['group']['name'], $new_group ? false : true), 'custom');
        wp_redirect(admin_url('admin.php?page=wpcf-edit&group_id=' . $group_id));
        die;
    } else {
        wpcf_admin_message_store(__('Error saving group', 'wpcf'), 'error');
    }
}
/**
 * Get allowed keys
 *
 * Function colect proper keys of all fields - built-in and CF's
 *
 * @since 1.9.0
 *
 * @param string $child post type slug
 * @return array array of allowed slugs
 */
function wpcf_post_relationship_get_specific_fields_keys($child)
{
    $options = array();
    $groups = wpcf_admin_get_groups_by_post_type($child);
    foreach ($groups as $group) {
        $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
        foreach ($fields as $key => $cf) {
            $__key = wpcf_types_cf_under_control('check_outsider', $key) ? $key : WPCF_META_PREFIX . $key;
            $options[$__key] = 1;
        }
    }
    $supports = wpcf_post_relationship_get_supported_fields_by_post_type($child);
    foreach ($supports as $child_field_key => $child_field_data) {
        $options[$child_field_data['name']] = 1;
    }
    return $options;
}
Пример #13
0
/**
 * Saves group's fields.
 * Modified by Gen, 13.02.2013
 *
 * @param type $group_id
 * @param type $fields
 */
function wpcf_admin_fields_save_group_fields($group_id, $fields, $add = false, $post_type = TYPES_CUSTOM_FIELD_GROUP_CPT_NAME)
{
    $meta_name = $post_type == TYPES_CUSTOM_FIELD_GROUP_CPT_NAME ? 'wpcf-fields' : 'wpcf-usermeta';
    $fields = wpcf_types_cf_under_control('add', array('fields' => $fields), $post_type, $meta_name);
    if ($add) {
        $existing_fields = wpcf_admin_fields_get_fields_by_group($group_id, 'slug', false, true, false, $post_type, $meta_name);
        $order = array();
        if (!empty($existing_fields)) {
            foreach ($existing_fields as $field_id => $field) {
                if (in_array($field['id'], $fields)) {
                    continue;
                }
                $order[] = $field['id'];
            }
            foreach ($fields as $field) {
                $order[] = sanitize_text_field($field);
            }
            $fields = $order;
        }
    }
    if (empty($fields)) {
        delete_post_meta($group_id, '_wp_types_group_fields');
        return false;
    }
    $fields = ',' . implode(',', (array) $fields) . ',';
    update_post_meta($group_id, '_wp_types_group_fields', $fields);
}
Пример #14
0
/**
 * 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;
}
 /**
  * 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);
         }
     }
 }
Пример #16
0
 function column_cb($item)
 {
     if (!wpcf_types_cf_under_control('check_exists', $item['id'])) {
         $item['id'] = $item['id'] . '_' . md5('wpcf_not_controlled');
     }
     return '<input type="checkbox" name="fields[]" value="' . $item['id'] . '" />';
 }
Пример #17
0
 private function save_filter_fields($group_id, $fields_data)
 {
     if (empty($fields_data)) {
         delete_post_meta($group_id, '_wp_types_group_fields');
         return;
     }
     $fields = array();
     // First check all fields
     foreach ($fields_data as $field_key => $field) {
         $field = wpcf_sanitize_field($field);
         $field = apply_filters('wpcf_field_pre_save', $field);
         if (!empty($field['is_new'])) {
             // Check name and slug
             if (wpcf_types_cf_under_control('check_exists', sanitize_title($field['name']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with name "%s" already exists', 'wpcf'), $field['name']), 'error');
                 return;
             }
             if (isset($field['slug']) && wpcf_types_cf_under_control('check_exists', sanitize_title($field['slug']), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) {
                 $this->triggerError();
                 wpcf_admin_message(sprintf(__('Field with slug "%s" already exists', 'wpcf'), $field['slug']), 'error');
                 return;
             }
         }
         $field['submit-key'] = $field_key;
         // Field ID and slug are same thing
         $field_slug = wpcf_admin_fields_save_field($field, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
         if (is_wp_error($field_slug)) {
             $this->triggerError();
             wpcf_admin_message($field_slug->get_error_message(), 'error');
             return;
         }
         if (!empty($field_slug)) {
             $fields[] = $field_slug;
         }
         // WPML
         if (defined('ICL_SITEPRESS_VERSION') && version_compare(ICL_SITEPRESS_VERSION, '3.2', '<')) {
             if (function_exists('wpml_cf_translation_preferences_store')) {
                 $real_custom_field_name = wpcf_types_get_meta_prefix(wpcf_admin_fields_get_field($field_slug, false, false, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION)) . $field_slug;
                 wpml_cf_translation_preferences_store($field_key, $real_custom_field_name);
             }
         }
     }
     wpcf_admin_fields_save_group_fields($group_id, $fields, false, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION);
 }