/** * Provide IDs of posts you want to add to the current batch. * * @param Batch $batch * @param array $post_ids */ private function add_posts(Batch $batch, $post_ids) { $post_ids = apply_filters('sme_prepare_post_ids', $post_ids); $post_ids = array_unique($post_ids); $posts = $this->post_dao->find_by_ids($post_ids); foreach ($posts as $post) { $this->add_post($batch, $post); } }
public function init() { $order_by = 'post_modified'; $order = 'desc'; $per_page = 10; $paged = 1; $status = array('draft'); $posts = array(); if (isset($_GET['orderby'])) { $order_by = $_GET['orderby']; } if (isset($_GET['order'])) { $order = $_GET['order']; } if (isset($_GET['per_page'])) { $per_page = $_GET['per_page']; } if (isset($_GET['paged'])) { $paged = $_GET['paged']; } $count = $this->batch_dao->count($status); $batches = $this->batch_dao->get_batches($status, $order_by, $order, $per_page, $paged); foreach ($batches as $batch) { // Get IDs of posts user selected to include in this batch. $post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post'); if (!is_array($post_ids)) { $post_ids = array(); } $posts = $this->post_dao->find_by_ids($post_ids); $batch->set_posts($posts); } // Prepare table of batches. $table = new Batch_History_Table(); $table->items = $batches; $table->set_pagination_args(array('total_items' => $count, 'per_page' => $per_page)); $table->prepare_items(); $data = array('table' => $table); $this->template->render('batch-history', $data); }
/** * Edit a content batch. Lets the user decide what posts to put in the * batch. */ public function edit_batch() { $batch_id = null; $order_by = 'post_modified'; $order = 'desc'; $per_page = 50; $paged = 1; // Make sure a query param ID exists in current URL. if (!isset($_GET['id'])) { wp_die(__('No batch ID has been provided.', 'sme-content-staging')); } // Get batch ID from URL query param. if ($_GET['id'] > 0) { $batch_id = intval($_GET['id']); } $batch = $this->api->get_batch($batch_id); if (isset($_GET['orderby'])) { $order_by = $_GET['orderby']; } if (isset($_GET['order'])) { $order = $_GET['order']; } if (isset($_GET['per_page'])) { $per_page = $_GET['per_page']; } if (isset($_GET['paged'])) { $paged = $_GET['paged']; } // Get IDs of posts user has selected to include in this batch. $post_ids = $this->batch_dao->get_post_meta($batch->get_id(), 'sme_selected_post'); /* * When fetching post IDs an empty string could be returned if no * post meta record with the given key exist since before. To * ensure the system can rely on us working with an array we perform a * check setting $post_ids to array if it is currently an empty. */ if (!$post_ids) { $post_ids = array(); } // Get selected posts. $selected_posts = array(); $chunks = array_chunk($post_ids, $per_page); if (isset($chunks[$paged - 1])) { $use_post_ids = $chunks[$paged - 1]; $selected_posts = $this->post_dao->find_by_ids($use_post_ids); } $status = apply_filters('sme_post_list_statuses', array('publish')); // Get posts user can select to include in the batch. $posts = $this->post_dao->get_posts($status, $order_by, $order, $per_page, $paged, $post_ids); $total_posts = $this->post_dao->get_posts_count($status); $posts = array_merge($selected_posts, $posts); // Create and prepare table of posts. $table = new Post_Table($batch); $table->items = $posts; $table->set_pagination_args(array('total_items' => $total_posts, 'per_page' => $per_page)); $table->prepare_items(); $type = get_post_type_object('sme_content_batch'); if (!$batch->get_id()) { $label = $type->labels->new_item; } else { $label = $type->labels->edit_item; } // Custom filters for finding posts to include in batch. $filters = apply_filters('sme_post_filters', $filters = '', $table); $data = array('batch' => $batch, 'label' => $label, 'filters' => $filters, 'table' => $table, 'post_ids' => implode(',', $post_ids)); $this->template->render('edit-batch', $data); }