WPSEO_Configuration_Service

Class WPSEO_Configuration_Service.

Defined (1)

The class is defined in the following location(s).

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