<?php
/**
* Setting API class.
*
* @package Magazine Blocks
*/
namespace MagazineBlocks;
defined( 'ABSPATH' ) || exit;
/**
* Setting class.
*/
class Setting {
/**
* Data.
*
* @var array
*/
private $data = array();
/**
* Default data.
*
* @var array
*/
private $defaults = array(
'blocks' => array(
'section' => true,
'heading' => true,
'advertisement' => true,
'banner-posts' => true,
'grid-module' => true,
'featured-posts' => true,
'featured-categories' => true,
'tab-post' => true,
'post-list' => true,
'post-video' => true,
'category-list' => true,
'news-ticker' => true,
'date-weather' => true,
'social-icons' => true,
'slider' => true,
),
'editor' => array(
'section-width' => 1170,
'editor-blocks-spacing' => 24,
'design-library' => true,
'responsive-breakpoints' => array(
'tablet' => 992,
'mobile' => 768,
),
'copy-paste-styles' => true,
'auto-collapse-panels' => true,
),
'performance' => array(
'local-google-fonts' => false,
'preload-local-fonts' => false,
'allow-only-selected-fonts' => false,
'allowed-fonts' => array(),
),
'asset-generation' => array(
'external-file' => false,
),
'version-control' => array(
'beta-tester' => false,
),
'maintenance-mode' => array(
'mode' => 'none',
'maintenance-page' => null,
),
'integrations' => array(
'dateWeatherApiKey' => '',
'dateWeatherZipCode' => '',
),
);
/**
* Sanitize callbacks.
*
* @var array
*/
private $sanitize_callbacks = array(
'blocks' => array(
'featured-posts' => 'magazine_blocks_string_to_bool',
'section' => 'magazine_blocks_string_to_bool',
),
'editor' => array(
'section-width' => 'absint',
'editor-blocks-spacing' => 'absint',
'design-library' => 'magazine_blocks_string_to_bool',
'copy-paste-styles' => 'magazine_blocks_string_to_bool',
'auto-collapse-panels' => 'magazine_blocks_string_to_bool',
'responsive-breakpoints' => array(
'tablet' => 'absint',
'mobile' => 'absint',
),
),
'performance' => array(
'local-google-fonts' => 'magazine_blocks_string_to_bool',
'preload-local-fonts' => 'magazine_blocks_string_to_bool',
'allow-only-selected-fonts' => 'magazine_blocks_string_to_bool',
),
'asset-generation' => array(
'external-file' => 'magazine_blocks_string_to_bool',
),
'version-control' => array(
'beta-tester' => 'magazine_blocks_string_to_bool',
),
'integrations' => array(
'dateWeatherApiKey' => 'sanitize_text_field',
'dateWeatherZipCode' => 'sanitize_text_field',
),
'maintenance-mode' => array(
'mode' => 'sanitize_text_field',
'maintenance-page' => array(
'id' => 'absint',
'title' => 'sanitize_text_field',
),
),
);
/**
* Constructor.
*/
public function __construct() {
$this->data = magazine_blocks_parse_args( get_option( '_magazine_blocks_settings', array() ), $this->defaults );
}
/**
* Retrieve all data.
*
* @return array
*/
public function get_data() {
return $this->data;
}
/**
* Set setting data.
*
* @param string $key Key to set.
* @param mixed $value Value to set.
* @return void
*/
public function set( $key, $value ) {
$value = $this->sanitize( $key, $value );
magazine_blocks_array_set( $this->data, $key, $value );
}
/**
* Sanitize value.
*
* @param string $key Key to sanitize.
* @param mixed $value Value to sanitize.
* @return mixed Sanitized value.
*/
public function sanitize( $key, $value ) {
$sanitize_callback = magazine_blocks_array_get( $this->sanitize_callbacks, $key );
if ( is_callable( $sanitize_callback ) || ( is_string( $sanitize_callback ) && function_exists( $sanitize_callback ) ) ) {
return call_user_func_array( $sanitize_callback, array( $value ) );
}
return $value;
}
/**
* Get setting data.
*
* @param string $key Key to retrieve data.
* @param mixed $default_value Default value.
* @return mixed
*/
public function get( $key = '', $default_value = null ) {
if ( empty( $key ) ) {
return $this->data;
}
return magazine_blocks_array_get( $this->data, $key, $default_value );
}
/**
* Reset setting data to default.
*
* @return void
*/
public function reset() {
$this->data = $this->defaults;
$this->save();
}
/**
* Save setting data.
*
* @return void
*/
public function save() {
$this->watch_responsive_breakpoints();
update_option( '_magazine_blocks_settings', $this->data );
}
/**
* Set data.
*
* @param array $data Array to set.
* @return void
*/
public function set_data( $data ) {
$data = magazine_blocks_array_dot( $data );
foreach ( $data as $key => $value ) {
$this->set( $key, $value );
}
}
/**
* Watch responsive breakpoints.
*
* @return void
*/
protected function watch_responsive_breakpoints() {
$new_breakpoints = $this->get( 'editor.responsive-breakpoints' );
$old_breakpoints = wp_parse_args(
magazine_blocks_array_get( get_option( '_magazine_blocks_settings', array() ), 'editor.responsive-breakpoints', array() ),
$this->defaults['editor']['responsive-breakpoints']
);
ksort( $new_breakpoints );
ksort( $old_breakpoints );
if ( $new_breakpoints !== $old_breakpoints ) {
do_action( 'magazine_blocks_responsive_breakpoints_changed', $new_breakpoints, $old_breakpoints );
}
}
}