Exemplo n.º 1
0
/**
 * All AJAX calls go here.
 */
function wpcf_ajax_embedded()
{
    if (!current_user_can('manage_options') || (!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)) {
                // TODO Remove
                //                $file = WPCF_EMBEDDED_INC_ABSPATH . '/fields/' . $field['type'] . '.php';
                //                if (file_exists($file)) {
                //                    require_once $file;
                $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;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
Exemplo 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 (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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
/**
 * All AJAX calls go here.
 */
function wpcf_ajax_embedded()
{
    if (!current_user_can('manage_options') || (!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)) {
                // TODO Remove
                //                $file = WPCF_EMBEDDED_INC_ABSPATH . '/fields/' . $field['type'] . '.php';
                //                if (file_exists($file)) {
                //                    require_once $file;
                $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':
            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_child_item($post, $post_type,
                //                        $_POST);
                $output = wpcf_pr_admin_save_post_hook($_GET['post_id']);
            }
            echo json_encode(array('output' => $output));
            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;
        default:
            break;
    }
    if (function_exists('wpcf_ajax')) {
        wpcf_ajax();
    }
    die;
}
Exemplo n.º 5
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;
}
Exemplo n.º 6
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;
}