/wp-content/plugins/wordpress-seo/admin/config-ui/class-configuration-storage.php

https://bitbucket.org/carloskikea/helpet · PHP · 200 lines · 105 code · 35 blank · 60 comment · 7 complexity · 58e991d08eac9ea6bd66080482391239 MD5 · raw file

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