'site-traffic-views', 'singular' => 'site-traffic-view', 'ajax' => false, ] ); $this->show_on_front = get_option( 'show_on_front' ); $this->page_on_front = 'page' === $this->show_on_front ? absint( get_option( 'page_on_front' ) ) : 0; $this->page_for_posts = 'page' === $this->show_on_front ? absint( get_option( 'page_for_posts' ) ) : 0; } /** * Define table columns. * * @return array */ public function get_columns() { return [ 'rank' => __( 'Rank', 'admin-optimizer' ), 'post' => __( 'Post', 'admin-optimizer' ), 'url_path' => __( 'URL Path', 'admin-optimizer' ), 'type' => __( 'Type', 'admin-optimizer' ), 'pageviews' => __( 'Total Page Views', 'admin-optimizer' ), ]; } /** * Prepare rows for display. * * @param array $items Ranked traffic rows. * @return void */ public function prepare_items( array $items = [] ) { $this->_column_headers = [ $this->get_columns(), [], [] ]; $this->items = array_values( $items ); } /** * Render the empty state. * * @return void */ public function no_items() { esc_html_e( 'No page view data has been recorded yet.', 'admin-optimizer' ); } /** * Render a fallback column value. * * @param array $item Row data. * @param string $column_name Column key. * @return string */ public function column_default( $item, $column_name ) { return isset( $item[ $column_name ] ) ? esc_html( (string) $item[ $column_name ] ) : ''; } /** * Render rank. * * @param array $item Row data. * @return string */ public function column_rank( $item ) { return esc_html( (string) ( $item['_rank'] ?? '' ) ); } /** * Render the post or view label. * * @param array $item Row data. * @return string */ public function column_post( $item ) { $post_id = isset( $item['post_id'] ) ? absint( $item['post_id'] ) : 0; if ( $post_id ) { $title = isset( $item['post_title'] ) && '' !== $item['post_title'] ? $item['post_title'] : __( '(no title)', 'admin-optimizer' ); } else { $url_path = isset( $item['url_path'] ) ? (string) $item['url_path'] : ''; $title = isset( $item['label'] ) && '' !== $item['label'] ? $item['label'] : $url_path; } return esc_html( $title ); } /** * Render a linked URL path when a view URL is available. * * @param array $item Row data. * @return string */ public function column_url_path( $item ) { $post_id = isset( $item['post_id'] ) ? absint( $item['post_id'] ) : 0; $url_path = isset( $item['url_path'] ) ? (string) $item['url_path'] : ''; $view_url = ''; if ( $post_id ) { $view_url = get_permalink( $post_id ); $url_path = $view_url ? (string) wp_parse_url( $view_url, PHP_URL_PATH ) : ''; } elseif ( '' !== $url_path ) { $view_url = home_url( $url_path ); } if ( ! $view_url ) { return esc_html( $url_path ); } return '' . esc_html( $url_path ) . ''; } /** * Render the view type label. * * @param array $item Row data. * @return string */ public function column_type( $item ) { $post_id = isset( $item['post_id'] ) ? absint( $item['post_id'] ) : 0; if ( ! $post_id ) { $url_path = isset( $item['url_path'] ) ? (string) $item['url_path'] : ''; $label = ( 'posts' === $this->show_on_front && '/' === $url_path ) ? __( 'Page (Home Page)', 'admin-optimizer' ) : __( 'Page', 'admin-optimizer' ); return esc_html( $label ); } $post_type = isset( $item['post_type'] ) ? get_post_type_object( $item['post_type'] ) : null; $label = $post_type ? $post_type->labels->singular_name : ( isset( $item['post_type'] ) ? $item['post_type'] : '' ); if ( $this->page_on_front && $post_id === $this->page_on_front ) { /* translators: %s: post type label (e.g. "Page"). */ $label = sprintf( __( '%s (Home Page)', 'admin-optimizer' ), $label ); } elseif ( $this->page_for_posts && $post_id === $this->page_for_posts ) { /* translators: %s: post type label (e.g. "Page"). */ $label = sprintf( __( '%s (Posts Page)', 'admin-optimizer' ), $label ); } return esc_html( $label ); } /** * Render formatted page views. * * @param array $item Row data. * @return string */ public function column_pageviews( $item ) { return esc_html( number_format_i18n( isset( $item['pageview'] ) ? (int) $item['pageview'] : 0 ) ); } }