説明なし

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 that was triggered.
  42. *
  43. * @type string $type The error type.
  44. * @type string $file The name of the file in which the error occurred.
  45. * @type string $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 Associative array of extension slugs to the error recorded.
  121. */
  122. public function get_all() {
  123. if ( ! $this->is_api_loaded() ) {
  124. return array();
  125. }
  126. $option_name = $this->get_option_name();
  127. if ( ! $option_name ) {
  128. return array();
  129. }
  130. $paused_extensions = (array) get_option( $option_name, array() );
  131. return isset( $paused_extensions[ $this->type ] ) ? $paused_extensions[ $this->type ] : array();
  132. }
  133. /**
  134. * Remove all paused extensions.
  135. *
  136. * @since 5.2.0
  137. *
  138. * @return bool
  139. */
  140. public function delete_all() {
  141. if ( ! $this->is_api_loaded() ) {
  142. return false;
  143. }
  144. $option_name = $this->get_option_name();
  145. if ( ! $option_name ) {
  146. return false;
  147. }
  148. $paused_extensions = (array) get_option( $option_name, array() );
  149. unset( $paused_extensions[ $this->type ] );
  150. if ( ! $paused_extensions ) {
  151. return delete_option( $option_name );
  152. }
  153. return update_option( $option_name, $paused_extensions );
  154. }
  155. /**
  156. * Checks whether the underlying API to store paused extensions is loaded.
  157. *
  158. * @since 5.2.0
  159. *
  160. * @return bool True if the API is loaded, false otherwise.
  161. */
  162. protected function is_api_loaded() {
  163. return function_exists( 'get_option' );
  164. }
  165. /**
  166. * Get the option name for storing paused extensions.
  167. *
  168. * @since 5.2.0
  169. *
  170. * @return string
  171. */
  172. protected function get_option_name() {
  173. if ( ! wp_recovery_mode()->is_active() ) {
  174. return '';
  175. }
  176. $session_id = wp_recovery_mode()->get_session_id();
  177. if ( empty( $session_id ) ) {
  178. return '';
  179. }
  180. return "{$session_id}_paused_extensions";
  181. }
  182. }