class-configuration-storage.php 4.6 KB

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