WPSEO_Configuration_Storage

Class WPSEO_Configuration_Storage.

Defined (1)

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

/admin/config-ui/class-configuration-storage.php  
  1. class WPSEO_Configuration_Storage { 
  2.  
  3. /** @var WPSEO_Configuration_Options_Adapter */ 
  4. protected $adapter; 
  5.  
  6. /** @var array WPSEO_Config_Field */ 
  7. protected $fields = array(); 
  8.  
  9. /** 
  10. * Add default fields 
  11. */ 
  12. public function add_default_fields() { 
  13. $fields = array( 
  14. new WPSEO_Config_Field_Upsell_Configuration_Service(),  
  15. new WPSEO_Config_Field_Upsell_Site_Review(),  
  16. new WPSEO_Config_Field_Success_Message(),  
  17. new WPSEO_Config_Field_Mailchimp_Signup(),  
  18. new WPSEO_Config_Field_Environment(),  
  19. new WPSEO_Config_Field_Site_Type(),  
  20. new WPSEO_Config_Field_Multiple_Authors(),  
  21. new WPSEO_Config_Field_Site_Name(),  
  22. new WPSEO_Config_Field_Separator(),  
  23. new WPSEO_Config_Field_Social_Profiles_Intro(),  
  24. new WPSEO_Config_Field_Profile_URL_Facebook(),  
  25. new WPSEO_Config_Field_Profile_URL_Twitter(),  
  26. new WPSEO_Config_Field_Profile_URL_Instagram(),  
  27. new WPSEO_Config_Field_Profile_URL_LinkedIn(),  
  28. new WPSEO_Config_Field_Profile_URL_MySpace(),  
  29. new WPSEO_Config_Field_Profile_URL_Pinterest(),  
  30. new WPSEO_Config_Field_Profile_URL_YouTube(),  
  31. new WPSEO_Config_Field_Profile_URL_GooglePlus(),  
  32. new WPSEO_Config_Field_Company_Or_Person(),  
  33. new WPSEO_Config_Field_Company_Name(),  
  34. new WPSEO_Config_Field_Company_Logo(),  
  35. new WPSEO_Config_Field_Person_Name(),  
  36. new WPSEO_Config_Field_Post_Type_Visibility(),  
  37. ); 
  38.  
  39. $post_type_factory = new WPSEO_Config_Factory_Post_Type(); 
  40. $fields = array_merge( $fields, $post_type_factory->get_fields() ); 
  41.  
  42. foreach ( $fields as $field ) { 
  43. $this->add_field( $field ); 
  44.  
  45. /** 
  46. * Allow for field injections 
  47. * @param WPSEO_Config_Field $field Field to add to the stack. 
  48. */ 
  49. public function add_field( WPSEO_Config_Field $field ) { 
  50. $this->fields[] = $field; 
  51.  
  52. if ( isset( $this->adapter ) ) { 
  53. $field->set_adapter( $this->adapter ); 
  54.  
  55. /** 
  56. * Set the adapter to use 
  57. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to use. 
  58. */ 
  59. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) { 
  60. $this->adapter = $adapter; 
  61.  
  62. foreach ( $this->fields as $field ) { 
  63. $field->set_adapter( $this->adapter ); 
  64.  
  65. /** 
  66. * Retrieve the current adapter 
  67. * @return WPSEO_Configuration_Options_Adapter 
  68. */ 
  69. public function get_adapter() { 
  70. return $this->adapter; 
  71.  
  72. /** 
  73. * Retrieve the registered fields 
  74. * @returns array List of settings. 
  75. */ 
  76. public function retrieve() { 
  77. $output = array(); 
  78.  
  79. /** @var WPSEO_Config_Field $field */ 
  80. foreach ( $this->fields as $field ) { 
  81.  
  82. $build = $field->to_array(); 
  83.  
  84. $data = $this->get_field_data( $field ); 
  85. if ( ! is_null( $data ) ) { 
  86. $build['data'] = $data; 
  87.  
  88. $output[ $field->get_identifier() ] = $build; 
  89.  
  90. return $output; 
  91.  
  92. /** 
  93. * Save the data 
  94. * @param array $data_to_store Data provided by the API which needs to be processed for saving. 
  95. * @return string Results 
  96. */ 
  97. public function store( $data_to_store ) { 
  98. $output = array(); 
  99.  
  100. /** @var WPSEO_Config_Field $field */ 
  101. foreach ( $this->fields as $field ) { 
  102.  
  103. $field_identifier = $field->get_identifier(); 
  104.  
  105. if ( ! array_key_exists( $field_identifier, $data_to_store ) ) { 
  106. continue; 
  107.  
  108. $field_data = array(); 
  109. if ( isset( $data_to_store[ $field_identifier ] ) ) { 
  110. $field_data = $data_to_store[ $field_identifier ]; 
  111.  
  112. $result = $this->adapter->set( $field, $field_data ); 
  113.  
  114. $build = array( 
  115. 'result' => $result,  
  116. ); 
  117.  
  118. // Set current data to object to be displayed. 
  119. $data = $this->get_field_data( $field ); 
  120. if ( ! is_null( $data ) ) { 
  121. $build['data'] = $data; 
  122.  
  123. $output[ $field_identifier ] = $build; 
  124.  
  125. return $output; 
  126.  
  127. /** 
  128. * Filter out null input values 
  129. * @param mixed $input Input to test against. 
  130. * @return bool 
  131. */ 
  132. protected function is_not_null( $input ) { 
  133. return ! is_null( $input ); 
  134.  
  135. /** 
  136. * Get data from a specific field 
  137. * @param WPSEO_Config_Field $field Field to get data for. 
  138. * @return array|mixed 
  139. */ 
  140. protected function get_field_data( WPSEO_Config_Field $field ) { 
  141. $data = $this->adapter->get( $field ); 
  142.  
  143. if ( is_array( $data ) ) { 
  144. $defaults = $field->get_data(); 
  145.  
  146. // Remove 'null' values from input. 
  147. $data = array_filter( $data, array( $this, 'is_not_null' ) ); 
  148.  
  149. // Merge defaults with data. 
  150. $data = array_merge( $defaults, $data ); 
  151.  
  152. if ( is_null( $data ) ) { 
  153. // Get default if no data was set. 
  154. $data = $field->get_data(); 
  155.  
  156. return $data; 
  157.  
  158. return $data;