mpty( $scheme ); } /** * Determines whether the target URL ends with a slash and adds one if necessary. * * @param string $target_url The url to format. * * @return string The url with trailing slash. */ protected function trailingslashit( $target_url ) { // Adds slash to target URL when permalink structure ends with a slash. if ( $this->requires_trailing_slash( $target_url ) ) { return \trailingslashit( $target_url ); } return $target_url; } /** * Formats the target url for the multisite if needed. * * @param string $target_url The url to format. * * @return string The formatted url. */ protected function format_for_multisite( $target_url ) { if ( ! \is_multisite() ) { return $target_url; } $blog_details = \get_blog_details(); if ( $blog_details && ! empty( $blog_details->path ) ) { $blog_path = \ltrim( $blog_details->path, '/' ); if ( ! empty( $blog_path ) && \strpos( $target_url, $blog_path ) === 0 ) { $target_url = \substr( $target_url, \strlen( $blog_path ) ); } } return $target_url; } /** * Gets the redirect URL by given URL. * * @param string $redirect_url The URL that has to be redirected. * * @return string The redirect url. */ protected function home_url( $redirect_url ) { $redirect_url = $this->strip_subdirectory( $redirect_url ); return \home_url( $redirect_url ); } /** * Strips the subdirectory from the given url. * * @param string $url The url to strip the subdirectory from. * * @return string The url with the stripped subdirectory. */ protected function strip_subdirectory( $url ) { return WPSEO_Redirect_Util::strip_base_url_path_from_url( $this->get_home_url(), $url ); } /** * Returns the URL PATH from the home url. * * @return string|null The url path or null if there isn't one. */ protected function get_home_url() { return \home_url(); } /** * Returns the redirects from the option table in the database. * * @return array The stored redirects. */ protected function get_redirects_from_options() { global $wpdb; static $redirects; if ( $redirects !== null ) { return $redirects; } // The code below is needed because we used to not autoload our redirect options. This fixes that. $all_options = \wp_cache_get( 'alloptions', 'options' ); foreach ( [ $this->normal_option_name, $this->regex_option_name ] as $option ) { $redirects[ $option ] = isset( $all_options[ $option ] ) ? \maybe_unserialize( $all_options[ $option ] ) : false; if ( $redirects[ $option ] === false ) { $redirects[ $option ] = \get_option( $option, false ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Normal methods only work if the option value has changed. $wpdb->update( $wpdb->options, [ 'autoload' => 'yes' ], [ 'option_name' => $option ] ); } } return $redirects; } /** * Sets the hook for setting the template include. This is the file that we want to show. * * @param string $template_to_set The template to look for. * * @return bool True when template should be included. */ protected function set_template_include_hook( $template_to_set ) { $this->template_file_path = $this->get_query_template( $template_to_set ); if ( ! empty( $this->template_file_path ) ) { \add_filter( 'template_include', [ $this, 'set_template_include' ] ); return true; } return false; } /** * Wraps the WordPress status_header function. * * @param int $code HTTP status code. * @param string $description Optional. A custom description for the HTTP status. * * @return void */ protected function status_header( $code, $description = '' ) { \status_header( $code, $description ); } /** * Returns instance of WP_Query. * * @return WP_Query Instance of WP_Query. */ protected function get_wp_query() { global $wp_query; if ( \is_object( $wp_query ) ) { return $wp_query; } return new WP_Query(); } /** * Handles the redirects without a target by setting the needed hooks. * * @param string $redirect_type The type of the redirect. * * @return void */ protected function handle_redirect_without_target( $redirect_type ) { if ( $redirect_type === 410 ) { \add_action( 'wp', [ $this, 'do_410' ] ); } if ( $redirect_type === 451 ) { \add_action( 'wp', [ $this, 'do_451' ] ); } } /** * Wrapper method for doing the actual redirect. * * @param string $location The path to redirect to. * @param int $status Status code to use. * * @return void */ protected function redirect( $location, $status = 302 ) { if ( ! \function_exists( 'wp_redirect' ) ) { require_once \ABSPATH . 'wp-includes/pluggable.php'; } \wp_redirect( $location, $status, 'Yoast SEO Premium' ); exit; } /** * Returns whether or not a target URL requires a trailing slash. * * @param string $target_url The target URL to check. * * @return bool True when trailing slash is required. */ protected function requires_trailing_slash( $target_url ) { return WPSEO_Redirect_Util::requires_trailing_slash( $target_url ); } /** * Returns the query template. * * @param string $filename Filename without extension. * * @return string Full path to template file. */ protected function get_query_template( $filename ) { return \get_query_template( $filename ); } }