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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. *
  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. *
  105. * @return array|null Error that is stored, or null if the extension is not paused.
  106. */
  107. public function get( $extension ) {
  108. if ( ! $this->is_api_loaded() ) {
  109. return null;
  110. }
  111. $paused_extensions = $this->get_all();
  112. if ( ! isset( $paused_extensions[ $extension ] ) ) {
  113. return null;
  114. }
  115. return $paused_extensions[ $extension ];
  116. }
  117. /**
  118. * Gets the paused extensions with their errors.
  119. *
  120. * @since 5.2.0
  121. *
  122. * @return array Associative array of extension slugs to the error recorded.
  123. */
  124. public function get_all() {
  125. if ( ! $this->is_api_loaded() ) {
  126. return array();
  127. }
  128. $option_name = $this->get_option_name();
  129. if ( ! $option_name ) {
  130. return array();
  131. }
  132. $paused_extensions = (array) get_option( $option_name, array() );
  133. return isset( $paused_extensions[ $this->type ] ) ? $paused_extensions[ $this->type ] : array();
  134. }
  135. /**
  136. * Remove all paused extensions.
  137. *
  138. * @since 5.2.0
  139. *
  140. * @return bool
  141. */
  142. public function delete_all() {
  143. if ( ! $this->is_api_loaded() ) {
  144. return false;
  145. }
  146. $option_name = $this->get_option_name();
  147. if ( ! $option_name ) {
  148. return false;
  149. }
  150. $paused_extensions = (array) get_option( $option_name, array() );
  151. unset( $paused_extensions[ $this->type ] );
  152. if ( ! $paused_extensions ) {
  153. return delete_option( $option_name );
  154. }
  155. return update_option( $option_name, $paused_extensions );
  156. }
  157. /**
  158. * Checks whether the underlying API to store paused extensions is loaded.
  159. *
  160. * @since 5.2.0
  161. *
  162. * @return bool True if the API is loaded, false otherwise.
  163. */
  164. protected function is_api_loaded() {
  165. return function_exists( 'get_option' );
  166. }
  167. /**
  168. * Get the option name for storing paused extensions.
  169. *
  170. * @since 5.2.0
  171. *
  172. * @return string
  173. */
  174. protected function get_option_name() {
  175. if ( ! wp_recovery_mode()->is_active() ) {
  176. return '';
  177. }
  178. $session_id = wp_recovery_mode()->get_session_id();
  179. if ( empty( $session_id ) ) {
  180. return '';
  181. }
  182. return "{$session_id}_paused_extensions";
  183. }
  184. }