'cloud-snippet', 'plural' => 'cloud-snippets', 'ajax' => false, ] ); // Strip the result query arg from the URL. $_SERVER['REQUEST_URI'] = remove_query_arg( [ 'result' ] ); $this->cloud_api = code_snippets()->cloud_api; } /** * Prepare items for the table. * * @return void */ public function prepare_items() { $this->cloud_snippets = $this->fetch_snippets(); $this->items = $this->cloud_snippets->snippets; $this->process_actions(); $this->set_pagination_args( [ 'per_page' => count( $this->cloud_snippets->snippets ), 'total_items' => $this->cloud_snippets->total_snippets, 'total_pages' => $this->cloud_snippets->total_pages, ] ); } /** * Process any actions that have been submitted, such as downloading cloud snippets to the local database. * * @return void */ public function process_actions() { $_SERVER['REQUEST_URI'] = remove_query_arg( [ 'action', 'snippet', '_wpnonce', 'source', 'cloud-bundle-run', 'cloud-bundle-show', 'bundle_share_name', 'cloud_bundles' ] ); if ( isset( $_REQUEST['action'], $_REQUEST['snippet'], $_REQUEST['source'] ) ) { cloud_lts_process_download_action( sanitize_key( wp_unslash( $_REQUEST['action'] ) ), sanitize_key( wp_unslash( $_REQUEST['source'] ) ), sanitize_key( wp_unslash( $_REQUEST['snippet'] ) ) ); } } /** * Output table rows. * * @return void */ public function display_rows() { /** * The current table item. * * @var $item Cloud_Snippet */ foreach ( $this->items as $item ) { ?>
process_description( $item->description ) ); ?>
', esc_html__( 'No snippets or codevault could be found with that search term. Please try again.', 'code-snippets' ), '
'; } else { echo '', esc_html__( 'Please enter a term to start searching code snippets in the cloud.', 'code-snippets' ), '
'; } } /** * Fetch the snippets used to populate the table. * * @return Cloud_Snippets */ public function fetch_snippets(): Cloud_Snippets { // Check if search term has been entered. if ( isset( $_REQUEST['type'], $_REQUEST['cloud_search'], $_REQUEST['cloud_select'] ) && 'cloud_search' === sanitize_key( wp_unslash( $_REQUEST['type'] ) ) ) { // If we have a search query, then send a search request to cloud server API search endpoint. $search_query = sanitize_text_field( wp_unslash( $_REQUEST['cloud_search'] ) ); $search_by = sanitize_text_field( wp_unslash( $_REQUEST['cloud_select'] ) ); return Cloud_API::fetch_search_results( $search_by, $search_query, $this->get_pagenum() - 1 ); } // If no search results, then return empty object. return new Cloud_Snippets(); } /** * Gets the current search result page number. * * @return integer */ public function get_pagenum(): int { $page = isset( $_REQUEST['search_page'] ) ? absint( $_REQUEST['search_page'] ) : 0; if ( isset( $this->_pagination_args['total_pages'] ) && $page > $this->_pagination_args['total_pages'] ) { $page = $this->_pagination_args['total_pages']; } return max( 1, $page ); } /** * Display the table. * * @return void */ public function display() { Cloud_API::render_cloud_snippet_thickbox(); parent::display(); } /** * Displays the pagination. * * @param string $which Context where the pagination will be displayed. * * @return void */ protected function pagination( $which ) { $total_items = $this->_pagination_args['total_items']; $total_pages = $this->_pagination_args['total_pages']; $pagenum = $this->get_pagenum(); if ( 'top' === $which && $total_pages > 1 ) { $this->screen->render_screen_reader_content( 'heading_pagination' ); } $paginate = cloud_lts_pagination( $which, 'search', $total_items, $total_pages, $pagenum ); $page_class = $paginate['page_class']; $output = $paginate['output']; $this->_pagination = "