should_attempt_redirect() ) { return; } $request_path = $this->current_path(); if ( '' === $request_path || '/' === $request_path ) { return; } $settings = C7DBT_Settings::get(); $map = $this->get_redirect_map( $settings ); if ( ! isset( $map[ $request_path ] ) ) { return; } $target = $map[ $request_path ]; if ( ! $target || $target === $request_path ) { return; } $dest = home_url( $target ); // Preserve query string (UTMs, etc.). $query = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : ''; // phpcs:ignore if ( is_string( $query ) && '' !== $query ) { $dest = add_query_arg( wp_parse_args( $query ), $dest ); } /** * Filter redirect destination URL. * * @param string $dest Destination. * @param string $request_path Request path. */ $dest = apply_filters( 'c7dbt_redirect_destination', $dest, $request_path ); wp_safe_redirect( $dest, 301 ); exit; } /** * Whether redirects are safe to run. * * @return bool */ private function should_attempt_redirect() { if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || is_preview() || is_customize_preview() ) { return false; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; } if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { return false; } // Elementor editor / preview. if ( isset( $_GET['elementor-preview'] ) || isset( $_GET['elementor_library'] ) ) { // phpcs:ignore return false; } if ( isset( $_GET['action'] ) && 'elementor' === $_GET['action'] ) { // phpcs:ignore return false; } // Feeds / robots / sitemaps. if ( is_feed() || is_robots() || is_trackback() ) { return false; } $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore if ( is_string( $request_uri ) && preg_match( '#/(wp-json|sitemap|wp-cron\.php)#i', $request_uri ) ) { return false; } return (bool) apply_filters( 'c7dbt_should_redirect', true ); } /** * Current request path with leading slash, no trailing slash (except root). * * @return string */ private function current_path() { $uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '/'; // phpcs:ignore $path = wp_parse_url( $uri, PHP_URL_PATH ); if ( ! is_string( $path ) || '' === $path ) { $path = '/'; } $home_path = wp_parse_url( home_url( '/' ), PHP_URL_PATH ); if ( is_string( $home_path ) && '/' !== $home_path && 0 === strpos( $path, rtrim( $home_path, '/' ) ) ) { $path = substr( $path, strlen( rtrim( $home_path, '/' ) ) ); if ( '' === $path ) { $path = '/'; } } $path = '/' . trim( $path, '/' ); return ( '/' === $path ) ? '/' : untrailingslashit( $path ); } /** * Redirect map: from => to (paths with leading slash). * * @param array $settings Settings. * @return array */ public function get_redirect_map( array $settings ) { $map = array(); $slug = isset( $settings['default_collection_slug'] ) ? C7DBT_Helpers::sanitize_slug( $settings['default_collection_slug'] ) : ''; if ( ! empty( $settings['redirect_collection_root'] ) && $slug ) { $map['/collection'] = '/collection/' . $slug; } if ( ! empty( $settings['redirect_product_root'] ) ) { $target = ! empty( $settings['product_redirect_target'] ) ? C7DBT_Helpers::sanitize_slug( $settings['product_redirect_target'] ) : ''; if ( $target ) { if ( 0 !== strpos( $target, 'product/' ) ) { $target = 'product/' . $target; } $map['/product'] = '/' . ltrim( $target, '/' ); } } if ( ! empty( $settings['custom_redirects'] ) && is_array( $settings['custom_redirects'] ) ) { foreach ( $settings['custom_redirects'] as $row ) { if ( empty( $row['from'] ) || empty( $row['to'] ) ) { continue; } $from = '/' . trim( C7DBT_Helpers::sanitize_slug( $row['from'] ), '/' ); $to = '/' . trim( C7DBT_Helpers::sanitize_slug( $row['to'] ), '/' ); if ( $from && $to && $from !== $to ) { $map[ $from ] = $to; } } } return apply_filters( 'c7dbt_redirect_map', $map ); } /** * Status for dashboard. * * @return array */ public function status() { $settings = C7DBT_Settings::get(); $map = $this->get_redirect_map( $settings ); $count = count( $map ); if ( empty( $settings['redirect_collection_root'] ) ) { return array( 'status' => 'warn', 'label' => __( 'Collection redirect off', '7labs-debug-branding-for-commerce7' ), 'detail' => __( 'Enable redirect so /collection is not an empty page.', '7labs-debug-branding-for-commerce7' ), 'count' => $count, ); } return array( 'status' => 'ok', 'label' => __( 'Redirects active', '7labs-debug-branding-for-commerce7' ), 'detail' => sprintf( /* translators: %d: number of redirects */ _n( '%d redirect rule active.', '%d redirect rules active.', $count, '7labs-debug-branding-for-commerce7' ), $count ), 'count' => $count, ); } }