/wp-content/plugins/jetpack/vendor/automattic/jetpack-config/src/class-config.php

https://github.com/livinglab/openlab · PHP · 272 lines · 129 code · 42 blank · 101 comment · 23 complexity · 3875914ffc195e90d27ccf968fc92787 MD5 · raw file

  1. <?php
  2. /**
  3. * The base Jetpack configuration class file.
  4. *
  5. * @package automattic/jetpack-config
  6. */
  7. namespace Automattic\Jetpack;
  8. /*
  9. * The Config package does not require the composer packages that
  10. * contain the package classes shown below. The consumer plugin
  11. * must require the corresponding packages to use these features.
  12. */
  13. use Automattic\Jetpack\Connection\Manager;
  14. use Automattic\Jetpack\Connection\Plugin;
  15. use Automattic\Jetpack\JITM as JITM;
  16. use Automattic\Jetpack\JITMS\JITM as JITMS_JITM;
  17. use Automattic\Jetpack\Post_List\Post_List as Post_List;
  18. use Automattic\Jetpack\Sync\Main as Sync_Main;
  19. /**
  20. * The configuration class.
  21. */
  22. class Config {
  23. const FEATURE_ENSURED = 1;
  24. const FEATURE_NOT_AVAILABLE = 0;
  25. const FEATURE_ALREADY_ENSURED = -1;
  26. /**
  27. * The initial setting values.
  28. *
  29. * @var Array
  30. */
  31. protected $config = array(
  32. 'jitm' => false,
  33. 'connection' => false,
  34. 'sync' => false,
  35. 'post_list' => false,
  36. 'identity_crisis' => false,
  37. );
  38. /**
  39. * Initialization options stored here.
  40. *
  41. * @var array
  42. */
  43. protected $feature_options = array();
  44. /**
  45. * Creates the configuration class instance.
  46. */
  47. public function __construct() {
  48. /**
  49. * Adding the config handler to run on priority 2 because the class itself is
  50. * being constructed on priority 1.
  51. */
  52. add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), 2 );
  53. }
  54. /**
  55. * Require a feature to be initialized. It's up to the package consumer to actually add
  56. * the package to their composer project. Declaring a requirement using this method
  57. * instructs the class to initialize it.
  58. *
  59. * @param String $feature the feature slug.
  60. * @param array $options Additional options, optional.
  61. */
  62. public function ensure( $feature, array $options = array() ) {
  63. $this->config[ $feature ] = true;
  64. $this->set_feature_options( $feature, $options );
  65. }
  66. /**
  67. * Runs on plugins_loaded hook priority with priority 2.
  68. *
  69. * @action plugins_loaded
  70. */
  71. public function on_plugins_loaded() {
  72. if ( $this->config['connection'] ) {
  73. $this->ensure_class( 'Automattic\Jetpack\Connection\Manager' )
  74. && $this->ensure_feature( 'connection' );
  75. }
  76. if ( $this->config['sync'] ) {
  77. $this->ensure_class( 'Automattic\Jetpack\Sync\Main' )
  78. && $this->ensure_feature( 'sync' );
  79. }
  80. if ( $this->config['jitm'] ) {
  81. // Check for the JITM class in both namespaces. The namespace was changed in jetpack-jitm v1.6.
  82. ( $this->ensure_class( 'Automattic\Jetpack\JITMS\JITM', false )
  83. || $this->ensure_class( 'Automattic\Jetpack\JITM' ) )
  84. && $this->ensure_feature( 'jitm' );
  85. }
  86. if ( $this->config['post_list'] ) {
  87. $this->ensure_class( 'Automattic\Jetpack\Post_List\Post_List' )
  88. && $this->ensure_feature( 'post_list' );
  89. }
  90. if ( $this->config['identity_crisis'] ) {
  91. $this->ensure_class( 'Automattic\Jetpack\Identity_Crisis' )
  92. && $this->ensure_feature( 'identity_crisis' );
  93. }
  94. }
  95. /**
  96. * Returns true if the required class is available and alerts the user if it's not available
  97. * in case the site is in debug mode.
  98. *
  99. * @param String $classname a fully qualified class name.
  100. * @param Boolean $log_notice whether the E_USER_NOTICE should be generated if the class is not found.
  101. *
  102. * @return Boolean whether the class is available.
  103. */
  104. protected function ensure_class( $classname, $log_notice = true ) {
  105. $available = class_exists( $classname );
  106. if ( $log_notice && ! $available && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  107. trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
  108. sprintf(
  109. /* translators: %1$s is a PHP class name. */
  110. esc_html__(
  111. 'Unable to load class %1$s. Please add the package that contains it using composer and make sure you are requiring the Jetpack autoloader',
  112. 'jetpack'
  113. ),
  114. esc_html( $classname )
  115. ),
  116. E_USER_NOTICE
  117. );
  118. }
  119. return $available;
  120. }
  121. /**
  122. * Ensures a feature is enabled, sets it up if it hasn't already been set up.
  123. * Run the options method (if exists) every time the method is called.
  124. *
  125. * @param String $feature slug of the feature.
  126. * @return Integer either FEATURE_ENSURED, FEATURE_ALREADY_ENSURED or FEATURE_NOT_AVAILABLE constants.
  127. */
  128. protected function ensure_feature( $feature ) {
  129. $method = 'enable_' . $feature;
  130. if ( ! method_exists( $this, $method ) ) {
  131. return self::FEATURE_NOT_AVAILABLE;
  132. }
  133. $method_options = 'ensure_options_' . $feature;
  134. if ( method_exists( $this, $method_options ) ) {
  135. $this->{ $method_options }();
  136. }
  137. if ( did_action( 'jetpack_feature_' . $feature . '_enabled' ) ) {
  138. return self::FEATURE_ALREADY_ENSURED;
  139. }
  140. $this->{ $method }();
  141. /**
  142. * Fires when a specific Jetpack package feature is initalized using the Config package.
  143. *
  144. * @since 1.1.0
  145. */
  146. do_action( 'jetpack_feature_' . $feature . '_enabled' );
  147. return self::FEATURE_ENSURED;
  148. }
  149. /**
  150. * Enables the JITM feature.
  151. */
  152. protected function enable_jitm() {
  153. if ( class_exists( 'Automattic\Jetpack\JITMS\JITM' ) ) {
  154. JITMS_JITM::configure();
  155. } else {
  156. // Provides compatibility with jetpack-jitm <v1.6.
  157. JITM::configure();
  158. }
  159. return true;
  160. }
  161. /**
  162. * Enables the Post_List feature.
  163. */
  164. protected function enable_post_list() {
  165. Post_List::configure();
  166. return true;
  167. }
  168. /**
  169. * Enables the Sync feature.
  170. */
  171. protected function enable_sync() {
  172. Sync_Main::configure();
  173. return true;
  174. }
  175. /**
  176. * Enables the Connection feature.
  177. */
  178. protected function enable_connection() {
  179. Manager::configure();
  180. return true;
  181. }
  182. /**
  183. * Enables the identity-crisis feature.
  184. */
  185. protected function enable_identity_crisis() {
  186. Identity_Crisis::init();
  187. }
  188. /**
  189. * Setup the Connection options.
  190. */
  191. protected function ensure_options_connection() {
  192. $options = $this->get_feature_options( 'connection' );
  193. if ( ! empty( $options['slug'] ) ) {
  194. // The `slug` and `name` are removed from the options because they need to be passed as arguments.
  195. $slug = $options['slug'];
  196. unset( $options['slug'] );
  197. $name = $slug;
  198. if ( ! empty( $options['name'] ) ) {
  199. $name = $options['name'];
  200. unset( $options['name'] );
  201. }
  202. ( new Plugin( $slug ) )->add( $name, $options );
  203. }
  204. return true;
  205. }
  206. /**
  207. * Temporary save initialization options for a feature.
  208. *
  209. * @param string $feature The feature slug.
  210. * @param array $options The options.
  211. *
  212. * @return bool
  213. */
  214. protected function set_feature_options( $feature, array $options ) {
  215. if ( $options ) {
  216. $this->feature_options[ $feature ] = $options;
  217. }
  218. return true;
  219. }
  220. /**
  221. * Get initialization options for a feature from the temporary storage.
  222. *
  223. * @param string $feature The feature slug.
  224. *
  225. * @return array
  226. */
  227. protected function get_feature_options( $feature ) {
  228. return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ];
  229. }
  230. }