class-configuration-service.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Configuration_Service.
  9. */
  10. class WPSEO_Configuration_Service {
  11. /**
  12. * Class holding the onboarding wizard configuration.
  13. *
  14. * @var WPSEO_Configuration_Structure
  15. */
  16. protected $structure;
  17. /**
  18. * Class holding the onboarding wizard components.
  19. *
  20. * @var WPSEO_Configuration_Components
  21. */
  22. protected $components;
  23. /**
  24. * Class handling the onboarding wizard persistence.
  25. *
  26. * @var WPSEO_Configuration_Storage
  27. */
  28. protected $storage;
  29. /**
  30. * Class handling the onboarding wizard endpoint.
  31. *
  32. * @var WPSEO_Configuration_Endpoint
  33. */
  34. protected $endpoint;
  35. /**
  36. * Adapter that converts onboarding wizard configuration to WordPress options.
  37. *
  38. * @var WPSEO_Configuration_Options_Adapter
  39. */
  40. protected $adapter;
  41. /**
  42. * Class handling the onboarding wizard endpoint.
  43. *
  44. * @var WPSEO_Configuration_Translations
  45. */
  46. protected $translations;
  47. /**
  48. * Hook into the REST API and switch language.
  49. */
  50. public function initialize() {
  51. $this->set_default_providers();
  52. $this->endpoint->register();
  53. }
  54. /**
  55. * Set default handlers.
  56. */
  57. public function set_default_providers() {
  58. $this->set_storage( new WPSEO_Configuration_Storage() );
  59. $this->set_options_adapter( new WPSEO_Configuration_Options_Adapter() );
  60. $this->set_components( new WPSEO_Configuration_Components() );
  61. $this->set_endpoint( new WPSEO_Configuration_Endpoint() );
  62. $this->set_structure( new WPSEO_Configuration_Structure() );
  63. $this->set_translations( new WPSEO_Configuration_Translations( WPSEO_Language_Utils::get_user_locale() ) );
  64. }
  65. /**
  66. * Set storage handler.
  67. *
  68. * @param WPSEO_Configuration_Storage $storage Storage handler to use.
  69. */
  70. public function set_storage( WPSEO_Configuration_Storage $storage ) {
  71. $this->storage = $storage;
  72. }
  73. /**
  74. * Set endpoint handler.
  75. *
  76. * @param WPSEO_Configuration_Endpoint $endpoint Endpoint implementation to use.
  77. */
  78. public function set_endpoint( WPSEO_Configuration_Endpoint $endpoint ) {
  79. $this->endpoint = $endpoint;
  80. $this->endpoint->set_service( $this );
  81. }
  82. /**
  83. * Set the options adapter.
  84. *
  85. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to use.
  86. */
  87. public function set_options_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  88. $this->adapter = $adapter;
  89. }
  90. /**
  91. * Set components provider.
  92. *
  93. * @param WPSEO_Configuration_Components $components Component provider to use.
  94. */
  95. public function set_components( WPSEO_Configuration_Components $components ) {
  96. $this->components = $components;
  97. }
  98. /**
  99. * Set structure provider.
  100. *
  101. * @param WPSEO_Configuration_Structure $structure Structure provider to use.
  102. */
  103. public function set_structure( WPSEO_Configuration_Structure $structure ) {
  104. $this->structure = $structure;
  105. }
  106. /**
  107. * Sets the translations object.
  108. *
  109. * @param WPSEO_Configuration_Translations $translations The translations object.
  110. */
  111. public function set_translations( WPSEO_Configuration_Translations $translations ) {
  112. $this->translations = $translations;
  113. }
  114. /**
  115. * Populate the configuration.
  116. */
  117. protected function populate_configuration() {
  118. // Switch to the user locale with fallback to the site locale.
  119. switch_to_locale( WPSEO_Language_Utils::get_user_locale() );
  120. // Make sure we have our translations available.
  121. wpseo_load_textdomain();
  122. $this->structure->initialize();
  123. $this->storage->set_adapter( $this->adapter );
  124. $this->storage->add_default_fields();
  125. $this->components->initialize();
  126. $this->components->set_storage( $this->storage );
  127. // @todo: check if this is really needed, since the switch happens only in the API.
  128. if ( function_exists( 'restore_current_locale' ) ) {
  129. restore_current_locale();
  130. }
  131. }
  132. /**
  133. * Used by endpoint to retrieve configuration.
  134. *
  135. * @return array List of settings.
  136. */
  137. public function get_configuration() {
  138. $this->populate_configuration();
  139. $fields = $this->storage->retrieve();
  140. $steps = $this->structure->retrieve();
  141. $translations = $this->translations->retrieve();
  142. return [
  143. 'fields' => $fields,
  144. 'steps' => $steps,
  145. 'translations' => $translations,
  146. ];
  147. }
  148. /**
  149. * Used by endpoint to store changes.
  150. *
  151. * @param WP_REST_Request $request Request from the REST API.
  152. *
  153. * @return array List of feedback per option if saving succeeded.
  154. */
  155. public function set_configuration( WP_REST_Request $request ) {
  156. $this->populate_configuration();
  157. return $this->storage->store( $request->get_json_params() );
  158. }
  159. }