'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() { $per_page = $this->get_items_per_page( 'snippets_per_page', 10 ); $user_per_page = (int) get_user_option( 'snippets_per_page', get_current_user_id() ); if ( $user_per_page > 0 ) { $per_page = $user_per_page; } // Fetch snippets, passing a 0-based page index to the Cloud API (WP list tables are 1-based). $page_index = max( 0, $this->get_pagenum() - 1 ); $this->cloud_snippets = $this->fetch_snippets( $per_page, $page_index ); $this->items = $this->cloud_snippets->snippets; $this->process_actions(); $this->set_pagination_args( [ 'per_page' => $per_page, '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' ] ); // Check request is coming from the cloud search page. if ( ! isset( $_REQUEST['type'] ) || 'cloud_search' !== sanitize_key( wp_unslash( $_REQUEST['type'] ) ) ) { return; } if ( ! isset( $_REQUEST['action'], $_REQUEST['snippet'], $_REQUEST['source'] ) ) { return; } $action = sanitize_key( wp_unslash( $_REQUEST['action'] ) ); $source = sanitize_key( wp_unslash( $_REQUEST['source'] ) ); $snippet_id = absint( wp_unslash( $_REQUEST['snippet'] ) ); if ( ! in_array( $action, [ 'download', 'update' ], true ) ) { return; } if ( ! $snippet_id ) { return; } check_admin_referer( cloud_lts_get_snippet_action_nonce_action( $action, $snippet_id, $source ) ); cloud_lts_process_download_action( $action, $source, (string) $snippet_id, ); } /** * Output table rows. * * @return void */ public function display_rows() { $status_descriptions = [ Cloud_API::STATUS_PUBLIC => __( 'Snippet has passed basic review.', 'code-snippets' ), Cloud_API::STATUS_AI_VERIFIED => __( 'Snippet has been tested by our AI bot.', 'code-snippets' ), Cloud_API::STATUS_UNVERIFIED => __( 'Snippet has not undergone any review yet.', 'code-snippets' ), ]; /** * 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( int $per_page = 10, int $page_index = 0 ): 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'] ) ); // Pass the provided 0-based page index to the API. return Cloud_API::fetch_search_results( $search_by, $search_query, $page_index ); } // 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 ) { if ( empty( $this->_pagination_args ) ) { return; } $total_items = $this->_pagination_args['total_items'] ?? 0; $total_pages = $this->_pagination_args['total_pages'] ?? 0; // get_pagenum already returns a 1-based page number used for display. $pagenum_display = $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_display ); $page_class = $paginate['page_class']; $output = $paginate['output']; $this->_pagination = "