*/ public static function detect() { $handlers = array(); if ( defined( 'RANK_MATH_VERSION' ) || class_exists( 'RankMath' ) ) { $modules = (array) get_option( 'rank_math_modules', array() ); $general = (array) get_option( 'rank-math-options-general', array() ); $redirects = in_array( 'redirections', $modules, true ); $auto = $redirects && isset( $general['redirections_post_redirect'] ) && 'on' === $general['redirections_post_redirect']; $handlers['rank_math'] = array( 'name' => 'Rank Math SEO', 'active' => true, 'redirects' => $redirects, 'auto_redirect' => $auto, 'note' => $auto ? __( 'Auto Post Redirect is on. Rank Math has no documented way to pause it, so a slug change may also create a redirect in Rank Math. Turn it off in Rank Math → General Settings → Redirections, or switch slugH to "Hand off to the SEO plugin".', 'slugh' ) : '', ); } if ( defined( 'WPSEO_PREMIUM_VERSION' ) || class_exists( 'WPSEO_Premium' ) ) { $handlers['yoast_premium'] = array( 'name' => 'Yoast SEO Premium', 'active' => true, 'redirects' => true, 'auto_redirect' => true, 'note' => __( 'In "slugH handles redirects" mode, slugH switches off Yoast Premium\'s automatic redirect during its own saves through the documented Yoast\WP\SEO\post_redirect_slug_change filter.', 'slugh' ), ); } if ( defined( 'REDIRECTION_VERSION' ) || class_exists( 'Red_Item' ) ) { $handlers['redirection'] = array( 'name' => 'Redirection', 'active' => true, 'redirects' => true, 'auto_redirect' => null, 'note' => __( 'Redirection may match requests before template_redirect, so its rule wins for overlapping sources. slugH reports overlaps but never edits Redirection data.', 'slugh' ), ); } return $handlers; } /** * Whether a supported SEO plugin with an enabled redirect feature is present (needed for ownership mode 2). * * @return bool */ public static function seo_redirects_available() { $handlers = self::detect(); return ( isset( $handlers['rank_math'] ) && $handlers['rank_math']['redirects'] ) || isset( $handlers['yoast_premium'] ); } /** * Whether Rank Math's Auto Post Redirect is on. * * @return bool */ public static function rank_math_auto_redirect_on() { $handlers = self::detect(); return isset( $handlers['rank_math'] ) && $handlers['rank_math']['auto_redirect']; } /** * Read-only overlap check against the Redirection plugin's table. * * @param string[] $paths Canonical source paths. * @return array */ public static function redirection_overlaps( array $paths ) { global $wpdb; if ( empty( $paths ) || ! isset( self::detect()['redirection'] ) ) { return array(); } $table = $wpdb->prefix . 'redirection_items'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( $table !== $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) ) { return array(); } $lookup = array_flip( $paths ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, url FROM %i WHERE status = 'enabled' AND regex = 0 LIMIT 50000", $table ) ); $overlaps = array(); foreach ( (array) $rows as $row ) { $path = Normalizer::normalize_path( $row->url ); if ( isset( $lookup[ $path ] ) ) { $overlaps[] = array( 'path' => $path, 'redirection_id' => (int) $row->id, 'url' => $row->url, ); } } return $overlaps; } }