PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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