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

206 lines
4.5 KiB
PHP
Raw Normal View History

2021-09-21 15:01:08 +02:00
<?php
/**
* WPSEO plugin file.
*
* @package WPSEO\Admin\ConfigurationUI
*/
/**
2021-09-21 15:05:30 +02:00
* Class WPSEO_Configuration_Storage.
2021-09-21 15:01:08 +02:00
*/
class WPSEO_Configuration_Storage {
2021-09-21 15:05:30 +02:00
/**
* Holds the configuration options adapter.
*
* @var \WPSEO_Configuration_Options_Adapter
*/
2021-09-21 15:01:08 +02:00
protected $adapter;
2021-09-21 15:05:30 +02:00
/**
* Holds the configuration fields.
*
* @var \WPSEO_Config_Field[]
*/
protected $fields = [];
2021-09-21 15:01:08 +02:00
/**
2021-09-21 15:05:30 +02:00
* Add default fields.
2021-09-21 15:01:08 +02:00
*/
public function add_default_fields() {
2021-09-21 15:05:30 +02:00
$fields = [
2021-09-21 15:01:08 +02:00
new WPSEO_Config_Field_Success_Message(),
new WPSEO_Config_Field_Mailchimp_Signup(),
new WPSEO_Config_Field_Environment(),
new WPSEO_Config_Field_Site_Type(),
new WPSEO_Config_Field_Multiple_Authors(),
new WPSEO_Config_Field_Title_Intro(),
new WPSEO_Config_Field_Site_Name(),
new WPSEO_Config_Field_Separator(),
new WPSEO_Config_Field_Profile_URL_Facebook(),
new WPSEO_Config_Field_Profile_URL_Twitter(),
new WPSEO_Config_Field_Profile_URL_Instagram(),
new WPSEO_Config_Field_Profile_URL_LinkedIn(),
new WPSEO_Config_Field_Profile_URL_MySpace(),
new WPSEO_Config_Field_Profile_URL_Pinterest(),
new WPSEO_Config_Field_Profile_URL_YouTube(),
2021-09-21 15:05:30 +02:00
new WPSEO_Config_Field_Profile_URL_Wikipedia(),
2021-09-21 15:01:08 +02:00
new WPSEO_Config_Field_Company_Or_Person(),
2021-09-21 15:05:30 +02:00
new WPSEO_Config_Field_Company_Info_Missing(),
2021-09-21 15:01:08 +02:00
new WPSEO_Config_Field_Company_Name(),
new WPSEO_Config_Field_Company_Logo(),
2021-09-21 15:05:30 +02:00
new WPSEO_Config_Field_Person(),
2021-09-21 15:01:08 +02:00
new WPSEO_Config_Field_Post_Type_Visibility(),
2021-09-21 15:05:30 +02:00
new WPSEO_Config_Field_Tracking_Intro(),
new WPSEO_Config_Field_Tracking(),
];
2021-09-21 15:01:08 +02:00
$post_type_factory = new WPSEO_Config_Factory_Post_Type();
$fields = array_merge( $fields, $post_type_factory->get_fields() );
foreach ( $fields as $field ) {
$this->add_field( $field );
}
}
/**
2021-09-21 15:05:30 +02:00
* Allow for field injections.
2021-09-21 15:01:08 +02:00
*
* @param WPSEO_Config_Field $field Field to add to the stack.
*/
public function add_field( WPSEO_Config_Field $field ) {
$this->fields[] = $field;
if ( isset( $this->adapter ) ) {
$field->set_adapter( $this->adapter );
}
}
/**
2021-09-21 15:05:30 +02:00
* Set the adapter to use.
2021-09-21 15:01:08 +02:00
*
* @param WPSEO_Configuration_Options_Adapter $adapter Adapter to use.
*/
public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
$this->adapter = $adapter;
foreach ( $this->fields as $field ) {
$field->set_adapter( $this->adapter );
}
}
/**
2021-09-21 15:05:30 +02:00
* Retrieve the current adapter.
2021-09-21 15:01:08 +02:00
*
* @return WPSEO_Configuration_Options_Adapter
*/
public function get_adapter() {
return $this->adapter;
}
/**
2021-09-21 15:05:30 +02:00
* Retrieve the registered fields.
2021-09-21 15:01:08 +02:00
*
2021-09-21 15:05:30 +02:00
* @return array List of settings.
2021-09-21 15:01:08 +02:00
*/
public function retrieve() {
2021-09-21 15:05:30 +02:00
$output = [];
2021-09-21 15:01:08 +02:00
foreach ( $this->fields as $field ) {
$build = $field->to_array();
$data = $this->get_field_data( $field );
if ( ! is_null( $data ) ) {
$build['data'] = $data;
}
$output[ $field->get_identifier() ] = $build;
}
return $output;
}
/**
2021-09-21 15:05:30 +02:00
* Save the data.
2021-09-21 15:01:08 +02:00
*
* @param array $data_to_store Data provided by the API which needs to be processed for saving.
*
* @return string Results
*/
public function store( $data_to_store ) {
2021-09-21 15:05:30 +02:00
$output = [];
2021-09-21 15:01:08 +02:00
foreach ( $this->fields as $field ) {
$field_identifier = $field->get_identifier();
if ( ! array_key_exists( $field_identifier, $data_to_store ) ) {
continue;
}
2021-09-21 15:05:30 +02:00
$field_data = [];
2021-09-21 15:01:08 +02:00
if ( isset( $data_to_store[ $field_identifier ] ) ) {
$field_data = $data_to_store[ $field_identifier ];
}
$result = $this->adapter->set( $field, $field_data );
2021-09-21 15:05:30 +02:00
$build = [
2021-09-21 15:01:08 +02:00
'result' => $result,
2021-09-21 15:05:30 +02:00
];
2021-09-21 15:01:08 +02:00
// Set current data to object to be displayed.
$data = $this->get_field_data( $field );
if ( ! is_null( $data ) ) {
$build['data'] = $data;
}
$output[ $field_identifier ] = $build;
}
return $output;
}
/**
2021-09-21 15:05:30 +02:00
* Filter out null input values.
2021-09-21 15:01:08 +02:00
*
* @param mixed $input Input to test against.
*
* @return bool
*/
protected function is_not_null( $input ) {
return ! is_null( $input );
}
/**
2021-09-21 15:05:30 +02:00
* Get data from a specific field.
2021-09-21 15:01:08 +02:00
*
* @param WPSEO_Config_Field $field Field to get data for.
*
* @return array|mixed
*/
protected function get_field_data( WPSEO_Config_Field $field ) {
$data = $this->adapter->get( $field );
if ( is_array( $data ) ) {
$defaults = $field->get_data();
// Remove 'null' values from input.
2021-09-21 15:05:30 +02:00
$data = array_filter( $data, [ $this, 'is_not_null' ] );
2021-09-21 15:01:08 +02:00
// Merge defaults with data.
$data = array_merge( $defaults, $data );
}
if ( is_null( $data ) ) {
// Get default if no data was set.
$data = $field->get_data();
return $data;
}
return $data;
}
}