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.")); } }
/** * Method to add a PostLink * * @access public * * @param int $pt_link_id * @param int $parent_id * @param int $child_id * * @return int ($link_id) */ public function add($pt_link_id, $parent_id, $child_id) { // Post Type Link Manager $post_type_link_manager = new SP_Connection_Manager(); // Get the Post Type Link $ptl = $post_type_link_manager->get_link($pt_link_id); // Create post link $link_id = wp_insert_post(array('post_title' => 'sp_' . $ptl->get_slug(), 'post_type' => SP_Constants::CPT_LINK, 'post_status' => 'publish', 'menu_order' => $this->get_link_count($pt_link_id, $parent_id))); // Create post meta add_post_meta($link_id, SP_Constants::PM_PT_LINK, $pt_link_id); add_post_meta($link_id, SP_Constants::PM_PARENT, $parent_id); add_post_meta($link_id, SP_Constants::PM_CHILD, $child_id); // Action do_action('sp_after_link_add', $link_id); // Return link id return $link_id; }
/** * 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 }