WordPress/wp-includes/class-wp-paused-extensions-storage.php
Felix Arntz 4e167c593d Options, Meta APIs: Explicitly pass $autoload parameter to when potentially adding new options.
It is recommended that for every option it is explicitly set whether to autoload it or not. This changeset updates relevant `update_option()` and `add_option()` calls.

Note that the `$autoload` parameter is only needed for `update_option()` if the option is potentially not present yet, i.e. the call will pass through to `add_option()`. Since WordPress core adds the majority of its options to the database during installation, only `update_option()` calls for dynamically added options need to be modified, which is what this changeset does.

As part of revisiting the autoload values for dynamically added WordPress core options, this changeset modifies some options to no longer be autoloaded, since they are only accessed in a few specific places that are not relevant for a regular request. These options are:
* `recently_activated`
* `_wp_suggested_policy_text_has_changed`
* `{upgradeLock}.lock`
* `dashboard_widget_options`
* `ftp_credentials`
* `adminhash`
* `nav_menu_options`
* `wp_force_deactivated_plugins`
* `delete_blog_hash`
* `allowedthemes`
* `{sessionId}_paused_extensions`
* `recovery_keys`
* `https_detection_errors`
* `fresh_site`

An upgrade routine is present as well that sets those options to no longer autoload for existing sites.

Props pbearne, flixos90, mukesh27, swissspidy, SergeyBiryukov, joemcgill, adamsilverstein.
Fixes #61103.

Built from https://develop.svn.wordpress.org/trunk@58975


git-svn-id: http://core.svn.wordpress.org/trunk@58371 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-03 18:19:14 +00:00

227 lines
5.0 KiB
PHP

<?php
/**
* Error Protection API: WP_Paused_Extensions_Storage class
*
* @package WordPress
* @since 5.2.0
*/
/**
* Core class used for storing paused extensions.
*
* @since 5.2.0
*/
#[AllowDynamicProperties]
class WP_Paused_Extensions_Storage {
/**
* Type of extension. Used to key extension storage. Either 'plugin' or 'theme'.
*
* @since 5.2.0
* @var string
*/
protected $type;
/**
* Constructor.
*
* @since 5.2.0
*
* @param string $extension_type Extension type. Either 'plugin' or 'theme'.
*/
public function __construct( $extension_type ) {
$this->type = $extension_type;
}
/**
* Records an extension error.
*
* Only one error is stored per extension, with subsequent errors for the same extension overriding the
* previously stored error.
*
* @since 5.2.0
*
* @param string $extension Plugin or theme directory name.
* @param array $error {
* Error information returned by `error_get_last()`.
*
* @type int $type The error type.
* @type string $file The name of the file in which the error occurred.
* @type int $line The line number in which the error occurred.
* @type string $message The error message.
* }
* @return bool True on success, false on failure.
*/
public function set( $extension, $error ) {
if ( ! $this->is_api_loaded() ) {
return false;
}
$option_name = $this->get_option_name();
if ( ! $option_name ) {
return false;
}
$paused_extensions = (array) get_option( $option_name, array() );
// Do not update if the error is already stored.
if ( isset( $paused_extensions[ $this->type ][ $extension ] ) && $paused_extensions[ $this->type ][ $extension ] === $error ) {
return true;
}
$paused_extensions[ $this->type ][ $extension ] = $error;
return update_option( $option_name, $paused_extensions, false );
}
/**
* Forgets a previously recorded extension error.
*
* @since 5.2.0
*
* @param string $extension Plugin or theme directory name.
* @return bool True on success, false on failure.
*/
public function delete( $extension ) {
if ( ! $this->is_api_loaded() ) {
return false;
}
$option_name = $this->get_option_name();
if ( ! $option_name ) {
return false;
}
$paused_extensions = (array) get_option( $option_name, array() );
// Do not delete if no error is stored.
if ( ! isset( $paused_extensions[ $this->type ][ $extension ] ) ) {
return true;
}
unset( $paused_extensions[ $this->type ][ $extension ] );
if ( empty( $paused_extensions[ $this->type ] ) ) {
unset( $paused_extensions[ $this->type ] );
}
// Clean up the entire option if we're removing the only error.
if ( ! $paused_extensions ) {
return delete_option( $option_name );
}
return update_option( $option_name, $paused_extensions, false );
}
/**
* Gets the error for an extension, if paused.
*
* @since 5.2.0
*
* @param string $extension Plugin or theme directory name.
* @return array|null Error that is stored, or null if the extension is not paused.
*/
public function get( $extension ) {
if ( ! $this->is_api_loaded() ) {
return null;
}
$paused_extensions = $this->get_all();
if ( ! isset( $paused_extensions[ $extension ] ) ) {
return null;
}
return $paused_extensions[ $extension ];
}
/**
* Gets the paused extensions with their errors.
*
* @since 5.2.0
*
* @return array {
* Associative array of errors keyed by extension slug.
*
* @type array ...$0 Error information returned by `error_get_last()`.
* }
*/
public function get_all() {
if ( ! $this->is_api_loaded() ) {
return array();
}
$option_name = $this->get_option_name();
if ( ! $option_name ) {
return array();
}
$paused_extensions = (array) get_option( $option_name, array() );
return isset( $paused_extensions[ $this->type ] ) ? $paused_extensions[ $this->type ] : array();
}
/**
* Remove all paused extensions.
*
* @since 5.2.0
*
* @return bool
*/
public function delete_all() {
if ( ! $this->is_api_loaded() ) {
return false;
}
$option_name = $this->get_option_name();
if ( ! $option_name ) {
return false;
}
$paused_extensions = (array) get_option( $option_name, array() );
unset( $paused_extensions[ $this->type ] );
if ( ! $paused_extensions ) {
return delete_option( $option_name );
}
return update_option( $option_name, $paused_extensions, false );
}
/**
* Checks whether the underlying API to store paused extensions is loaded.
*
* @since 5.2.0
*
* @return bool True if the API is loaded, false otherwise.
*/
protected function is_api_loaded() {
return function_exists( 'get_option' );
}
/**
* Get the option name for storing paused extensions.
*
* @since 5.2.0
*
* @return string
*/
protected function get_option_name() {
if ( ! wp_recovery_mode()->is_active() ) {
return '';
}
$session_id = wp_recovery_mode()->get_session_id();
if ( empty( $session_id ) ) {
return '';
}
return "{$session_id}_paused_extensions";
}
}