/**
  * Enrich the connection
  *
  * @param SP_Connection $connection
  *
  * @return SP_Connection $connection
  */
 public function run($connection)
 {
     // Check sortable
     $sortable = 0;
     if (isset($_POST['sortable']) && $_POST['sortable'] == '1') {
         $sortable = 1;
     }
     $connection->set_sortable($sortable);
     // Check the display parents
     $after_post_display_parents = 0;
     if (isset($_POST['after_post_display_parents']) && $_POST['after_post_display_parents'] == '1') {
         $after_post_display_parents = 1;
     }
     $connection->set_after_post_display_parents($after_post_display_parents);
     // Check the backwards linking
     $backwards_linking = 0;
     if (isset($_POST['backwards_linking']) && $_POST['backwards_linking'] == '1') {
         $backwards_linking = 1;
     }
     $connection->set_backwards_linking($backwards_linking);
     // Check APDC excerpt
     $apdc_excerpt = 0;
     if (isset($_POST['after_post_display_children_excerpt']) && $_POST['after_post_display_children_excerpt'] == '1') {
         $apdc_excerpt = 1;
     }
     $connection->set_after_post_display_children_excerpt($apdc_excerpt);
     // Check APDC image
     $apdc_image = 0;
     if (isset($_POST['after_post_display_children_image']) && $_POST['after_post_display_children_image'] == '1') {
         $apdc_image = 1;
     }
     $connection->set_after_post_display_children_image($apdc_image);
     return $connection;
 }
 /**
  * This function is used to create a connection between two post types.
  *
  * The following parameters should be passed when create a post type link:
  *
  * Slug (string) – The new slug of the PTL
  * Title (string) – The new title of the PTL
  * Parent (string) – The parent post type id
  * Child (string) – The child post type id
  * Sortable (int) – Whether the item is sortable. 1 = yes, 0 = no.
  * Add new (int) – Whether the user can add and link new items. 1 = yes, 0 = no.
  * Add existing (int) – Whether the user can link existing items. 1 = yes, 0 = no.
  * After post display children - Display child posts automatically on post. 1 = yes, 0 = no.
  * After post display parents - Display parent posts automatically on post. 1 = yes, 0 = no.
  * Backwards Linking - Set to 1 if you want to allow backwards linking. 1 = enabled, 2 = disabled.
  * Related - Whether this connection is a related conection or not. 1 = enabled, 2 = disabled.
  *
  * The function will return a SP_Connection object.
  *
  * @since  1.5.0
  * @access public
  *
  * @param $slug
  * @param $title
  * @param $parent
  * @param $child
  * @param $sortable
  * @param $add_new
  * @param $add_existing
  * @param $after_post_display_children
  * @param $after_post_display_parents
  * @param $backwards_linking
  * @param $deprecated
  *
  * @return SP_Connection
  */
 public function add_connection($slug, $title, $parent, $child, $sortable, $add_new, $add_existing, $after_post_display_children = 0, $after_post_display_parents = 0, $backwards_linking = 0, $deprecated = false)
 {
     // Create the Connection Manager
     $connection_manager = new SP_Connection_Manager();
     // Create the connection
     $connection = new SP_Connection();
     $connection->set_slug($slug);
     $connection->set_title($title);
     $connection->set_parent($parent);
     $connection->set_child($child);
     $connection->set_add_new($add_new);
     $connection->set_add_existing($add_existing);
     $connection->set_after_post_display_children($after_post_display_children);
     $connection->set_sortable($sortable);
     $connection->set_after_post_display_parents($after_post_display_parents);
     $connection->set_backwards_linking($backwards_linking);
     // Save the connection
     return $connection_manager->save($connection);
 }
    /**
     * Setup edit screen
     *
     * @access public
     * @return void
     */
    public function screen_edit()
    {
        $connection_manager = new SP_Connection_Manager();
        if (isset($_GET['id'])) {
            $connection = $connection_manager->get_connection($_GET['id']);
        } else {
            $connection = new SP_Connection();
        }
        // Get Post Types
        $raw_post_types = get_post_types(array('_builtin' => false), 'objects');
        $post_types = array();
        $post_types['post'] = __('Posts', 'post-connector');
        $post_types['page'] = __('Pages', 'post-connector');
        if (count($raw_post_types) > 0) {
            foreach ($raw_post_types as $pt_key => $post_type) {
                if ($pt_key == 'attachments' || $pt_key == 'sub_posts_link') {
                    continue;
                }
                $post_types[$pt_key] = $post_type->labels->name;
            }
        }
        ?>
		<div class="wrap">
			<h2>Post Connector: <?php 
        if (isset($_GET['id'])) {
            _e('Edit connection', 'post-connector');
        } else {
            _e('Create a new connection', 'post-connector');
        }
        ?>
</h2>

			<div class="pc-content pc-edit-screen">
				<form method="post" action="" id="pc-connection-form">
					<?php 
        // Add nonce
        wp_nonce_field(plugin_basename(__FILE__), 'sp_edit_nonce');
        if ($connection->get_id() != 0) {
            echo "<input type='hidden' name='id' value='" . $connection->get_id() . "' />\n";
        }
        echo "<input type='hidden' name='pc-ajax-nonce' id='pc-ajax-nonce' value='" . wp_create_nonce('post-connector-ajax-nonce-omgrandomword') . "' />\n";
        ?>
					<input type="hidden" name="sp_save_check" value="true" />

					<p><?php 
        _e("To create or edit a post connection, fill in the below form and press the 'save connection' button.", 'post-connector');
        ?>
</p>

					<h3 class="pc-title"><?php 
        _e('Main settings', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row">
								<label for="title" class="sp_label"><?php 
        _e('Connection Title', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<input type="text" name="title" value="<?php 
        echo $connection->get_title();
        ?>
" placeholder="<?php 
        _e('Title', 'post-connector');
        ?>
" class="widefat mandatory" id="title" />

								<p class="help"><?php 
        _e('The title of the connection, can be automatically shown above linked posts.', 'post-connector');
        ?>
</p>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="slug"><?php 
        _e('Connection Slug', 'post-connector');
        ?>
</label></th>
							<td colspan="2" class="nowrap">
								<input type="text" name="slug" value="<?php 
        echo $connection->get_slug();
        ?>
" placeholder="<?php 
        _e('Slug', 'post-connector');
        ?>
" class="widefat" id="slug"<?php 
        echo isset($_GET['id']) ? ' disabled="disabled"' : '';
        ?>
 />

								<p class="help"><?php 
        _e('Unique identifier of the connection, will automatically generate if left empty.', 'post-connector');
        ?>
</p>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="parent" class="sp_label"><?php 
        _e('Parent post type', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<select name="parent" class="widefat mandatory" id="parent">
									<option value="0"><?php 
        _e('Select a post type', 'post-connector');
        ?>
</option>
									<?php 
        if (count($post_types) > 0) {
            foreach ($post_types as $pt_key => $pt_name) {
                echo "<option value='{$pt_key}'" . ($connection->get_parent() == $pt_key ? " selected='selected'" : "") . ">{$pt_name}</option>";
            }
        }
        ?>
								</select>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row">
								<label for="child" class="sp_label"><?php 
        _e('Child post type', 'post-connector');
        ?>
</label>
							</th>
							<td colspan="2" class="nowrap">
								<select name="child" class="widefat mandatory" id="child">
									<option value="0"><?php 
        _e('Select a post type', 'post-connector');
        ?>
</option>
									<?php 
        if (count($post_types) > 0) {
            foreach ($post_types as $pt_key => $pt_name) {
                echo "<option value='{$pt_key}'" . ($connection->get_child() == $pt_key ? " selected='selected'" : "") . ">{$pt_name}</option>";
            }
        }
        ?>
								</select>
							</td>
						</tr>

						</tbody>
					</table>

					<h3 class="pc-title"><?php 
        _e('Create new link settings', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Add New', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="add_new" class=""><input type="checkbox" name="add_new" id="add_new" value="1"<?php 
        echo $connection->get_add_new() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Allow users to create and link new posts.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Add Existing', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="add_existing" class=""><input type="checkbox" name="add_existing" id="add_existing" value="1"<?php 
        echo $connection->get_add_existing() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Allow users to link existing posts.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

						</tbody>
					</table>

					<h3 class="pc-title"><?php 
        _e('Automatically display linked children', 'post-connector');
        ?>
</h3>

					<table class="form-table">
						<tbody>

						<tr valign="top">
							<th scope="row"><?php 
        _e('Display linked children?', 'post-connector');
        ?>
</th>
							<td colspan="2" class="nowrap">
								<label for="after_post_display_children" class=""><input type="checkbox" name="after_post_display_children" id="after_post_display_children" value="1"<?php 
        echo $connection->get_after_post_display_children() == '1' ? " checked='checked'" : "";
        ?>
 /> <?php 
        _e('Display the linked child posts under each parent post.', 'post-connector');
        ?>
								</label>
							</td>
						</tr>

                        <?php 
        /**
         * Action: 'pc_connection_edit_form_display_linked_children' - Allow adding custom fields to display linked children section
         *
         * @api SP_Connection $connection The Connection that is currently edited
         */
        do_action('pc_connection_edit_form_display_linked_children', $connection);
        ?>

						</tbody>
					</table>

					<?php 
        /**
         * Action: 'pc_connection_edit_form' - Allow adding custom fields to connection edit screen
         *
         * @api SP_Connection $connection The Connection that is currently edited
         */
        do_action('pc_connection_edit_form', $connection);
        ?>

					<p class="submit">
						<input name="save" type="submit" class="button-primary" id="publish" accesskey="p" value="<?php 
        _e('Save Connection', 'post-connector');
        ?>
">
					</p>
				</form>
			</div>
			<?php 
        $this->sidebar();
        ?>
		</div>
	<?php 
    }
 /**
  * Method to get a Connection by id
  *
  * @access public
  *
  * @param int $id
  *
  * @return SP_Connection
  */
 public function get_connection($id)
 {
     $connection = null;
     $connection_query = new WP_Query(array('p' => $id, 'post_type' => SP_Constants::CPT_PT_LINK, 'posts_per_page' => '1'));
     if ($connection_query->have_posts()) {
         $connection_query->next_post();
         // Create the connection
         $connection = new SP_Connection();
         // Set the connection properties
         $connection->set_id($connection_query->post->ID);
         $connection->set_slug(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_SLUG, true));
         $connection->set_title(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_TITLE, true));
         $connection->set_parent(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_PARENT, true));
         $connection->set_child(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_CHILD, true));
         $connection->set_add_new(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_ADD_NEW, true));
         $connection->set_add_existing(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_ADD_EXISTING, true));
         $connection->set_after_post_display_children(get_post_meta($connection_query->post->ID, SP_Constants::PM_PTL_APDC, true));
         /**
          * Action: 'pc_connection_init' - Action that is ran after the connection is initialized
          *
          * @api SP_Connection $connection The Connection
          */
         $connection = apply_filters('pc_connection_init', $connection);
     }
     return $connection;
 }
 /**
  * Enrich the connection
  *
  * @param SP_Connection $connection
  *
  * @return SP_Connection $connection
  */
 public function run($connection)
 {
     // Enricht the connection
     $connection->set_sortable(get_post_meta($connection->get_id(), SP_Constants::PM_PTL_SORTABLE, true));
     $connection->set_after_post_display_parents(get_post_meta($connection->get_id(), SP_Constants::PM_PTL_APDP, true));
     $connection->set_backwards_linking(get_post_meta($connection->get_id(), SP_Constants::PM_PTL_BACKWARDS, true));
     $connection->set_after_post_display_children_excerpt(get_post_meta($connection->get_id(), SP_Constants::PM_PTL_APDC_EXCERPT, true));
     $connection->set_after_post_display_children_image(get_post_meta($connection->get_id(), SP_Constants::PM_PTL_APDC_IMAGE, true));
     return $connection;
 }