Merged in feature/MAW-855-import-code-into-aws (pull request #2)
code import from pantheon * code import from pantheon
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Initializers;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Initializers\Initializer_Interface;
|
||||
|
||||
/**
|
||||
* Index_Now_Key class
|
||||
*/
|
||||
class Index_Now_Key implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* The options helper.
|
||||
*
|
||||
* @var Options_Helper
|
||||
*/
|
||||
private $options_helper;
|
||||
|
||||
/**
|
||||
* Holds the IndexNow key.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* Index_Now_Key initializer constructor.
|
||||
*
|
||||
* @param Options_Helper $options_helper The option helper.
|
||||
*/
|
||||
public function __construct( Options_Helper $options_helper ) {
|
||||
$this->options_helper = $options_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* This is the place to register hooks and filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
\add_action( 'init', [ $this, 'add_rewrite_rule' ], 1 );
|
||||
\add_action( 'plugins_loaded', [ $this, 'load' ], 15 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the integration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load() {
|
||||
if ( $this->options_helper->get( 'enable_index_now' ) === false ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->key = $this->options_helper->get( 'index_now_key' );
|
||||
if ( $this->key === '' ) {
|
||||
$this->generate_key();
|
||||
}
|
||||
\add_action( 'wp', [ $this, 'output_key' ], 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the rewrite rule for the IndexNow key txt file.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function add_rewrite_rule() {
|
||||
if ( $this->options_helper->get( 'enable_index_now' ) !== true ) {
|
||||
return;
|
||||
}
|
||||
global $wp;
|
||||
|
||||
$wp->add_query_var( 'yoast_index_now_key' );
|
||||
\add_rewrite_rule( '^yoast-index-now-([a-zA-Z0-9-]+)\.txt$', 'index.php?yoast_index_now_key=$matches[1]', 'top' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the key when it matches the key in the database.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function output_key() {
|
||||
$key_in_url = \get_query_var( 'yoast_index_now_key' );
|
||||
if ( empty( $key_in_url ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $key_in_url === $this->key ) {
|
||||
// Remove all headers.
|
||||
\header_remove();
|
||||
// Only send plain text header.
|
||||
\header( 'Content-Type: text/plain;charset=UTF-8' );
|
||||
echo \esc_html( $this->key );
|
||||
die;
|
||||
}
|
||||
|
||||
// Trying keys? Good luck.
|
||||
global $wp_query;
|
||||
$wp_query->set_404();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates an IndexNow key.
|
||||
*
|
||||
* Adapted from wp_generate_password to include dash (-) and not be filtered.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function generate_key() {
|
||||
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-';
|
||||
|
||||
for ( $i = 0; $i < 100; $i++ ) {
|
||||
$this->key .= \substr( $chars, \wp_rand( 0, ( \strlen( $chars ) - 1 ) ), 1 );
|
||||
}
|
||||
$this->options_helper->set( 'index_now_key', $this->key );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Initializers;
|
||||
|
||||
use Yoast\WP\SEO\Conditionals\Admin\Yoast_Admin_Conditional;
|
||||
use Yoast\WP\SEO\Helpers\Current_Page_Helper;
|
||||
use Yoast\WP\SEO\Initializers\Initializer_Interface;
|
||||
use Yoast\WP\SEO\Introductions\Application\Current_Page_Trait;
|
||||
use Yoast\WP\SEO\Introductions\Domain\Introduction_Interface;
|
||||
|
||||
/**
|
||||
* Initializes Premium introductions.
|
||||
*/
|
||||
class Introductions_Initializer implements Initializer_Interface {
|
||||
const SCRIPT_HANDLE = 'wp-seo-premium-introductions';
|
||||
|
||||
use Current_Page_Trait;
|
||||
|
||||
/**
|
||||
* Holds the current page helper.
|
||||
*
|
||||
* @var Current_Page_Helper
|
||||
*/
|
||||
private $current_page_helper;
|
||||
|
||||
/**
|
||||
* Holds the introductions.
|
||||
*
|
||||
* @var Introduction_Interface
|
||||
*/
|
||||
private $introductions;
|
||||
|
||||
/**
|
||||
* Constructs the new features integration.
|
||||
*
|
||||
* @param Current_Page_Helper $current_page_helper The current page helper.
|
||||
* @param Introduction_Interface ...$introductions The introductions.
|
||||
*/
|
||||
public function __construct( Current_Page_Helper $current_page_helper, Introduction_Interface ...$introductions ) {
|
||||
$this->current_page_helper = $current_page_helper;
|
||||
$this->introductions = $introductions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the conditionals based in which this loadable should be active.
|
||||
*
|
||||
* In this case: when on an admin page.
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [ Yoast_Admin_Conditional::class ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the action to enqueue the needed script(s).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
if ( $this->is_on_installation_page() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
\add_filter( 'wpseo_introductions', [ $this, 'add_introductions' ] );
|
||||
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the Premium introductions.
|
||||
*
|
||||
* @param Introduction_Interface[] $introductions The introductions.
|
||||
*
|
||||
* @return array The merged introductions.
|
||||
*/
|
||||
public function add_introductions( $introductions ) {
|
||||
// Safety check and bail.
|
||||
if ( ! \is_array( $introductions ) ) {
|
||||
return $introductions;
|
||||
}
|
||||
|
||||
return \array_merge( $introductions, $this->introductions );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue the workouts app.
|
||||
*/
|
||||
public function enqueue_assets() {
|
||||
\wp_enqueue_script( self::SCRIPT_HANDLE );
|
||||
\wp_localize_script(
|
||||
self::SCRIPT_HANDLE,
|
||||
'wpseoPremiumIntroductions',
|
||||
[
|
||||
'pluginUrl' => \plugins_url( '', \WPSEO_PREMIUM_FILE ),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,8 @@ class Plugin implements Initializer_Interface {
|
||||
public function wpseo_premium_deactivate() {
|
||||
\do_action( 'wpseo_register_capabilities_premium' );
|
||||
WPSEO_Capability_Manager_Factory::get( 'premium' )->remove();
|
||||
|
||||
$this->options_helper->set( 'tracking', false );
|
||||
if ( $this->options_helper->get( 'toggled_tracking' ) !== true ) {
|
||||
$this->options_helper->set( 'tracking', false );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Initializers;
|
||||
namespace Yoast\WP\SEO\Premium\Initializers;
|
||||
|
||||
use WP_Query;
|
||||
use WPSEO_Premium_Redirect_Option;
|
||||
use WPSEO_Redirect_Option;
|
||||
use WPSEO_Redirect_Util;
|
||||
use Yoast\WP\SEO\Conditionals\Front_End_Conditional;
|
||||
use Yoast\WP\SEO\Initializers\Initializer_Interface;
|
||||
|
||||
/**
|
||||
* Class Redirect_Handler.
|
||||
@@ -33,20 +35,6 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
*/
|
||||
protected $is_redirected = false;
|
||||
|
||||
/**
|
||||
* The options where the URL redirects are stored.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $normal_option_name = 'wpseo-premium-redirects-export-plain';
|
||||
|
||||
/**
|
||||
* The option name where the regex redirects are stored.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $regex_option_name = 'wpseo-premium-redirects-export-regex';
|
||||
|
||||
/**
|
||||
* The URL that is called at the moment.
|
||||
*
|
||||
@@ -81,15 +69,15 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the requested URL.
|
||||
$this->set_request_url();
|
||||
|
||||
// Check the normal redirects.
|
||||
$this->handle_normal_redirects( $this->request_url );
|
||||
|
||||
// Check the regex redirects.
|
||||
if ( $this->is_redirected() === false ) {
|
||||
$this->handle_regex_redirects();
|
||||
if ( ! \function_exists( 'is_plugin_active_for_network' ) ) {
|
||||
require_once \ABSPATH . 'wp-admin/includes/plugin.php';
|
||||
}
|
||||
// If the plugin is network activated, we wait for the plugins to be loaded before initializing.
|
||||
if ( \is_plugin_active_for_network( \WPSEO_PREMIUM_BASENAME ) ) {
|
||||
\add_action( 'plugins_loaded', [ $this, 'handle_redirects' ], 16 );
|
||||
}
|
||||
else {
|
||||
$this->handle_redirects();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +162,7 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
*/
|
||||
protected function handle_normal_redirects( $request_url ) {
|
||||
// Setting the redirects.
|
||||
$redirects = $this->get_redirects( $this->normal_option_name );
|
||||
$redirects = $this->get_redirects( WPSEO_Redirect_Option::OPTION_PLAIN );
|
||||
$this->redirects = $this->normalize_redirects( $redirects );
|
||||
|
||||
$request_url = $this->normalize_url( $request_url );
|
||||
@@ -218,7 +206,7 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
*/
|
||||
protected function handle_regex_redirects() {
|
||||
// Setting the redirects.
|
||||
$this->redirects = $this->get_redirects( $this->regex_option_name );
|
||||
$this->redirects = $this->get_redirects( WPSEO_Redirect_Option::OPTION_REGEX );
|
||||
|
||||
foreach ( $this->redirects as $regex => $redirect ) {
|
||||
// Check if the URL matches the $regex.
|
||||
@@ -270,7 +258,11 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
* @return array Returns the redirects for the given option.
|
||||
*/
|
||||
protected function get_redirects( $option ) {
|
||||
$redirects = $this->get_redirects_from_options();
|
||||
static $redirects;
|
||||
|
||||
if ( ! isset( $redirects[ $option ] ) ) {
|
||||
$redirects[ $option ] = \get_option( $option, false );
|
||||
}
|
||||
|
||||
if ( ! empty( $redirects[ $option ] ) ) {
|
||||
return $redirects[ $option ];
|
||||
@@ -356,23 +348,19 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the request URI, with fallback for super global.
|
||||
* Gets the request URI.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function get_request_uri() {
|
||||
$options = [ 'options' => [ 'default' => '' ] ];
|
||||
$request_uri = \filter_input( \INPUT_SERVER, 'REQUEST_URI', \FILTER_SANITIZE_URL, $options );
|
||||
$request_uri = '';
|
||||
|
||||
// Because there isn't an usable value, try the fallback.
|
||||
if ( empty( $request_uri ) && isset( $_SERVER['REQUEST_URI'] ) ) {
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- this value is compared. I don't want to change the behavior.
|
||||
$request_uri = \filter_var( $_SERVER['REQUEST_URI'], \FILTER_SANITIZE_URL, $options );
|
||||
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- We sanitize after decoding.
|
||||
$request_uri = \sanitize_text_field( \rawurldecode( \wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
|
||||
}
|
||||
|
||||
$request_uri = $this->strip_subdirectory( $request_uri );
|
||||
|
||||
return \rawurldecode( $request_uri );
|
||||
return $this->strip_subdirectory( $request_uri );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -564,33 +552,6 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
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.
|
||||
*
|
||||
@@ -691,4 +652,22 @@ class Redirect_Handler implements Initializer_Interface {
|
||||
protected function get_query_template( $filename ) {
|
||||
return \get_query_template( $filename );
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually handles redirects.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle_redirects() {
|
||||
// Set the requested URL.
|
||||
$this->set_request_url();
|
||||
|
||||
// Check the normal redirects.
|
||||
$this->handle_normal_redirects( $this->request_url );
|
||||
|
||||
// Check the regex redirects.
|
||||
if ( $this->is_redirected() === false ) {
|
||||
$this->handle_regex_redirects();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Initializers;
|
||||
|
||||
use Automattic\WooCommerce\Utilities\FeaturesUtil;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Initializers\Initializer_Interface;
|
||||
|
||||
/**
|
||||
* Declares compatibility with the WooCommerce HPOS feature.
|
||||
*/
|
||||
class Woocommerce implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* Hooks into WooCommerce.
|
||||
*/
|
||||
public function initialize() {
|
||||
\add_action( 'before_woocommerce_init', [ $this, 'declare_custom_order_tables_compatibility' ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Declares compatibility with the WooCommerce HPOS feature.
|
||||
*/
|
||||
public function declare_custom_order_tables_compatibility() {
|
||||
if ( \class_exists( FeaturesUtil::class ) ) {
|
||||
FeaturesUtil::declare_compatibility( 'custom_order_tables', \WPSEO_PREMIUM_FILE, true );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Initializers;
|
||||
|
||||
use WP_CLI;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Initializers\Initializer_Interface;
|
||||
|
||||
/**
|
||||
* Wp_Cli_Initializer class
|
||||
*/
|
||||
class Wp_Cli_Initializer implements Initializer_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* Initializes the integration.
|
||||
*
|
||||
* This is the place to register hooks and filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function initialize() {
|
||||
if ( \defined( 'WP_CLI' ) && \WP_CLI ) {
|
||||
\add_action( 'plugins_loaded', [ $this, 'wpseo_cli_init' ], 20 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the WP-CLI integration.
|
||||
*
|
||||
* The WP-CLI integration needs PHP 5.3 support, which should be automatically
|
||||
* enforced by the check for the WP_CLI constant. As WP-CLI itself only runs
|
||||
* on PHP 5.3+, the constant should only be set when requirements are met.
|
||||
*/
|
||||
public function wpseo_cli_init() {
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect list',
|
||||
'WPSEO_CLI_Redirect_List_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect create',
|
||||
'WPSEO_CLI_Redirect_Create_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect update',
|
||||
'WPSEO_CLI_Redirect_Update_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect delete',
|
||||
'WPSEO_CLI_Redirect_Delete_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect has',
|
||||
'WPSEO_CLI_Redirect_Has_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
|
||||
WP_CLI::add_command(
|
||||
'yoast redirect follow',
|
||||
'WPSEO_CLI_Redirect_Follow_Command',
|
||||
[ 'before_invoke' => 'WPSEO_CLI_Premium_Requirement::enforce' ]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user