prefix . 'abds_seo_redirects'; } /** * Whether any rule exists at all (cheap: one autoloaded option). * * @return bool */ public function has_rules(): bool { return (int) get_option( self::COUNT_OPTION, 0 ) > 0; } /** * One rule by ID. * * @param int $id Rule ID. * @return array|null */ public function get( int $id ): ?array { global $wpdb; $table = $this->table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table name is $wpdb->prefix plus a literal; value prepared; admin read. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ), ARRAY_A ); return is_array( $row ) ? $this->shape( $row ) : null; } /** * Every rule, for validation (id, source, target, type, active). * * @return array */ public function all_rules(): array { global $wpdb; $table = $this->table(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table name is $wpdb->prefix plus a literal; the limit is prepared; admin write path only. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, source, target, type, active FROM {$table} LIMIT %d", self::VALIDATION_LIMIT ), ARRAY_A ); return array_map( array( $this, 'shape' ), is_array( $rows ) ? $rows : array() ); } /** * A page of rules for the screen. * * @param string $search Substring of source or target ('' = all). * @param int $page 1-based page. * @param int $per_page Rows per page. * @return array{items:array, total:int} */ public function paged( string $search, int $page, int $per_page ): array { global $wpdb; $table = $this->table(); $like = '%' . $wpdb->esc_like( $search ) . '%'; $offset = max( 0, ( $page - 1 ) * $per_page ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- table name is $wpdb->prefix plus a literal; every value is prepared; admin screen read. $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE source LIKE %s OR target LIKE %s", $like, $like ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE source LIKE %s OR target LIKE %s ORDER BY id DESC LIMIT %d OFFSET %d", $like, $like, $per_page, $offset ), ARRAY_A ); // phpcs:enable return array( 'items' => array_map( array( $this, 'shape' ), is_array( $rows ) ? $rows : array() ), 'total' => $total, ); } /** * Inserts a rule that already passed ABDS_SEO_Redirect_Matcher::check_rule(). * * @param array $rule {source, target, type, active}. * @return int|false New ID, false on failure. */ public function create( array $rule ) { global $wpdb; $now = current_time( 'mysql' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $wpdb->insert() escapes every value via the format array; table is $wpdb->prefix plus a literal. $ok = $wpdb->insert( $this->table(), array( 'source' => (string) $rule['source'], 'target' => (string) $rule['target'], 'type' => (int) $rule['type'], 'active' => empty( $rule['active'] ) ? 0 : 1, 'created_at' => $now, 'updated_at' => $now, ), array( '%s', '%s', '%d', '%d', '%s', '%s' ) ); $this->changed(); return false === $ok ? false : (int) $wpdb->insert_id; } /** * Updates a rule that already passed check_rule(). * * @param int $id Rule ID. * @param array $rule {source, target, type, active}. * @return bool */ public function update( int $id, array $rule ): bool { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- $wpdb->update() escapes every value via the format arrays; table is $wpdb->prefix plus a literal. $ok = $wpdb->update( $this->table(), array( 'source' => (string) $rule['source'], 'target' => (string) $rule['target'], 'type' => (int) $rule['type'], 'active' => empty( $rule['active'] ) ? 0 : 1, 'updated_at' => current_time( 'mysql' ), ), array( 'id' => $id ), array( '%s', '%s', '%d', '%d', '%s' ), array( '%d' ) ); $this->changed(); return false !== $ok; } /** * Deletes a rule. * * @param int $id Rule ID. * @return bool */ public function delete( int $id ): bool { 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. $ok = $wpdb->delete( $this->table(), array( 'id' => $id ), array( '%d' ) ); $this->changed(); return false !== $ok; } /** * Counts a hit on a rule. * * @param int $id Rule ID. */ public function record_hit( int $id ): void { 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; one tiny write per redirected request. $wpdb->query( $wpdb->prepare( "UPDATE {$table} SET hits = hits + 1, last_hit = %s WHERE id = %d", current_time( 'mysql' ), $id ) ); } /** * Rule finder for request time: an exact source via the index, otherwise * the longest matching wildcard from the cached list. Active rules only. * * @return callable fn( string $path ): ?array */ public function finder(): callable { $wildcards = null; return function ( string $path ) use ( &$wildcards ) { 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; value prepared; unique-key lookup on a redirect-bearing request. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE source = %s AND active = 1", $path ), ARRAY_A ); if ( is_array( $row ) ) { return $this->shape( $row ); } if ( null === $wildcards ) { $wildcards = ABDS_SEO_Redirect_Matcher::finder_for( $this->active_wildcards() ); } return call_user_func( $wildcards, $path ); }; } /** * Active wildcard rules, cached. * * @return array */ protected function active_wildcards(): array { $cached = get_transient( self::WILDCARD_TRANSIENT ); if ( is_array( $cached ) ) { return $cached; } 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; the LIKE value is a constant; cached in a transient right after. $rows = $wpdb->get_results( "SELECT * FROM {$table} WHERE active = 1 AND source LIKE '%*'", ARRAY_A ); $list = array_map( array( $this, 'shape' ), is_array( $rows ) ? $rows : array() ); set_transient( self::WILDCARD_TRANSIENT, $list, DAY_IN_SECONDS ); return $list; } /** * Refreshes the cheap count and drops the wildcard cache after a write. */ protected function changed(): void { 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; runs after an admin write. update_option( self::COUNT_OPTION, (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ), true ); delete_transient( self::WILDCARD_TRANSIENT ); } /** * Row => typed rule array. * * @param array $row Database row. * @return array */ protected function shape( array $row ): array { return array( 'id' => (int) $row['id'], 'source' => (string) $row['source'], 'target' => (string) $row['target'], 'type' => (int) $row['type'], 'active' => (bool) $row['active'], 'hits' => isset( $row['hits'] ) ? (int) $row['hits'] : 0, 'last_hit' => isset( $row['last_hit'] ) ? (string) $row['last_hit'] : '', 'created_at' => isset( $row['created_at'] ) ? (string) $row['created_at'] : '', ); } }