PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/remyvianne/krowkaramel
PHP | 305 lines | 149 code | 47 blank | 109 comment | 26 complexity | a544c6e85e147e8a61f88087e65b329c 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-config'
  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. * Setup the Identity Crisis options.
  208. *
  209. * @return bool
  210. */
  211. protected function ensure_options_identity_crisis() {
  212. $options = $this->get_feature_options( 'identity_crisis' );
  213. if ( is_array( $options ) && count( $options ) ) {
  214. add_filter(
  215. 'jetpack_idc_consumers',
  216. function ( $consumers ) use ( $options ) {
  217. $consumers[] = $options;
  218. return $consumers;
  219. }
  220. );
  221. }
  222. return true;
  223. }
  224. /**
  225. * Setup the Sync options.
  226. */
  227. protected function ensure_options_sync() {
  228. $options = $this->get_feature_options( 'sync' );
  229. if ( method_exists( 'Automattic\Jetpack\Sync\Main', 'set_sync_data_options' ) ) {
  230. Sync_Main::set_sync_data_options( $options );
  231. }
  232. return true;
  233. }
  234. /**
  235. * Temporary save initialization options for a feature.
  236. *
  237. * @param string $feature The feature slug.
  238. * @param array $options The options.
  239. *
  240. * @return bool
  241. */
  242. protected function set_feature_options( $feature, array $options ) {
  243. if ( $options ) {
  244. $this->feature_options[ $feature ] = $options;
  245. }
  246. return true;
  247. }
  248. /**
  249. * Get initialization options for a feature from the temporary storage.
  250. *
  251. * @param string $feature The feature slug.
  252. *
  253. * @return array
  254. */
  255. protected function get_feature_options( $feature ) {
  256. return empty( $this->feature_options[ $feature ] ) ? array() : $this->feature_options[ $feature ];
  257. }
  258. }