'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 ) ); ?>

%s', esc_url( sprintf( 'https://codesnippets.cloud/codevault/%s', $item->codevault ) ), esc_html( $item->codevault ) ); ?>

updated ) ) ) ); ?>
cloud_api->get_status_name_from_status( $item->status ); printf( '%s', esc_attr( sanitize_title_with_dashes( strtolower( $status_name ) ) ), esc_html( $status_name ) ); ?>
', esc_html__( 'Not indicated by author', 'code-snippets' ), ''; } else { // translators: tested status. $text = sprintf( __( 'Author states %s', 'code-snippets' ), $wp_tested ); echo '', esc_html( $text ), ''; } ?>
'#TB_inline?&width=700&height=500&inlineId=show-code-preview', 'cloud-snippet-downloaded' => false, ]; } /** * Process the description text - limit to 150 characters. * * @param string|null $description Description as provided by the API. * * @return string formatted description string max 150 chars. */ protected function process_description( ?string $description ): string { $description = wp_strip_all_tags( $description ); return strlen( $description ) > 150 ? substr( $description, 0, 150 ) . '…' : $description; } /** * Text displayed when no snippet data is available. * * @return void */ public function no_items() { if ( ! empty( $_REQUEST['cloud_search'] ) && count( $this->cloud_snippets->snippets ) < 1 ) { echo '

', 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 = "
$output
"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->_pagination; // echo wp_kses_post( $this->_pagination ); TODO: This removes the top input box for page number. } }