'telemetry_log', 'plural' => 'telemetry_logs', 'ajax' => false, ) ); } /** * Get columns. * * @return array */ public function get_columns() { return array( 'created_at' => __( 'Date', 'robotstxt-telemetry' ), 'method' => __( 'Method', 'robotstxt-telemetry' ), 'host' => __( 'Host', 'robotstxt-telemetry' ), 'path' => __( 'Path', 'robotstxt-telemetry' ), ); } /** * Prepare items for display. * * @return void */ public function prepare_items() { global $wpdb; $table_name = Robotstxt_Telemetry_DB::get_table_name(); $paged = max( 1, $this->get_pagenum() ); $offset = ( $paged - 1 ) * $this->per_page; $columns = $this->get_columns(); $this->_column_headers = array( $columns, array(), array() ); $method = strtoupper( $this->get_filter_value( 'method' ) ); $host = $this->get_filter_value( 'host' ); $date_from = $this->sanitize_date_filter( $this->get_filter_value( 'date_from' ) ); $date_from_value = ! empty( $date_from ) ? $date_from . ' 00:00:00' : ''; $date_to = $this->sanitize_date_filter( $this->get_filter_value( 'date_to' ) ); $date_to_value = ! empty( $date_to ) ? $date_to . ' 23:59:59' : ''; $this->items = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; list output is never cached. $wpdb->prepare( "SELECT * FROM {$table_name} WHERE ( %s = '' OR method = %s ) AND ( %s = '' OR host = %s ) AND ( %s = '' OR created_at >= %s ) AND ( %s = '' OR created_at <= %s ) ORDER BY created_at DESC LIMIT %d OFFSET %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2. $method, $method, $host, $host, $date_from_value, $date_from_value, $date_to_value, $date_to_value, $this->per_page, $offset ), ARRAY_A ); $total = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; pagination count is never cached. $wpdb->prepare( "SELECT COUNT(*) FROM {$table_name} WHERE ( %s = '' OR method = %s ) AND ( %s = '' OR host = %s ) AND ( %s = '' OR created_at >= %s ) AND ( %s = '' OR created_at <= %s )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2. $method, $method, $host, $host, $date_from_value, $date_from_value, $date_to_value, $date_to_value ) ); $this->set_pagination_args( array( 'total_items' => $total, 'per_page' => $this->per_page, ) ); } /** * Render default column output. * * @param array $item Column data. * @param string $column_name Column name. * @return string */ public function column_default( $item, $column_name ) { switch ( $column_name ) { case 'created_at': case 'method': case 'host': case 'path': $value = isset( $item[ $column_name ] ) && is_string( $item[ $column_name ] ) ? $item[ $column_name ] : ''; return esc_html( $value ); default: return ''; } } /** * Add row actions. * * @param array $item Item data. * @return string */ public function column_created_at( $item ) { $log_id = isset( $item['id'] ) && is_numeric( $item['id'] ) ? (int) $item['id'] : 0; $view_url = add_query_arg( array( 'page' => 'robotstxt-telemetry-logs', 'action' => 'view', 'log' => $log_id, ), Robotstxt_Telemetry_Network::base_url() ); $delete_url = wp_nonce_url( add_query_arg( array( 'page' => 'robotstxt-telemetry-logs', 'action' => 'delete', 'log' => $log_id, ), Robotstxt_Telemetry_Network::base_url() ), 'robotstxt_telemetry_delete_log_' . $log_id ); $actions = array( 'view' => '' . esc_html__( 'View', 'robotstxt-telemetry' ) . '', 'delete' => '' . esc_html__( 'Delete', 'robotstxt-telemetry' ) . '', ); $created_at = isset( $item['created_at'] ) && is_string( $item['created_at'] ) ? $item['created_at'] : ''; return esc_html( $created_at ) . $this->row_actions( $actions ); } /** * Render the URL column without query parameters. * * @param array $item Item data. * @return string */ public function column_url( $item ) { $url = isset( $item['url'] ) && is_string( $item['url'] ) ? $item['url'] : ''; if ( '' === $url ) { return ''; } $parsed_url = wp_parse_url( $url ); if ( ! is_array( $parsed_url ) || empty( $parsed_url['host'] ) ) { return esc_html( $url ); } $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] : 'https'; $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; $port = isset( $parsed_url['port'] ) ? ':' . (int) $parsed_url['port'] : ''; $base = sprintf( '%s://%s%s%s', $scheme, $parsed_url['host'], $port, $path ); return esc_html( $base ); } /** * Extra controls for filtering. * * @param string $which Top or bottom. * @return void */ public function extra_tablenav( $which ) { if ( 'top' !== $which ) { return; } $method = strtoupper( $this->get_filter_value( 'method' ) ); $host = $this->get_filter_value( 'host' ); $date_from = $this->get_filter_value( 'date_from' ); $date_to = $this->get_filter_value( 'date_to' ); echo '
'; echo ''; echo ''; echo ''; printf( '', esc_attr( (string) $host ), esc_attr__( 'Host', 'robotstxt-telemetry' ) ); echo ''; printf( '', esc_attr( (string) $date_from ) ); echo ''; printf( '', esc_attr( (string) $date_to ) ); submit_button( __( 'Filter', 'robotstxt-telemetry' ), '', 'filter_action', false ); echo '
'; } /** * Get a sanitized filter value from the request. * * Values are only used to build read-only list filters. * * @param string $key Filter key. * @return string */ private function get_filter_value( $key ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only list filters. if ( ! isset( $_GET[ $key ] ) || ! is_string( $_GET[ $key ] ) ) { return ''; } return sanitize_text_field( wp_unslash( $_GET[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } /** * Sanitize date inputs. * * @param string|null $value Date string. * @return string|null */ private function sanitize_date_filter( $value ) { if ( empty( $value ) ) { return null; } $date = DateTime::createFromFormat( 'Y-m-d', $value ); if ( false === $date ) { return null; } return $date->format( 'Y-m-d' ); } } }