post ID. */ const OPTION = 'cfsp_moved'; /** Old paths renamed soon after publishing (quick fixes): never redirected. Old path => post ID. */ const SKIP_OPTION = 'cfsp_moved_skip'; /** Deleted categories, tags and other terms: old path => [ taxonomy, parent term ID ]. */ const TERMS_OPTION = 'cfsp_moved_terms'; /** Renamed (or moved) terms: old path => [ taxonomy, term ID ]. */ const TERM_MOVES_OPTION = 'cfsp_moved_term_paths'; /** Term addresses captured just before an edit: term ID => path. */ private static $before_edit = array(); /** Logged moves kept at most (oldest dropped first). */ const MAX_LOG = 5000; /** Redirect lines written at most; Cloudflare Pages allows 2,000 static redirects in total. */ const MAX_RULES = 1500; public static function init() { add_action( 'post_updated', array( __CLASS__, 'on_updated' ), 10, 3 ); add_action( 'pre_delete_term', array( __CLASS__, 'on_term_delete' ), 10, 2 ); add_action( 'edit_terms', array( __CLASS__, 'before_term_edit' ), 10, 2 ); add_action( 'edited_term', array( __CLASS__, 'after_term_edit' ), 10, 3 ); } /** * Before a term is saved: remember its address and its sub-terms' (a new parent slug * changes the address of every term below it). */ public static function before_term_edit( $term_id, $taxonomy ) { $tax = get_taxonomy( $taxonomy ); if ( ! $tax || ! $tax->public ) { return; } $ids = array( (int) $term_id ); if ( is_taxonomy_hierarchical( $taxonomy ) ) { $children = get_term_children( (int) $term_id, $taxonomy ); $ids = array_merge( $ids, is_wp_error( $children ) ? array() : array_map( 'intval', $children ) ); } foreach ( $ids as $id ) { $link = get_term_link( $id, $taxonomy ); if ( ! is_wp_error( $link ) ) { self::$before_edit[ $taxonomy . ':' . $id ] = self::path( $link ); } } } /** After a term is saved: log every address that changed. */ public static function after_term_edit( $term_id, $tt_id, $taxonomy ) { $moves = self::term_moves(); $changed = false; foreach ( self::$before_edit as $key => $old ) { list( $tax, $id ) = explode( ':', $key ); if ( $tax !== $taxonomy ) { continue; } clean_term_cache( (int) $id, $taxonomy ); $link = get_term_link( (int) $id, $taxonomy ); $new = is_wp_error( $link ) ? '' : self::path( $link ); if ( '' !== $old && '/' !== $old && '' !== $new && $old !== $new ) { unset( $moves[ $old ] ); $moves[ $old ] = array( (string) $taxonomy, (int) $id ); $changed = true; } unset( self::$before_edit[ $key ] ); } if ( $changed ) { update_option( self::TERM_MOVES_OPTION, array_slice( $moves, -self::MAX_LOG, null, true ), false ); } } /** Log the address of a category, tag or other public term that is about to be deleted. */ public static function on_term_delete( $term_id, $taxonomy ) { $tax = get_taxonomy( $taxonomy ); $term = get_term( (int) $term_id, $taxonomy ); if ( ! $tax || ! $tax->public || ! $term || is_wp_error( $term ) ) { return; } $link = get_term_link( $term ); $old = is_wp_error( $link ) ? '' : self::path( $link ); if ( '' === $old || '/' === $old ) { return; } $terms = self::terms_log(); unset( $terms[ $old ] ); $terms[ $old ] = array( (string) $taxonomy, (int) $term->parent ); update_option( self::TERMS_OPTION, array_slice( $terms, -self::MAX_LOG, null, true ), false ); } /** Log the old address when a published post's address changes. */ public static function on_updated( $post_id, $after, $before ) { if ( ! CFSP_URLs::is_public_post( $before ) || ! CFSP_URLs::is_public_post( $after ) ) { return; } $old = self::path( get_permalink( $before ) ); $new = self::path( get_permalink( $after ) ); if ( '' === $old || $old === $new || '/' === $old ) { return; } $log = self::log(); $skip = self::skip_list(); unset( $log[ $old ], $skip[ $old ] ); // A rename within the grace period after publishing fixes a wrong address before anyone // relies on it: no redirect, and WordPress's own old-slug record for it is ignored too. $grace = (int) CFSP_Settings::get( 'redirect_grace' ) * MINUTE_IN_SECONDS; $published = (int) get_post_time( 'U', true, $before ); if ( $grace > 0 && $published && time() - $published < $grace ) { $skip[ $old ] = (int) $post_id; } else { $log[ $old ] = (int) $post_id; } update_option( self::OPTION, array_slice( $log, -self::MAX_LOG, null, true ), false ); update_option( self::SKIP_OPTION, array_slice( $skip, -self::MAX_LOG, null, true ), false ); } /** * _redirects lines: "/old-path/ /current-path/ 301", newest moves first. * * @return string[] */ public static function lines() { if ( ! CFSP_Settings::get( 'redirect_renamed' ) ) { return array(); } $moves = self::log(); // WordPress's old slugs: the current address with its last slug swapped for the old one. $types = CFSP_URLs::post_types(); if ( $types ) { $ids = get_posts( array( 'post_type' => $types, 'post_status' => 'publish', 'has_password' => false, 'meta_key' => '_wp_old_slug', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Only posts that were renamed; read once per push. 'numberposts' => self::MAX_RULES, 'orderby' => 'modified', 'order' => 'DESC', 'fields' => 'ids', ) ); foreach ( $ids as $id ) { $current = self::path( get_permalink( $id ) ); $slug = rawurldecode( (string) get_post_field( 'post_name', $id ) ); // Stored percent-encoded for non-Latin slugs. $pos = '' === $slug ? false : strrpos( $current, '/' . $slug . '/' ); if ( false === $pos ) { continue; } foreach ( (array) get_post_meta( $id, '_wp_old_slug' ) as $old_slug ) { $old_slug = rawurldecode( sanitize_title( (string) $old_slug ) ); if ( '' === $old_slug || $old_slug === $slug ) { continue; } $old = substr_replace( $current, '/' . $old_slug . '/', $pos, strlen( $slug ) + 2 ); if ( ! isset( $moves[ $old ] ) ) { $moves = array( $old => (int) $id ) + $moves; } } } } $moves = array_diff_key( $moves, self::skip_list() ); $lines = array(); foreach ( array_reverse( $moves, true ) as $old => $post_id ) { if ( count( $lines ) >= self::MAX_RULES ) { break; } $post = get_post( $post_id ); if ( ! $post || ! CFSP_URLs::is_public_post( $post ) ) { continue; // Unpublished or deleted since: the old address just disappears. } $old = (string) $old; $current = self::path( get_permalink( $post ) ); if ( '' === $current || $current === $old || self::taken( $old ) || CFSP_Exporter::is_excluded( home_url( $old ) ) ) { continue; } $lines[] = self::encode( $old ) . ' ' . self::encode( $current ) . ' 301'; } // Renamed terms: to the term's current address (the homepage if it was deleted since). foreach ( array_reverse( self::term_moves(), true ) as $old => $info ) { if ( count( $lines ) >= self::MAX_RULES ) { break; } $old = (string) $old; if ( self::term_taken( $old, $info[0] ) || CFSP_Exporter::is_excluded( home_url( $old ) ) ) { continue; } $term = get_term( (int) $info[1], $info[0] ); $link = $term && ! is_wp_error( $term ) ? get_term_link( $term ) : ''; $target = $link && ! is_wp_error( $link ) ? self::path( $link ) : '/'; if ( '' !== $target && $target !== $old ) { $lines[] = self::encode( $old ) . ' ' . self::encode( $target ) . ' 301'; } } // Deleted terms: to the nearest parent that still exists, otherwise the homepage. foreach ( array_reverse( self::terms_log(), true ) as $old => $info ) { if ( count( $lines ) >= self::MAX_RULES ) { break; } $old = (string) $old; if ( self::term_taken( $old, $info[0] ) || CFSP_Exporter::is_excluded( home_url( $old ) ) ) { continue; // A new term (or page) uses this address now. } $target = '/'; $parent = (int) $info[1]; while ( $parent ) { $term = get_term( $parent, $info[0] ); if ( ! $term || is_wp_error( $term ) ) { break; // Parent deleted too: its own entry, or the homepage. } if ( $term->count > 0 ) { $link = get_term_link( $term ); $target = is_wp_error( $link ) ? '/' : self::path( $link ); break; } $parent = (int) $term->parent; // Empty parent: its page isn't exported, go up. } if ( $target !== $old ) { $lines[] = self::encode( $old ) . ' ' . self::encode( $target ) . ' 301'; } } return $lines; } /** Is this address in use by current content (another post took the old slug)? */ private static function taken( $path ) { $types = CFSP_URLs::post_types(); $slug = basename( untrailingslashit( $path ) ); if ( ! $types || '' === $slug ) { return false; } $candidates = get_posts( array( 'name' => sanitize_title( $slug ), 'post_type' => $types, 'post_status' => 'publish', 'numberposts' => 5, 'fields' => 'ids', ) ); $page = get_page_by_path( trim( $path, '/' ), OBJECT, $types ); if ( $page ) { $candidates[] = $page->ID; } foreach ( $candidates as $id ) { if ( self::path( get_permalink( $id ) ) === $path ) { return true; } } return false; } /** Site-relative path of a WordPress address, e.g. "/hong-xiang-m733w/" ('' if none). */ private static function path( $url ) { return $url ? CFSP_Exporter::relative_path( $url ) : ''; } /** Percent-encode non-ASCII characters (Cloudflare Pages matches the encoded path). */ private static function encode( $path ) { return preg_replace_callback( '/[^\x21-\x7E]/', function ( $m ) { return rawurlencode( $m[0] ); }, $path ); } /** Is this address used by a current term of the taxonomy, or by a post or page? */ private static function term_taken( $path, $taxonomy ) { $slug = basename( untrailingslashit( $path ) ); $term = '' === $slug ? false : get_term_by( 'slug', sanitize_title( $slug ), $taxonomy ); if ( $term && ! is_wp_error( get_term_link( $term ) ) && self::path( get_term_link( $term ) ) === $path ) { return true; } return self::taken( $path ); } /** @return array Old path => [ taxonomy, term ID ], oldest first. */ private static function term_moves() { $moves = get_option( self::TERM_MOVES_OPTION, array() ); return is_array( $moves ) ? $moves : array(); } /** @return array Old path => [ taxonomy, parent term ID ], oldest first. */ private static function terms_log() { $terms = get_option( self::TERMS_OPTION, array() ); return is_array( $terms ) ? $terms : array(); } /** @return array Old path => post ID of quick-fix renames. */ private static function skip_list() { $skip = get_option( self::SKIP_OPTION, array() ); return is_array( $skip ) ? $skip : array(); } /** @return array Old path => post ID, oldest first. */ private static function log() { $log = get_option( self::OPTION, array() ); return is_array( $log ) ? $log : array(); } }