Nessuna descrizione

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

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