prefix . 'abds_seo_not_found'; } /** * Current settings, defaults filled in and bounded. * * @return array{log_enabled:bool, log_limit:int} */ public static function settings(): array { $stored = (array) get_option( self::SETTINGS_OPTION, array() ); $limit = (int) ( $stored['log_limit'] ?? self::DEFAULT_LIMIT ); return array( 'log_enabled' => ! array_key_exists( 'log_enabled', $stored ) || (bool) $stored['log_enabled'], 'log_limit' => max( self::MIN_LIMIT, min( self::MAX_LIMIT, $limit ) ), ); } /** * Saves the settings (bounded). * * @param array $input { log_enabled, log_limit }. * @return array The settings as stored. */ public static function save_settings( array $input ): array { $current = self::settings(); $next = array( 'log_enabled' => array_key_exists( 'log_enabled', $input ) ? (bool) $input['log_enabled'] : $current['log_enabled'], 'log_limit' => max( self::MIN_LIMIT, min( self::MAX_LIMIT, (int) ( $input['log_limit'] ?? $current['log_limit'] ) ) ), ); update_option( self::SETTINGS_OPTION, $next, false ); return $next; } /** * Records one missing address. A no-op when the log is off or the policy * says the request is not worth a row. * * @param string $path Normalized request path. * @param string $referer Referer header. * @param string $user_agent User-Agent header. */ public function record( string $path, string $referer, string $user_agent ): void { $settings = self::settings(); if ( ! $settings['log_enabled'] || ! ABDS_SEO_Not_Found_Policy::should_log( $path, $user_agent ) ) { return; } global $wpdb; $table = $this->table(); $now = current_time( 'mysql' ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; every value is prepared; one bounded write per logged 404. $known = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE path = %s", $path ) ); if ( null === $known ) { $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); if ( $count >= $settings['log_limit'] ) { $this->evict( $count - $settings['log_limit'] + 1 ); } } $wpdb->query( $wpdb->prepare( "INSERT INTO {$table} (path, hits, first_seen, last_seen, last_referer, last_user_agent) VALUES (%s, 1, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hits = hits + 1, last_seen = VALUES(last_seen), last_referer = VALUES(last_referer), last_user_agent = VALUES(last_user_agent)", $path, $now, $now, self::clip( $referer, 255 ), self::clip( $user_agent, 191 ) ) ); // phpcs:enable } /** * Removes the $n least valuable rows (see ABDS_SEO_Not_Found_Policy::eviction_candidate()). * * @param int $n How many to remove. */ protected function evict( int $n ): void { global $wpdb; $table = $this->table(); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; the limit is a cast int; runs only when the log is full. $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} ORDER BY hits ASC, last_seen ASC LIMIT %d", max( 1, $n ) ) ); // phpcs:enable } /** * A page of the log. * * @param string $orderby 'hits' or 'last_seen'. * @param string $order 'asc' or 'desc'. * @param int $page 1-based page. * @param int $per_page Rows per page. * @return array{items:array, total:int} */ public function paged( string $orderby, string $order, int $page, int $per_page ): array { global $wpdb; $table = $this->table(); $column = 'last_seen' === $orderby ? 'last_seen' : 'hits'; $dir = 'asc' === strtolower( $order ) ? 'ASC' : 'DESC'; $offset = max( 0, ( $page - 1 ) * $per_page ); $tie = 'hits' === $column ? 'last_seen DESC' : 'hits DESC'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; $column and $dir come from a two-value whitelist above; limits are prepared. $total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} ORDER BY {$column} {$dir}, {$tie} LIMIT %d OFFSET %d", $per_page, $offset ), ARRAY_A ); // phpcs:enable $items = array(); foreach ( is_array( $rows ) ? $rows : array() as $row ) { $items[] = array( 'id' => (int) $row['id'], 'path' => (string) $row['path'], 'hits' => (int) $row['hits'], 'first_seen' => (string) $row['first_seen'], 'last_seen' => (string) $row['last_seen'], 'last_referer' => (string) $row['last_referer'], 'last_user_agent' => (string) $row['last_user_agent'], ); } return array( 'items' => $items, 'total' => $total, ); } /** * Missing addresses asked for at least FREQUENT_HITS times, most asked first. * * @param int $limit Max rows. * @return array */ public function frequent( int $limit = 5 ): array { global $wpdb; $table = $this->table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; values prepared; read on an admin screen. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT path, hits FROM {$table} WHERE hits >= %d ORDER BY hits DESC LIMIT %d", self::FREQUENT_HITS, $limit ), ARRAY_A ); return array_map( static fn( array $row ): array => array( 'path' => (string) $row['path'], 'hits' => (int) $row['hits'], ), is_array( $rows ) ? $rows : array() ); } /** * Number of rows. * * @return int */ public function count(): int { global $wpdb; $table = $this->table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; no user input. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); } /** * Removes one row. * * @param int $id Row ID. */ public function delete( int $id ): void { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $wpdb->delete() escapes via the format array; table is $wpdb->prefix plus a literal. $wpdb->delete( $this->table(), array( 'id' => $id ), array( '%d' ) ); } /** * Empties the log. */ public function clear(): void { global $wpdb; $table = $this->table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching -- table is $wpdb->prefix plus a literal; no user input; an explicit admin action. $wpdb->query( "TRUNCATE TABLE {$table}" ); } /** * Cuts a string to a byte length without splitting a UTF-8 character. * * @param string $value Text. * @param int $length Max bytes. * @return string */ protected static function clip( string $value, int $length ): string { if ( strlen( $value ) <= $length ) { return $value; } $cut = substr( $value, 0, $length ); return function_exists( 'mb_strcut' ) ? mb_strcut( $value, 0, $length, 'UTF-8' ) : (string) preg_replace( '/[\x80-\xBF]+$/', '', $cut ); } }