class-configuration-service.php 4.1 KB

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