plugin updates

This commit is contained in:
Tony Volpe
2024-06-17 15:33:26 -04:00
parent 3751a5a1a6
commit e4e274a9a7
2674 changed files with 0 additions and 507851 deletions

View File

@@ -1,29 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
/**
* Enforce premium version requirement on commands covering premium features.
*/
final class WPSEO_CLI_Premium_Requirement {
/**
* Enforces license requirements for commands representing premium
* functionality.
*
* @return void
*/
public static function enforce() {
if ( YoastSEO()->helpers->product->is_premium() ) {
return;
}
// No premium commands allowed.
WP_CLI::error(
'This command can only be run with an active Yoast SEO Premium license.'
);
}
}

View File

@@ -1,152 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
/**
* Base class for the redirect commands.
*/
class WPSEO_CLI_Redirect_Base_Command extends WP_CLI_Command {
/**
* Redirect Manager instance to use.
*
* @var WPSEO_Redirect_Manager
*/
protected $redirect_manager;
/**
* Instantiates a WPSEO_CLI_Redirect_Create_Command object.
*/
public function __construct() {
// This could potentially have the Redirect Manager injected.
$this->redirect_manager = new WPSEO_Redirect_Manager();
}
/**
* Creates a new redirect.
*
* @param string $origin Origin of the redirect.
* @param string $target Target of the redirect.
* @param string $type Type of the redirect.
* @param string $format Format of the redirect.
*
* @return bool Whether creation was successful.
*/
protected function create_redirect( $origin, $target, $type, $format ) {
$redirect = new WPSEO_Redirect( $origin, $target, $type, $format );
return $this->redirect_manager->create_redirect( $redirect );
}
/**
* Updates an existing redirect.
*
* @param string $old_origin Origin of the redirect.
* @param string $new_origin Origin of the redirect.
* @param string $target Target of the redirect.
* @param string $type Type of the redirect.
* @param string $format Format of the redirect.
*
* @return bool Whether updating was successful.
*/
protected function update_redirect( $old_origin, $new_origin, $target, $type, $format ) {
$old_redirect = new WPSEO_Redirect( $old_origin );
$new_redirect = new WPSEO_Redirect( $new_origin, $target, $type, $format );
return $this->redirect_manager->update_redirect( $old_redirect, $new_redirect );
}
/**
* Deletes an existing redirect.
*
* @param string $origin Origin of the redirect.
*
* @return bool Whether deletion was successful.
*/
protected function delete_redirect( $origin ) {
$redirect = new WPSEO_Redirect( $origin );
return $this->redirect_manager->delete_redirects( [ $redirect ] );
}
/**
* Gets the redirect for a given origin.
*
* @param string $origin Origin to check for.
*
* @return WPSEO_Redirect|false Redirect value object, or false if not found.
*/
protected function get_redirect( $origin ) {
return $this->redirect_manager->get_redirect( $origin );
}
/**
* Checks whether a redirect for a given origin already exists.
*
* @param string $origin Origin to check for.
*
* @return bool Whether a redirect for the given origin was found.
*/
protected function has_redirect( $origin ) {
return $this->get_redirect( $origin ) !== false;
}
/**
* Checks whether a given redirect is valid.
*
* @param string $new_origin New origin of the redirect.
* @param string $target Target of the redirect.
* @param int $type Type of the redirect.
* @param string $format Format of the redirect.
* @param string|null $old_origin Optional. Old origin of the redirect to update.
*
* @return void
*/
protected function validate( $new_origin, $target, $type, $format, $old_origin = null ) {
$new_redirect = new WPSEO_Redirect( $new_origin, $target, $type, $format );
$old_redirect = null;
if ( $old_origin !== null ) {
$old_redirect = $this->get_redirect( $old_origin );
}
$validator = new WPSEO_Redirect_Validator();
if ( $validator->validate( $new_redirect, $old_redirect ) === true ) {
return;
}
$error = $validator->get_error();
$message = sprintf(
'Failed to validate redirect \'%s\' => \'%s\': %s',
$new_redirect->get_origin(),
$new_redirect->get_target(),
$this->reformat_error( $error->get_message() )
);
if ( $error->get_type() === 'warning' ) {
WP_CLI::warning( $message );
}
if ( $error->get_type() === 'error' ) {
WP_CLI::error( $message );
}
}
/**
* Reformats error messages by removing excessive whitespace.
*
* @param string $message Error message to reformat.
*
* @return string Reformatted error message.
*/
protected function reformat_error( $message ) {
$message = preg_replace( '/\s+/', ' ', $message );
return trim( $message );
}
}

View File

@@ -1,16 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
use WP_CLI\Dispatcher\CommandNamespace;
/**
* Lists, creates, updates, deletes and follows Yoast SEO redirects.
*/
final class WPSEO_CLI_Redirect_Command_Namespace extends CommandNamespace {
// Intentionally left empty.
}

View File

@@ -1,89 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
use WP_CLI\Utils;
/**
* Implementation of the 'redirect create' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_Create_Command extends WPSEO_CLI_Redirect_Base_Command {
/**
* Creates a new Yoast SEO redirect.
*
* ## OPTIONS
*
* <origin>
* : Origin of the redirect.
*
* <target>
* : Target of the redirect.
*
* [--type=<type>]
* : Type of the redirect.
* ---
* default: 301
* options:
* - 301
* - 302
* - 307
* - 410
* - 451
* ---
*
* [--format=<format>]
* : Format of the redirect.
* ---
* default: plain
* options:
* - plain
* - regex
* ---
*
* [--force]
* : Force creation of the redirect, bypassing any validation.
* ---
* default: false
* ---
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
list( $origin, $target ) = $args;
$type = (int) Utils\get_flag_value( $assoc_args, 'type', '301' );
$format = Utils\get_flag_value( $assoc_args, 'format', 'plain' );
$force = Utils\get_flag_value( $assoc_args, 'force', false );
$exists = $this->has_redirect( $origin );
if ( $exists && ! $force ) {
WP_CLI::error( "Redirect already exists for '{$origin}'." );
}
if ( ! $force ) {
$this->validate( $origin, $target, $type, $format );
}
if ( $exists ) {
$success = $this->update_redirect( $origin, $origin, $target, $type, $format );
}
if ( ! $exists ) {
$success = $this->create_redirect( $origin, $target, $type, $format );
}
if ( ! $success ) {
WP_CLI::error( "Could not create redirect: '{$origin}' => '{$target}'." );
}
WP_CLI::success( "Redirect created: '{$origin}' => '{$target}'." );
}
}

View File

@@ -1,41 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
/**
* Implementation of the 'redirect delete' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_Delete_Command extends WPSEO_CLI_Redirect_Base_Command {
/**
* Deletes an existing Yoast SEO redirect.
*
* ## OPTIONS
*
* <origin>
* : Origin of the redirect.
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
list( $origin ) = $args;
if ( ! $this->has_redirect( $origin ) ) {
WP_CLI::error( "Redirect does not exist for '{$origin}'." );
}
$success = $this->delete_redirect( $origin );
if ( ! $success ) {
WP_CLI::error( "Could not delete redirect: '{$origin}'." );
}
WP_CLI::success( "Redirect delete: '{$origin}'." );
}
}

View File

@@ -1,118 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
use WP_CLI\Utils;
/**
* Implementation of the 'redirect follow' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_Follow_Command extends WPSEO_CLI_Redirect_Base_Command {
/**
* Whether a redirect loop was detected.
*
* @var bool
*/
private $detected_loop = false;
/**
* Stack of traversed targets.
*
* @var string[]
*/
private $stack = [];
/**
* Follows a Yoast SEO redirect chain to get the final target it resolves to.
*
* ## OPTIONS
*
* <origin>
* : Origin of the redirect.
*
* [--trace]
* : Show a trace of all intermediary steps.
*
* [--limit=<limit>]
* : Limit the number of jumps to follow the redirect chain. '0' means unlimited.
* ---
* default: 0
* ---
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
list( $origin ) = $args;
$trace = (bool) Utils\get_flag_value( $assoc_args, 'trace', false );
$limit = (int) Utils\get_flag_value( $assoc_args, 'limit', '0' );
$redirect = $this->get_redirect( $origin );
if ( $redirect === false ) {
WP_CLI::error( "Redirect does not exist for '{$origin}'." );
}
$stack = $this->get_stack( $redirect, $limit );
if ( ! $trace ) {
$stack = (array) array_pop( $stack );
}
array_map( 'WP_CLI::line', $stack );
if ( $this->detected_loop ) {
WP_CLI::error( "Detected redirect loop for redirect: '{$origin}'." );
}
}
/**
* Gets the stack of redirect targets for a given starting redirect.
*
* @param WPSEO_Redirect $redirect Redirect to get the stack for.
* @param int $limit Number of steps to limit the stack to.
*
* @return array Array of target URL steps.
*/
private function get_stack( WPSEO_Redirect $redirect, $limit ) {
$steps = 0;
while ( ! $this->detected_loop && $redirect !== false ) {
++$steps;
if ( $limit > 0 && $steps >= $limit ) {
break;
}
$target = $redirect->get_target();
$this->add_to_stack( $target );
$redirect = $this->get_redirect( $target );
}
return array_keys( $this->stack );
}
/**
* Adds a new target to the stack.
*
* @param string $target Target to add to the stack.
*
* @return void
*/
private function add_to_stack( $target ) {
if ( array_key_exists( $target, $this->stack ) ) {
$this->detected_loop = true;
return;
}
$this->stack[ $target ] = true;
}
}

View File

@@ -1,31 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
/**
* Implementation of the 'redirect has' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_Has_Command extends WPSEO_CLI_Redirect_Base_Command {
/**
* Checks whether a given Yoast SEO redirect exists.
*
* ## OPTIONS
*
* <origin>
* : Origin of the redirect.
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
list( $origin ) = $args;
exit( $this->has_redirect( $origin ) ? 0 : 1 );
}
}

View File

@@ -1,185 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
use WP_CLI\Formatter;
/**
* Implementation of the 'redirect list' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_List_Command extends WPSEO_CLI_Redirect_Base_Command {
public const ALL_FIELDS = 'origin,target,type,format';
/**
* Filter to use for filtering the list of redirects.
*
* @var array
*/
private $filter;
/**
* Lists Yoast SEO redirects.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter the list to only show specific values for a given field.
*
* [--field=<field>]
* : Prints the value of a single field for each redirect.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
* ---
* default: origin,target,type,format
* ---
*
* [--output=<output>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* - count
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each redirect:
*
* * origin
* * target
* * type
* * format
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
$this->filter = $this->get_filter( $assoc_args );
/*
* By default, WP-CLI uses `--format=<format>` to define the output
* format for lists. As we also have a `format` field here and want to
* be able to easily filter the list by a given format, we use
* `--output=<output>` to define the format.
* We need to rename it back again here to be able to use the default
* format handling provided by WP-CLI.
*/
$assoc_args['format'] = $assoc_args['output'];
$formatter = new Formatter(
$assoc_args,
$this->get_fields( $assoc_args )
);
$redirects = array_filter(
$this->get_redirects(),
[ $this, 'filter_redirect' ]
);
$formatter->display_items( $redirects );
}
/**
* Gets the filtered list of redirects.
*
* @return array Associative array of redirects.
*/
private function get_redirects() {
$redirect_objects = $this->redirect_manager->get_all_redirects();
return array_map(
[ $this, 'adapt_redirect_data' ],
$redirect_objects
);
}
/**
* Filters the redirects based on whether they match the provided filter
* array.
*
* @param array $redirect Array data for an individual redirect.
*
* @return bool Whether to include the redirect or not.
*/
private function filter_redirect( $redirect ) {
foreach ( $this->filter as $key => $value ) {
/*
* Loose comparison to ignore type, as CLI arguments are always
* strings.
*/
if ( $value != $redirect[ $key ] ) {
return false;
}
}
return true;
}
/**
* Adapts redirect data fetched from the redirect manager to fit WP_CLI
* requirements.
*
* @param WPSEO_Redirect $redirect Redirection value object.
*
* @return array Associative array of redirects.
*/
private function adapt_redirect_data( $redirect ) {
return [
'origin' => $redirect->get_origin(),
'target' => $redirect->get_target(),
'type' => $redirect->get_type(),
'format' => $redirect->get_format(),
];
}
/**
* Gets the array of field names to use for formatting the table columns.
*
* @param array $assoc_args Parameters passed to command. Determines
* formatting.
*
* @return array Array of fields to use.
*/
private function get_fields( $assoc_args ) {
if ( empty( $assoc_args['fields'] ) ) {
return explode( ',', self::ALL_FIELDS );
}
if ( is_string( $assoc_args['fields'] ) ) {
return explode( ',', $assoc_args['fields'] );
}
return $assoc_args['fields'];
}
/**
* Gets the filter array to filter values against.
*
* @param array $assoc_args Parameters passed to command. Determines
* formatting.
*
* @return array Associative array of filter values.
*/
private function get_filter( $assoc_args ) {
$filter = [];
foreach ( [ 'origin', 'target', 'type', 'format' ] as $type ) {
if ( isset( $assoc_args[ $type ] ) ) {
$filter[ $type ] = $assoc_args[ $type ];
}
}
return $filter;
}
}

View File

@@ -1,86 +0,0 @@
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\CLI
*/
use WP_CLI\Utils;
/**
* Implementation of the 'redirect create' WP-CLI command.
*/
final class WPSEO_CLI_Redirect_Update_Command extends WPSEO_CLI_Redirect_Base_Command {
/**
* Updates an existing Yoast SEO redirect.
*
* ## OPTIONS
*
* <origin>
* : Origin of the redirect to update.
*
* <new-origin>
* : New origin of the redirect.
*
* <target>
* : Target of the redirect.
*
* [--type=<type>]
* : Type of the redirect.
* ---
* default: 301
* options:
* - 301
* - 302
* - 307
* - 410
* - 451
* ---
*
* [--format=<format>]
* : Format of the redirect.
* ---
* default: plain
* options:
* - plain
* - regex
* ---
*
* [--force]
* : Force updating of the redirect, bypassing any validation.
* ---
* default: false
* ---
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Associative array of associative arguments.
*
* @return void
*/
public function __invoke( $args, $assoc_args ) {
list( $origin, $new_origin, $target ) = $args;
$type = (int) Utils\get_flag_value( $assoc_args, 'type', '301' );
$format = Utils\get_flag_value( $assoc_args, 'format', 'plain' );
$force = Utils\get_flag_value( $assoc_args, 'force', false );
$exists = $this->has_redirect( $origin );
if ( ! $exists && ! $force ) {
WP_CLI::error( "Redirect does not exist for '{$origin}'." );
}
if ( ! $force ) {
$this->validate( $new_origin, $target, $type, $format, $origin );
}
$success = $this->update_redirect( $origin, $new_origin, $target, $type, $format );
if ( ! $success ) {
WP_CLI::error( "Could not update redirect: '{$new_origin}' => '{$target}'." );
}
WP_CLI::success( "Redirect updated: '{$new_origin}' => '{$target}'." );
}
}