class-wp-paused-extensions-storage.php 4.9 KB

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