Ejemplo n.º 1
0
/**
 * Editor callback form.
 */
function wpcf_fields_checkboxes_editor_callback()
{
    $form = array();
    $form['#form']['callback'] = 'wpcf_fields_checkboxes_editor_submit';
    $form['display'] = array('#type' => 'radios', '#default_value' => 'display_all', '#name' => 'display', '#options' => array('display_from_db' => array('#title' => __('Display the value of this field from the database', 'wpcf'), '#name' => 'display', '#value' => 'db', '#inline' => true, '#after' => '<br />'), 'display_all' => array('#title' => __('Display all values with separator', 'wpcf'), '#name' => 'display', '#value' => 'display_all', '#inline' => true, '#after' => '&nbsp;' . wpcf_form_simple(array('separator' => array('#type' => 'textfield', '#name' => 'separator', '#value' => ', ', '#inline' => true))) . '<br />'), 'display_values' => array('#title' => __('Show one of these two values:', 'wpcf'), '#name' => 'display', '#value' => 'value')), '#inline' => true);
    if (isset($_GET['field_id'])) {
        $field = wpcf_admin_fields_get_field($_GET['field_id']);
        if (!empty($field['data']['options'])) {
            foreach ($field['data']['options'] as $option_key => $option) {
                $form[$option_key . '-markup'] = array('#type' => 'markup', '#markup' => '<h3>' . $option['title'] . '</h3>');
                $form[$option_key . '-display-value-1'] = array('#type' => 'textfield', '#title' => '<td style="text-align:right;">' . __('Not selected:', 'wpcf') . '</td><td>', '#name' => 'options[' . $option_key . '][display_value_not_selected]', '#value' => $option['display_value_not_selected'], '#inline' => true, '#before' => '<table><tr>', '#after' => '</td></tr>');
                $form[$option_key . '-display-value-2'] = array('#type' => 'textfield', '#title' => '<td style="text-align:right;">' . __('Selected:', 'wpcf') . '</td><td>', '#name' => 'options[' . $option_key . '][display_value_selected]', '#value' => $option['display_value_selected'], '#after' => '</tr></table>');
            }
        }
    }
    $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Save Changes'), '#attributes' => array('class' => 'button-primary'));
    $f = wpcf_form('wpcf-form', $form);
    wpcf_admin_ajax_head('Insert checkbox', 'wpcf');
    echo '<form method="post" action="">';
    echo $f->renderForm();
    echo '</form>';
    wpcf_admin_ajax_footer();
}
Ejemplo n.º 2
0
/**
 * All AJAX calls go here.
 *
 * @todo auth
 */
function wpcf_ajax_embedded()
{
    if (isset($_REQUEST['_typesnonce'])) {
        if (!wp_verify_nonce($_REQUEST['_typesnonce'], '_typesnonce')) {
            die('Verification failed');
        }
    } else {
        if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) {
            die('Verification failed');
        }
    }
    global $wpcf;
    switch ($_REQUEST['wpcf_action']) {
        case 'insert_skype_button':
            if (!current_user_can('edit_posts')) {
                die('Authentication failed');
            }
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php';
            wpcf_fields_skype_meta_box_ajax();
            break;
        case 'editor_callback':
            if (!current_user_can('edit_posts')) {
                die('Authentication failed');
            }
            // Determine Field type and context
            $views_usermeta = false;
            $field_id = sanitize_text_field($_GET['field_id']);
            // todo this could be written in like four lines
            if (isset($_GET['field_type']) && $_GET['field_type'] == 'usermeta') {
                // Group filter
                wp_enqueue_script('suggest');
                $field = types_get_field($field_id, 'usermeta');
                $meta_type = 'usermeta';
            } elseif (isset($_GET['field_type']) && $_GET['field_type'] == 'views-usermeta') {
                $field = types_get_field($field_id, 'usermeta');
                $meta_type = 'usermeta';
                $views_usermeta = true;
            } else {
                $field = types_get_field($field_id);
                $meta_type = 'postmeta';
            }
            $parent_post_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : null;
            $shortcode = isset($_GET['shortcode']) ? urldecode($_GET['shortcode']) : null;
            $callback = isset($_GET['callback']) ? sanitize_text_field($_GET['callback']) : false;
            if (!empty($field)) {
                // Editor
                WPCF_Loader::loadClass('editor');
                $editor = new WPCF_Editor();
                $editor->frame($field, $meta_type, $parent_post_id, $shortcode, $callback, $views_usermeta);
            }
            break;
        case 'dismiss_message':
            if (!is_user_logged_in()) {
                die('Authentication failed');
            }
            if (isset($_GET['id'])) {
                $messages = get_option('wpcf_dismissed_messages', array());
                $messages[] = sanitize_text_field($_GET['id']);
                update_option('wpcf_dismissed_messages', $messages);
            }
            break;
        case 'pr_add_child_post':
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) {
                $relationships = get_option('wpcf_post_relationship', array());
                $parent_post_id = intval($_GET['post_id']);
                $parent_post = get_post($parent_post_id);
                if (!empty($parent_post->ID)) {
                    $post_type = sanitize_text_field($_GET['post_type_child']);
                    $parent_post_type = sanitize_text_field($_GET['post_type_parent']);
                    // @todo isset & error handling
                    $data = $relationships[$parent_post_type][$post_type];
                    /*
                     * Since Types 1.1.5
                     * 
                     * We save new post
                     * CHECKPOINT
                     */
                    $id = $wpcf->relationship->add_new_child($parent_post->ID, $post_type);
                    if (is_wp_error($id)) {
                        $output = $id->get_error_message();
                    } else {
                        /*
                         * Here we set Relationship
                         * CHECKPOINT
                         */
                        $parent = get_post($parent_post_id);
                        $child = get_post($id);
                        if (!empty($parent->ID) && !empty($child->ID)) {
                            // Set post
                            $wpcf->post = $child;
                            // Set relationship :)
                            $wpcf->relationship->_set($parent, $child, $data);
                            // Render new row
                            $output = $wpcf->relationship->child_row($parent_post->ID, $id, $data);
                        } else {
                            $output = __('Error creating post relationship', 'wpcf');
                        }
                    }
                } else {
                    $output = __('Error getting parent post', 'wpcf');
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output . wpcf_form_render_js_validation('#post', false), 'child_id' => $id));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post')), 'child_id' => $id));
            }
            break;
        case 'pr_save_all':
            $output = '';
            if (current_user_can('edit_posts') && isset($_POST['post_id'])) {
                $parent_id = intval($_POST['post_id']);
                $post_type = sanitize_text_field($_POST['post_type']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id])) {
                    $children = wpcf_sanitize_post_realtionship_input((array) $_POST['wpcf_post_relationship'][$parent_id]);
                    $wpcf->relationship->save_children($parent_id, $children);
                    $output = $wpcf->relationship->child_meta_form($parent_id, strval($post_type));
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                // TODO Move to conditional
                $output .= '<script type="text/javascript">wpcfConditionalInit();</script>';
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_save_child_post':
            ob_start();
            // Try to catch any errors
            $output = '';
            if (current_user_can('edit_posts') && isset($_GET['post_id']) && isset($_GET['parent_id']) && isset($_GET['post_type_parent']) && isset($_GET['post_type_child']) && isset($_POST['wpcf_post_relationship'])) {
                $parent_id = intval($_GET['parent_id']);
                $child_id = intval($_GET['post_id']);
                $parent_post_type = sanitize_text_field($_GET['post_type_parent']);
                $child_post_type = sanitize_text_field($_GET['post_type_child']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
                    $fields = wpcf_sanitize_post_relationship_input_fields((array) $_POST['wpcf_post_relationship'][$parent_id][$child_id]);
                    $wpcf->relationship->save_child($parent_id, $child_id, $fields);
                    $output = $wpcf->relationship->child_row($parent_id, $child_id, $wpcf->relationship->settings($parent_post_type, $child_post_type));
                    if (!defined('WPTOOLSET_FORMS_VERSION')) {
                        // TODO Move to conditional
                        $output .= '<script type="text/javascript">wpcfConditionalInit(\'#types-child-row-' . $child_id . '\');</script>';
                    }
                }
            }
            $errors = ob_get_clean();
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output, 'errors' => $errors));
            } else {
                echo json_encode(array('output' => $output, 'errors' => $errors, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_delete_child_post':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['post_id'])) {
                $output = wpcf_pr_admin_delete_child_item(intval($_GET['post_id']));
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr-update-belongs':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_POST['post_id']) && isset($_POST['wpcf_pr_belongs'][$_POST['post_id']])) {
                $parent_post_id = intval($_POST['post_id']);
                $belongs_assignments = array();
                foreach ($_POST['wpcf_pr_belongs'][$parent_post_id] as $post_type_raw => $post_id_raw) {
                    $belongs_assignments[sanitize_text_field($post_type_raw)] = intval($post_id_raw);
                }
                $updated = wpcf_pr_admin_update_belongs($parent_post_id, $belongs_assignments);
                $output = is_wp_error($updated) ? $updated->get_error_message() : $updated;
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_pagination':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                global $wpcf;
                $parent = get_post(intval($_GET['post_id']));
                $child_post_type = sanitize_text_field($_GET['post_type']);
                if (!empty($parent->ID)) {
                    // Set post in loop
                    $wpcf->post = $parent;
                    // Save items_per_page
                    $wpcf->relationship->save_items_per_page($parent->post_type, $child_post_type, intval($_GET[$wpcf->relationship->items_per_page_option_name]));
                    $output = $wpcf->relationship->child_meta_form($parent->ID, $child_post_type);
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_sort':
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $output = $wpcf->relationship->child_meta_form(intval($_GET['post_id']), sanitize_text_field($_GET['post_type']));
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
            // Not used anywhere
            /*case 'pr_sort_parent':
              $output = 'Passed wrong parameters';
              if ( isset( $_GET['field'] ) && isset( $_GET['sort'] ) && isset( $_GET['post_id'] ) && isset( $_GET['post_type'] ) ) {
                  $output = $wpcf->relationship->child_meta_form(
                          intval( $_GET['post_id'] ), strval( $_GET['post_type'] )
                  );
              }
              if ( !defined( 'WPTOOLSET_FORMS_VERSION' ) ) {
                  echo json_encode( array(
                      'output' => $output,
                  ) );
              } else {
                  echo json_encode( array(
                      'output' => $output,
                      'conditionals' => array('#post' => wptoolset_form_get_conditional_data( 'post' )),
                  ) );
              }
              break;*/
            /* Usermeta */
        // Not used anywhere
        /*case 'pr_sort_parent':
          $output = 'Passed wrong parameters';
          if ( isset( $_GET['field'] ) && isset( $_GET['sort'] ) && isset( $_GET['post_id'] ) && isset( $_GET['post_type'] ) ) {
              $output = $wpcf->relationship->child_meta_form(
                      intval( $_GET['post_id'] ), strval( $_GET['post_type'] )
              );
          }
          if ( !defined( 'WPTOOLSET_FORMS_VERSION' ) ) {
              echo json_encode( array(
                  'output' => $output,
              ) );
          } else {
              echo json_encode( array(
                  'output' => $output,
                  'conditionals' => array('#post' => wptoolset_form_get_conditional_data( 'post' )),
              ) );
          }
          break;*/
        /* Usermeta */
        case 'um_repetitive_add':
            if (isset($_GET['user_id'])) {
                $user_id = $_GET['user_id'];
            } else {
                $user_id = wpcf_usermeta_get_user();
            }
            if (isset($_GET['field_id']) && current_user_can('edit_user', $user_id)) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field_id']), false, false, false, 'wpcf-usermeta');
                global $wpcf;
                $wpcf->usermeta_repeater->set($user_id, $field);
                /*
                 * 
                 * Force empty values!
                 */
                $wpcf->usermeta_repeater->cf['value'] = null;
                $wpcf->usermeta_repeater->meta = null;
                $form = $wpcf->usermeta_repeater->get_field_form(null, true);
                echo json_encode(array('output' => wpcf_form_simple($form) . wpcf_form_render_js_validation('#your-profile', false)));
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'um_repetitive_delete':
            if (isset($_POST['user_id']) && isset($_POST['field_id']) && current_user_can('edit_user', intval($_POST['user_id']))) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $user_id = intval($_POST['user_id']);
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_POST['field_id']), false, false, false, 'wpcf-usermeta');
                $meta_id = intval($_POST['meta_id']);
                if (!empty($field) && !empty($user_id) && !empty($meta_id)) {
                    /*
                     * 
                     * 
                     * Changed.
                     * Since Types 1.2
                     */
                    global $wpcf;
                    $wpcf->usermeta_repeater->set($user_id, $field);
                    $wpcf->usermeta_repeater->delete($meta_id);
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field or post not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
            /* End Usermeta */
        /* End Usermeta */
        case 'repetitive_add':
            if (current_user_can('edit_posts') && isset($_GET['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field_id']));
                $parent_post_id = intval($_GET['post_id']);
                /*
                 * When post is new - post_id is 0
                 * We can safely set post_id to 1 cause
                 * values compared are filtered anyway.
                 */
                if ($parent_post_id == 0) {
                    $parent_post_id = 1;
                }
                $parent_post = get_post($parent_post_id);
                global $wpcf;
                $wpcf->repeater->set($parent_post, $field);
                /*
                 * 
                 * Force empty values!
                 */
                $wpcf->repeater->cf['value'] = null;
                $wpcf->repeater->meta = null;
                $form = $wpcf->repeater->get_field_form(null, true);
                echo json_encode(array('output' => wpcf_form_simple($form) . wpcf_form_render_js_validation('#post', false)));
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'repetitive_delete':
            if (current_user_can('edit_posts') && isset($_POST['post_id']) && isset($_POST['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $post_id = intval($_POST['post_id']);
                $parent_post = get_post($post_id);
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_POST['field_id']));
                $meta_id = intval($_POST['meta_id']);
                if (!empty($field) && !empty($parent_post->ID) && !empty($meta_id)) {
                    /*
                     * 
                     * 
                     * Changed.
                     * Since Types 1.2
                     */
                    global $wpcf;
                    $wpcf->repeater->set($parent_post, $field);
                    $wpcf->repeater->delete($meta_id);
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field or post not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'cd_verify':
            if (!current_user_can('edit_posts') || empty($_POST['wpcf']) && empty($_POST['wpcf_post_relationship'])) {
                die;
            }
            WPCF_Loader::loadClass('helper.ajax');
            $js_execute = WPCF_Helper_Ajax::conditionalVerify($_POST);
            // Render JSON
            if (!empty($js_execute)) {
                echo json_encode(array('output' => '', 'execute' => $js_execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            }
            die;
            break;
        case 'cd_group_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $group = wpcf_admin_fields_get_group(sanitize_text_field($_POST['group_id']));
            if (!current_user_can('edit_posts') || empty($group)) {
                echo json_encode(array('output' => ''));
                die;
            }
            $execute = '';
            $group['conditional_display'] = get_post_meta($group['id'], '_wpcf_conditional_display', true);
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            $parent_post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $parent_post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$parent_post) {
                $parent_post = new stdClass();
                $parent_post->ID = 1;
            }
            if (!empty($group['conditional_display']['conditions'])) {
                $result = wpcf_cd_post_groups_filter(array(0 => $group), $parent_post, 'group');
                if (!empty($result)) {
                    $result = array_shift($result);
                    $passed = $result['_conditional_display'] == 'passed' ? true : false;
                } else {
                    $passed = false;
                }
                if (!$passed) {
                    $execute = 'jQuery("#wpcf-group-' . $group['slug'] . '").slideUp().find(".wpcf-cd-group")' . '.addClass(\'wpcf-cd-group-failed\')' . '.removeClass(\'wpcf-cd-group-passed\').hide();';
                } else {
                    $execute = 'jQuery("#wpcf-group-' . $group['slug'] . '").show().find(".wpcf-cd-group")' . '.addClass(\'wpcf-cd-group-passed\')' . '.removeClass(\'wpcf-cd-group-failed\').slideDown();';
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            break;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
Ejemplo n.º 3
0
/**
 * Edit fields form.
 * 
 * @global type $wpdb
 * @param type $parent
 * @param type $child 
 */
function wpcf_pr_admin_edit_fields($parent, $child)
{
    global $wpdb;
    $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'));
    }
    $relationships = get_option('wpcf_post_relationship', array());
    if (!isset($relationships[$parent][$child])) {
        die(__('Relationship do not exist'));
    }
    $data = $relationships[$parent][$child];
    wp_enqueue_script('jquery');
    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'] = $_POST['fields_setting'];
        $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;
    }
    $form = array();
    $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('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']);
    $options = array();
    $options['_wp_title'] = array('#title' => __('Post title', 'wpcf'), '#name' => 'fields[_wp_title]', '#default_value' => isset($data['fields']['_wp_title']) ? 1 : 0);
    $options['_wp_body'] = array('#title' => __('Post body', 'wpcf'), '#name' => 'fields[_wp_body]', '#default_value' => isset($data['fields']['_wp_body']) ? 1 : 0);
    //    $wpcf_fields = wpcf_admin_fields_get_fields(true);
    $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) {
            $options[WPCF_META_PREFIX . $key] = array();
            $options[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
            $options[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
            $options[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $key]) ? 1 : 0;
        }
    }
    $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;
    }
    //    foreach ($wpcf_fields as $key => $cf) {
    //        $options[WPCF_META_PREFIX . $key] = array();
    //        $options[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
    //        $options[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
    //        $options[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $key]) ? 1 : 0;
    //    }
    //    $cf_all = $wpdb->get_results("
    //		SELECT meta_id, meta_key
    //		FROM $wpdb->postmeta
    //		GROUP BY meta_key
    //		HAVING meta_key NOT LIKE '\_%'
    //		ORDER BY meta_key");
    //    foreach ($cf_all as $key => $cf) {
    //        if (isset($options[$cf->meta_key])) {
    //            continue;
    //        }
    //        $options[$key] = array();
    //        $options[$key]['#title'] = $cf->meta_key;
    //        $options[$key]['#name'] = 'fields[' . $cf->meta_key . ']';
    //        $options[$key]['#default_value'] = isset($data['fields'][$cf->meta_key]) ? 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'), '#attributes' => array('class' => 'button-primary'));
    echo '<form method="post" action="">';
    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();
            }
            jQuery('input[name="fields_setting"]').change(function(){
                if (jQuery(this).val() == 'specific') {
                    jQuery('#wpcf-specific').slideDown();
                } else {
                    jQuery('#wpcf-specific').slideUp();
                }
            });
        });
    </script>
    <?php 
    wpcf_admin_ajax_footer();
}
Ejemplo n.º 4
0
/**
 * Renders meta box content.
 * 
 * Core function. Works and stable.
 * If required, add hooks only.
 * 
 * @todo Revise this 1.1.5
 * 
 * @param type $post
 * @param type $group 
 */
function wpcf_admin_post_meta_box($post, $group)
{
    global $wpcf;
    static $nonce_added = false;
    /*
     * TODO Document where this is used
     */
    if (!$nonce_added) {
        $nonce_action = 'update-' . $post->post_type . '_' . $post->ID;
        wp_nonce_field($nonce_action, '_wpcf_post_wpnonce');
        $nonce_added = true;
    }
    /*
     * TODO Move to Conditional code
     * 
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        if ($group['args']['_conditional_display'] == 'failed') {
            echo '<div class="wpcf-cd-group wpcf-cd-group-failed" style="display:none;">';
        } else {
            echo '<div class="wpcf-cd-group wpcf-cd-group-passed">';
        }
    }
    /*
     * TODO Move this into Field code
     * Process fields
     */
    if (!empty($group['args']['fields'])) {
        // Display description
        if (!empty($group['args']['description'])) {
            echo '<div class="wpcf-meta-box-description">' . wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description', $group['args']['description'])) . '</div>';
        }
        foreach ($group['args']['fields'] as $field_slug => $field) {
            if (empty($field) || !is_array($field)) {
                continue;
            }
            $field = $wpcf->field->_parse_cf_form_element($field);
            if (!isset($field['#id'])) {
                $field['#id'] = wpcf_unique_id(serialize($field));
            }
            // Render form elements
            if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
                // Especially for WYSIWYG
                echo '<div class="wpcf-wysiwyg">';
                echo '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">';
                echo isset($field['#before']) ? $field['#before'] : '';
                echo '
<label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>';
                echo '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
' . wpautop($field['#description']) . '</div>';
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                echo '</div>';
                echo isset($field['#after']) ? $field['#after'] : '';
                echo '</div><br /><br />';
            } else {
                if ($field['#type'] == 'wysiwyg') {
                    $field['#type'] = 'textarea';
                }
                echo wpcf_form_simple(array($field['#id'] => $field));
            }
            do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form', $field);
            }
        }
    }
    /*
     * TODO Move to Conditional code
     * 
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        echo '</div>';
    }
}
Ejemplo n.º 5
0
/**
 * TinyMCE editor form.
 */
function wpcf_fields_date_editor_callback()
{
    $last_settings = wpcf_admin_fields_get_field_last_settings($_GET['field_id']);
    wp_enqueue_script('jquery');
    $form = array();
    $form['#form']['callback'] = 'wpcf_fields_date_editor_form_submit';
    $form['style'] = array('#type' => 'radios', '#name' => 'wpcf[style]', '#options' => array(__('Show as calendar', 'wpcf') => 'calendar', __('Show as text', 'wpcf') => 'text'), '#default_value' => isset($last_settings['style']) ? $last_settings['style'] : 'text', '#after' => '<br />');
    $date_formats = apply_filters('date_formats', array(__('F j, Y'), 'Y/m/d', 'm/d/Y', 'd/m/Y'));
    $options = array();
    foreach ($date_formats as $format) {
        $title = date($format, time());
        $field['#title'] = $title;
        $field['#value'] = $format;
        $options[] = $field;
    }
    $custom_format = isset($last_settings['format-custom']) ? $last_settings['format-custom'] : get_option('date_format');
    $options[] = array('#title' => __('Custom', 'wpcf'), '#value' => 'custom', '#suffix' => wpcf_form_simple(array('custom' => array('#name' => 'wpcf[format-custom]', '#type' => 'textfield', '#value' => $custom_format, '#suffix' => '&nbsp;' . date($custom_format, time()), '#inline' => true))));
    $form['toggle-open'] = array('#type' => 'markup', '#markup' => '<div id="wpcf-toggle" style="display:none;">');
    $form['format'] = array('#type' => 'radios', '#name' => 'wpcf[format]', '#options' => $options, '#default_value' => isset($last_settings['format']) ? $last_settings['format'] : get_option('date_format'), '#after' => '<a href="http://codex.wordpress.org/Formatting_Date_and_Time" target="_blank">' . __('Documentation on date and time formatting', 'wpcf') . '</a>');
    $form['toggle-close'] = array('#type' => 'markup', '#markup' => '</div>');
    $form['field_id'] = array('#type' => 'hidden', '#name' => 'wpcf[field_id]', '#value' => $_GET['field_id']);
    $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Insert date', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
    $f = wpcf_form('wpcf-fields-date-editor', $form);
    add_action('admin_head_wpcf_ajax', 'wpcf_fields_date_editor_form_script');
    wpcf_admin_ajax_head(__('Insert date', 'wpcf'));
    echo '<form id="wpcf-form" method="post" action="">';
    echo $f->renderForm();
    echo '</form>';
    wpcf_admin_ajax_footer();
}
Ejemplo n.º 6
0
/**
 * Edit fields form.
 * 
 * @global type $wpdb
 * @param type $parent
 * @param type $child 
 */
function wpcf_pr_admin_edit_fields($parent, $child)
{
    global $wpdb;
    $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'));
    }
    $relationships = get_option('wpcf_post_relationship', array());
    if (!isset($relationships[$parent][$child])) {
        die(__('Relationship do not exist'));
    }
    $data = $relationships[$parent][$child];
    wp_enqueue_script('jquery');
    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'] = $_POST['fields_setting'];
        $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) {
            $options_cf[WPCF_META_PREFIX . $key] = array();
            $options_cf[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
            $options_cf[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
            $options_cf[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $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[WPCF_META_PREFIX . $key]['#after'] = !isset($data['fields'][WPCF_META_PREFIX . $key]) ? '<div class="message error" style="display:none;"><p>' : '<div class="message error"><p>';
                $options_cf[WPCF_META_PREFIX . $key]['#after'] .= $repetitive_warning_txt;
                $options_cf[WPCF_META_PREFIX . $key]['#after'] .= '</p></div>';
                $options_cf[WPCF_META_PREFIX . $key]['#attributes'] = array('onclick' => 'jQuery(this).parent().find(\'.message\').toggle();');
            }
        }
    }
    $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', __('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']);
    $options = array();
    $options['_wp_title'] = array('#title' => __('Post title', 'wpcf'), '#name' => 'fields[_wp_title]', '#default_value' => isset($data['fields']['_wp_title']) ? 1 : 0);
    $options['_wp_body'] = array('#title' => __('Post body', 'wpcf'), '#name' => 'fields[_wp_body]', '#default_value' => isset($data['fields']['_wp_body']) ? 1 : 0);
    $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;
    }
    $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="">';
    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) {
        ?>
                                    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) {
        ?>
                                        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 field_add_existed()
 {
     /**
      * check nonce
      */
     if (0 || !isset($_REQUEST['_wpnonce']) || !isset($_REQUEST['id']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'fields_insert')) {
         $this->verification_failed_and_die();
     }
     /**
      * get field definition
      */
     require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
     $field = wpcf_admin_fields_get_field(sanitize_text_field($_REQUEST['id']), false, true, false, $this->type);
     if (!empty($field)) {
         echo wpcf_form_simple($this->get_field_form_data($field['type'], $field));
     }
     die;
 }
 /**
  * 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;
 }
Ejemplo n.º 9
0
/**
 * All AJAX calls go here.
 */
function wpcf_ajax_embedded()
{
    if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) {
        die('Verification failed');
    }
    switch ($_REQUEST['wpcf_action']) {
        case 'editor_insert_date':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/date.php';
            wpcf_fields_date_editor_form();
            break;
        case 'insert_skype_button':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php';
            wpcf_fields_skype_meta_box_ajax();
            break;
        case 'editor_callback':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            $field = wpcf_admin_fields_get_field($_GET['field_id']);
            if (!empty($field)) {
                $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
                if (function_exists($function)) {
                    call_user_func($function);
                }
            }
            break;
        case 'dismiss_message':
            if (isset($_GET['id'])) {
                $messages = get_option('wpcf_dismissed_messages', array());
                $messages[] = $_GET['id'];
                update_option('wpcf_dismissed_messages', $messages);
            }
            break;
        case 'pr_add_child_post':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) {
                $relationships = get_option('wpcf_post_relationship', array());
                $post = get_post($_GET['post_id']);
                $post_type = $_GET['post_type_child'];
                $parent_post_type = $_GET['post_type_parent'];
                $data = $relationships[$parent_post_type][$post_type];
                $output = wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, false);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_save_child_post':
            ob_start();
            // Try to catch any errors
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = array();
            if (isset($_GET['post_id']) && isset($_GET['post_type_child'])) {
                $post = get_post($_GET['post_id']);
                $post_type = $_GET['post_type_child'];
                $output = wpcf_pr_admin_save_post_hook($_GET['post_id']);
            }
            $errors = ob_get_clean();
            echo json_encode(array('output' => $output, 'errors' => $errors));
            break;
        case 'pr_delete_child_post':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id'])) {
                $output = wpcf_pr_admin_delete_child_item($_GET['post_id']);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr-update-belongs':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_POST['post_id']) && isset($_POST['wpcf_pr_belongs'])) {
                $output = wpcf_pr_admin_update_belongs($_POST['post_id'], $_POST['wpcf_pr_belongs']);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_pagination':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $post = get_post($_GET['post_id']);
                $post_type = $_GET['post_type'];
                $has = wpcf_pr_admin_get_has($post->post_type);
                $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_sort':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $post = get_post($_GET['post_id']);
                $post_type = $_GET['post_type'];
                $has = wpcf_pr_admin_get_has($post->post_type);
                $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_sort_parent':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $post = get_post($_GET['post_id']);
                $post_type = $_GET['post_type'];
                $has = wpcf_pr_admin_get_has($post->post_type);
                $output = wpcf_pr_admin_post_meta_box_has_form($post, $post_type, $has[$post_type], $post->post_type);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_save_all':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = array();
            if (isset($_POST['post_id']) && isset($_POST['wpcf_post_relationship'])) {
                $output = wpcf_pr_admin_save_post_hook($_POST['post_id']);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'repetitive_add':
            if (isset($_GET['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                $field = wpcf_admin_fields_get_field($_GET['field_id']);
                // Pass as normal
                unset($field['data']['repetitive']);
                $fields = array($_GET['field_id'] => $field);
                $element = wpcf_admin_post_process_fields(false, $fields, false, false, 'repetitive');
                if ($field['type'] == 'skype') {
                    $key = key($element);
                    unset($element[$key]['#title']);
                    echo json_encode(array('output' => wpcf_form_simple($element) . wpcf_form_render_js_validation('#post', false)));
                } else {
                    $element = array_shift($element);
                    if (!in_array($field['type'], array('checkbox'))) {
                        unset($element['#title']);
                    }
                    echo json_encode(array('output' => wpcf_form_simple(array('repetitive' => $element)) . wpcf_form_render_js_validation('#post', false)));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'repetitive_delete':
            if (isset($_POST['post_id']) && isset($_POST['field_id']) && isset($_POST['old_value'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $field = wpcf_admin_fields_get_field($_POST['field_id']);
                if (!empty($field)) {
                    if ($field['type'] == 'date') {
                        delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], strtotime(base64_decode($_POST['old_value'])));
                    } else {
                        if ($field['type'] == 'skype') {
                            delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], unserialize(base64_decode($_POST['old_value'])));
                        } else {
                            delete_post_meta($_POST['post_id'], wpcf_types_get_meta_prefix($field) . $field['id'], base64_decode($_POST['old_value']));
                        }
                    }
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'cd_verify':
            if (!is_array($_POST['wpcf'])) {
                die;
            }
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $passed_fields = array();
            $failed_fields = array();
            $post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $_POST['post_ID'] = $vars['post'];
                        $post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$post) {
                $post = new stdClass();
                $post->ID = 1;
            }
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            foreach ($_POST['wpcf'] as $field_id => $field_value) {
                $element = array();
                $field = wpcf_admin_fields_get_field($field_id);
                if (!empty($field['data']['conditional_display']['conditions'])) {
                    $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group');
                    if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') {
                        $passed_fields[] = 'wpcf[' . $field['id'] . ']';
                    } else {
                        $failed_fields[] = 'wpcf[' . $field['id'] . ']';
                    }
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            if (!empty($passed_fields) || !empty($failed_fields)) {
                $execute = '';
                foreach ($passed_fields as $field_name) {
                    $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " ";
                }
                foreach ($failed_fields as $field_name) {
                    $execute .= 'jQuery(\'[name^="' . $field_name . '"]\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " ";
                }
                echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            }
            die;
            break;
        case 'cd_group_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $group = wpcf_admin_fields_get_group($_POST['group_id']);
            if (empty($group)) {
                echo json_encode(array('output' => ''));
                die;
            }
            $execute = '';
            $group['conditional_display'] = get_post_meta($group['id'], '_wpcf_conditional_display', true);
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            $post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $_POST['post_ID'] = $vars['post'];
                        $post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$post) {
                $post = new stdClass();
                $post->ID = 1;
            }
            if (!empty($group['conditional_display']['conditions'])) {
                $result = wpcf_cd_post_groups_filter(array(0 => $group), $post, 'group');
                if (!empty($result)) {
                    $result = array_shift($result);
                    $passed = $result['_conditional_display'] == 'passed' ? true : false;
                } else {
                    $passed = false;
                }
                if (!$passed) {
                    $execute = 'jQuery("#' . $group['slug'] . '").slideUp().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-failed\').removeClass(\'wpcf-cd-group-passed\').hide();';
                } else {
                    $execute = 'jQuery("#' . $group['slug'] . '").show().find(".wpcf-cd-group").addClass(\'wpcf-cd-group-passed\').removeClass(\'wpcf-cd-group-failed\').slideDown();';
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            break;
        case 'pr_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $passed_fields = array();
            $failed_fields = array();
            $post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $_POST['post_ID'] = $vars['post'];
                        $post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$post) {
                $post = new stdClass();
                $post->ID = 1;
            }
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4);
            if (isset($_POST['wpcf_post_relationship'])) {
                $child_post_id = key($_POST['wpcf_post_relationship']);
                $data = $_POST['wpcf_post_relationship'] = array_shift($_POST['wpcf_post_relationship']);
                foreach ($data as $field_id => $field_value) {
                    $element = array();
                    $field = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $field_id));
                    if (!empty($field['data']['conditional_display']['conditions'])) {
                        $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group');
                        if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') {
                            $passed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id'];
                        } else {
                            $failed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id'];
                        }
                    }
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4);
            if (!empty($passed_fields) || !empty($failed_fields)) {
                $execute = '';
                foreach ($passed_fields as $field_name) {
                    $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " ";
                }
                foreach ($failed_fields as $field_name) {
                    $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " ";
                }
                echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            }
            die;
            break;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
Ejemplo n.º 10
0
/**
 * Returns HTML formatted field form (draggable).
 *
 * @param type $type
 * @param type $form_data
 * @return type
 */
function wpcf_fields_get_field_form($type, $form_data = array())
{
    $form = wpcf_fields_get_field_form_data($type, $form_data);
    if ($form) {
        $return = '<div class="ui-draggable">' . wpcf_form_simple($form) . '</div>';
        /**
         * add extra condition check if this is checkbox
         */
        foreach ($form as $key => $value) {
            if (!array_key_exists('value', $value) || !array_key_exists('#attributes', $value['value']) || !array_key_exists('data-wpcf-type', $value['value']['#attributes']) || 'checkbox' != $value['value']['#attributes']['data-wpcf-type']) {
                continue;
            }
            echo '<script type="text/javascript">';
            printf('jQuery(document).ready(function($){wpcf_checkbox_value_zero(jQuery(\'[name="%s"]\'));});', $value['value']['#name']);
            echo '</script>';
        }
        return $return;
    }
    return '<div>' . __('Wrong field requested', 'wpcf') . '</div>';
}
Ejemplo n.º 11
0
/**
 * Renders meta box content.
 * 
 * @param type $post
 * @param type $group 
 */
function wpcf_admin_post_meta_box($post, $group)
{
    if (!empty($group['args']['fields'])) {
        // Display description
        if (!empty($group['args']['description'])) {
            echo '<div class="wpcf-meta-box-description">' . wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description', $group['args']['description'])) . '</div>';
        }
        foreach ($group['args']['fields'] as $field_slug => $field) {
            // Render form elements
            if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
                // Especially for WYSIWYG
                unset($field['#before'], $field['#after']);
                echo '<div class="wpcf-wysiwyg">';
                echo '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">
<label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>';
                echo '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
' . wpautop($field['#description']) . '</div>';
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                echo '</div></div><br /><br />';
            } else {
                if ($field['#type'] == 'wysiwyg') {
                    $field['#type'] = 'textarea';
                }
                echo wpcf_form_simple(array($field['#id'] => $field));
            }
            do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form', $field);
            }
        }
    }
}
Ejemplo n.º 12
0
/**
 * All AJAX calls go here.
 */
function wpcf_ajax_embedded()
{
    if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) {
        die('Verification failed');
    }
    global $wpcf;
    switch ($_REQUEST['wpcf_action']) {
        case 'editor_insert_date':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/date.php';
            wpcf_fields_date_editor_form();
            break;
        case 'insert_skype_button':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php';
            wpcf_fields_skype_meta_box_ajax();
            break;
        case 'editor_callback':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            $field = wpcf_admin_fields_get_field($_GET['field_id']);
            if (!empty($field)) {
                $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
                if (function_exists($function)) {
                    call_user_func($function);
                }
            }
            break;
        case 'dismiss_message':
            if (isset($_GET['id'])) {
                $messages = get_option('wpcf_dismissed_messages', array());
                $messages[] = $_GET['id'];
                update_option('wpcf_dismissed_messages', $messages);
            }
            break;
        case 'pr_add_child_post':
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) {
                // Fix for Common Conditional check
                $_POST['post_ID'] = $_GET['post_id'];
                $relationships = get_option('wpcf_post_relationship', array());
                $post = get_post(intval($_GET['post_id']));
                if (!empty($post->ID)) {
                    $post_type = strval($_GET['post_type_child']);
                    $parent_post_type = strval($_GET['post_type_parent']);
                    $data = $relationships[$parent_post_type][$post_type];
                    /*
                     * Since Types 1.1.5
                     * 
                     * We save new post
                     * CHECKPOINT
                     */
                    $id = $wpcf->relationship->add_new_child($post->ID, $post_type);
                    if (!is_wp_error($id)) {
                        /*
                         * Here we set Relationship
                         * CHECKPOINT
                         */
                        $parent = get_post(intval($_GET['post_id']));
                        $child = get_post($id);
                        if (!empty($parent->ID) && !empty($child->ID)) {
                            // Set post
                            $wpcf->post = $child;
                            // Set relationship :)
                            $wpcf->relationship->_set($parent, $child, $data);
                            // Render new row
                            $output = $wpcf->relationship->child_row($post->ID, $id, $data);
                        } else {
                            $output = __('Error creating post relationship', 'wpcf');
                        }
                    } else {
                        $output = $id->get_error_message();
                    }
                } else {
                    $output = __('Error getting parent post', 'wpcf');
                }
            }
            echo json_encode(array('output' => $output . wpcf_form_render_js_validation('#post', false)));
            break;
        case 'pr_save_all':
            $output = '';
            if (isset($_POST['post_id'])) {
                // Fix for Common Conditional check
                $_POST['post_ID'] = $_POST['post_id'];
                $parent_id = intval($_POST['post_id']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id])) {
                    $wpcf->relationship->save_children($parent_id, (array) $_POST['wpcf_post_relationship'][$parent_id]);
                    $output = $wpcf->relationship->child_meta_form($parent_id, strval($_POST['post_type']));
                }
            }
            // TODO Move to conditional
            $output .= '<script type="text/javascript">wpcfConditionalInit();</script>';
            echo json_encode(array('output' => $output));
            break;
        case 'pr_save_child_post':
            ob_start();
            // Try to catch any errors
            $output = '';
            if (isset($_GET['post_id']) && isset($_GET['parent_id']) && isset($_GET['post_type_parent']) && isset($_GET['post_type_child']) && isset($_POST['wpcf_post_relationship'])) {
                // Fix for Common Conditional check
                $_POST['post_ID'] = $_POST['post_id'];
                $parent_id = intval($_GET['parent_id']);
                $child_id = intval($_GET['post_id']);
                $parent_post_type = strval($_GET['post_type_parent']);
                $child_post_type = strval($_GET['post_type_child']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
                    $fields = (array) $_POST['wpcf_post_relationship'][$parent_id][$child_id];
                    $wpcf->relationship->save_child($parent_id, $child_id, $fields);
                    $output = $wpcf->relationship->child_row($parent_id, $child_id, $wpcf->relationship->settings($parent_post_type, $child_post_type));
                    // TODO Move to conditional
                    $output .= '<script type="text/javascript">wpcfConditionalInit(\'#types-child-row-' . $child_id . '\');</script>';
                }
            }
            $errors = ob_get_clean();
            echo json_encode(array('output' => $output, 'errors' => $errors));
            break;
        case 'pr_delete_child_post':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id'])) {
                $output = wpcf_pr_admin_delete_child_item(intval($_GET['post_id']));
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr-update-belongs':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_POST['post_id']) && isset($_POST['wpcf_pr_belongs'][$_POST['post_id']])) {
                $post_id = intval($_POST['post_id']);
                $output = wpcf_pr_admin_update_belongs($post_id, $_POST['wpcf_pr_belongs'][$post_id]);
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_pagination':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (isset($_GET['post_id']) && isset($_GET['post_type'])) {
                global $wpcf;
                $parent = get_post(esc_attr($_GET['post_id']));
                $child_post_type = esc_attr($_GET['post_type']);
                if (!empty($parent->ID)) {
                    // Set post in loop
                    $wpcf->post = $parent;
                    // Save items_per_page
                    $wpcf->relationship->save_items_per_page($parent->post_type, $child_post_type, intval($_GET[$wpcf->relationship->items_per_page_option_name]));
                    $output = $wpcf->relationship->child_meta_form($parent->ID, $child_post_type);
                }
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_sort':
            $output = 'Passed wrong parameters';
            if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $output = $wpcf->relationship->child_meta_form(intval($_GET['post_id']), strval($_GET['post_type']));
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_sort_parent':
            $output = 'Passed wrong parameters';
            if (isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $output = $wpcf->relationship->child_meta_form(intval($_GET['post_id']), strval($_GET['post_type']));
            }
            echo json_encode(array('output' => $output));
            break;
        case 'repetitive_add':
            if (isset($_GET['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                $field = wpcf_admin_fields_get_field($_GET['field_id']);
                $post_id = intval($_GET['post_id']);
                /*
                 * When post is new - post_id is 0
                 * We can safely set post_id to 1 cause
                 * values compared are filtered anyway.
                 */
                if ($post_id == 0) {
                    $post_id = 1;
                }
                $post = get_post($post_id);
                global $wpcf;
                $wpcf->repeater->set($post, $field);
                /*
                 * 
                 * Force empty values!
                 */
                $wpcf->repeater->cf['value'] = null;
                $wpcf->repeater->meta = null;
                $form = $wpcf->repeater->get_field_form(null, true);
                echo json_encode(array('output' => wpcf_form_simple($form) . wpcf_form_render_js_validation('#post', false)));
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'repetitive_delete':
            if (isset($_POST['post_id']) && isset($_POST['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $post = get_post($_POST['post_id']);
                $field = wpcf_admin_fields_get_field($_POST['field_id']);
                $meta_id = $_POST['meta_id'];
                if (!empty($field) && !empty($post->ID) && !empty($meta_id)) {
                    /*
                     * 
                     * 
                     * Changed.
                     * Since Types 1.2
                     */
                    global $wpcf;
                    $wpcf->repeater->set($post, $field);
                    $wpcf->repeater->delete($meta_id);
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field or post not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'cd_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            global $wpcf;
            $post = null;
            $fields = array();
            $passed_fields = array();
            $failed_fields = array();
            $_flag_relationship = false;
            /*
             * We're accepting main form and others too
             * (like 'wpcf_post_relationship')
             * $fields = apply_filters( 'conditional_submitted_data', $data );
             */
            if (!empty($_POST['wpcf'])) {
                $fields = apply_filters('types_ajax_conditional_post', $_POST['wpcf']);
            }
            // TODO Move this to conditional and use hooks
            if (empty($fields) && empty($_POST['wpcf_post_relationship'])) {
                die;
            } else {
                if (empty($fields) && !empty($_POST['wpcf_post_relationship'])) {
                    /*
                     * 
                     * 
                     * Relationship case
                     * TODO Move to relationship or elsewhere.
                     */
                    $_temp = $_POST['wpcf_post_relationship'];
                    $parent_id = key($_temp);
                    $_data = array_shift($_temp);
                    $post_id = key($_data);
                    $post = get_post($post_id);
                    $fields = $_data[$post_id];
                    // Force
                    $_POST['post_ID'] = $post_id;
                    /*
                     * TODO This is temporary fix. Find better way to get fields
                     * rendered in child form
                     */
                    $_all_fields = wpcf_admin_fields_get_fields();
                    foreach ($_all_fields as $_field) {
                        $_slug = WPCF_META_PREFIX . $_field['slug'];
                        if (!isset($fields[$_slug])) {
                            $fields[$_slug] = null;
                        }
                    }
                    $_flag_relationship = true;
                    /*
                     * 
                     * 
                     * 
                     * 
                     * Regular submission
                     * TODO Make better?
                     */
                } else {
                    if (isset($_POST['wpcf_main_post_id'])) {
                        $_POST['post_ID'] = intval($_POST['wpcf_main_post_id']);
                        $post = get_post($_POST['post_ID']);
                    } else {
                        if (isset($_SERVER['HTTP_REFERER'])) {
                            $split = explode('?', $_SERVER['HTTP_REFERER']);
                            if (isset($split[1])) {
                                parse_str($split[1], $vars);
                                if (isset($vars['post'])) {
                                    $_POST['post_ID'] = $vars['post'];
                                    $post = get_post($vars['post']);
                                }
                            }
                        }
                    }
                    /*
                     * 
                     * Get fields by post and group.
                     */
                    //                $group_id = isset( $_POST['wpcf_group'] ) ? $_POST['wpcf_group'] : false;
                    //                if ( $group_id ) {
                    //                    $group = wpcf_admin_fields_get_group( $group_id );
                    //                    if ( !empty( $group ) ) {
                    //                        $_fields = wpcf_admin_fields_get_fields_by_group( $group['id'] );
                    //                        /*
                    //                         * Set missing fields to null (checkboxes and radios)
                    //                         */
                    //                        foreach ( $_fields as $_field ) {
                    //                            if ( !isset( $fields[$_field['slug']] ) ) {
                    //                                $fields[$_field['slug']] = null;
                    //                            }
                    //                        }
                    //                    }
                    //                }
                    // We need all fields
                    $_all_fields = wpcf_admin_fields_get_fields();
                    foreach ($_all_fields as $_field) {
                        if (!isset($fields[$_field['slug']])) {
                            $fields[$_field['slug']] = null;
                        }
                    }
                }
            }
            // Dummy post
            if (empty($post->ID)) {
                $post = new stdClass();
                $post->ID = 1;
            }
            foreach ($fields as $field_id => $field_value) {
                // Set conditional
                $wpcf->conditional->set($post, $field_id);
                if (!empty($wpcf->conditional->cf['data']['conditional_display']['conditions'])) {
                    if ($_flag_relationship) {
                        // Set context
                        $wpcf->conditional->context = 'relationship';
                        /*
                         * We need parent and child
                         */
                        $_relationship_name = false;
                        // Set name
                        $parent = get_post($parent_id);
                        if (!empty($parent->ID)) {
                            $wpcf->relationship->set($parent, $post);
                            $wpcf->relationship->cf->set($post, $field_id);
                            $_child = $wpcf->relationship->get_child();
                            $_child->form->cf->set($post, $field_id);
                            $_relationship_name = $_child->form->alter_form_name('wpcf[' . $wpcf->conditional->cf['id'] . ']');
                        }
                        if (!$_relationship_name) {
                            continue;
                        }
                        add_filter('types_field_get_submitted_data', 'wpcf_relationship_ajax_data_filter', 10, 2);
                        $name = $_relationship_name;
                    } else {
                        $name = 'wpcf[' . $wpcf->conditional->cf['id'] . ']';
                    }
                    /*
                     * Since Types 1.2
                     * Moved to WPCF_Conditional class.
                     */
                    // Evaluate
                    $passed = $wpcf->conditional->evaluate();
                    if ($passed) {
                        $passed_fields[] = $name;
                    } else {
                        $failed_fields[] = $name;
                    }
                }
            }
            /*
             * 
             * 
             * Render JS
             */
            if (!empty($passed_fields) || !empty($failed_fields)) {
                $execute = '';
                foreach ($passed_fields as $field_name) {
                    $execute .= $wpcf->conditional->render_js_show($field_name);
                }
                foreach ($failed_fields as $field_name) {
                    $execute .= $wpcf->conditional->render_js_hide($field_name);
                }
                echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            }
            die;
            break;
        case 'cd_group_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $group = wpcf_admin_fields_get_group($_POST['group_id']);
            if (empty($group)) {
                echo json_encode(array('output' => ''));
                die;
            }
            $execute = '';
            $group['conditional_display'] = get_post_meta($group['id'], '_wpcf_conditional_display', true);
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            $post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $_POST['post_ID'] = $vars['post'];
                        $post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$post) {
                $post = new stdClass();
                $post->ID = 1;
            }
            if (!empty($group['conditional_display']['conditions'])) {
                $result = wpcf_cd_post_groups_filter(array(0 => $group), $post, 'group');
                if (!empty($result)) {
                    $result = array_shift($result);
                    $passed = $result['_conditional_display'] == 'passed' ? true : false;
                } else {
                    $passed = false;
                }
                if (!$passed) {
                    $execute = 'jQuery("#' . $group['slug'] . '").slideUp().find(".wpcf-cd-group")' . '.addClass(\'wpcf-cd-group-failed\')' . '.removeClass(\'wpcf-cd-group-passed\').hide();';
                } else {
                    $execute = 'jQuery("#' . $group['slug'] . '").show().find(".wpcf-cd-group")' . '.addClass(\'wpcf-cd-group-passed\')' . '.removeClass(\'wpcf-cd-group-failed\').slideDown();';
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_meta_ajax_validation_filter', 10, 4);
            echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            break;
        case 'pr_verify':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/conditional-display.php';
            $passed_fields = array();
            $failed_fields = array();
            $post = false;
            if (isset($_SERVER['HTTP_REFERER'])) {
                $split = explode('?', $_SERVER['HTTP_REFERER']);
                if (isset($split[1])) {
                    parse_str($split[1], $vars);
                    if (isset($vars['post'])) {
                        $_POST['post_ID'] = $vars['post'];
                        $post = get_post($vars['post']);
                    }
                }
            }
            // Dummy post
            if (!$post) {
                $post = new stdClass();
                $post->ID = 1;
            }
            // Filter meta values (switch them with $_POST values)
            add_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4);
            //            add_filter( 'types_field_get_submitted_data',
            //                    'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4 );
            if (isset($_POST['wpcf_post_relationship'])) {
                $child_post_id = key($_POST['wpcf_post_relationship']);
                $data = $_POST['wpcf_post_relationship'] = array_shift($_POST['wpcf_post_relationship']);
                foreach ($data as $field_id => $field_value) {
                    $element = array();
                    $field = wpcf_admin_fields_get_field(str_replace(WPCF_META_PREFIX, '', $field_id));
                    if (!empty($field['data']['conditional_display']['conditions'])) {
                        $element = wpcf_cd_post_edit_field_filter($element, $field, $post, 'group');
                        if (isset($element['__wpcf_cd_status']) && $element['__wpcf_cd_status'] == 'passed') {
                            $passed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id'];
                        } else {
                            $failed_fields[] = 'wpcf_post_relationship_' . $child_post_id . '_' . $field['id'];
                        }
                    }
                }
            }
            // Remove filter meta values (switch them with $_POST values)
            remove_filter('get_post_metadata', 'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4);
            //            remove_filter( 'types_field_get_submitted_data',
            //                    'wpcf_cd_pr_meta_ajax_validation_filter', 10, 4 );
            if (!empty($passed_fields) || !empty($failed_fields)) {
                $execute = '';
                foreach ($passed_fields as $field_name) {
                    $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').show().removeClass(\'wpcf-cd-failed\').addClass(\'wpcf-cd-passed\');' . " ";
                }
                foreach ($failed_fields as $field_name) {
                    $execute .= 'jQuery(\'#' . $field_name . '\').parents(\'.wpcf-cd\').hide().addClass(\'wpcf-cd-failed\').removeClass(\'wpcf-cd-passed\');' . " ";
                }
                echo json_encode(array('output' => '', 'execute' => $execute, 'wpcf_nonce_ajax_callback' => wp_create_nonce('execute')));
            }
            die;
            break;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
Ejemplo n.º 13
0
function wpcf_wpml_translation_options($sections)
{
    $wpml_installed = apply_filters('wpml_setting', false, 'setup_complete');
    if ($wpml_installed) {
        $settings = wpcf_get_settings();
        $form['register_translations_on_import'] = array('#id' => 'register_translations_on_import', '#name' => 'wpcf_register_translations_on_import', '#type' => 'checkbox', '#label' => __("When importing, add texts to WPML's String Translation table", 'wpcf'), '#inline' => true, '#default_value' => !empty($settings['register_translations_on_import']), '#pattern' => '<p><ELEMENT><LABEL><DESCRIPTION></p>');
        $section_content = wpcf_form_simple($form);
        $sections['wpml-wpcf'] = array('slug' => 'wpml-wpcf', 'title' => __('Types and WPML integration', 'wpcf'), 'content' => $section_content);
    }
    return $sections;
}
Ejemplo n.º 14
0
/**
 * Add form inputs related to term field group and field definition import.
 * 
 * @param SimpleXMLElement $data Import data from XML
 * @return array Enlimbo form elements (yuck).
 * @since 2.1
 */
function wpcf_admin_import_export_settings_for_terms($data)
{
    $form = array();
    if (!empty($data->term_groups)) {
        $form['title-terms'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Term field groups to be added or updated', 'wpcf') . '</h2>');
        $group_factory = Types_Field_Group_Term_Factory::get_instance();
        $groups_check = array();
        foreach ($data->term_groups->group as $group) {
            $group = (array) $group;
            $group_id = $group['ID'];
            $group_slug = $group['post_title'];
            $form['term-group-add-' . $group_id] = array('#type' => 'checkbox', '#name' => 'term_groups[' . $group_id . '][add]', '#default_value' => true, '#title' => '<strong>' . esc_html($group_slug) . '</strong>', '#inline' => true, '#after' => '<br /><br />');
            $existing_groups = $group_factory->query_groups(array('name' => $group_slug, 'post_type' => $group['post_type']));
            $group_already_exists = count($existing_groups) > 0;
            if ($group_already_exists) {
                $form['term-group-add-' . $group_id]['#after'] = wpcf_form_simple(array('term-group-add-update-' . $group_id => array('#type' => 'radios', '#name' => 'term_groups[' . $group_id . '][update]', '#inline' => true, '#options' => array(__('Update', 'wpcf') => 'update', __('Create new', 'wpcf') => 'add'), '#default_value' => 'update', '#before' => '<br />', '#after' => '<br />')));
            }
            $groups_check[] = $group_slug;
        }
        $groups_existing = get_posts(array('post_type' => Types_Field_Group_Term::POST_TYPE, 'post_status' => null));
        if (!empty($groups_existing)) {
            $groups_to_be_deleted = array();
            foreach ($groups_existing as $post) {
                if (!in_array($post->post_title, $groups_check)) {
                    $groups_to_be_deleted['<strong>' . $post->post_title . '</strong>'] = $post->ID;
                }
            }
            if (!empty($groups_to_be_deleted)) {
                $form['title-term-groups-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Term groups to be deleted', 'wpcf') . '</h2>');
                $form['term-groups-deleted'] = array('#type' => 'checkboxes', '#name' => 'term-groups-to-be-deleted', '#options' => $groups_to_be_deleted);
            }
        }
    }
    // Check term fields
    if (!empty($data->term_fields)) {
        $form['term-title-fields'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Term fields to be added/updated', 'wpcf') . '</h2>');
        $fields_existing = wpcf_admin_fields_get_fields(false, false, false, WPCF_Field_Definition_Factory_Term::FIELD_DEFINITIONS_OPTION);
        $fields_check = array();
        $fields_to_be_deleted = array();
        foreach ($data->term_fields->field as $field) {
            $field = (array) $field;
            if (empty($field['id']) || empty($field['name'])) {
                continue;
            }
            $form['term-field-add-' . $field['id']] = array('#type' => 'checkbox', '#name' => 'term_fields[' . $field['id'] . '][add]', '#default_value' => true, '#title' => '<strong>' . $field['name'] . '</strong>', '#inline' => true, '#after' => '<br />');
            $fields_check[] = $field['id'];
        }
        foreach ($fields_existing as $field_id => $field) {
            if (!in_array($field_id, $fields_check)) {
                $fields_to_be_deleted['<strong>' . $field['name'] . '</strong>'] = $field['id'];
            }
        }
        if (!empty($fields_to_be_deleted)) {
            $form['term-title-fields-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Term ields to be deleted', 'wpcf') . '</h2>');
            $form['term-fields-deleted'] = array('#type' => 'checkboxes', '#name' => 'term-fields-to-be-deleted', '#options' => $fields_to_be_deleted);
        }
    }
    return $form;
}
Ejemplo n.º 15
0
/**
 * Import settings.
 * 
 * @global type $wpdb
 * @param SimpleXMLElement $data
 * @return string 
 */
function wpcf_admin_import_export_settings($data)
{
    global $wpdb;
    $form = array();
    $form['title'] = array('#type' => 'markup', '#markup' => '<h2>' . __('General Settings', 'wpcf') . '</h2>');
    $form['overwrite-or-add-groups'] = array('#type' => 'checkbox', '#title' => __('Bulk overwrite groups if exist', 'wpcf'), '#name' => 'overwrite-groups', '#inline' => true, '#after' => '<br />');
    $form['delete-groups'] = array('#type' => 'checkbox', '#title' => __("Delete group if don't exist", 'wpcf'), '#name' => 'delete-groups', '#inline' => true, '#after' => '<br />');
    $form['delete-fields'] = array('#type' => 'checkbox', '#title' => __("Delete field if don't exist", 'wpcf'), '#name' => 'delete-fields', '#inline' => true, '#after' => '<br />');
    $form['delete-types'] = array('#type' => 'checkbox', '#title' => __("Delete custom post type if don't exist", 'wpcf'), '#name' => 'delete-types', '#inline' => true, '#after' => '<br />');
    $form['delete-tax'] = array('#type' => 'checkbox', '#title' => __("Delete custom taxonomy if don't exist", 'wpcf'), '#name' => 'delete-tax', '#inline' => true, '#after' => '<br />');
    libxml_use_internal_errors(true);
    $data = simplexml_load_string($data);
    if (!$data) {
        echo '<div class="message error"><p>' . __('Error parsing XML', 'wpcf') . '</p></div>';
        foreach (libxml_get_errors() as $error) {
            echo '<div class="message error"><p>' . $error->message . '</p></div>';
        }
        libxml_clear_errors();
        return false;
    }
    //    $data = new SimpleXMLElement($data);
    // Check groups
    if (!empty($data->groups)) {
        $form['title-1'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Groups to be added/updated', 'wpcf') . '</h2>');
        $groups_check = array();
        foreach ($data->groups->group as $group) {
            $group = (array) $group;
            $form['group-add-' . $group['ID']] = array('#type' => 'checkbox', '#name' => 'groups[' . $group['ID'] . '][add]', '#default_value' => true, '#title' => '<strong>' . esc_html($group['post_title']) . '</strong>', '#inline' => true, '#after' => '<br /><br />');
            $post = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n                    WHERE post_title = %s AND post_type = %s", $group['post_title'], $group['post_type']));
            if (!empty($post)) {
                $form['group-add-' . $group['ID']]['#after'] = wpcf_form_simple(array('group-add-update-' . $group['ID'] => array('#type' => 'radios', '#name' => 'groups[' . $group['ID'] . '][update]', '#inline' => true, '#options' => array(__('Update', 'wpcf') => 'update', __('Create new', 'wpcf') => 'add'), '#default_value' => 'update', '#before' => '<br />', '#after' => '<br />')));
            }
            $groups_check[] = $group['post_title'];
        }
        $groups_existing = get_posts('post_type=wp-types-group&post_status=null');
        if (!empty($groups_existing)) {
            $groups_to_be_deleted = array();
            foreach ($groups_existing as $post) {
                if (!in_array($post->post_title, $groups_check)) {
                    $groups_to_be_deleted['<strong>' . $post->post_title . '</strong>'] = $post->ID;
                }
            }
            if (!empty($groups_to_be_deleted)) {
                $form['title-groups-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Groups to be deleted', 'wpcf') . '</h2>');
                $form['groups-deleted'] = array('#type' => 'checkboxes', '#name' => 'groups-to-be-deleted', '#options' => $groups_to_be_deleted);
            }
        }
    }
    // Check fields
    if (!empty($data->fields)) {
        $form['title-fields'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields to be added/updated', 'wpcf') . '</h2>');
        $fields_existing = wpcf_admin_fields_get_fields();
        $fields_check = array();
        $fields_to_be_deleted = array();
        foreach ($data->fields->field as $field) {
            $field = (array) $field;
            $form['field-add-' . $field['id']] = array('#type' => 'checkbox', '#name' => 'fields[' . $field['id'] . '][add]', '#default_value' => true, '#title' => '<strong>' . $field['name'] . '</strong>', '#inline' => true, '#after' => '<br />');
            $fields_check[] = $field['id'];
        }
        foreach ($fields_existing as $field_id => $field) {
            if (!in_array($field_id, $fields_check)) {
                $fields_to_be_deleted['<strong>' . $field['name'] . '</strong>'] = $field['id'];
            }
        }
        if (!empty($fields_to_be_deleted)) {
            $form['title-fields-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Fields to be deleted', 'wpcf') . '</h2>');
            $form['fields-deleted'] = array('#type' => 'checkboxes', '#name' => 'fields-to-be-deleted', '#options' => $fields_to_be_deleted);
        }
    }
    // Check types
    if (!empty($data->types)) {
        $form['title-types'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Custom post types to be added/updated', 'wpcf') . '</h2>');
        $types_existing = get_option('wpcf-custom-types', array());
        $types_check = array();
        $types_to_be_deleted = array();
        foreach ($data->types->type as $type) {
            $type = (array) $type;
            $form['type-add-' . $type['id']] = array('#type' => 'checkbox', '#name' => 'types[' . $type['id'] . '][add]', '#default_value' => true, '#title' => '<strong>' . $type['labels']->name . '</strong>', '#inline' => true, '#after' => '<br />');
            $types_check[] = $type['id'];
        }
        foreach ($types_existing as $type_id => $type) {
            if (!in_array($type_id, $types_check)) {
                $types_to_be_deleted['<strong>' . $type['labels']['name'] . '</strong>'] = $type_id;
            }
        }
        if (!empty($types_to_be_deleted)) {
            $form['title-types-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Custom post types to be deleted', 'wpcf') . '</h2>');
            $form['types-deleted'] = array('#type' => 'checkboxes', '#name' => 'types-to-be-deleted', '#options' => $types_to_be_deleted);
        }
    }
    // Check taxonomies
    if (!empty($data->taxonomies)) {
        $form['title-tax'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Custom taxonomies to be added/updated', 'wpcf') . '</h2>');
        $taxonomies_existing = get_option('wpcf-custom-taxonomies', array());
        $taxonomies_check = array();
        $taxonomies_to_be_deleted = array();
        foreach ($data->taxonomies->taxonomy as $taxonomy) {
            $taxonomy = (array) $taxonomy;
            $form['taxonomy-add-' . $taxonomy['id']] = array('#type' => 'checkbox', '#name' => 'taxonomies[' . $taxonomy['id'] . '][add]', '#default_value' => true, '#title' => '<strong>' . $taxonomy['labels']->name . '</strong>', '#inline' => true, '#after' => '<br />');
            $taxonomies_check[] = $taxonomy['id'];
        }
        foreach ($taxonomies_existing as $taxonomy_id => $taxonomy) {
            if (!in_array($taxonomy_id, $taxonomies_check)) {
                $taxonomies_to_be_deleted['<strong>' . $taxonomy['labels']['name'] . '</strong>'] = $taxonomy_id;
            }
        }
        if (!empty($taxonomies_to_be_deleted)) {
            $form['title-taxonomies-deleted'] = array('#type' => 'markup', '#markup' => '<h2>' . __('Custom taxonomies to be deleted', 'wpcf') . '</h2>');
            $form['taxonomies-deleted'] = array('#type' => 'checkboxes', '#name' => 'taxonomies-to-be-deleted', '#options' => $taxonomies_to_be_deleted);
        }
    }
    return $form;
}
Ejemplo n.º 16
0
/**
 * Meta boxes contents output.
 *
 * @param type $post
 * @param type $args
 */
function wpcf_pr_admin_post_meta_box_output($post, $args)
{
    if (empty($post) || empty($post->ID)) {
        return array();
    }
    global $wpcf;
    $output = '';
    $relationships = $args;
    $post_id = !empty($post->ID) ? $post->ID : -1;
    $current_post_type = wpcf_admin_get_edited_post_type($post);
    /*
     * Render has form (child form)
     */
    if (!empty($relationships['has'])) {
        foreach ($relationships['has'] as $post_type => $data) {
            if (isset($data['fields_setting']) && 'only_list' == $data['fields_setting']) {
                $output .= $wpcf->relationship->child_list($post, $post_type, $data);
            } else {
                $output .= $wpcf->relationship->child_meta_form($post, $post_type, $data);
            }
        }
    }
    /*
     * Render belongs form (parent form)
     */
    if (!empty($relationships['belongs'])) {
        $meta = get_post_custom($post_id);
        $belongs = array('belongs' => array(), 'posts' => array());
        foreach ($meta as $meta_key => $meta_value) {
            if (strpos($meta_key, '_wpcf_belongs_') === 0) {
                $temp_post = get_post($meta_value[0]);
                if (!empty($temp_post)) {
                    $belongs['posts'][$temp_post->ID] = $temp_post;
                    $belongs['belongs'][$temp_post->post_type] = $temp_post->ID;
                }
            }
        }
        foreach ($relationships['belongs'] as $post_type => $data) {
            $output .= '<div style="margin: 20px 0 10px 0">';
            if ($x = wpcf_form_simple(wpcf_pr_admin_post_meta_box_belongs_form($post, $post_type, $belongs))) {
                $output .= sprintf(__('This <i>%s</i> belongs to:', 'wpcf'), get_post_type_object($current_post_type)->labels->singular_name);
                $output .= $x;
            } else {
                $output .= get_post_type_object($post_type)->labels->not_found;
            }
            $output .= '</div>';
        }
    }
    return $output;
}
Ejemplo n.º 17
0
/**
 * Returns HTML formatted field form (draggable).
 * 
 * @param type $type
 * @param type $form_data
 * @return type 
 */
function wpcf_fields_get_field_form($type, $form_data = array())
{
    $form = wpcf_fields_get_field_form_data($type, $form_data);
    if ($form) {
        return '<div class="ui-draggable">' . wpcf_form_simple($form) . '</div>';
    }
    return '<div>' . __('Wrong field requested', 'wpcf') . '</div>';
}
Ejemplo n.º 18
0
/**
 * AJAX call from Bulk actions.
 */
function wpcf_admin_custom_fields_control_bulk_ajax()
{
    if (empty($_REQUEST['fields'])) {
        die(__('Please select fields', 'wpcf'));
    }
    if (!empty($_POST)) {
        if (!empty($_POST['groups']) && !empty($_POST['fields'])) {
            $action = isset($_POST['wpcf_action_control']) ? sanitize_text_field($_POST['wpcf_action_control']) : 'wpcf-add-to-group-bulk';
            foreach ($_POST['groups'] as $group_id) {
                $group_id = sanitize_text_field($group_id);
                switch ($action) {
                    case 'wpcf-add-to-group-bulk':
                        wpcf_admin_fields_save_group_fields($group_id, array_values((array) $_POST['fields']), true);
                        break;
                    case 'wpcf-remove-from-group-bulk':
                        wpcf_admin_fields_remove_field_from_group_bulk($group_id, array_values((array) $_POST['fields']));
                        break;
                    default:
                        break;
                }
            }
        } else {
            if (!empty($_POST['type']) && !empty($_POST['fields'])) {
                wpcf_admin_custom_fields_change_type($_POST['fields'], sanitize_text_field($_POST['type']));
            }
        }
        echo '<script type="text/javascript">
            window.parent.jQuery("#TB_closeWindowButton").click();
            window.parent.location.href=window.parent.location.href;
</script>';
        die;
    }
    $groups = wpcf_admin_fields_get_groups();
    $output = array();
    if (in_array($_GET['wpcf_bulk_action'], array('wpcf-add-to-group-bulk', 'wpcf-remove-from-group-bulk'))) {
        foreach ($groups as $group_id => $group) {
            $output[$group['id']] = array('#type' => 'checkbox', '#name' => 'groups[]', '#title' => $group['name'], '#value' => $group['id'], '#default_value' => false, '#inline' => true);
        }
    } else {
        if ($_GET['wpcf_bulk_action'] == 'wpcf-change-type-bulk') {
            $output['types'] = wpcf_admin_custom_fields_control_change_type_dropdown();
        } else {
            die('Not valid action');
        }
    }
    foreach ($_GET['fields'] as $field_id) {
        $field_id = sanitize_text_field($field_id);
        $output[$field_id] = array('#type' => 'hidden', '#name' => 'fields[]', '#value' => $field_id);
    }
    $output['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Save Changes'), '#attributes' => array('class' => 'button-primary'));
    echo '<form method="post" action="">';
    echo wpcf_form_simple($output);
    wp_nonce_field('custom_fields_control_bulk');
    echo '<input type="hidden" name="action" value="wpcf_ajax" />';
    echo '<input type="hidden" name="wpcf_action" value="custom_fields_control_bulk" />';
    echo '<input type="hidden" name="wpcf_action_control" value="' . esc_attr($_GET['wpcf_bulk_action']) . '" />';
    echo '</form>';
}
Ejemplo n.º 19
0
/**
 * 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)) . '&nbsp;' . '<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;
}
Ejemplo n.º 20
0
/**
 * All AJAX calls go here.
 *
 * @todo auth
 */
function wpcf_ajax_embedded()
{
    if (isset($_REQUEST['_typesnonce'])) {
        if (!wp_verify_nonce($_REQUEST['_typesnonce'], '_typesnonce')) {
            die('Verification failed (1)');
        }
    } else {
        if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $_REQUEST['wpcf_action'])) {
            die('Verification failed (2)');
        }
    }
    global $wpcf;
    switch ($_REQUEST['wpcf_action']) {
        case 'insert_skype_button':
            if (!current_user_can('edit_posts')) {
                die('Authentication failed');
            }
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields/skype.php';
            wpcf_fields_skype_meta_box_ajax();
            break;
        case 'editor_callback':
            if (!current_user_can('edit_posts')) {
                die('Authentication failed');
            }
            // Determine Field type and context
            $views_meta = false;
            $field_id = sanitize_text_field($_GET['field_id']);
            // todo this could be written in like four lines
            if (isset($_GET['field_type']) && $_GET['field_type'] == 'usermeta') {
                // Group filter
                wp_enqueue_script('suggest');
                $field = types_get_field($field_id, 'usermeta');
                $meta_type = 'usermeta';
            } elseif (isset($_GET['field_type']) && $_GET['field_type'] == 'views-usermeta') {
                $field = types_get_field($field_id, 'usermeta');
                $meta_type = 'usermeta';
                $views_meta = true;
            } elseif (isset($_GET['field_type']) && $_GET['field_type'] == 'termmeta') {
                // Group filter
                wp_enqueue_script('suggest');
                $field = types_get_field($field_id, 'termmeta');
                $meta_type = 'termmeta';
            } elseif (isset($_GET['field_type']) && $_GET['field_type'] == 'views-termmeta') {
                $field = types_get_field($field_id, 'termmeta');
                $meta_type = 'termmeta';
                $views_meta = true;
            } else {
                $field = types_get_field($field_id);
                $meta_type = 'postmeta';
            }
            $parent_post_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : null;
            $shortcode = isset($_GET['shortcode']) ? urldecode($_GET['shortcode']) : null;
            $callback = isset($_GET['callback']) ? sanitize_text_field($_GET['callback']) : false;
            if (!empty($field)) {
                // Editor
                WPCF_Loader::loadClass('editor');
                $editor = new WPCF_Editor();
                $editor->frame($field, $meta_type, $parent_post_id, $shortcode, $callback, $views_meta);
            }
            break;
        case 'dismiss_message':
            if (!is_user_logged_in()) {
                die('Authentication failed');
            }
            if (isset($_GET['id'])) {
                $messages = get_option('wpcf_dismissed_messages', array());
                $messages[] = sanitize_text_field($_GET['id']);
                update_option('wpcf_dismissed_messages', $messages);
            }
            break;
        case 'pr_add_child_post':
            global $current_user;
            $output = '<tr>' . __('Passed wrong parameters', 'wpcf') . '</tr>';
            $id = 0;
            $target_post_type = isset($_GET['post_type_child']) ? sanitize_text_field($_GET['post_type_child']) : '';
            $has_permissions = current_user_can('publish_posts');
            $has_permissions = apply_filters('toolset_access_api_get_post_type_permissions', $has_permissions, $target_post_type, 'publish');
            if (!$has_permissions) {
                $output = '<tr><td>' . __('You do not have rights to create new items', 'wpcf') . '</td></tr>';
            } else {
                if (isset($_GET['post_id']) && isset($_GET['post_type_child']) && isset($_GET['post_type_parent'])) {
                    $relationships = get_option('wpcf_post_relationship', array());
                    $parent_post_id = intval($_GET['post_id']);
                    $parent_post = get_post($parent_post_id);
                    if (!empty($parent_post->ID)) {
                        $post_type = sanitize_text_field($_GET['post_type_child']);
                        $parent_post_type = sanitize_text_field($_GET['post_type_parent']);
                        // @todo isset & error handling
                        $data = $relationships[$parent_post_type][$post_type];
                        /*
                         * Since Types 1.1.5
                         * 
                         * We save new post
                         * CHECKPOINT
                         */
                        $id = $wpcf->relationship->add_new_child($parent_post->ID, $post_type);
                        if (is_wp_error($id)) {
                            $output = '<tr>' . $id->get_error_message() . '</tr>';
                        } else {
                            /*
                             * Here we set Relationship
                             * CHECKPOINT
                             */
                            $parent = get_post($parent_post_id);
                            $child = get_post($id);
                            if (!empty($parent->ID) && !empty($child->ID)) {
                                // Set post
                                $wpcf->post = $child;
                                // Set relationship :)
                                $wpcf->relationship->_set($parent, $child, $data);
                                // Render new row
                                $output = $wpcf->relationship->child_row($parent_post->ID, $id, $data);
                            } else {
                                $output = '<tr>' . __('Error creating post relationship', 'wpcf') . '</tr>';
                            }
                        }
                    } else {
                        $output = '<tr>' . __('Error getting parent post', 'wpcf') . '</tr>';
                    }
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output . wpcf_form_render_js_validation('#post', false), 'child_id' => $id));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post')), 'child_id' => $id));
            }
            break;
        case 'pr_save_all':
            ob_start();
            // Try to catch any errors
            $output = '';
            if (current_user_can('edit_posts') && isset($_POST['post_id'])) {
                $parent_id = intval($_POST['post_id']);
                $post_type = sanitize_text_field($_POST['post_type']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id])) {
                    $children = wpcf_sanitize_post_realtionship_input((array) $_POST['wpcf_post_relationship'][$parent_id]);
                    $wpcf->relationship->save_children($parent_id, $children);
                    $output = $wpcf->relationship->child_meta_form($parent_id, strval($post_type));
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                // TODO Move to conditional
                $output .= '<script type="text/javascript">wpcfConditionalInit();</script>';
            }
            wpcf_show_admin_messages('echo');
            $errors = ob_get_clean();
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output, 'errors' => $errors));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post')), 'errors' => $errors));
            }
            break;
        case 'pr_save_child_post':
            ob_start();
            // Try to catch any errors
            $output = '';
            if (current_user_can('edit_posts') && isset($_GET['post_id']) && isset($_GET['parent_id']) && isset($_GET['post_type_parent']) && isset($_GET['post_type_child']) && isset($_POST['wpcf_post_relationship'])) {
                $parent_id = intval($_GET['parent_id']);
                $child_id = intval($_GET['post_id']);
                $parent_post_type = sanitize_text_field($_GET['post_type_parent']);
                $child_post_type = sanitize_text_field($_GET['post_type_child']);
                if (isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
                    $fields = wpcf_sanitize_post_relationship_input_fields((array) $_POST['wpcf_post_relationship'][$parent_id][$child_id]);
                    $wpcf->relationship->save_child($parent_id, $child_id, $fields);
                    $output = $wpcf->relationship->child_row($parent_id, $child_id, $wpcf->relationship->settings($parent_post_type, $child_post_type));
                    if (!defined('WPTOOLSET_FORMS_VERSION')) {
                        // TODO Move to conditional
                        $output .= '<script type="text/javascript">wpcfConditionalInit(\'#types-child-row-' . $child_id . '\');</script>';
                    }
                }
            }
            wpcf_show_admin_messages('echo');
            $errors = ob_get_clean();
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output, 'errors' => $errors));
            } else {
                echo json_encode(array('output' => $output, 'errors' => $errors, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_delete_child_post':
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['post_id'])) {
                $output = wpcf_pr_admin_delete_child_item(intval($_GET['post_id']));
            }
            echo json_encode(array('output' => $output));
            break;
        case 'pr_pagination':
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
            require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
            require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                global $wpcf;
                $parent = get_post(intval($_GET['post_id']));
                $child_post_type = sanitize_text_field($_GET['post_type']);
                if (!empty($parent->ID)) {
                    // Set post in loop
                    $wpcf->post = $parent;
                    // Save items_per_page
                    $wpcf->relationship->save_items_per_page($parent->post_type, $child_post_type, intval($_GET[$wpcf->relationship->items_per_page_option_name]));
                    $output = $wpcf->relationship->child_meta_form($parent->ID, $child_post_type);
                }
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
        case 'pr_sort':
            $output = 'Passed wrong parameters';
            if (current_user_can('edit_posts') && isset($_GET['field']) && isset($_GET['sort']) && isset($_GET['post_id']) && isset($_GET['post_type'])) {
                $output = $wpcf->relationship->child_meta_form(intval($_GET['post_id']), sanitize_text_field($_GET['post_type']));
            }
            if (!defined('WPTOOLSET_FORMS_VERSION')) {
                echo json_encode(array('output' => $output));
            } else {
                echo json_encode(array('output' => $output, 'conditionals' => array('#post' => wptoolset_form_get_conditional_data('post'))));
            }
            break;
            // Not used anywhere
            /*case 'pr_sort_parent':
              $output = 'Passed wrong parameters';
              if ( isset( $_GET['field'] ) && isset( $_GET['sort'] ) && isset( $_GET['post_id'] ) && isset( $_GET['post_type'] ) ) {
                  $output = $wpcf->relationship->child_meta_form(
                          intval( $_GET['post_id'] ), strval( $_GET['post_type'] )
                  );
              }
              if ( !defined( 'WPTOOLSET_FORMS_VERSION' ) ) {
                  echo json_encode( array(
                      'output' => $output,
                  ) );
              } else {
                  echo json_encode( array(
                      'output' => $output,
                      'conditionals' => array('#post' => wptoolset_form_get_conditional_data( 'post' )),
                  ) );
              }
              break;*/
            /* Usermeta */
        // Not used anywhere
        /*case 'pr_sort_parent':
          $output = 'Passed wrong parameters';
          if ( isset( $_GET['field'] ) && isset( $_GET['sort'] ) && isset( $_GET['post_id'] ) && isset( $_GET['post_type'] ) ) {
              $output = $wpcf->relationship->child_meta_form(
                      intval( $_GET['post_id'] ), strval( $_GET['post_type'] )
              );
          }
          if ( !defined( 'WPTOOLSET_FORMS_VERSION' ) ) {
              echo json_encode( array(
                  'output' => $output,
              ) );
          } else {
              echo json_encode( array(
                  'output' => $output,
                  'conditionals' => array('#post' => wptoolset_form_get_conditional_data( 'post' )),
              ) );
          }
          break;*/
        /* Usermeta */
        case 'um_repetitive_add':
            if (isset($_GET['user_id'])) {
                $user_id = (int) $_GET['user_id'];
            } else {
                $user_id = wpcf_usermeta_get_user();
            }
            if (isset($_GET['field_id']) && current_user_can('edit_user', $user_id)) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/usermeta-post.php';
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field_id']), false, false, false, 'wpcf-usermeta');
                global $wpcf;
                $wpcf->usermeta_repeater->set($user_id, $field);
                /*
                 * 
                 * Force empty values!
                 */
                $wpcf->usermeta_repeater->cf['value'] = null;
                $wpcf->usermeta_repeater->meta = null;
                $form = $wpcf->usermeta_repeater->get_field_form(null, true);
                echo json_encode(array('output' => wpcf_form_simple($form) . wpcf_form_render_js_validation('#your-profile', false)));
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'um_repetitive_delete':
            if (isset($_POST['user_id']) && isset($_POST['field_id']) && current_user_can('edit_user', intval($_POST['user_id']))) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $user_id = intval($_POST['user_id']);
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_POST['field_id']), false, false, false, 'wpcf-usermeta');
                $meta_id = intval($_POST['meta_id']);
                if (!empty($field) && !empty($user_id) && !empty($meta_id)) {
                    /*
                     * 
                     * 
                     * Changed.
                     * Since Types 1.2
                     */
                    global $wpcf;
                    $wpcf->usermeta_repeater->set($user_id, $field);
                    $wpcf->usermeta_repeater->delete($meta_id);
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field or post not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
            /* End Usermeta */
        /* End Usermeta */
        case 'repetitive_add':
            if (current_user_can('edit_posts') && isset($_GET['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_GET['field_id']));
                $parent_post_id = intval($_GET['post_id']);
                /*
                 * When post is new - post_id is 0
                 * We can safely set post_id to 1 cause
                 * values compared are filtered anyway.
                 */
                if ($parent_post_id == 0) {
                    $parent_post_id = 1;
                }
                $parent_post = get_post($parent_post_id);
                global $wpcf;
                $wpcf->repeater->set($parent_post, $field);
                /*
                 * 
                 * Force empty values!
                 */
                $wpcf->repeater->cf['value'] = null;
                $wpcf->repeater->meta = null;
                $form = $wpcf->repeater->get_field_form(null, true);
                echo json_encode(array('output' => wpcf_form_simple($form) . wpcf_form_render_js_validation('#post', false)));
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'repetitive_delete':
            if (current_user_can('edit_posts') && isset($_POST['post_id']) && isset($_POST['field_id'])) {
                require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
                $post_id = intval($_POST['post_id']);
                $parent_post = get_post($post_id);
                $field = wpcf_admin_fields_get_field(sanitize_text_field($_POST['field_id']));
                $meta_id = intval($_POST['meta_id']);
                if (!empty($field) && !empty($parent_post->ID) && !empty($meta_id)) {
                    /*
                     * 
                     * 
                     * Changed.
                     * Since Types 1.2
                     */
                    global $wpcf;
                    $wpcf->repeater->set($parent_post, $field);
                    $wpcf->repeater->delete($meta_id);
                    echo json_encode(array('output' => 'deleted'));
                } else {
                    echo json_encode(array('output' => 'field or post not found'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'wpcf_entry_search':
            if (current_user_can('edit_posts') && isset($_REQUEST['post_type'])) {
                $posts_per_page = apply_filters('wpcf_pr_belongs_post_numberposts', 10);
                $args = array('posts_per_page' => apply_filters('wpcf_pr_belongs_post_posts_per_page', $posts_per_page), 'post_status' => apply_filters('wpcf_pr_belongs_post_status', array('publish', 'private')), 'post_type' => sanitize_text_field($_REQUEST['post_type']), 'suppress_filters' => 1);
                if (isset($_REQUEST['s'])) {
                    $args['s'] = $_REQUEST['s'];
                }
                if (isset($_REQUEST['page']) && preg_match('/^\\d+$/', $_REQUEST['page'])) {
                    $args['paged'] = intval($_REQUEST['page']);
                }
                $the_query = new WP_Query($args);
                $posts = array('items' => array(), 'total_count' => $the_query->found_posts, 'incomplete_results' => $the_query->found_posts > $posts_per_page, 'posts_per_page' => $posts_per_page);
                if ($the_query->have_posts()) {
                    while ($the_query->have_posts()) {
                        $the_query->the_post();
                        $post_title = get_the_title();
                        if (empty($post_title)) {
                            $post_title = sprintf(__('[empty title] ID: %d', 'wpcf'), get_the_ID());
                        }
                        $posts['items'][] = array('ID' => get_the_ID(), 'post_title' => $post_title);
                    }
                }
                /* Restore original Post Data */
                wp_reset_postdata();
                echo json_encode($posts);
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        case 'wpcf_entry_entry':
            if (current_user_can('edit_posts') && isset($_REQUEST['p'])) {
                $wpcf_post = get_post((int) $_REQUEST['p'], ARRAY_A);
                if (isset($wpcf_post['ID'])) {
                    $post_title = $wpcf_post['post_title'];
                    if (empty($post_title)) {
                        $post_title = sprintf(__('[empty title] ID: %d', 'wpcf'), $wpcf_post['ID']);
                    }
                    echo json_encode(array('ID' => $wpcf_post['ID'], 'post_title' => $wpcf_post['post_title']));
                } else {
                    echo json_encode(array('output' => 'params missing'));
                }
            } else {
                echo json_encode(array('output' => 'params missing'));
            }
            break;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
/**
 * Meta boxes contents output.
 *
 * @param type $post
 * @param type $args
 */
function wpcf_pr_admin_post_meta_box_output($post, $args)
{
    if (empty($post) || empty($post->ID)) {
        return array();
    }
    global $wpcf;
    $output = '';
    $relationships = $args;
    $post_id = !empty($post->ID) ? $post->ID : -1;
    $current_post_type = wpcf_admin_get_edited_post_type($post);
    /*
     * Render has form (child form)
     */
    if (!empty($relationships['has'])) {
        foreach ($relationships['has'] as $post_type => $data) {
            if (isset($data['fields_setting']) && 'only_list' == $data['fields_setting']) {
                $output .= $wpcf->relationship->child_list($post, $post_type, $data);
            } else {
                $output .= $wpcf->relationship->child_meta_form($post, $post_type, $data);
            }
        }
    }
    /*
     * Render belongs form (parent form)
     */
    if (!empty($relationships['belongs'])) {
        $meta = get_post_custom($post_id);
        $belongs = array('belongs' => array(), 'posts' => array());
        foreach ($meta as $meta_key => $meta_value) {
            if (strpos($meta_key, '_wpcf_belongs_') === 0) {
                $temp_post = get_post($meta_value[0]);
                if (!empty($temp_post)) {
                    $belongs['posts'][$temp_post->ID] = $temp_post;
                    $belongs['belongs'][$temp_post->post_type] = $temp_post->ID;
                }
            }
        }
        foreach ($relationships['belongs'] as $post_type => $data) {
            $parent_post_type_object = get_post_type_object($post_type);
            $output .= '<div class="belongs">';
            $form = wpcf_pr_admin_post_meta_box_belongs_form($post, $post_type, $belongs);
            if (isset($form[$post_type])) {
                $form[$post_type]['#before'] = sprintf('<p>%s %s</p>', sprintf(__('This <em>%s</em> belongs to <em>%s</em>', 'wpcf'), get_post_type_object($current_post_type)->labels->singular_name, $parent_post_type_object->labels->singular_name), sprintf(' <a href="%s" class="button disabled">%s</a>', get_edit_post_link($form[$post_type]['#value']), $parent_post_type_object->labels->edit_item));
            }
            if ($x = wpcf_form_simple($form)) {
                $output .= $x;
            } else {
                $output .= $parent_post_type_object->labels->not_found;
            }
            $output .= '</div>';
            unset($parent_post_type_object);
        }
    }
    return $output;
}
Ejemplo n.º 22
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();
}
Ejemplo n.º 23
0
/**
 * Menu page display.
 */
function wpcf_admin_menu_import_export()
{
    echo wpcf_add_admin_header(__('Import/Export', 'wpcf'));
    echo '<br /><form method="post" action="" class="wpcf-import-export-form ' . 'wpcf-form-validate" enctype="multipart/form-data">';
    echo wpcf_form_simple(wpcf_admin_import_export_form());
    echo '</form>';
    echo wpcf_add_admin_footer();
}
Ejemplo n.º 24
0
/**
 * Renders meta box content.
 *
 * Core function. Works and stable.
 * If required, add hooks only.
 *
 * @todo Revise this 1.1.5
 *
 * @param type $post
 * @param type $group
 * @param type $echo
 * @param type boolean $open_style_editor if true use code for open style editor when edit group
 */
function wpcf_admin_post_meta_box($post, $group, $echo = '', $open_style_editor = false)
{
    if (false === $open_style_editor && defined('WPTOOLSET_FORMS_VERSION')) {
        if (isset($group['args']['html'])) {
            /**
             * show group description
             */
            if (array_key_exists('description', $group['args']) && !empty($group['args']['description'])) {
                echo '<div class="wpcf-meta-box-description">';
                echo wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description', $group['args']['description']));
                echo '</div>';
            }
            foreach ($group['args']['html'] as $field) {
                echo is_array($field) ? wptoolset_form_field('post', $field['config'], $field['meta']) : $field;
            }
        }
        return;
    }
    global $wpcf;
    /**
     * fake post object if need
     */
    $post = wpcf_admin_create_fake_post_if_need($post);
    static $nonce_added = false;
    $group_output = '';
    if (!isset($group['title'])) {
        $temp = $group;
        $group = '';
        $group['args'] = $temp;
        $group['id'] = $temp['slug'];
        $group['title'] = $temp['name'];
        $name = $temp['name'];
    }
    if (!empty($echo)) {
        $group_output = '<h3>This Preview generated for latest post "' . $post->post_title . '"</h3>' . "\n" . '<!-- Previous lines visible only in Admin Style Editor.-->' . "\n\n";
        $group_output .= '<div id="wpcf-group-' . $group['id'] . '" class="postbox " >
            <h3 class=\'hndle\'><span>' . $name . '</span></h3>
            <div class="inside">' . "\n";
    }
    /*
     * TODO Document where this is used
     */
    if (!$nonce_added && empty($echo)) {
        $nonce_action = 'update-' . $post->post_type . '_' . $post->ID;
        wp_nonce_field($nonce_action, '_wpcf_post_wpnonce');
        $nonce_added = true;
    }
    $group_output .= "\n\n" . '<div id="wpcf-group-metabox-id-' . $group['args']['slug'] . '">' . "\n";
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        if ($group['args']['_conditional_display'] == 'failed') {
            $group_output .= '<div class="wpcf-cd-group wpcf-cd-group-failed" style="display:none;">';
        } else {
            $group_output .= '<div class="wpcf-cd-group wpcf-cd-group-passed">';
        }
    }
    /*
     * TODO Move this into Field code
     * Process fields
     */
    if (!empty($group['args']['fields'])) {
        // Display description
        if (!empty($group['args']['description'])) {
            $group_output .= '<div class="wpcf-meta-box-description">' . wpautop(wpcf_translate('group ' . $group['args']['id'] . ' description', $group['args']['description'])) . '</div>';
        }
        foreach ($group['args']['fields'] as $field_slug => $field) {
            if (empty($field) || !is_array($field)) {
                continue;
            }
            $field = $wpcf->field->_parse_cf_form_element($field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                $field = apply_filters('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form_value_display', $field);
            }
            if (!isset($field['#id'])) {
                $field['#id'] = wpcf_unique_id(serialize($field));
            }
            // Render form elements
            if (wpcf_compare_wp_version() && array_key_exists('#type', $field) && 'wysiwyg' == $field['#type'] && !isset($field['#attributes']['disabled'])) {
                //                if ( isset( $field['#attributes']['disabled'] ) ) {
                //                    $field['#editor_settings']['tinymce'] = false;
                //                    $field['#editor_settings']['teeny'] = false;
                //                    $field['#editor_settings']['media_buttons'] = false;
                //                    $field['#editor_settings']['quicktags'] = false;
                //                    $field['#editor_settings']['dfw'] = false;
                //                }
                // Especially for WYSIWYG
                $group_output .= '<div class="wpcf-wysiwyg">';
                $group_output .= '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">';
                $group_output .= isset($field['#before']) ? $field['#before'] : '';
                $group_output .= '<label class="wpcf-form-label wpcf-form-textarea-label">' . stripslashes($field['#title']) . '</label>';
                $group_output .= '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">
                    ' . wpautop($field['#description']) . '</div>';
                ob_start();
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $group_output .= ob_get_clean() . "\n\n";
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                $group_output .= '</div>';
                $group_output .= isset($field['#after']) ? $field['#after'] : '';
                $group_output .= '</div>';
            } else {
                if (array_key_exists('#type', $field) && 'wysiwyg' == $field['#type']) {
                    $field['#type'] = 'textarea';
                }
                if (!empty($echo)) {
                    $field['#validate'] = '';
                }
                $group_output .= wpcf_form_simple(array($field['#id'] => $field));
            }
            do_action('wpcf_fields_' . $field_slug . '_meta_box_form', $field);
            if (isset($field['wpcf-type'])) {
                // May be ignored
                do_action('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form', $field);
            }
        }
    }
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['args']['_conditional_display'])) {
        $group_output .= '</div>';
    }
    $group_output .= '</div>';
    if (!empty($echo)) {
        $group_output .= "\n\n</div></div>";
        return $group_output;
    } else {
        echo $group_output;
    }
}
Ejemplo n.º 25
0
 /**
  * Content for choose parent column.
  *
  * @return boolean
  */
 function _parent_form($post_parent = '')
 {
     $item_parents = wpcf_pr_admin_get_belongs($this->child_post_type);
     if ($item_parents) {
         foreach ($item_parents as $parent => $temp_data) {
             // Skip if only current available
             if ($parent == $this->parent_post_type) {
                 continue;
             }
             if (!empty($post_parent) && $parent != $post_parent) {
                 continue;
             }
             // Get parent ID
             $meta = get_post_meta($this->child->ID, '_wpcf_belongs_' . $parent . '_id', true);
             $meta = empty($meta) ? 0 : $meta;
             // Get form
             $belongs_data = array('belongs' => array($parent => $meta));
             $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($this->child, $parent, $belongs_data);
             if (empty($temp_form)) {
                 return '<span class="types-small-italic">' . __('No parents available', 'wpcf') . '</span>';
             }
             unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']);
             $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $this->parent->ID . '][' . $this->child->ID . '][parents][' . $parent . ']';
             // Return HTML formatted output
             return wpcf_form_simple($temp_form);
         }
     }
     return '<span class="types-small-italic">' . __('No parents available', 'wpcf') . '</span>';
 }
Ejemplo n.º 26
0
/**
 * 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;
}
 /**
  * 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 boolean $return_form Description.
  * @return type Description.
  */
 public function group_condition_get($return_form = false)
 {
     if (!isset($_REQUEST['group_id']) && isset($_REQUEST['id'])) {
         $_REQUEST['group_id'] = $_REQUEST['id'];
     }
     /**
      * check nonce
      */
     if (!(isset($_REQUEST['id']) && $return_form === true) && !(isset($_REQUEST['group_id']) && isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'wpcf-conditional-get-' . $_REQUEST['group_id']))) {
         $this->verification_failed_and_die();
     }
     /**
      * get group definition
      */
     $group = wpcf_admin_fields_get_group($_REQUEST['group_id']);
     if (empty($group)) {
         __('Wrong group.', 'wpcf');
         die;
     }
     $group['meta_type'] = 'custom-fields-group';
     $group['data'] = array('conditional_display' => get_post_meta($group['id'], '_wpcf_conditional_display', true));
     /**
      * define conditional
      */
     require_once WPCF_INC_ABSPATH . '/classes/class.types.fields.conditional.php';
     new Types_Fields_Conditional();
     /**
      * get form data
      */
     /**
      * Summary.
      *
      * Description.
      *
      * @since x.x.x
      *
      * @param type  $var Description.
      * @param array $args {
      *     Short description about this hash.
      *
      *     @type type $var Description.
      *     @type type $var Description.
      * }
      * @param type  $var Description.
      */
     $form = $this->get_field_conditionals(array(), $group);
     if (empty($form)) {
         __('Wrong group.', 'wpcf');
         die;
     }
     /**
      * return form
      */
     if ($return_form) {
         return $form;
     }
     /**
      * produce form
      */
     echo wpcf_form_simple($form);
     die;
 }
Ejemplo n.º 28
0
/**
 * 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)) . '&nbsp;' . '<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;
}
Ejemplo n.º 29
0
function wpcf_admin_render_fields($group, $user_id, $echo = '')
{
    global $wpcf;
    $output = '<div class="wpcf-group-area wpcf-group-area_' . $group['slug'] . '">' . "\n\n";
    $output .= '<h3>' . wpcf_translate('group ' . $group['id'] . ' name', $group['name']) . '</h3>' . "\n\n";
    if (!empty($group['fields'])) {
        // Display description
        if (!empty($group['description'])) {
            $output .= '<span>' . wpautop(wpcf_translate('group ' . $group['id'] . ' description', $group['description'])) . '</span>' . "\n\n";
        }
        $output .= '<div class="wpcf-profile-field-line">' . "\n\n";
        foreach ($group['fields'] as $field_slug => $field) {
            if (empty($field) || !is_array($field)) {
                continue;
            }
            $field = $wpcf->usermeta_field->_parse_cf_form_element($field);
            if (!isset($field['#id'])) {
                $field['#id'] = wpcf_unique_id(serialize($field));
            }
            if (isset($field['wpcf-type'])) {
                // May be ignored
                $field = apply_filters('wpcf_fields_' . $field['wpcf-type'] . '_meta_box_form_value_display', $field);
            }
            // Render form elements
            if (wpcf_compare_wp_version() && $field['#type'] == 'wysiwyg') {
                $field['#editor_settings']['media_buttons'] = '';
                if (!empty($echo)) {
                    $field['#editor_settings']['wpautop'] = true;
                }
                // Especially for WYSIWYG
                $output .= "\n" . '<div class="wpcf-profile-field-line">' . "\n\n";
                $output .= '<div class="wpcf-wysiwyg">' . "\n\n";
                $output .= '<div id="wpcf-textarea-textarea-wrapper" class="form-item form-item-textarea wpcf-form-item wpcf-form-item-textarea">' . "\n\n";
                $output .= isset($field['#before']) ? $field['#before'] : '';
                $output .= '<label class="wpcf-form-label wpcf-form-textarea-label">' . $field['#title'] . '</label>' . "\n\n";
                $output .= '<div class="description wpcf-form-description wpcf-form-description-textarea description-textarea">' . "\n\n" . wpautop($field['#description']) . '</div>' . "\n\n";
                ob_start();
                wp_editor($field['#value'], $field['#id'], $field['#editor_settings']);
                $output .= ob_get_clean() . "\n\n";
                $field['slug'] = str_replace(WPCF_META_PREFIX . 'wysiwyg-', '', $field_slug);
                $field['type'] = 'wysiwyg';
                $output .= '</div>' . "\n\n";
                $output .= isset($field['#after']) ? $field['#after'] : '';
                $output .= '</div>' . "\n\n";
                $output .= '</div>' . "\n\n";
            } else {
                if ($field['#type'] == 'wysiwyg') {
                    $field['#type'] = 'textarea';
                }
                $field['#pattern'] = "\n" . '<div class="wpcf-profile-field-line">
	<div class="wpcf-profile-line-left">
		<LABEL><DESCRIPTION>
	</div>
	<div class="wpcf-profile-line-right"><BEFORE><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER></div>
</div>' . "\n\n";
                if (isset($field['#name']) && (strpos($field['#name'], '[hour]') !== false || strpos($field['#name'], '[minute]') !== false)) {
                    if (isset($field['#attributes']) && $field['#attributes']['class'] == 'wpcf-repetitive') {
                        $field['#pattern'] = strpos($field['#name'], '[hour]') !== false ? __('Hour', 'wpcf') : __('Minute', 'wpcf');
                        $field['#pattern'] .= '<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER>' . "\n\n";
                    } else {
                        if (strpos($field['#name'], '[hour]') !== false) {
                            $field['#pattern'] = "\n" . '<div class="wpcf-profile-field-line">
	<div class="wpcf-profile-line-left">&nbsp;&nbsp;&nbsp;&nbsp;' . __('Time', 'wpcf') . '</div>
	<div class="wpcf-profile-line-right">
	<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER>' . "\n";
                        } else {
                            $field['#pattern'] = "\n" . '
	<LABEL><DESCRIPTION><ERROR><PREFIX><ELEMENT><SUFFIX><AFTER></div>
</div>' . "\n\n";
                        }
                    }
                }
                if (!empty($echo)) {
                    $field['#validate'] = '';
                }
                $output .= wpcf_form_simple(array($field['#id'] => $field));
            }
        }
        $output .= '</div>';
    }
    /*
     * TODO Move to Conditional code
     *
     * This is already checked. Use hook to add wrapper DIVS and apply CSS.
     */
    if (!empty($group['_conditional_display'])) {
        $output .= '</div>';
    }
    $output .= "\n\n" . '</div>';
    if (!empty($echo)) {
        return $output;
    } else {
        echo $output;
    }
}
Ejemplo n.º 30
-1
 /**
  * Renders Thickbox content.
  *
  * Field should provide callback function
  * that will be called automatically.
  *
  * Function should be named like:
  * 'wpcf_fields_' . $field_type . '_editor_callback'
  * e.g. 'wpcf_fields_checkbox__editor_callback'
  *
  * Function should return array with elements:
  * 'supports' - parameters or other feature supported, e.g. 'styling' will
  *     enable 'Styling' options
  *
  * Tabs is array with elements:
  * 'menu_title' - used for menu title
  * 'title' - used for main title
  * 'content' - HTML content of tab
  *
  * @param type $field
  * @param type $meta_type
  * @param type $post_id
  * @param string $shortcode
  */
 function frame($field, $meta_type = 'postmeta', $post_id = -1, $shortcode = null, $callback = false, $views_usermeta = false)
 {
     global $wp_version, $wpcf;
     // Queue rendering JS settings
     add_action('admin_print_footer_scripts', array($this, 'renderTedSettings'), 1);
     wp_enqueue_script('types');
     wp_enqueue_script('types-knockout');
     wp_enqueue_script('types-editor');
     wp_enqueue_script('wp-pointer');
     wp_enqueue_style('types-editor');
     wp_enqueue_style('wp-pointer');
     wp_enqueue_style('toolset-font-awesome');
     // Load cloned WP Media Modal CSS
     if (version_compare($wp_version, '3.5', '<')) {
         wp_enqueue_style('types-editor-cloned');
     }
     $this->field = $field;
     $this->_meta_type = $meta_type;
     $this->_post = get_post($post_id);
     $this->_settings = is_null($shortcode) ? array() : $this->shortcodeToParameters($shortcode);
     $this->callback = $callback;
     $this->_data = array('meta_type' => $meta_type, 'field' => $field, 'field_type_data' => WPCF_Fields::getFieldTypeData($field['type']), 'settings' => array(), 'tabs' => array(), 'supports' => array(), 'post' => $this->_post, 'post_types' => get_post_types(array('show_ui' => true)), 'style' => isset($this->_settings['style']) ? $this->_settings['style'] : '', 'class' => isset($this->_settings['class']) ? $this->_settings['class'] : '', 'output' => 'html', 'user_form' => '');
     // Set title if updated
     if (!is_null($shortcode)) {
         $this->_data['title'] = sprintf(__('Update %s', 'wpcf'), $this->_data['field_type_data']['title']);
         $this->_data['submit_button_title'] = __('Update shortcode', 'wpcf');
     }
     // Exclude post types
     foreach ($wpcf->excluded_post_types as $_post_type) {
         unset($this->_data['post_types'][$_post_type]);
     }
     /*
      * Callback
      */
     $function = 'wpcf_fields_' . $field['type'] . '_editor_callback';
     if (function_exists($function)) {
         // Main callback
         $callback = call_user_func($function, $field, $this->_settings, $this->_meta_type, $this->_post);
         // Add supports
         if (!empty($callback['supports']) && is_array($callback['supports'])) {
             $this->_data['supports'] = $callback['supports'];
         }
         // Add tabs
         if (!empty($callback['tabs']) && is_array($callback['tabs'])) {
             $this->_data['tabs'] = $callback['tabs'];
         }
         // Unify settings
         if (!empty($callback['settings']) && is_array($callback['settings'])) {
             $this->_settings = array_merge($this->_settings, self::sanitizeParams($callback['settings'], 'array'));
         }
     }
     // If no tabs
     if (empty($this->_data['tabs'])) {
         $this->_data['tabs']['display'] = array('menu_title' => __('Display', 'wpcf'), 'title' => __('Display', 'wpcf'), 'content' => sprintf(__('There are no additional display options for the %s field.', 'wpcf'), $this->_data['field_type_data']['title']));
     }
     // Add User ID form
     if ($this->_meta_type == 'usermeta') {
         if (!$views_usermeta) {
             $this->_data['user_form'] = wpcf_form_simple(wpcf_get_usermeta_form_addon($this->_settings));
             $this->_data['supports'][] = 'user_id';
         }
     } else {
         // Add Post ID form
         $this->_data['supports'][] = 'post_id';
     }
     // Get parents
     if (!empty($this->_post->ID)) {
         $this->_data['parents'] = WPCF_Relationship::get_parents($this->_post);
     }
     // Set icons
     $icons = array('audio' => 'icon-music', 'checkbox' => 'icon-check', 'checkboxes' => 'icon-checkboxes', 'colorpicker' => 'icon-tint', 'date' => 'icon-calendar', 'email' => 'icon-envelope-alt', 'embed' => 'icon-youtube-play', 'file' => 'icon-file-alt', 'image' => 'icon-picture', 'map' => 'icon-map-marker', 'numeric' => 'icon-numeric', 'phone' => 'icon-phone', 'radio' => 'icon-radio-button', 'select' => 'icon-select-box', 'skype' => 'icon-skype', 'textarea' => 'icon-text-area', 'textfield' => 'icon-text-field', 'url' => 'icon-link', 'video' => 'icon-film', 'wysiwyg' => 'icon-wysiwyg');
     $this->_data['icon_class'] = isset($icons[$field['type']]) ? $icons[$field['type']] : 'icon-text-field';
     // Is repetitive
     $this->_data['is_repetitive'] = (bool) types_is_repetitive($field);
     if ($this->_data['is_repetitive']) {
         $this->_data['supports'][] = 'separator';
     }
     // Render header
     wpcf_admin_ajax_head();
     // Check if submitted
     $this->_thickbox_check_submit();
     // Render form
     echo '<form method="post" action="" id="types-editor-modal-form">';
     echo WPCF_Loader::view('editor-modal-window', $this->_data);
     wp_nonce_field('types_editor_frame', '__types_editor_nonce');
     echo '</form>';
     // Render footer
     wpcf_admin_ajax_footer();
 }