public function prepare_items()
 {
     $columns = $this->get_columns();
     $hidden = array();
     $sortable = $this->get_sortable_columns();
     $this->_column_headers = array($columns, $hidden, $sortable);
     // Get Data
     $this->data = array();
     $pt_link_manager = new SP_Connection_Manager();
     /**
      * @todo Change te $pt_link_manager and the $link variables
      */
     foreach ($pt_link_manager->get_connections() as $link) {
         // Get parent
         $parent = get_post_types(array('name' => $link->get_parent()), 'object');
         $pt_parent = array_shift($parent);
         // Get child
         $child = get_post_types(array('name' => $link->get_child()), 'object');
         $pt_child = array_shift($child);
         $this->data[] = array('ID' => $link->get_id(), 'title' => $link->get_title(), 'slug' => $link->get_slug(), 'parent' => $pt_parent->labels->name, 'child' => $pt_child->labels->name);
     }
     // Sort
     if (count($this->data) > 0) {
         usort($this->data, array($this, 'custom_reorder'));
     }
     // Set
     $this->items = $this->data;
 }
 public function run()
 {
     // Check nonce
     if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'sp_ajax_sc_gpp')) {
         echo '-1';
         return;
     }
     $identifier = esc_sql($_POST['identifier']);
     $ptl_manager = new SP_Connection_Manager();
     if (isset($_POST['by_slug']) && 'true' == $_POST['by_slug']) {
         $ptl = $ptl_manager->get_link_by_slug($identifier);
     } else {
         $ptl = $ptl_manager->get_link($identifier);
     }
     // Get children
     $parent_posts = get_posts(array('post_type' => $ptl->get_child(), 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
     $json_posts = array();
     if (count($parent_posts) > 0) {
         foreach ($parent_posts as $parent_post) {
             $json_posts[$parent_post->ID] = $parent_post->post_title;
         }
     }
     // Send the JSON
     wp_send_json($json_posts);
     exit;
     // Better safe than sorry lol
 }
 /**
  * Check if current user is allowed to link posts
  *
  * @todo check if we can use $this->ptl instead of creating a new $ptl object
  *
  * @param $sp_pt_link
  */
 private function check_if_allowed($sp_pt_link)
 {
     // Check if user is allowed to do this
     $ptl_manager = new SP_Connection_Manager();
     $ptl = $ptl_manager->get_link($sp_pt_link);
     if ($ptl->get_add_new() != '1') {
         wp_die(__("You're not allowed to do this."));
     }
 }
 public function run()
 {
     $connection_manager = new SP_Connection_Manager();
     $pt_links = $connection_manager->get_connections();
     // Setup Meta Boxes
     if (count($pt_links) > 0) {
         foreach ($pt_links as $connection) {
             new SP_Meta_Box_Manage($connection);
             new SP_Meta_Box_Meta($connection);
             /**
              * Action: 'pc_meta_box_creation_after' - Allow actions to be added in meta box manage construct
              *
              * @api SP_Connection $connection The Connection used in this meta box
              */
             do_action('pc_meta_box_creation_after', $connection);
         }
     }
 }
 /**
  * the_content filter that will add linked posts to the bottom of the main post content
  *
  * @param $content
  *
  * @return string
  */
 public function run($content)
 {
     /**
      * Wow, what's going on here?! Well, setup_postdata() sets a lot of variables but does not change the $post variable.
      * All checks return the main queried ID but we want to check if this specific filter call is the for the 'main' content.
      * The method setup_postdata() does global and set the $id variable, so we're checking that.
      */
     global $id;
     // Only run on single
     if (!is_singular() || !is_main_query() || $id != get_queried_object_id()) {
         return $content;
     }
     $ptl_manager = new SP_Connection_Manager();
     // Add a meta query so we only get relations that have the PM_PTL_APDC or PM_PTL_APDP set to true (1)
     $args = array('meta_query' => array(array('key' => SP_Constants::PM_PTL_APDC, 'value' => '1')));
     // Get the connections
     $relations = $ptl_manager->get_connections($args);
     // Check relations
     if (count($relations) > 0) {
         // Post Link Manager
         $pl_manager = new SP_Post_Link_Manager();
         // Current post ID
         $post_id = get_the_ID();
         // Loop relations
         foreach ($relations as $relation) {
             // Check if this relation allows children links to show
             if ('1' === $relation->get_after_post_display_children()) {
                 $children_relations[] = $relation;
             }
         }
         // Opening the wrapper div
         $content .= "<div class='pc-post-children'>\n";
         foreach ($relations as $relation) {
             // Get the linked posts
             $pc_posts = $pl_manager->get_children($relation->get_slug(), $post_id);
             if (count($pc_posts) > 0) {
                 $content .= $this->create_post_list($relation->get_slug(), $relation->get_title(), $pc_posts);
             }
         }
         // Close the wrapper div
         $content .= "</div>\n";
     }
     return $content;
 }
 /**
  * Get children based on link_id and parent_id.
  * It's possible to add extra arguments to the WP_Query with the $extra_args argument
  *
  * @access public
  *
  * @param string $pt_slug
  * @param int $parent_id
  * @param array $extra_args
  *
  * @return array
  */
 public function get_children($pt_slug, $parent_id, $extra_args = null)
 {
     global $post;
     // Store current post
     $o_post = $post;
     // Only check if WP_DEBUG is true
     if (WP_DEBUG) {
         // Check if a PTL with given slug exists
         $ptl_manager = new SP_Connection_Manager();
         if (!$ptl_manager->slug_exists($pt_slug)) {
             // Trigger error
             trigger_error(sprintf(__("Slug '%s' does not exists"), $pt_slug), E_USER_NOTICE);
             // Return empty array for backwards compatibility
             return array();
         }
     }
     // Do WP_Query
     $link_args = $this->create_link_args($pt_slug, SP_Constants::PM_PARENT, $parent_id);
     /*
      * Check $extra_args for `posts_per_page`.
      * This is the only arg that should be added to link query instead of the child query
      */
     if (isset($extra_args['posts_per_page'])) {
         // Set posts_per_page to link arguments
         $link_args['posts_per_page'] = $extra_args['posts_per_page'];
         unset($extra_args['posts_per_page']);
     }
     /*
      * Check $extra_args for `order`.
      * If 'order' is set without 'orderby', we should add it to the link arguments
      */
     if (isset($extra_args['order']) && !isset($extra_args['orderby'])) {
         $link_args['order'] = $extra_args['order'];
         unset($extra_args['order']);
     }
     // Create link query
     $link_query = new WP_Query($link_args);
     // Store child ids
     // @todo remove the usage of get_the_id()
     $child_ids = array();
     while ($link_query->have_posts()) {
         $link_query->the_post();
         $child_ids[get_the_id()] = get_post_meta(get_the_id(), SP_Constants::PM_CHILD, true);
     }
     // Get children with custom args
     if ($extra_args !== null && count($extra_args) > 0) {
         if (!isset($extra_args['orderby'])) {
             $this->temp_child_order = array();
             foreach ($child_ids as $child_id) {
                 $this->temp_child_order[] = $child_id;
             }
         }
         // Get child again, but this time by $extra_args
         $children = array();
         //Child WP_Query arguments
         if (count($child_ids) > 0) {
             $child_id_values = array_values($child_ids);
             $child_post_type = get_post_type(array_shift($child_id_values));
             $child_args = array('post_type' => $child_post_type, 'posts_per_page' => -1, 'post__in' => $child_ids);
             // Extra arguments
             $child_args = array_merge_recursive($child_args, $extra_args);
             // Child Query
             $child_query = new WP_Query($child_args);
             while ($child_query->have_posts()) {
                 $child_query->the_post();
                 // Add post to correct original sort key
                 $children[array_search($child_query->post->ID, $child_ids)] = $child_query->post;
             }
             // Fix sorting
             if (!isset($extra_args['orderby'])) {
                 uasort($children, array($this, 'sort_get_children_children'));
             }
         }
     } else {
         // No custom arguments found, get all objects of stored ID's
         $children = array();
         foreach ($child_ids as $link_id => $child_id) {
             $children[$link_id] = get_post($child_id);
         }
     }
     // Reset global post variables
     wp_reset_postdata();
     // Restoring post
     $post = $o_post;
     // Return children
     return $children;
 }
 public function form($instance)
 {
     $instance = array_merge($this->default_vars, $instance);
     $selected_link = null;
     echo "<div class='pc_ajax_child'>\n";
     wp_nonce_field('sp_ajax_sc_gpp', 'sp_widget_child_nonce');
     echo "<p>";
     echo '<label for="' . $this->get_field_id('title') . '">' . __('Title', 'post-connector') . ':</label>';
     echo '<input class="widefat" id="' . $this->get_field_id('title') . '" type="text" name="' . $this->get_field_name('title') . '" value="' . esc_attr($instance['title']) . '" />';
     echo "</p>\n";
     echo "<p>";
     echo '<label for="' . $this->get_field_id('postlink') . '">' . __('Post Link', 'post-connector') . ':</label>';
     // Get the connections
     $connection_manager = new SP_Connection_Manager();
     $links = $connection_manager->get_connections();
     echo '<select class="widefat postlink" name="' . $this->get_field_name('postlink') . '" id="' . $this->get_field_id('postlink') . '" >';
     echo '<option value="0">' . __('Select Post Link', 'post-connector') . '</option>';
     if (count($links) > 0) {
         foreach ($links as $link) {
             echo '<option value="' . $link->get_id() . '"';
             if ($link->get_id() == $instance['postlink']) {
                 echo ' selected="selected"';
                 $selected_link = $link;
             }
             echo '>' . $link->get_title() . '</option>';
         }
     }
     echo '</select>';
     echo "</p>\n";
     echo "<p>";
     echo '<label for="' . $this->get_field_id('parent') . '">' . __('Parent', 'post-connector') . ':</label>';
     echo '<select class="widefat child" name="' . $this->get_field_name('parent') . '" id="' . $this->get_field_id('parent') . '" >';
     if ($selected_link != null) {
         $parent_posts = get_posts(array('post_type' => $selected_link->get_parent(), 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC'));
         if (count($parent_posts) > 0) {
             echo "<option value='current'>" . __('Current page', 'post-connector') . "</option>\n";
             foreach ($parent_posts as $parent_post) {
                 echo "<option value='{$parent_post->ID}'";
                 if ($parent_post->ID == $instance['parent']) {
                     echo " selected='selected'";
                 }
                 echo ">{$parent_post->post_title}</option>\n";
             }
         }
     }
     echo '</select>';
     echo "</p>\n";
     echo "<p>";
     echo '<label for="' . $this->get_field_id('link') . '">' . __('Make children clickable', 'post-connector') . ':</label>';
     echo '<select class="widefat" name="' . $this->get_field_name('link') . '" id="' . $this->get_field_id('link') . '" >';
     echo '<option value="true"' . ($instance['link'] == true ? ' selected="selected"' : '') . '>' . __('Yes', 'post-connector') . '</option>';
     echo '<option value="false"' . ($instance['link'] == false ? ' selected="selected"' : '') . '>' . __('No', 'post-connector') . '</option>';
     echo '</select>';
     echo "</p>\n";
     echo "<p>";
     echo '<label for="' . $this->get_field_id('excerpt') . '">' . __('Display excerpt', 'post-connector') . ':</label>';
     echo '<select class="widefat" name="' . $this->get_field_name('excerpt') . '" id="' . $this->get_field_id('excerpt') . '" >';
     echo '<option value="true"' . ($instance['excerpt'] == true ? ' selected="selected"' : '') . '>' . __('Yes', 'post-connector') . '</option>';
     echo '<option value="false"' . ($instance['excerpt'] == false ? ' selected="selected"' : '') . '>' . __('No', 'post-connector') . '</option>';
     echo '</select>';
     echo "</p>\n";
     echo "<p>";
     echo '<label for="' . $this->get_field_id('thumbnail') . '">' . __('Display thumbnail', 'post-connector') . ':</label>';
     echo '<select class="widefat" name="' . $this->get_field_name('thumbnail') . '" id="' . $this->get_field_id('thumbnail') . '" >';
     echo '<option value="true"' . ($instance['thumbnail'] == true ? ' selected="selected"' : '') . '>' . __('Yes', 'post-connector') . '</option>';
     echo '<option value="false"' . ($instance['thumbnail'] == false ? ' selected="selected"' : '') . '>' . __('No', 'post-connector') . '</option>';
     echo '</select>';
     echo "</p>\n";
     echo "</div>\n";
 }
 /**
  * This function is used to delete a post type link by the post type link ID.
  * Note that this will also delete all created links between posts using this post type link.
  *
  * @since  1.0.0
  * @access public
  *
  * @param $ptl_id
  *
  * @return bool
  */
 public function delete_post_type_link($ptl_id)
 {
     $post_type_link_manager = new SP_Connection_Manager();
     return $post_type_link_manager->delete($ptl_id);
 }
    public function run($post_id)
    {
        if (in_array(basename($_SERVER['SCRIPT_FILENAME']), array('post.php', 'page.php', 'page-new.php', 'post-new.php'))) {
            add_thickbox();
            ?>
			<div id="sp_tb_shortcode" style="height: 500px; display:none;">
				<div class="wrap">
					<div>
						<div style="padding:15px 15px 0 15px;">
							<h2><?php 
            _e('Insert Post Connector show_children shortcode', 'post-connector');
            ?>
</h2>
							<span><?php 
            _e('Use the form below to generate a show_children shortcode.', 'post-connector');
            ?>
</span>
						</div>
						<div style="padding:15px 15px 0 15px;">

							<?php 
            echo "<div class='sp_showchilds_ajax'>\n";
            wp_nonce_field('sp_ajax_sc_gpp', 'sp_widget_child_nonce');
            echo '<input type="hidden" name="by_slug" id="by_slug" value="true" />';
            echo "<p>";
            echo '<label for="sp_sc_postlink">' . __('Connection', 'post-connector') . ':</label>';
            // Get the connections
            $connection_manger = new SP_Connection_Manager();
            $connections = $connection_manger->get_connections();
            echo '<select class="widefat mandatory postlink" name="sp_sc_postlink" id="sp_sc_postlink" >';
            echo '<option value="0">' . __('Select Connection', 'post-connector') . '</option>';
            if (count($connections) > 0) {
                foreach ($connections as $connection) {
                    echo '<option value="' . get_post_meta($connection->get_id(), SP_Constants::PM_PTL_SLUG, true) . '">' . $connection->get_title() . '</option>';
                }
            }
            echo '</select>';
            echo "</p>\n";
            echo "<p>";
            echo '<label for="sp_sc_parent">' . __('Parent', 'post-connector') . ':</label>';
            echo '<select class="widefat mandatory parent" name="sp_sc_parent" id="sp_sc_parent" >';
            echo '</select>';
            echo "</p>\n";
            echo "<p>";
            echo '<label for="sp_sc_link">' . __('Make children clickable', 'post-connector') . ':</label>';
            echo '<select class="widefat" name="sp_sc_link" id="sp_sc_link" >';
            echo '<option value="true">Yes</option>';
            echo '<option value="false">No</option>';
            echo '</select>';
            echo "</p>\n";
            echo "<p>";
            echo '<label for="sp_sc_excerpt">' . __('Display excerpt', 'post-connector') . ':</label>';
            echo '<select class="widefat" name="sp_sc_excerpt" id="sp_sc_excerpt" >';
            echo '<option value="true">Yes</option>';
            echo '<option value="false">No</option>';
            echo '</select>';
            echo "</p>\n";
            echo "</div>\n";
            ?>

						</div>


						<div style="padding:15px 15px 0;">
							<input type="button" class="button-primary" value="<?php 
            _e('Insert Shortcode', 'post-connector');
            ?>
" onclick="insertShortcode_ShowChilds();" />&nbsp;&nbsp;&nbsp;
							<a class="button" style="color:#bbb;" href="#" onclick="tb_remove(); return false;"><?php 
            _e('Cancel', 'post-connector');
            ?>
</a>
						</div>
					</div>
				</div>
			</div>
		<?php 
        }
    }
 /**
  * AJAX method to delete link
  *
  * @access public
  * @return void
  */
 public function ajax_delete_link()
 {
     // Check if user is allowed to do this
     if (!current_user_can('manage_options')) {
         return;
     }
     // Check nonce
     if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], plugin_basename(__FILE__))) {
         return;
     }
     // Delete PT link
     $pt_type_manager = new SP_Connection_Manager();
     $pt_type_manager->delete($_POST['id']);
     // Generate JSON response
     $response = json_encode(array('success' => true));
     header('Content-Type: application/json');
     echo $response;
     // Bye
     exit;
 }
    /**
     * The screen content
     */
    public function link_post_screen_content()
    {
        // Get the connection
        $ptl_manager = new SP_Connection_Manager();
        $connection = $ptl_manager->get_link($_GET['sp_pt_link']);
        // Parent
        $parent = SP_Parent_Param::get_current_parent($_GET['sp_parent']);
        // Get child post type
        if ('1' == $parent[2]) {
            $post_type = get_post_type_object($connection->get_parent());
        } else {
            $post_type = get_post_type_object($connection->get_child());
        }
        // Setup cancel URL
        $cancel_url = get_admin_url() . "post.php?post={$parent[0]}&action=edit";
        // Check if parent as a ptl
        if (isset($parent[1]) && $parent[1] != '') {
            $cancel_url .= '&sp_pt_link=' . $parent[1];
        }
        // Check if there are any parents left
        $sp_parent_rest = SP_Parent_Param::strip_sp_parent_parent($_GET['sp_parent']);
        if ($sp_parent_rest != '') {
            $cancel_url .= '&sp_parent=' . $sp_parent_rest;
        }
        // Catch search string
        $search = null;
        if (isset($_POST['s']) && $_POST['s'] != '') {
            $search = $_POST['s'];
        }
        ?>
		<div class="wrap">
			<h2>
				<?php 
        echo $post_type->labels->name;
        ?>
				<a href="<?php 
        echo $cancel_url;
        ?>
" class="add-new-h2"><?php 
        _e('Cancel linking', 'post-connector');
        ?>
</a>
			</h2>

			<form id="sp-list-table-form" method="post">
				<input type="hidden" name="page" value="<?php 
        echo $_REQUEST['page'];
        ?>
" />
				<?php 
        // Create the link table
        $list_table = new SP_Create_Link_List_Table($post_type->name, $connection);
        // Set the search
        $list_table->set_search($search);
        // Load the items
        $list_table->prepare_items();
        // Add the search box
        $list_table->search_box(__('Search', 'post-connector'), 'sp-search');
        // Display the table
        $list_table->display();
        ?>
			</form>
		</div>

	<?php 
    }
 /**
  * An update is required, do it
  *
  * @param $current_version
  */
 private function do_update($current_version)
 {
     /**
      * Move the current version option, a tricky upgrade.
      * This upgrade routine was introduced in version 1.5.2
      */
     if ($current_version == 1) {
         // Get the original option and save it as the current version.
         // If there is no original option the $current_version will remain 1.
         $current_version = get_option('sp_current_version', 1);
         // Only resave the version if it changed.
         if (1 != $current_version) {
             // Set the new version option
             update_option(SP_Constants::OPTION_CURRENT_PREMIUM_VERSION, $current_version);
             // Delete the old version option
             delete_option('sp_current_version');
         }
     }
     // < 1.5.2
     if ($current_version < 22) {
         /**
          * Upgrade to version 1.5.2
          *
          * - Save the license key in the new option and remove the old license option
          */
         // Resave the license key
         $license_key = get_option('post-connector_license', '');
         if ('' != $license_key) {
             update_option('post-connector-premium_license', $license_key);
             delete_option('connector_license');
         }
     }
     // < 1.4.0
     if ($current_version < 18) {
         /**
          * Upgrade to version 1.4.0.0
          *
          * - Change the title of all post link to the post type link slug, see #123 for more info.
          * - Remove 'sp_install_version' option from database, this option was and will never be used.
          */
         // -- Remove 'sp_install_version' option from database, this option was and will never be used.
         delete_option('sp_install_version');
         // -- Change the title of all post link to the post type link slug, see #123 for more info.
         // Post Type Link Manager
         $post_type_link_manager = new SP_Connection_Manager();
         // Add post title check filter
         add_filter('posts_clauses', array($this, 'alter_slug_update_query'), 10, 2);
         // Update all link titles to new slug setup
         $post_links = get_posts(array('post_type' => SP_Constants::CPT_LINK, 'posts_per_page' => -1, 'suppress_filters' => false));
         // Remove the check filter
         remove_filter('posts_clauses', array($this, 'alter_slug_update_query'));
         if (count($post_links) > 0) {
             foreach ($post_links as $post_link) {
                 // Get Post Type Link ID
                 $ptl_id = get_post_meta($post_link->ID, 'sp_pt_link', true);
                 // Check if PTL ID is != ''
                 if ('' != $ptl_id) {
                     // Get the Connection object
                     $ptl = $post_type_link_manager->get_connection($ptl_id);
                     // Update the post link with the new title
                     wp_update_post(array('ID' => $post_link->ID, 'post_title' => 'sp_' . $ptl->get_slug()));
                 }
             }
         }
     }
     // < 1.5.0
     if ($current_version < 20) {
         /**
          * Upgrade to version 1.5.0
          *
          * - Change the title of all post link to the post type link slug, see #123 for more info.
          * - Remove 'sp_install_version' option from database, this option was and will never be used.
          */
         // Migrate license key and status
         $license_manager = new Yoast_Plugin_License_Manager(new SP_Product_Post_Connector());
         $license_manager->set_license_key(get_option('subposts_license_key', ''));
         $license_manager->set_license_status(get_option('subposts_license_status', ''));
     }
 }