plugin updates
This commit is contained in:
@@ -1,207 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
|
||||
use Yoast\WP\SEO\Helpers\Crawl_Cleanup_Helper;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Helpers\Redirect_Helper;
|
||||
use Yoast\WP\SEO\Helpers\Url_Helper;
|
||||
|
||||
/**
|
||||
* Class Crawl_Cleanup_Permalinks.
|
||||
*/
|
||||
class Crawl_Cleanup_Permalinks implements Initializer_Interface {
|
||||
|
||||
/**
|
||||
* The options helper.
|
||||
*
|
||||
* @var Options_Helper
|
||||
*/
|
||||
private $options_helper;
|
||||
|
||||
/**
|
||||
* The URL helper.
|
||||
*
|
||||
* @var Url_Helper
|
||||
*/
|
||||
private $url_helper;
|
||||
|
||||
/**
|
||||
* The Redirect_Helper.
|
||||
*
|
||||
* @var Redirect_Helper
|
||||
*/
|
||||
private $redirect_helper;
|
||||
|
||||
/**
|
||||
* The Crawl_Cleanup_Helper.
|
||||
*
|
||||
* @var Crawl_Cleanup_Helper
|
||||
*/
|
||||
private $crawl_cleanup_helper;
|
||||
|
||||
/**
|
||||
* Crawl Cleanup Basic integration constructor.
|
||||
*
|
||||
* @param Options_Helper $options_helper The option helper.
|
||||
* @param Url_Helper $url_helper The URL helper.
|
||||
* @param Redirect_Helper $redirect_helper The Redirect Helper.
|
||||
* @param Crawl_Cleanup_Helper $crawl_cleanup_helper The Crawl_Cleanup_Helper.
|
||||
*/
|
||||
public function __construct(
|
||||
Options_Helper $options_helper,
|
||||
Url_Helper $url_helper,
|
||||
Redirect_Helper $redirect_helper,
|
||||
Crawl_Cleanup_Helper $crawl_cleanup_helper
|
||||
) {
|
||||
$this->options_helper = $options_helper;
|
||||
$this->url_helper = $url_helper;
|
||||
$this->redirect_helper = $redirect_helper;
|
||||
$this->crawl_cleanup_helper = $crawl_cleanup_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
// We need to hook after 10 because otherwise our options helper isn't available yet.
|
||||
\add_action( 'plugins_loaded', [ $this, 'register_hooks' ], 15 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks our required hooks.
|
||||
*
|
||||
* This is the place to register hooks and filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_hooks() {
|
||||
if ( $this->options_helper->get( 'clean_campaign_tracking_urls' ) && ! empty( \get_option( 'permalink_structure' ) ) ) {
|
||||
\add_action( 'template_redirect', [ $this, 'utm_redirect' ], 0 );
|
||||
}
|
||||
if ( $this->options_helper->get( 'clean_permalinks' ) && ! empty( \get_option( 'permalink_structure' ) ) ) {
|
||||
\add_action( 'template_redirect', [ $this, 'clean_permalinks' ], 1 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the conditionals based in which this loadable should be active.
|
||||
*
|
||||
* @return array The array of conditionals.
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [ Front_End_Conditional::class ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect utm variables away.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function utm_redirect() {
|
||||
// Prevents WP CLI from throwing an error.
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
||||
if ( ! isset( $_SERVER['REQUEST_URI'] ) || \strpos( $_SERVER['REQUEST_URI'], '?' ) === false ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
||||
if ( ! \stripos( $_SERVER['REQUEST_URI'], 'utm_' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
||||
$parsed = \wp_parse_url( $_SERVER['REQUEST_URI'] );
|
||||
|
||||
$query = \explode( '&', $parsed['query'] );
|
||||
$utms = [];
|
||||
$other_args = [];
|
||||
|
||||
foreach ( $query as $query_arg ) {
|
||||
if ( \stripos( $query_arg, 'utm_' ) === 0 ) {
|
||||
$utms[] = $query_arg;
|
||||
continue;
|
||||
}
|
||||
$other_args[] = $query_arg;
|
||||
}
|
||||
|
||||
if ( empty( $utms ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$other_args_str = '';
|
||||
if ( \count( $other_args ) > 0 ) {
|
||||
$other_args_str = '?' . \implode( '&', $other_args );
|
||||
}
|
||||
|
||||
$new_path = $parsed['path'] . $other_args_str . '#' . \implode( '&', $utms );
|
||||
|
||||
$message = \sprintf(
|
||||
/* translators: %1$s: Yoast SEO */
|
||||
\__( '%1$s: redirect utm variables to #', 'wordpress-seo' ),
|
||||
'Yoast SEO'
|
||||
);
|
||||
|
||||
$this->redirect_helper->do_safe_redirect( \trailingslashit( $this->url_helper->recreate_current_url( false ) ) . \ltrim( $new_path, '/' ), 301, $message );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes unneeded query variables from the URL.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function clean_permalinks() {
|
||||
|
||||
if ( $this->crawl_cleanup_helper->should_avoid_redirect() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_url = $this->url_helper->recreate_current_url();
|
||||
$allowed_params = $this->crawl_cleanup_helper->allowed_params( $current_url );
|
||||
|
||||
// If we had only allowed params, let's just bail out, no further processing needed.
|
||||
if ( empty( $allowed_params['query'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$url_type = $this->crawl_cleanup_helper->get_url_type();
|
||||
|
||||
switch ( $url_type ) {
|
||||
case 'singular_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->singular_url();
|
||||
break;
|
||||
case 'front_page_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->front_page_url();
|
||||
break;
|
||||
case 'page_for_posts_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->page_for_posts_url();
|
||||
break;
|
||||
case 'taxonomy_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->taxonomy_url();
|
||||
break;
|
||||
case 'search_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->search_url();
|
||||
break;
|
||||
case 'page_not_found_url':
|
||||
$proper_url = $this->crawl_cleanup_helper->page_not_found_url( $current_url );
|
||||
break;
|
||||
default:
|
||||
$proper_url = '';
|
||||
}
|
||||
|
||||
if ( $this->crawl_cleanup_helper->is_query_var_page( $proper_url ) ) {
|
||||
$proper_url = $this->crawl_cleanup_helper->query_var_page_url( $proper_url );
|
||||
}
|
||||
|
||||
$proper_url = \add_query_arg( $allowed_params['allowed_query'], $proper_url );
|
||||
|
||||
if ( empty( $proper_url ) || $current_url === $proper_url ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->crawl_cleanup_helper->do_clean_redirect( $proper_url );
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Helpers\Redirect_Helper;
|
||||
|
||||
/**
|
||||
* Disables the WP core sitemaps.
|
||||
*/
|
||||
class Disable_Core_Sitemaps implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The options helper.
|
||||
*
|
||||
* @var Options_Helper
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* The redirect helper.
|
||||
*
|
||||
* @var Redirect_Helper
|
||||
*/
|
||||
private $redirect;
|
||||
|
||||
/**
|
||||
* Sitemaps_Enabled_Conditional constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param Options_Helper $options The options helper.
|
||||
* @param Redirect_Helper $redirect The redirect helper.
|
||||
*/
|
||||
public function __construct( Options_Helper $options, Redirect_Helper $redirect ) {
|
||||
$this->options = $options;
|
||||
$this->redirect = $redirect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable the WP core XML sitemaps.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
// This needs to be on priority 15 as that is after our options initialize.
|
||||
\add_action( 'plugins_loaded', [ $this, 'maybe_disable_core_sitemaps' ], 15 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the core sitemaps if Yoast SEO sitemaps are enabled.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function maybe_disable_core_sitemaps() {
|
||||
if ( $this->options->get( 'enable_xml_sitemap' ) ) {
|
||||
\add_filter( 'wp_sitemaps_enabled', '__return_false' );
|
||||
|
||||
\add_action( 'template_redirect', [ $this, 'template_redirect' ], 0 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects requests to the WordPress sitemap to the Yoast sitemap.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function template_redirect() {
|
||||
// If there is no path, nothing to do.
|
||||
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
return;
|
||||
}
|
||||
$path = \sanitize_text_field( \wp_unslash( $_SERVER['REQUEST_URI'] ) );
|
||||
|
||||
// If it's not a wp-sitemap request, nothing to do.
|
||||
if ( \substr( $path, 0, 11 ) !== '/wp-sitemap' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$redirect = $this->get_redirect_url( $path );
|
||||
|
||||
if ( ! $redirect ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->redirect->do_safe_redirect( \home_url( $redirect ), 301 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relative sitemap URL to redirect to.
|
||||
*
|
||||
* @param string $path The original path.
|
||||
*
|
||||
* @return string|false The path to redirct to. False if no redirect should be done.
|
||||
*/
|
||||
private function get_redirect_url( $path ) {
|
||||
// Start with the simple string comparison so we avoid doing unnecessary regexes.
|
||||
if ( $path === '/wp-sitemap.xml' ) {
|
||||
return '/sitemap_index.xml';
|
||||
}
|
||||
|
||||
if ( \preg_match( '/^\/wp-sitemap-(posts|taxonomies)-(\w+)-(\d+)\.xml$/', $path, $matches ) ) {
|
||||
$index = ( (int) $matches[3] - 1 );
|
||||
$index = ( $index === 0 ) ? '' : (string) $index;
|
||||
|
||||
return '/' . $matches[2] . '-sitemap' . $index . '.xml';
|
||||
}
|
||||
|
||||
if ( \preg_match( '/^\/wp-sitemap-users-(\d+)\.xml$/', $path, $matches ) ) {
|
||||
$index = ( (int) $matches[1] - 1 );
|
||||
$index = ( $index === 0 ) ? '' : (string) $index;
|
||||
|
||||
return '/author-sitemap' . $index . '.xml';
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
|
||||
use Yoast\WP\SEO\Loadable_Interface;
|
||||
|
||||
/**
|
||||
* Integration interface definition.
|
||||
*
|
||||
* An interface for registering integrations with WordPress.
|
||||
*/
|
||||
interface Initializer_Interface extends Loadable_Interface {
|
||||
|
||||
/**
|
||||
* Runs this initializer.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize();
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
|
||||
use Exception;
|
||||
use Yoast\WP\Lib\Migrations\Adapter;
|
||||
use Yoast\WP\Lib\Migrations\Migration;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Config\Migration_Status;
|
||||
use Yoast\WP\SEO\Loader;
|
||||
|
||||
/**
|
||||
* Triggers database migrations and handles results.
|
||||
*/
|
||||
class Migration_Runner implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The migrations adapter.
|
||||
*
|
||||
* @var Adapter
|
||||
*/
|
||||
protected $adapter;
|
||||
|
||||
/**
|
||||
* The loader.
|
||||
*
|
||||
* @var Loader
|
||||
*/
|
||||
protected $loader;
|
||||
|
||||
/**
|
||||
* The migration status.
|
||||
*
|
||||
* @var Migration_Status
|
||||
*/
|
||||
protected $migration_status;
|
||||
|
||||
/**
|
||||
* Retrieves the conditionals for the migrations.
|
||||
*
|
||||
* @return array The conditionals.
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrations constructor.
|
||||
*
|
||||
* @param Migration_Status $migration_status The migration status.
|
||||
* @param Loader $loader The loader.
|
||||
* @param Adapter $adapter The migrations adapter.
|
||||
*/
|
||||
public function __construct(
|
||||
Migration_Status $migration_status,
|
||||
Loader $loader,
|
||||
Adapter $adapter
|
||||
) {
|
||||
$this->migration_status = $migration_status;
|
||||
$this->loader = $loader;
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs this initializer.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception When a migration errored.
|
||||
*/
|
||||
public function initialize() {
|
||||
$this->run_free_migrations();
|
||||
// The below actions is used for when queries fail, this may happen in a multisite environment when switch_to_blog is used.
|
||||
\add_action( '_yoast_run_migrations', [ $this, 'run_free_migrations' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the free migrations.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception When a migration errored.
|
||||
*/
|
||||
public function run_free_migrations() {
|
||||
$this->run_migrations( 'free' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the migrations.
|
||||
*
|
||||
* @param string $name The name of the migration.
|
||||
* @param string $version The current version.
|
||||
*
|
||||
* @return bool True on success, false on failure.
|
||||
*
|
||||
* @throws Exception If the migration fails and YOAST_ENVIRONMENT is not production.
|
||||
*/
|
||||
public function run_migrations( $name, $version = \WPSEO_VERSION ) {
|
||||
if ( ! $this->migration_status->should_run_migration( $name, $version ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ! $this->migration_status->lock_migration( $name ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$migrations = $this->loader->get_migrations( $name );
|
||||
|
||||
if ( $migrations === false ) {
|
||||
$this->migration_status->set_error( $name, "Could not perform $name migrations. No migrations found.", $version );
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->adapter->create_schema_version_table();
|
||||
$all_versions = \array_keys( $migrations );
|
||||
$migrated_versions = $this->adapter->get_migrated_versions();
|
||||
$to_do_versions = \array_diff( $all_versions, $migrated_versions );
|
||||
|
||||
\sort( $to_do_versions, \SORT_STRING );
|
||||
|
||||
foreach ( $to_do_versions as $to_do_version ) {
|
||||
$class = $migrations[ $to_do_version ];
|
||||
$this->run_migration( $to_do_version, $class );
|
||||
}
|
||||
} catch ( Exception $exception ) {
|
||||
// Something went wrong...
|
||||
$this->migration_status->set_error( $name, $exception->getMessage(), $version );
|
||||
|
||||
if ( \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT !== 'production' ) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->migration_status->set_success( $name, $version );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a single migration.
|
||||
*
|
||||
* @param string $version The version.
|
||||
* @param string $migration_class The migration class.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception If the migration failed. Caught by the run_migrations function.
|
||||
*/
|
||||
protected function run_migration( $version, $migration_class ) {
|
||||
/**
|
||||
* The migration to run.
|
||||
*
|
||||
* @var Migration
|
||||
*/
|
||||
$migration = new $migration_class( $this->adapter );
|
||||
try {
|
||||
$this->adapter->start_transaction();
|
||||
$migration->up();
|
||||
$this->adapter->add_version( $version );
|
||||
$this->adapter->commit_transaction();
|
||||
} catch ( Exception $e ) {
|
||||
$this->adapter->rollback_transaction();
|
||||
throw new Exception( \sprintf( '%s - %s', $migration_class, $e->getMessage() ), 0, $e );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
|
||||
use Automattic\WooCommerce\Utilities\FeaturesUtil;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
|
||||
/**
|
||||
* Declares compatibility with the WooCommerce HPOS feature.
|
||||
*/
|
||||
class Woocommerce implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* Hooks into WooCommerce.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
\add_action( 'before_woocommerce_init', [ $this, 'declare_custom_order_tables_compatibility' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Declares compatibility with the WooCommerce HPOS feature.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function declare_custom_order_tables_compatibility() {
|
||||
if ( \class_exists( FeaturesUtil::class ) ) {
|
||||
FeaturesUtil::declare_compatibility( 'custom_order_tables', \WPSEO_FILE, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user