[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Error Protection API: WP_Paused_Extensions_Storage class 4 * 5 * @package WordPress 6 * @since 5.2.0 7 */ 8 9 /** 10 * Core class used for storing paused extensions. 11 * 12 * @since 5.2.0 13 */ 14 class WP_Paused_Extensions_Storage { 15 16 /** 17 * Type of extension. Used to key extension storage. 18 * 19 * @since 5.2.0 20 * @var string 21 */ 22 protected $type; 23 24 /** 25 * Constructor. 26 * 27 * @since 5.2.0 28 * 29 * @param string $extension_type Extension type. Either 'plugin' or 'theme'. 30 */ 31 public function __construct( $extension_type ) { 32 $this->type = $extension_type; 33 } 34 35 /** 36 * Records an extension error. 37 * 38 * Only one error is stored per extension, with subsequent errors for the same extension overriding the 39 * previously stored error. 40 * 41 * @since 5.2.0 42 * 43 * @param string $extension Plugin or theme directory name. 44 * @param array $error { 45 * Error information returned by `error_get_last()`. 46 * 47 * @type int $type The error type. 48 * @type string $file The name of the file in which the error occurred. 49 * @type int $line The line number in which the error occurred. 50 * @type string $message The error message. 51 * } 52 * @return bool True on success, false on failure. 53 */ 54 public function set( $extension, $error ) { 55 if ( ! $this->is_api_loaded() ) { 56 return false; 57 } 58 59 $option_name = $this->get_option_name(); 60 61 if ( ! $option_name ) { 62 return false; 63 } 64 65 $paused_extensions = (array) get_option( $option_name, array() ); 66 67 // Do not update if the error is already stored. 68 if ( isset( $paused_extensions[ $this->type ][ $extension ] ) && $paused_extensions[ $this->type ][ $extension ] === $error ) { 69 return true; 70 } 71 72 $paused_extensions[ $this->type ][ $extension ] = $error; 73 74 return update_option( $option_name, $paused_extensions ); 75 } 76 77 /** 78 * Forgets a previously recorded extension error. 79 * 80 * @since 5.2.0 81 * 82 * @param string $extension Plugin or theme directory name. 83 * @return bool True on success, false on failure. 84 */ 85 public function delete( $extension ) { 86 if ( ! $this->is_api_loaded() ) { 87 return false; 88 } 89 90 $option_name = $this->get_option_name(); 91 92 if ( ! $option_name ) { 93 return false; 94 } 95 96 $paused_extensions = (array) get_option( $option_name, array() ); 97 98 // Do not delete if no error is stored. 99 if ( ! isset( $paused_extensions[ $this->type ][ $extension ] ) ) { 100 return true; 101 } 102 103 unset( $paused_extensions[ $this->type ][ $extension ] ); 104 105 if ( empty( $paused_extensions[ $this->type ] ) ) { 106 unset( $paused_extensions[ $this->type ] ); 107 } 108 109 // Clean up the entire option if we're removing the only error. 110 if ( ! $paused_extensions ) { 111 return delete_option( $option_name ); 112 } 113 114 return update_option( $option_name, $paused_extensions ); 115 } 116 117 /** 118 * Gets the error for an extension, if paused. 119 * 120 * @since 5.2.0 121 * 122 * @param string $extension Plugin or theme directory name. 123 * @return array|null Error that is stored, or null if the extension is not paused. 124 */ 125 public function get( $extension ) { 126 if ( ! $this->is_api_loaded() ) { 127 return null; 128 } 129 130 $paused_extensions = $this->get_all(); 131 132 if ( ! isset( $paused_extensions[ $extension ] ) ) { 133 return null; 134 } 135 136 return $paused_extensions[ $extension ]; 137 } 138 139 /** 140 * Gets the paused extensions with their errors. 141 * 142 * @since 5.2.0 143 * 144 * @return array { 145 * Associative array of errors keyed by extension slug. 146 * 147 * @type array ...$0 Error information returned by `error_get_last()`. 148 * } 149 */ 150 public function get_all() { 151 if ( ! $this->is_api_loaded() ) { 152 return array(); 153 } 154 155 $option_name = $this->get_option_name(); 156 157 if ( ! $option_name ) { 158 return array(); 159 } 160 161 $paused_extensions = (array) get_option( $option_name, array() ); 162 163 return isset( $paused_extensions[ $this->type ] ) ? $paused_extensions[ $this->type ] : array(); 164 } 165 166 /** 167 * Remove all paused extensions. 168 * 169 * @since 5.2.0 170 * 171 * @return bool 172 */ 173 public function delete_all() { 174 if ( ! $this->is_api_loaded() ) { 175 return false; 176 } 177 178 $option_name = $this->get_option_name(); 179 180 if ( ! $option_name ) { 181 return false; 182 } 183 184 $paused_extensions = (array) get_option( $option_name, array() ); 185 186 unset( $paused_extensions[ $this->type ] ); 187 188 if ( ! $paused_extensions ) { 189 return delete_option( $option_name ); 190 } 191 192 return update_option( $option_name, $paused_extensions ); 193 } 194 195 /** 196 * Checks whether the underlying API to store paused extensions is loaded. 197 * 198 * @since 5.2.0 199 * 200 * @return bool True if the API is loaded, false otherwise. 201 */ 202 protected function is_api_loaded() { 203 return function_exists( 'get_option' ); 204 } 205 206 /** 207 * Get the option name for storing paused extensions. 208 * 209 * @since 5.2.0 210 * 211 * @return string 212 */ 213 protected function get_option_name() { 214 if ( ! wp_recovery_mode()->is_active() ) { 215 return ''; 216 } 217 218 $session_id = wp_recovery_mode()->get_session_id(); 219 if ( empty( $session_id ) ) { 220 return ''; 221 } 222 223 return "{$session_id}_paused_extensions"; 224 } 225 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 14 01:00:07 2024 | Cross-referenced by PHPXref 0.7.1 |