/**
 * Init function.
 * 
 * Enqueues styles and scripts on post edit page.
 * 
 * @param type $post_type
 * @param type $post
 * @param type $groups
 * @param type $wpcf_active 
 */
function wpcf_pr_admin_post_init_action($post_type, $post, $groups, $wpcf_active)
{
    // See if any data
    $has = wpcf_pr_admin_get_has($post_type);
    $belongs = wpcf_pr_admin_get_belongs($post_type);
    /*
     * 
     * Enqueue styles and scripts
     */
    if (!empty($has) || !empty($belongs)) {
        add_action('admin_head', 'wpcf_pr_add_field_js');
        $output = wpcf_pr_admin_post_meta_box_output($post, array('post_type' => $post_type, 'has' => $has, 'belongs' => $belongs));
        add_meta_box('wpcf-post-relationship', __('Fields table', 'wpcf'), 'wpcf_pr_admin_post_meta_box', $post_type, 'normal', 'default', array('output' => $output));
        if (!empty($output)) {
            wp_enqueue_script('wpcf-post-relationship', WPCF_EMBEDDED_RELPATH . '/resources/js/post-relationship.js', array('jquery'), WPCF_VERSION);
            wp_enqueue_style('wpcf-post-relationship', WPCF_EMBEDDED_RELPATH . '/resources/css/post-relationship.css', array(), WPCF_VERSION);
            if (!$wpcf_active) {
                wpcf_enqueue_scripts();
                wp_enqueue_style('wpcf-pr-post', WPCF_EMBEDDED_RES_RELPATH . '/css/fields-post.css', array(), WPCF_VERSION);
                wp_enqueue_script('wpcf-form-validation', WPCF_EMBEDDED_RES_RELPATH . '/js/' . 'jquery-form-validation/jquery.validate.min.js', array('jquery'), WPCF_VERSION);
                wp_enqueue_script('wpcf-form-validation-additional', WPCF_EMBEDDED_RES_RELPATH . '/js/' . 'jquery-form-validation/additional-methods.min.js', array('jquery'), WPCF_VERSION);
            }
            wpcf_admin_add_js_settings('wpcf_pr_del_warning', '\'' . __('Are you sure about deleting this post?', 'wpcf') . '\'');
            wpcf_admin_add_js_settings('wpcf_pr_pagination_warning', '\'' . __('If you continue without saving your changes, it might get lost.', 'wpcf') . '\'');
        }
    }
}
function wpcf_admin_notice_post_locked_no_parent()
{
    if (!($post = get_post())) {
        return;
    }
    $parent_type = wpcf_pr_admin_get_belongs($post->post_type);
    if (is_array($parent_type) && count($parent_type)) {
        $parent_type = array_shift(array_keys($parent_type));
        $parent_type = get_post_type_object($parent_type);
    } else {
        return;
    }
    if (($sendback = wp_get_referer()) && false === strpos($sendback, 'post.php') && false === strpos($sendback, 'post-new.php')) {
        $sendback_text = __('Go back', 'wpcf');
    } else {
        $sendback = admin_url('edit.php');
        if ('post' != $post->post_type) {
            $sendback = esc_url(add_query_arg('post_type', $post->post_type, $sendback));
        }
        $sendback_text = get_post_type_object($post->post_type)->labels->all_items;
    }
    ?>
<div id="post-lock-dialog" class="notification-dialog-wrap">
    <div class="notification-dialog-background"></div>
        <div class="notification-dialog">
            <div class="post-locked-message">
                <p>
<?php 
    if ('auto-draft' == $post->post_status) {
        printf(__('You will be able to add child posts after saving at least one <b>%s</b>.', 'wpcf'), $parent_type->labels->singular_name);
    } else {
        printf(__('You will be able to edit child posts after saving at least one <b>%s</b>.', 'wpcf'), $parent_type->labels->singular_name);
    }
    ?>
                </p>
                <p><a class="button button-primary wp-tab-last" href="<?php 
    echo $sendback;
    ?>
"><?php 
    echo $sendback_text;
    ?>
</a></p>
            </div>
        </div>
    </div>
</div>
<?php 
}
Exemple #3
0
/**
 * Gets post type with data to which belongs.
 * 
 * @param type $post_type
 * @return type 
 */
function wpcf_pr_get_belongs($post_type)
{
    require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
    return wpcf_pr_admin_get_belongs($post_type);
}
Exemple #4
0
 /**
  * Gets all parents per post type.
  *
  * @param type $child
  * @return type
  */
 public static function get_parents($child)
 {
     $parents = array();
     $item_parents = wpcf_pr_admin_get_belongs($child->post_type);
     if ($item_parents) {
         foreach ($item_parents as $post_type => $data) {
             // Get parent ID
             $meta = wpcf_get_post_meta($child->ID, '_wpcf_belongs_' . $post_type . '_id', true);
             if (!empty($meta)) {
                 $parent_post = get_post($meta);
                 if (!empty($parent_post)) {
                     $parents[$parent_post->post_type] = $parent_post;
                 }
             }
         }
     }
     return $parents;
 }
function wpcf_admin_metabox_relationship($post_type)
{
    $form = array();
    $form['table-pr-open'] = wpcf_admin_metabox_begin(__('Post Relationship', 'wpcf'), 'relationship', 'wpcf-types-form-pr-table', false);
    /**
     * belongs/children section
     */
    $has = wpcf_pr_admin_get_has($post_type['slug']);
    $belongs = wpcf_pr_admin_get_belongs($post_type['slug']);
    $post_types = get_post_types('', 'objects');
    if (empty($has) || empty($post_type['slug'])) {
        $txt_has = __("Children: None", 'wpcf');
    } else {
        $txt_has = array();
        foreach ($has as $pr_key => $pr_data) {
            $txt_has[] = isset($post_types[$pr_key]) ? $post_types[$pr_key]->labels->singular_name : $pr_key;
        }
        $txt_has = sprintf(__("Children: %s", 'wpcf'), implode(', ', $txt_has));
    }
    if (!empty($belongs)) {
        $txt_belongs = array();
        foreach ($belongs as $pr_key => $pr_data) {
            $txt_belongs[] = isset($post_types[$pr_key]) ? $post_types[$pr_key]->labels->singular_name : $pr_key;
        }
        $txt_belongs = sprintf(__("Parent: %s", 'wpcf'), implode(', ', $txt_belongs));
    } else {
        $txt_belongs = __("Parent: None", 'wpcf');
    }
    // Others belonging to
    if (!empty($belongs)) {
        $txt_belongs = array();
        foreach ($belongs as $pr_key => $pr_data) {
            $txt_belongs[] = isset($post_types[$pr_key]) ? $post_types[$pr_key]->labels->singular_name : $pr_key;
        }
        $txt_belongs = sprintf(__("Parent: %s", 'wpcf'), implode(', ', $txt_belongs));
    } else {
        $txt_belongs = __("Parent: None", 'wpcf');
    }
    $form['table-pr-belongs'] = array('#type' => 'markup', '#markup' => '<div style="margin: 10px 0 10px 0;"><span class="wpcf-pr-belongs-summary">' . $txt_belongs . '</span>&nbsp;' . '<a href="javascript:void(0);" id="wpcf-pr-belongs-edit" class="button-secondary wpcf-pr-edit">' . __('Edit', 'wpcf') . '</a>');
    $options = array();
    /**
     * build excluded post types
     */
    global $wpcf;
    $excluded_post_types = $wpcf->excluded_post_types;
    $excluded_post_types[] = $post_type['slug'];
    foreach ($post_types as $temp_post_type_slug => $temp_post_type) {
        if (in_array($temp_post_type_slug, $excluded_post_types) || !$temp_post_type->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true)) {
            continue;
        }
        // Check if it's in has
        if (isset($has[$temp_post_type_slug])) {
            continue;
        }
        $options[$temp_post_type_slug]['#name'] = 'ct[post_relationship][belongs][' . $temp_post_type_slug . ']';
        $options[$temp_post_type_slug]['#title'] = $temp_post_type->labels->singular_name;
        $options[$temp_post_type_slug]['#default_value'] = isset($belongs[$temp_post_type_slug]);
        $options[$temp_post_type_slug]['#inline'] = true;
        $options[$temp_post_type_slug]['#after'] = '&nbsp;&nbsp;';
        if (is_rtl()) {
            $options[$temp_post_type_slug]['#before'] = '<div style="float:right;margin-left:10px;">';
            $options[$temp_post_type_slug]['#after'] .= '</div>';
        }
    }
    $form['table-pr-has-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#before' => '<div style="display:none; margin: 10px 0 20px 0;">', '#after' => '<br /><br /><a href="javascript:void(0);" class="button-primary wpcf-pr-belongs-apply">' . __('Apply', 'wpcf') . '</a>&nbsp;<a href="javascript:void(0);" class="button-secondary wpcf-pr-belongs-cancel">' . __('Cancel', 'wpcf') . '</a></div></div>', '#inline' => true);
    $form['table-pr-has'] = array('#type' => 'markup', '#markup' => '<div style="margin: 10px 0 5px 0;"><span class="wpcf-pr-has-summary">' . $txt_has . '</span>&nbsp;' . '<a href="javascript:void(0);" id="wpcf-pr-has-edit" class="button-secondary wpcf-pr-edit">' . __('Edit', 'wpcf') . '</a>');
    $options = array();
    foreach ($post_types as $temp_post_type_slug => $temp_post_type) {
        if (in_array($temp_post_type_slug, $excluded_post_types) || !$temp_post_type->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true)) {
            continue;
        }
        // Check if it already belongs
        if (isset($belongs[$temp_post_type_slug])) {
            continue;
        }
        $options[$temp_post_type_slug]['#name'] = 'ct[post_relationship][has][' . $temp_post_type_slug . ']';
        $options[$temp_post_type_slug]['#title'] = $temp_post_type->labels->singular_name;
        $options[$temp_post_type_slug]['#default_value'] = isset($has[$temp_post_type_slug]);
        $options[$temp_post_type_slug]['#inline'] = true;
        $options[$temp_post_type_slug]['#after'] = isset($has[$temp_post_type_slug]) ? '' . '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&wpcf_action=pt_edit_fields&child=' . $temp_post_type_slug . '&parent=' . $post_type['slug'] . '&_wpnonce=' . wp_create_nonce('pt_edit_fields') . '&KeepThis=true&TB_iframe=true') . '" class="thickbox" title="' . __('Select child fields to be displayed', 'wpcf') . '">(' . __('Edit fields', 'wpcf') . ')</a>&nbsp;&nbsp;' : '' . '<a href="javascript:void(0);" style="color:Gray;" title="' . __('Please save the page first, before you can edit the child items', 'wpcf') . '">(' . __('Edit fields', 'wpcf') . ')</a>&nbsp;&nbsp;';
        if (is_rtl()) {
            $options[$temp_post_type_slug]['#before'] = '<div style="float:right;margin-left:10px;">';
            $options[$temp_post_type_slug]['#after'] .= '</div>';
        }
    }
    $form['table-pr-belongs-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#before' => '<div style="display:none; margin: 10px 0 20px 0;">', '#after' => '<br /><br /><a href="javascript:void(0);" class="button-primary wpcf-pr-has-apply">' . __('Apply', 'wpcf') . '</a>&nbsp;<a href="javascript:void(0);" class="button-secondary wpcf-pr-has-cancel">' . __('Cancel', 'wpcf') . '</a></div></div>', '#inline' => true);
    $form['table-pr-explanation'] = array('#type' => 'markup', '#markup' => '<p>' . __("You can choose which fields will show when editing parent pages.", 'wpcf') . '<br />' . __("Click on the 'edit' button to select them for each parent.", 'wpcf') . '<br />' . sprintf(__('Learn about %sPost Type Relationships%s', 'wpcf'), '<a href="http://wp-types.com/documentation/user-guides/creating-post-type-relationships/" target="_blank">', ' &raquo;</a>') . '</p>');
    /**
     * close form
     */
    $form['table-pr-close'] = wpcf_admin_metabox_end();
    /**
     * additional settings
     */
    wpcf_admin_add_js_settings('wpcf_pr_has_empty_txt', '\'' . __("Children: None", 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_pr_has_txt', '\'' . __("Children: %s", 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_pr_belongs_empty_txt', '\'' . __("Parent: None", 'wpcf') . '\'');
    wpcf_admin_add_js_settings('wpcf_pr_belongs_txt', '\'' . __("Parent: %s", 'wpcf') . '\'');
    return $form;
}
Exemple #6
0
 /**
  * Content for choose parent column.
  *
  * @return boolean
  */
 function _parent_form($post_parent = '')
 {
     $item_parents = wpcf_pr_admin_get_belongs($this->child_post_type);
     if ($item_parents) {
         foreach ($item_parents as $parent => $temp_data) {
             // Skip if only current available
             if ($parent == $this->parent_post_type) {
                 continue;
             }
             if (!empty($post_parent) && $parent != $post_parent) {
                 continue;
             }
             // Get parent ID
             $meta = get_post_meta($this->child->ID, '_wpcf_belongs_' . $parent . '_id', true);
             $meta = empty($meta) ? 0 : $meta;
             // Get form
             $belongs_data = array('belongs' => array($parent => $meta));
             $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($this->child, $parent, $belongs_data);
             if (empty($temp_form)) {
                 return '<span class="types-small-italic">' . __('No parents available', 'wpcf') . '</span>';
             }
             unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']);
             $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $this->parent->ID . '][' . $this->child->ID . '][parents][' . $parent . ']';
             // Return HTML formatted output
             return wpcf_form_simple($temp_form);
         }
     }
     return '<span class="types-small-italic">' . __('No parents available', 'wpcf') . '</span>';
 }
 /**
  * Summary.
  *
  * Description.
  *
  * @since x.x.x
  * @access (for functions: only use if private)
  *
  * @see Function/method/class relied on
  * @link URL
  * @global type $varname Description.
  * @global type $varname Description.
  *
  * @param type $var Description.
  * @param type $var Optional. Description.
  * @return type Description.
  */
 public function prepare_field_select_screen()
 {
     /**
      * check nonce
      */
     if (0 || !isset($_REQUEST['_wpnonce']) || !isset($_REQUEST['parent']) || !isset($_REQUEST['child']) || !wp_verify_nonce($_REQUEST['_wpnonce'], $this->get_nonce('child-post-fields', $_REQUEST['parent'], $_REQUEST['child']))) {
         $this->verification_failed_and_die();
     }
     $parent = $_REQUEST['parent'];
     $child = $_REQUEST['child'];
     $post_type_parent = get_post_type_object($parent);
     $post_type_child = get_post_type_object($child);
     if (empty($post_type_parent) || empty($post_type_child)) {
         die(__('Wrong post types', 'wpcf'));
     }
     $relationships = get_option('wpcf_post_relationship', array());
     if (!isset($relationships[$parent][$child])) {
         $this->print_notice_and_die(__('Please save Post Type first to edit these fields.', 'wpcf'));
     }
     $repetitive_warning_markup = array();
     $data = $relationships[$parent][$child];
     $form = array();
     $form['repetitive_warning_markup'] = $repetitive_warning_markup;
     $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('Do not show management options for this post type', 'wpcf') => 'only_list', __('All fields, including the standard post fields', 'wpcf') => 'all_cf_standard', __('Specific fields', 'wpcf') => 'specific'), '#attributes' => array('display' => 'ul'), '#default_value' => empty($data['fields_setting']) ? 'all_cf' : $data['fields_setting']);
     /**
      * check default, to avoid missing configuration
      */
     if (!in_array($form['select']['#default_value'], $form['select']['#options'])) {
         $form['select']['#default_value'] = 'all_cf';
     }
     /**
      * Specific options
      */
     $groups = wpcf_admin_get_groups_by_post_type($child);
     $options_cf = array();
     $repetitive_warning = false;
     $repetitive_warning_txt = __('Repeating fields should not be used in child posts. Types will update all field values.', 'wpcf');
     foreach ($groups as $group) {
         $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
         foreach ($fields as $key => $cf) {
             $__key = wpcf_types_cf_under_control('check_outsider', $key) ? $key : WPCF_META_PREFIX . $key;
             $options_cf[$__key] = array('#title' => $cf['name'], '#name' => 'fields[' . $__key . ']', '#default_value' => isset($data['fields'][$__key]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
             // Repetitive warning
             if (wpcf_admin_is_repetitive($cf)) {
                 if (!$repetitive_warning) {
                     $repetitive_warning_markup = array('#type' => 'markup', '#markup' => '<div class="message error" style="display:none;" id="wpcf-repetitive-warning"><p>' . $repetitive_warning_txt . '</p></div>');
                 }
                 $repetitive_warning = true;
                 $options_cf[$__key]['#after'] = !isset($data['fields'][$__key]) ? '<div class="message error" style="display:none;"><p>' : '<div class="message error"><p>';
                 $options_cf[$__key]['#after'] .= $repetitive_warning_txt;
                 $options_cf[$__key]['#after'] .= '</p></div></li>';
                 $options_cf[$__key]['#attributes'] = array('onclick' => 'jQuery(this).parent().find(\'.message\').toggle();', 'disabled' => 'disabled');
             }
         }
     }
     /**
      * build options for "Specific fields"
      */
     $options = array();
     /**
      * check and add built-in properites
      */
     require_once WPCF_INC_ABSPATH . '/post-relationship.php';
     $supports = wpcf_post_relationship_get_supported_fields_by_post_type($child);
     foreach ($supports as $child_field_key => $child_field_data) {
         $options[$child_field_data['name']] = array('#title' => $child_field_data['title'], '#name' => sprintf('fields[%s]', $child_field_data['name']), '#default_value' => isset($data['fields'][$child_field_data['name']]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
     }
     /**
      * add custom fields
      */
     $options = $options + $options_cf;
     $temp_belongs = wpcf_pr_admin_get_belongs($child);
     foreach ($temp_belongs as $temp_parent => $temp_data) {
         if ($temp_parent == $parent) {
             continue;
         }
         $temp_parent_type = get_post_type_object($temp_parent);
         $options[$temp_parent] = array('#title' => $temp_parent_type->label, '#name' => 'fields[_wpcf_pr_parents][' . $temp_parent . ']', '#default_value' => isset($data['fields']['_wpcf_pr_parents'][$temp_parent]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
     }
     /**
      * remove "Specific fields" if there is no fields
      */
     if (empty($options)) {
         unset($form['select']['#options'][__('Specific fields', 'wpcf')]);
         if ('specific' == $form['select']['#default_value']) {
             $form['select']['#default_value'] = 'all_cf';
         }
     }
     // Taxonomies
     $taxonomies = get_object_taxonomies($post_type_child->name, 'objects');
     if (!empty($taxonomies)) {
         foreach ($taxonomies as $tax_id => $taxonomy) {
             $options[$tax_id] = array('#title' => sprintf(__('Taxonomy - %s', 'wpcf'), $taxonomy->label), '#name' => 'fields[_wpcf_pr_taxonomies][' . $tax_id . ']', '#default_value' => isset($data['fields']['_wpcf_pr_taxonomies'][$tax_id]) ? 1 : 0, '#inline' => true, '#before' => '<li>', '#after' => '</li>');
         }
     }
     $form['specific'] = array('#type' => 'checkboxes', '#name' => 'fields', '#options' => $options, '#default_value' => isset($data['fields']), '#before' => sprintf('<ul id="wpcf-specific" class="%s">', 'specific' == $form['select']['#default_value'] ? '' : 'hidden'), '#after' => '</ul>');
     $form['nonce'] = array('#type' => 'hidden', '#value' => wp_create_nonce($this->get_nonce('child-post-fields-save', $parent, $child)), '#name' => 'wpcf-fields-save-nonce', '#id' => 'wpcf-fields-save-nonce');
     $form['parent'] = array('#type' => 'hidden', '#value' => esc_attr($parent), '#name' => 'wpcf-parent', '#id' => 'wpcf-parent');
     $form['child'] = array('#type' => 'hidden', '#value' => esc_attr($child), '#name' => 'wpcf-child', '#id' => 'wpcf-child');
     echo wpcf_form_simple($form);
     die;
 }
/**
 * Edit fields form.
 * 
 * @global type $wpdb
 * @param type $parent
 * @param type $child 
 */
function wpcf_pr_admin_edit_fields($parent, $child)
{
    global $wpdb;
    $post_type_parent = get_post_type_object($parent);
    $post_type_child = get_post_type_object($child);
    if (empty($post_type_parent) || empty($post_type_child)) {
        die(__('Wrong post types'));
    }
    $relationships = get_option('wpcf_post_relationship', array());
    if (!isset($relationships[$parent][$child])) {
        die(__('Relationship do not exist'));
    }
    $data = $relationships[$parent][$child];
    wp_enqueue_script('jquery');
    wpcf_admin_ajax_head('Edit fields', 'wpcf');
    // Process submit
    if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'pt_edit_fields')) {
        $relationships[$parent][$child]['fields_setting'] = $_POST['fields_setting'];
        $relationships[$parent][$child]['fields'] = isset($_POST['fields']) ? $_POST['fields'] : array();
        update_option('wpcf_post_relationship', $relationships);
        ?>
        <script type="text/javascript">
            window.parent.jQuery('#TB_closeWindowButton').trigger('click');
            window.parent.location.reload();
        </script>
        <?php 
        die;
    }
    $form = array();
    $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('All fields, including the standard post fields', 'wpcf') => 'all_cf_standard', __('Specific fields', 'wpcf') => 'specific'), '#default_value' => empty($data['fields_setting']) ? 'all_cf' : $data['fields_setting']);
    $options = array();
    $options['_wp_title'] = array('#title' => __('Post title', 'wpcf'), '#name' => 'fields[_wp_title]', '#default_value' => isset($data['fields']['_wp_title']) ? 1 : 0);
    $options['_wp_body'] = array('#title' => __('Post body', 'wpcf'), '#name' => 'fields[_wp_body]', '#default_value' => isset($data['fields']['_wp_body']) ? 1 : 0);
    //    $wpcf_fields = wpcf_admin_fields_get_fields(true);
    $groups = wpcf_admin_get_groups_by_post_type($child);
    foreach ($groups as $group) {
        $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
        foreach ($fields as $key => $cf) {
            $options[WPCF_META_PREFIX . $key] = array();
            $options[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
            $options[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
            $options[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $key]) ? 1 : 0;
        }
    }
    $temp_belongs = wpcf_pr_admin_get_belongs($child);
    foreach ($temp_belongs as $temp_parent => $temp_data) {
        if ($temp_parent == $parent) {
            continue;
        }
        $temp_parent_type = get_post_type_object($temp_parent);
        $options[$temp_parent] = array();
        $options[$temp_parent]['#title'] = $temp_parent_type->label;
        $options[$temp_parent]['#name'] = 'fields[_wpcf_pr_parents][' . $temp_parent . ']';
        $options[$temp_parent]['#default_value'] = isset($data['fields']['_wpcf_pr_parents'][$temp_parent]) ? 1 : 0;
    }
    //    foreach ($wpcf_fields as $key => $cf) {
    //        $options[WPCF_META_PREFIX . $key] = array();
    //        $options[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
    //        $options[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
    //        $options[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $key]) ? 1 : 0;
    //    }
    //    $cf_all = $wpdb->get_results("
    //		SELECT meta_id, meta_key
    //		FROM $wpdb->postmeta
    //		GROUP BY meta_key
    //		HAVING meta_key NOT LIKE '\_%'
    //		ORDER BY meta_key");
    //    foreach ($cf_all as $key => $cf) {
    //        if (isset($options[$cf->meta_key])) {
    //            continue;
    //        }
    //        $options[$key] = array();
    //        $options[$key]['#title'] = $cf->meta_key;
    //        $options[$key]['#name'] = 'fields[' . $cf->meta_key . ']';
    //        $options[$key]['#default_value'] = isset($data['fields'][$cf->meta_key]) ? 1 : 0;
    //    }
    $form['specific'] = array('#type' => 'checkboxes', '#name' => 'fields', '#options' => $options, '#default_value' => isset($data['fields']), '#before' => '<div id="wpcf-specific" style="display:none;margin:10px 0 0 20px;">', '#after' => '</div>');
    $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Save'), '#attributes' => array('class' => 'button-primary'));
    echo '<form method="post" action="">';
    echo wpcf_form_simple($form);
    echo wp_nonce_field('pt_edit_fields');
    echo '</form>';
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function(){
            if (jQuery('input[name="fields_setting"]:checked').val() == 'specific') {
                jQuery('#wpcf-specific').show();
            }
            jQuery('input[name="fields_setting"]').change(function(){
                if (jQuery(this).val() == 'specific') {
                    jQuery('#wpcf-specific').slideDown();
                } else {
                    jQuery('#wpcf-specific').slideUp();
                }
            });
        });
    </script>
    <?php 
    wpcf_admin_ajax_footer();
}
/**
 * Edit fields form.
 * 
 * @global type $wpdb
 * @param type $parent
 * @param type $child 
 */
function wpcf_pr_admin_edit_fields($parent, $child)
{
    global $wpdb;
    $post_type_parent = get_post_type_object($parent);
    $post_type_child = get_post_type_object($child);
    if (empty($post_type_parent) || empty($post_type_child)) {
        die(__('Wrong post types'));
    }
    $relationships = get_option('wpcf_post_relationship', array());
    if (!isset($relationships[$parent][$child])) {
        die(__('Relationship do not exist'));
    }
    $data = $relationships[$parent][$child];
    wp_enqueue_script('jquery');
    wpcf_admin_ajax_head('Edit fields', 'wpcf');
    // Process submit
    if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'pt_edit_fields')) {
        $relationships[$parent][$child]['fields_setting'] = $_POST['fields_setting'];
        $relationships[$parent][$child]['fields'] = isset($_POST['fields']) ? $_POST['fields'] : array();
        update_option('wpcf_post_relationship', $relationships);
        ?>
        <script type="text/javascript">
            window.parent.jQuery('#TB_closeWindowButton').trigger('click');
            window.parent.location.reload();
        </script>
        <?php 
        die;
    }
    $groups = wpcf_admin_get_groups_by_post_type($child);
    $options_cf = array();
    $repetitive_warning = false;
    $repetitive_warning_markup = array();
    $repetitive_warning_txt = __('Repeating fields should not be used in child posts. Types will update all field values.', 'wpcf');
    foreach ($groups as $group) {
        $fields = wpcf_admin_fields_get_fields_by_group($group['id']);
        foreach ($fields as $key => $cf) {
            $options_cf[WPCF_META_PREFIX . $key] = array();
            $options_cf[WPCF_META_PREFIX . $key]['#title'] = $cf['name'];
            $options_cf[WPCF_META_PREFIX . $key]['#name'] = 'fields[' . WPCF_META_PREFIX . $key . ']';
            $options_cf[WPCF_META_PREFIX . $key]['#default_value'] = isset($data['fields'][WPCF_META_PREFIX . $key]) ? 1 : 0;
            // Repetitive warning
            if (wpcf_admin_is_repetitive($cf)) {
                if (!$repetitive_warning) {
                    $repetitive_warning_markup = array('#type' => 'markup', '#markup' => '<div class="message error" style="display:none;" id="wpcf-repetitive-warning"><p>' . $repetitive_warning_txt . '</p></div>');
                }
                $repetitive_warning = true;
                $options_cf[WPCF_META_PREFIX . $key]['#after'] = !isset($data['fields'][WPCF_META_PREFIX . $key]) ? '<div class="message error" style="display:none;"><p>' : '<div class="message error"><p>';
                $options_cf[WPCF_META_PREFIX . $key]['#after'] .= $repetitive_warning_txt;
                $options_cf[WPCF_META_PREFIX . $key]['#after'] .= '</p></div>';
                $options_cf[WPCF_META_PREFIX . $key]['#attributes'] = array('onclick' => 'jQuery(this).parent().find(\'.message\').toggle();');
            }
        }
    }
    $form = array();
    $form['repetitive_warning_markup'] = $repetitive_warning_markup;
    $form['select'] = array('#type' => 'radios', '#name' => 'fields_setting', '#options' => array(__('Title, all custom fields and parents', 'wpcf') => 'all_cf', __('All fields, including the standard post fields', 'wpcf') => 'all_cf_standard', __('Specific fields', 'wpcf') => 'specific'), '#default_value' => empty($data['fields_setting']) ? 'all_cf' : $data['fields_setting']);
    $options = array();
    $options['_wp_title'] = array('#title' => __('Post title', 'wpcf'), '#name' => 'fields[_wp_title]', '#default_value' => isset($data['fields']['_wp_title']) ? 1 : 0);
    $options['_wp_body'] = array('#title' => __('Post body', 'wpcf'), '#name' => 'fields[_wp_body]', '#default_value' => isset($data['fields']['_wp_body']) ? 1 : 0);
    $options = $options + $options_cf;
    $temp_belongs = wpcf_pr_admin_get_belongs($child);
    foreach ($temp_belongs as $temp_parent => $temp_data) {
        if ($temp_parent == $parent) {
            continue;
        }
        $temp_parent_type = get_post_type_object($temp_parent);
        $options[$temp_parent] = array();
        $options[$temp_parent]['#title'] = $temp_parent_type->label;
        $options[$temp_parent]['#name'] = 'fields[_wpcf_pr_parents][' . $temp_parent . ']';
        $options[$temp_parent]['#default_value'] = isset($data['fields']['_wpcf_pr_parents'][$temp_parent]) ? 1 : 0;
    }
    $form['specific'] = array('#type' => 'checkboxes', '#name' => 'fields', '#options' => $options, '#default_value' => isset($data['fields']), '#before' => '<div id="wpcf-specific" style="display:none;margin:10px 0 0 20px;">', '#after' => '</div>');
    $form['submit'] = array('#type' => 'submit', '#name' => 'submit', '#value' => __('Save', 'wpcf'), '#attributes' => array('class' => 'button-primary'));
    echo '<form method="post" action="">';
    echo wpcf_form_simple($form);
    echo wp_nonce_field('pt_edit_fields');
    echo '</form>';
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function(){
            if (jQuery('input[name="fields_setting"]:checked').val() == 'specific') {
                jQuery('#wpcf-specific').show();
            } else {
    <?php 
    if ($repetitive_warning) {
        ?>
                                    jQuery('#wpcf-repetitive-warning').show();
        <?php 
    }
    ?>
                            }
                            jQuery('input[name="fields_setting"]').change(function(){
                                if (jQuery(this).val() == 'specific') {
                                    jQuery('#wpcf-specific').slideDown();
                                } else {
                                    jQuery('#wpcf-specific').slideUp();
    <?php 
    if ($repetitive_warning) {
        ?>
                                        jQuery('#wpcf-repetitive-warning').show();
        <?php 
    }
    ?>
                                }
                            });
                        });
    </script>
    <?php 
    wpcf_admin_ajax_footer();
}
function wpcf_admin_metabox_relationship($post_type)
{
    $form = array();
    $form['description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', sprintf('%s <a href="%s" target="_blank">%s</a>.', __('Parent/child relationship lets you connect between posts of different types. When you edit a parent, you will see its children listed in a table and you can edit the fields of the children. When you edit a child, you can choose the parents. On the front-end, you can display a list of children or information about the parents.', 'wpcf'), Types_Helper_Url::get_url('post-relationship', true, 'parent_child_relationship', Types_Helper_Url::UTM_MEDIUM_POSTEDIT), __('Parent/child documentation', 'wpcf'))));
    $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    $is_error = false;
    $error_message = '';
    // Detect situations when we cannot configure post relationships yet. Render a message and finish.
    $is_unsaved_post_type = !isset($_REQUEST['wpcf-post-type']) || !isset($custom_types[$_REQUEST['wpcf-post-type']]);
    if ($is_unsaved_post_type) {
        $is_error = true;
        $error_message = __('Please save first, before you can edit the relationship.', 'wpcf');
    }
    $is_attachment = isset($_REQUEST['wpcf-post-type']) && 'attachment' == $_REQUEST['wpcf-post-type'];
    if ($is_attachment) {
        $is_error = true;
        $error_message = __('Post relationships are not allowed for the Media post type.', 'wpcf');
    }
    if ($is_error) {
        $form['alert'] = array('#type' => 'notice', '#markup' => $error_message);
        $form = wpcf_form(__FUNCTION__, $form);
        echo $form->renderForm();
        return;
    }
    $post_type = $custom_types[$_REQUEST['wpcf-post-type']];
    unset($custom_types);
    // No problems detected, go ahead and render the options.
    // belongs/children section
    $has = wpcf_pr_admin_get_has($post_type['slug']);
    $belongs = wpcf_pr_admin_get_belongs($post_type['slug']);
    $post_types = get_post_types('', 'objects');
    // parents
    $form['parent-h3'] = array('#type' => 'markup', '#markup' => sprintf('<h3>%s</h3>', __('Parent Post Types:', 'wpcf')));
    $form['parent-description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Select which post types will be parents of this type.', 'wpcf')));
    $options = array();
    // Build excluded post types
    global $wpcf;
    $excluded_post_types = $wpcf->excluded_post_types;
    $excluded_post_types[] = $post_type['slug'];
    // Explicitly exclude attachments for post relationships because there is no GUI for it
    // (but we're not excluding them from all Types functionality)
    $excluded_post_types[] = 'attachment';
    foreach ($post_types as $post_type_option_slug => $post_type_option) {
        $is_excluded = in_array($post_type_option_slug, $excluded_post_types);
        $has_no_ui = !$post_type_option->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true);
        if ($is_excluded || $has_no_ui) {
            continue;
        }
        $options[$post_type_option_slug] = array('#name' => 'ct[post_relationship][belongs][' . $post_type_option_slug . ']', '#title' => $post_type_option->labels->singular_name, '#default_value' => isset($belongs[$post_type_option_slug]), '#inline' => true, '#before' => '<li>', '#after' => '</li>', '#attributes' => array('class' => 'js-wpcf-relationship-checkbox', 'data-wpcf-type' => 'belongs', 'data-wpcf-value' => esc_attr($post_type_option_slug), 'data-wpcf-message-disabled' => esc_attr__('This post type is disabled, becouse is used as child post.', 'wpcf')));
        if (isset($has[$post_type_option_slug])) {
            $options[$post_type_option_slug]['#before'] = '<li class="disabled">';
            $options[$post_type_option_slug]['#attributes']['disabled'] = 'disabled';
        }
    }
    $form['table-pr-belongs-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
    // child posts
    $form['child-h3'] = array('#type' => 'markup', '#markup' => sprintf('<hr /><h3>%s</h3>', __('Children Post Types:', 'wpcf')));
    $form['child-description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Select which post types will be children of this type.', 'wpcf')));
    $options = array();
    foreach ($post_types as $post_type_option_slug => $post_type_option) {
        if (in_array($post_type_option_slug, $excluded_post_types) || !$post_type_option->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true)) {
            continue;
        }
        $nonce = sprintf('child-post-fields-%s-%s', $post_type['slug'], $post_type_option_slug);
        $a = sprintf(' <span>(<a class="js-wpcf-edit-child-post-fields" href="#" data-wpcf-nonce="%s" data-wpcf-child="%s" data-wpcf-parent="%s" data-wpcf-title="%s" data-wpcf-buttons-apply="%s" data-wpcf-buttons-cancel="%s" data-wpcf-message-loading="%s" data-wpcf-save-status="%s">%s</a>)</span>', esc_attr(wp_create_nonce($nonce)), esc_attr($post_type_option_slug), esc_attr($post_type['slug']), esc_attr(sprintf(__('Select child fields from %s to be displayed in Post Relationship table', 'wpcf'), $post_type_option->labels->singular_name)), esc_attr__('Apply', 'wpcf'), esc_attr__('Cancel', 'wpcf'), esc_attr__('Please Wait, Loading…', 'wpcf'), esc_attr(isset($has[$post_type_option_slug]) ? 'saved' : 'new'), esc_attr__('Select fields', 'wpcf'));
        $options[$post_type_option_slug] = array('#name' => 'ct[post_relationship][has][' . $post_type_option_slug . ']', '#title' => $post_type_option->labels->singular_name, '#inline' => true, '#before' => '<li>', '#after' => $a . '</li>', '#attributes' => array('class' => 'js-wpcf-relationship-checkbox', 'data-wpcf-type' => 'has', 'data-wpcf-value' => esc_attr($post_type_option_slug), 'data-wpcf-message-disabled' => esc_attr__('This post type is disabled, becouse is used as parent post.', 'wpcf')));
        // Check if it already belongs
        if (isset($belongs[$post_type_option_slug])) {
            $options[$post_type_option_slug]['#before'] = '<li class="disabled">';
            $options[$post_type_option_slug]['#attributes']['disabled'] = 'disabled';
        } else {
            if (isset($has[$post_type_option_slug])) {
                $options[$post_type_option_slug]['#default_value'] = true;
                $options[$post_type_option_slug]['#before'] = '<li class="active">';
            }
        }
    }
    $form['table-pr-has-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
    $form = wpcf_form(__FUNCTION__, $form);
    echo $form->renderForm();
}
/**
 * Has form table row.
 * 
 * @param type $post
 * @param type $post_type
 * @param type $data
 * @param type $parent_post_type
 * @param stdClass $item
 * @return string 
 */
function wpcf_pr_admin_post_meta_box_has_row($post, $post_type, $data, $parent_post_type, $item)
{
    $new_item = false;
    $date_trigger = false;
    // Set item
    if (empty($item)) {
        $item = new stdClass();
        $item->ID = 'new_' . mt_rand();
        $item->post_title = '';
        $item->post_content = '';
        $item->post_type = $post_type;
        $new_item = true;
    }
    // Cleanup data
    if (empty($data['fields_setting'])) {
        $data['fields_setting'] = 'all_cf';
    }
    $item_parents = isset($data['fields']['_wpcf_pr_parents']) ? $data['fields']['_wpcf_pr_parents'] : array();
    unset($data['fields']['_wpcf_pr_parents']);
    $row_data = array();
    $wpcf_fields = wpcf_admin_fields_get_fields();
    $row_data[] = wpcf_form_simple(array('field' => array('#type' => 'textfield', '#id' => 'wpcf_post_relationship_' . $item->ID . '_wp_title', '#name' => 'wpcf_post_relationship[' . $item->ID . '][_wp_title]', '#value' => $item->post_title, '#inline' => true, '#attributes' => $new_item || $data['fields_setting'] == 'all_cf_standard' || isset($data['fields']['_wp_title']) ? array() : array('readonly' => 'readonly'))));
    if ($data['fields_setting'] == 'specific' && !empty($data['fields'])) {
        foreach ($data['fields'] as $field_key => $true) {
            if ($field_key == '_wp_title') {
                continue;
            } else {
                if ($field_key == '_wp_body') {
                    $value = wp_trim_words($item->post_content, 10, null);
                    $element = wpcf_form_simple(array('field' => array('#type' => 'textarea', '#id' => 'wpcf_post_relationship_' . $item->ID . '_' . $field_key, '#name' => 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']', '#value' => $item->post_content, '#attributes' => array('style' => 'width:300px;height:100px;'), '#inline' => true)));
                } else {
                    $wpcf_key = str_replace(WPCF_META_PREFIX, '', $field_key);
                    if (strpos($field_key, WPCF_META_PREFIX) === 0 && isset($wpcf_fields[$wpcf_key])) {
                        // Date trigger
                        if ($wpcf_fields[$wpcf_key]['type'] == 'date') {
                            $date_trigger = true;
                        }
                        // Get WPCF form
                        $element = wpcf_admin_post_process_fields($item, array('field' => $wpcf_fields[$wpcf_key]), false, false, 'post_relationship');
                        $element = array_shift($element);
                        // TODO There may still be problem with IDs
                        if (!in_array($wpcf_fields[$wpcf_key]['type'], array('image', 'file'))) {
                            $element['#id'] = 'wpcf_post_relationship_' . $item->ID . '_' . $wpcf_key;
                        }
                        $element['#name'] = 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']';
                        $element['#inline'] = true;
                        unset($element['#title'], $element['#description']);
                        if (in_array($wpcf_fields[$wpcf_key]['type'], array('wysiwyg'))) {
                            $element['#type'] = 'textarea';
                            $element['#attributes'] = array('style' => 'width:300px;height:100px;');
                        }
                        if (in_array($wpcf_fields[$wpcf_key]['type'], array('checkbox'))) {
                            $element['#suffix'] = '<input type="hidden" name="wpcf_post_relationship_checkbox[' . $item->ID . '][' . $wpcf_key . ']" value="1" />';
                        }
                        $value = get_post_meta($item->ID, $field_key, true);
                        $element = wpcf_form_simple(array('field' => $element));
                    } else {
                        // Just render textfield
                        $value = get_post_meta($item->ID, $field_key, true);
                        $element = wpcf_form_simple(array('field' => array('#type' => 'textfield', '#id' => 'wpcf_post_relationship_' . $item->ID . '_' . $field_key, '#name' => 'wpcf_post_relationship[' . $item->ID . '][' . $field_key . ']', '#value' => $value, '#inline' => true)));
                    }
                }
            }
            $row_data[] = $element;
        }
        // Get other parents
        foreach ($item_parents as $parent => $temp_data) {
            if ($parent == $parent_post_type) {
                continue;
            }
            $meta = get_post_meta($item->ID, '_wpcf_belongs_' . $parent . '_id', true);
            $meta = empty($meta) ? 0 : $meta;
            $belongs_data = array('belongs' => array($parent => $meta));
            $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($item, $parent, $belongs_data);
            unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']);
            $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $item->ID . '][parents][' . $parent . ']';
            $row_data[] = wpcf_form_simple($temp_form);
        }
    } else {
        $groups = wpcf_admin_post_get_post_groups_fields($item, 'post_relationships');
        if ($data['fields_setting'] == 'all_cf_standard') {
            $element = wpcf_form_simple(array('field' => array('#type' => 'textarea', '#id' => 'wpcf_post_relationship_' . $item->ID . '_wp_body', '#name' => 'wpcf_post_relationship[' . $item->ID . '][_wp_body]', '#value' => $item->post_content, '#attributes' => array('style' => 'width:300px;height:100px;'), '#inline' => true)));
            $row_data[] = $element;
        }
        foreach ($groups as $group) {
            foreach ($group['fields'] as $field) {
                // Date trigger
                if ($field['type'] == 'date') {
                    $date_trigger = true;
                }
                // Get WPCF form
                $element_org = wpcf_admin_post_process_fields($item, array('field' => $field), false, false, 'post_relationship');
                $element = array_shift($element_org);
                // @todo Check if this is needed for skype only
                //                if ($field['type'] == 'skype') {
                //                    $temp_second_element = array_pop($element_org);
                //                    if (!empty($temp_second_element['#validate'])) {
                //                        $element['#validate'] = $temp_second_element['#validate'];
                //                    }
                //
                //                }
                // TODO Monitor if may still be problem with IDs
                if (!in_array($field['type'], array('image', 'file'))) {
                    $element['#id'] = 'wpcf_post_relationship_' . $item->ID . '_' . $field['id'];
                }
                $element['#name'] = 'wpcf_post_relationship[' . $item->ID . '][' . wpcf_types_get_meta_prefix($field) . $field['slug'] . ']';
                $element['#inline'] = true;
                unset($element['#title'], $element['#description']);
                if (in_array($field['type'], array('wysiwyg'))) {
                    $element['#type'] = 'textarea';
                    $element['#attributes'] = array('style' => 'width:300px;height:100px;');
                }
                if (in_array($field['type'], array('checkbox'))) {
                    $element['#suffix'] = '<input type="hidden" name="wpcf_post_relationship_checkbox[' . $item->ID . '][' . wpcf_types_get_meta_prefix($field) . $field['slug'] . ']" value="1" />';
                }
                $value = get_post_meta($item->ID, wpcf_types_get_meta_prefix($field) . $field['slug'], true);
                $element = array('field' => $element);
                $element = wpcf_form_simple($element);
                $row_data[] = $element;
            }
        }
        // Get all parents
        $item_parents = wpcf_pr_admin_get_belongs($post_type);
        if ($item_parents) {
            foreach ($item_parents as $parent => $temp_data) {
                if ($parent == $parent_post_type) {
                    continue;
                }
                $meta = get_post_meta($item->ID, '_wpcf_belongs_' . $parent . '_id', true);
                $meta = empty($meta) ? 0 : $meta;
                $belongs_data = array('belongs' => array($parent => $meta));
                $temp_form = wpcf_pr_admin_post_meta_box_belongs_form($item, $parent, $belongs_data);
                unset($temp_form[$parent]['#suffix'], $temp_form[$parent]['#prefix'], $temp_form[$parent]['#title']);
                $temp_form[$parent]['#name'] = 'wpcf_post_relationship[' . $item->ID . '][parents][' . $parent . ']';
                $row_data[] = wpcf_form_simple($temp_form);
            }
        }
    }
    if (!empty($row_data)) {
        $output = '';
        $output .= '<tr><td>' . implode('</td><td>', $row_data) . '<input type="hidden" name="wpcf_post_relationship[' . $item->ID . '][post_type]" value="' . $post_type . '" /></td><td class="actions">' . '<a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;' . 'wpcf_action=pr_save_child_post&amp;post_type_parent=' . $parent_post_type . '&amp;post_id=' . $post->ID . '&amp;post_type_child=' . $post_type . '&_wpnonce=' . wp_create_nonce('pr_save_child_post')) . '" class="wpcf-pr-save-ajax button-secondary">' . __('Save') . '</a>';
        $output .= strpos($item->ID, 'new_') === false ? ' <a href="' . get_edit_post_link($item->ID) . '" class="button-secondary">' . __('Edit') . '</a>' : '';
        $output .= strpos($item->ID, 'new_') === false ? ' <a href="' . admin_url('admin-ajax.php?action=wpcf_ajax&amp;' . 'wpcf_action=pr_delete_child_post' . '&amp;post_id=' . $item->ID . '&_wpnonce=' . wp_create_nonce('pr_delete_child_post')) . '" class="wpcf-pr-delete-ajax button-secondary">' . __('Delete') . '</a>' : '';
        if ($date_trigger) {
            $output .= '<script type="text/javascript">
        //<![CDATA[
        jQuery(document).ready(function(){
            wpcfFieldsDateInit("#wpcf-post-relationship");
        });
        //]]>
    </script>';
        }
        $output .= wpcf_form_render_js_validation('#post', false) . '</td></tr>';
        return $output;
    }
    return $output = '<tr><td><span style="color:Red;">' . __('Error occured', 'wpcf') . '</span></td></tr>';
}
function wpcf_admin_metabox_relationship($post_type)
{
    $form = array();
    $form['description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Parent/child relationship lets you connect between posts of different types. When you edit a parent, you will see its children listed in a table and you can edit the fields of the children. When you edit a child, you can choose the parents. On the front-end, you can display a list of children or information about the parents. <a href="https://wp-types.com/documentation/user-guides/creating-post-type-relationships/?utm_source=typesplugin&utm_medium=postedit&utm_campaign=parent_child_relationship" target="_blank">Parent/child documentation</a>.', 'wpcf')));
    $custom_types = get_option(WPCF_OPTION_NAME_CUSTOM_TYPES, array());
    if (!isset($_REQUEST['wpcf-post-type']) || !isset($custom_types[$_REQUEST['wpcf-post-type']])) {
        $form['alert'] = array('#type' => 'notice', '#markup' => __('Please save first, before you can edit the relationship.', 'wpcf'));
        $form = wpcf_form(__FUNCTION__, $form);
        echo $form->renderForm();
        return false;
    }
    $post_type = false;
    $post_type = $custom_types[$_REQUEST['wpcf-post-type']];
    unset($custom_types);
    /**
     * belongs/children section
     */
    $has = wpcf_pr_admin_get_has($post_type['slug']);
    $belongs = wpcf_pr_admin_get_belongs($post_type['slug']);
    $post_types = get_post_types('', 'objects');
    /**
     * parents
     */
    $form['parent-h3'] = array('#type' => 'markup', '#markup' => sprintf('<h3>%s</h3>', __('Parent Post Types:', 'wpcf')));
    $form['parent-description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Select which post types will be parents of this type.', 'wpcf')));
    $options = array();
    /**
     * build excluded post types
     */
    global $wpcf;
    $excluded_post_types = $wpcf->excluded_post_types;
    $excluded_post_types[] = $post_type['slug'];
    foreach ($post_types as $temp_post_type_slug => $temp_post_type) {
        if (in_array($temp_post_type_slug, $excluded_post_types) || !$temp_post_type->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true)) {
            continue;
        }
        $options[$temp_post_type_slug] = array('#name' => 'ct[post_relationship][belongs][' . $temp_post_type_slug . ']', '#title' => $temp_post_type->labels->singular_name, '#default_value' => isset($belongs[$temp_post_type_slug]), '#inline' => true, '#before' => '<li>', '#after' => '</li>', '#attributes' => array('class' => 'js-wpcf-relationship-checkbox', 'data-wpcf-type' => 'belongs', 'data-wpcf-value' => esc_attr($temp_post_type_slug), 'data-wpcf-message-disabled' => esc_attr__('This post type is disabled, becouse is used as child post.', 'wpcf')));
        if (isset($has[$temp_post_type_slug])) {
            $options[$temp_post_type_slug]['#before'] = '<li class="disabled">';
            $options[$temp_post_type_slug]['#attributes']['disabled'] = 'disabled';
        }
    }
    $form['table-pr-belongs-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
    /**
     * child posts
     */
    $form['child-h3'] = array('#type' => 'markup', '#markup' => sprintf('<hr /><h3>%s</h3>', __('Children Post Types:', 'wpcf')));
    $form['child-description'] = array('#type' => 'markup', '#markup' => sprintf('<p class="description">%s</p>', __('Select which post types will be children of this type.', 'wpcf')));
    $options = array();
    foreach ($post_types as $temp_post_type_slug => $temp_post_type) {
        if (in_array($temp_post_type_slug, $excluded_post_types) || !$temp_post_type->show_ui && !apply_filters('wpcf_show_ui_hide_in_relationships', true)) {
            continue;
        }
        $nonce = sprintf('child-post-fields-%s-%s', $post_type['slug'], $temp_post_type_slug);
        $a = sprintf(' <span>(<a class="js-wpcf-edit-child-post-fields" href="#" data-wpcf-nonce="%s" data-wpcf-child="%s" data-wpcf-parent="%s" data-wpcf-title="%s" data-wpcf-buttons-apply="%s" data-wpcf-buttons-cancel="%s" data-wpcf-message-loading="%s" data-wpcf-save-status="%s">%s</a>)</span>', esc_attr(wp_create_nonce($nonce)), esc_attr($temp_post_type_slug), esc_attr($post_type['slug']), esc_attr(sprintf(__('Select child fields from %s to be displayed in Post Relationship table', 'wpcf'), $temp_post_type->labels->singular_name)), esc_attr__('Apply', 'wpcf'), esc_attr__('Cancel', 'wpcf'), esc_attr__('Please Wait, Loading…', 'wpcf'), esc_attr(isset($has[$temp_post_type_slug]) ? 'saved' : 'new'), esc_attr__('Select fields', 'wpcf'));
        $options[$temp_post_type_slug] = array('#name' => 'ct[post_relationship][has][' . $temp_post_type_slug . ']', '#title' => $temp_post_type->labels->singular_name, '#inline' => true, '#before' => '<li>', '#after' => $a . '</li>', '#attributes' => array('class' => 'js-wpcf-relationship-checkbox', 'data-wpcf-type' => 'has', 'data-wpcf-value' => esc_attr($temp_post_type_slug), 'data-wpcf-message-disabled' => esc_attr__('This post type is disabled, becouse is used as parent post.', 'wpcf')));
        // Check if it already belongs
        if (isset($belongs[$temp_post_type_slug])) {
            $options[$temp_post_type_slug]['#before'] = '<li class="disabled">';
            $options[$temp_post_type_slug]['#attributes']['disabled'] = 'disabled';
        } else {
            if (isset($has[$temp_post_type_slug])) {
                $options[$temp_post_type_slug]['#default_value'] = true;
                $options[$temp_post_type_slug]['#before'] = '<li class="active">';
            }
        }
    }
    $form['table-pr-has-form'] = array('#type' => 'checkboxes', '#options' => $options, '#name' => 'ct[post_relationship]', '#inline' => true, '#before' => '<ul>', '#after' => '</ul>');
    $form = wpcf_form(__FUNCTION__, $form);
    echo $form->renderForm();
}