*/ namespace RankMath\Redirections; use RankMath\Helper; use MyThemeShop\Helpers\Param; use MyThemeShop\Admin\List_Table; /** * Table class. */ class Table extends List_Table { /** * The Constructor. */ public function __construct() { parent::__construct([ 'singular' => esc_html__( 'redirection', 'rank-math' ), 'plural' => esc_html__( 'redirections', 'rank-math' ), 'no_items' => $this->is_trashed_page() ? esc_html__( 'No redirections found in Trash.', 'rank-math' ) : wp_kses_post( __( 'No redirections added yet. Add New Redirection', 'rank-math' ) ), ]); } /** * Prepares the list of items for displaying. */ public function prepare_items() { global $per_page; $per_page = $this->get_items_per_page( 'rank_math_redirections_per_page' ); $data = DB::get_redirections([ 'limit' => $per_page, 'order' => $this->get_order(), 'orderby' => $this->get_orderby( 'id' ), 'paged' => $this->get_pagenum(), 'search' => $this->get_search(), 'status' => Param::request( 'status', 'any' ), ]); $this->items = $data['redirections']; $this->set_pagination_args([ 'total_items' => $data['count'], 'per_page' => $per_page, ]); } /** * Handles the checkbox column output. * * @codeCoverageIgnore * * @param object $item The current item. */ public function column_cb( $item ) { return sprintf( '', $item['id'] ); } /** * Handle the sources column. * * @param object $item The current item. */ protected function column_sources( $item ) { return $this->get_sources_html( maybe_unserialize( $item['sources'] ) ) . $this->column_actions( $item ); } /** * Handle the last accessed column. * * @param object $item The current item. */ protected function column_last_accessed( $item ) { $no_last_accessed = ( empty( $item['last_accessed'] ) || '0000-00-00 00:00:00' === $item['last_accessed'] ); return $no_last_accessed ? '' : mysql2date( 'F j, Y, G:i', $item['last_accessed'] ); } /** * Handles the default column output. * * @codeCoverageIgnore * * @param object $item The current item. * @param string $column_name The current column name. */ public function column_default( $item, $column_name ) { if ( in_array( $column_name, [ 'hits', 'header_code', 'url_to' ], true ) ) { return $item[ $column_name ]; } return print_r( $item, true ); } /** * Get html for sources column * * @codeCoverageIgnore * * @param array $sources Array of sources. * @return string */ private function get_sources_html( $sources ) { if ( empty( $sources ) ) { return ''; } $comparison_hash = Helper::choices_comparison_types(); // First one. $html = $this->get_source_html( $sources[0], $comparison_hash ); unset( $sources[0] ); if ( empty( $sources ) ) { return $html; } // Show more button. $html .= ' […]'; $html .= '
'; // Loop remaining. $parts = []; foreach ( $sources as $source ) { $parts[] = $this->get_source_html( $source, $comparison_hash ); } $html .= join( '
', $parts ); $html .= '
[' . esc_html__( 'Hide', 'rank-math' ) . ']'; $html .= '
'; return $html; } /** * Get html of a source * * @codeCoverageIgnore * * @param array $source Source for which render html. * @param array $comparison_hash Comparison array hash. * @return string */ private function get_source_html( $source, $comparison_hash ) { $html = '' . esc_html( stripslashes( $source['pattern'] ) ) . ''; if ( 'exact' !== $source['comparison'] ) { $html .= ' (' . esc_html( $comparison_hash[ $source['comparison'] ] ) . ')'; } return $html; } /** * Generate row actions div. * * @codeCoverageIgnore * * @param object $item The current item. */ public function column_actions( $item ) { $url = esc_url( Helper::get_admin_url( 'redirections', [ 'redirection' => $item['id'], 'security' => wp_create_nonce( 'redirection_list_action' ), ] ) ); if ( $this->is_trashed_page() ) { return $this->row_actions([ 'restore' => '' . esc_html__( 'Restore', 'rank-math' ) . '', 'delete' => '' . esc_html__( 'Delete Permanently', 'rank-math' ) . '', ]); } return $this->row_actions([ 'edit' => '' . esc_html__( 'Edit', 'rank-math' ) . '', 'deactivate' => '' . esc_html__( 'Deactivate', 'rank-math' ) . '', 'activate' => '' . esc_html__( 'Activate', 'rank-math' ) . '', 'trash' => '' . esc_html__( 'Trash', 'rank-math' ) . '', ]); } /** * Get a list of columns. * * @return array */ public function get_columns() { return [ 'cb' => '', 'sources' => esc_html__( 'From', 'rank-math' ), 'url_to' => esc_html__( 'To', 'rank-math' ), 'header_code' => esc_html__( 'Type', 'rank-math' ), 'hits' => esc_html__( 'Hits', 'rank-math' ), 'last_accessed' => esc_html__( 'Last Accessed', 'rank-math' ), ]; } /** * Get a list of sortable columns. * * @return array */ public function get_sortable_columns() { return [ 'url_to' => [ 'url_to', false ], 'header_code' => [ 'header_code', false ], 'hits' => [ 'hits', false ], 'last_accessed' => [ 'last_accessed', false ], ]; } /** * Get an associative array ( option_name => option_title ) with the list * of bulk actions available on this table. * * @codeCoverageIgnore * * @return array */ public function get_bulk_actions() { if ( $this->is_trashed_page() ) { return [ 'restore' => esc_html__( 'Restore', 'rank-math' ), 'delete' => esc_html__( 'Delete Permanently', 'rank-math' ), ]; } return [ 'activate' => esc_html__( 'Activate', 'rank-math' ), 'deactivate' => esc_html__( 'Deactivate', 'rank-math' ), 'trash' => esc_html__( 'Move to Trash', 'rank-math' ), ]; } /** * Get an associative array ( id => link ) with the list of views available on this table. * * @return array */ public function get_views() { $url = Helper::get_admin_url( 'redirections' ); $current = Param::get( 'status', 'all' ); $counts = DB::get_counts(); $labels = [ 'all' => esc_html__( 'All', 'rank-math' ), 'active' => esc_html__( 'Active', 'rank-math' ), 'inactive' => esc_html__( 'Inactive', 'rank-math' ), 'trashed' => esc_html__( 'Trash', 'rank-math' ), ]; $links = []; foreach ( $labels as $key => $label ) { $links[ $key ] = sprintf( '%3$s (%4$d)', $url . '&status=' . $key, $key === $current ? ' class="current"' : '', $label, number_format_i18n( $counts[ $key ] ) ); } return $links; } /** * Generates content for a single row of the table. * * @codeCoverageIgnore * * @param object $item The current item. */ public function single_row( $item ) { echo ''; $this->single_row_columns( $item ); echo ''; } /** * Prints extra table nav. * * @param string $which The position. Accepts `top` or `bottom`. */ public function extra_tablenav( $which ) { parent::extra_tablenav( $which ); if ( ! $this->is_trashed_page() ) { return; } $counts = DB::get_counts(); if ( empty( $counts['trashed'] ) || ! intval( $counts['trashed'] ) ) { return; } echo '
'; submit_button( esc_html__( 'Empty Trash', 'rank-math' ), '', 'delete_all', false ); echo '
'; } /** * Checks if is in trashed page. * * @return bool */ protected function is_trashed_page() { return 'trashed' === Param::get( 'status' ); } }