✨feature: Initial functional push
This commit is contained in:
146
includes/class-settings.php
Normal file
146
includes/class-settings.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace SiteSync;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles Site Sync plugin settings and options.
|
||||
*/
|
||||
class Settings {
|
||||
public const OPTION_KEY = 'site_sync_settings';
|
||||
private const DEFAULT_META_KEYS = array( '_thumbnail_id' );
|
||||
|
||||
/**
|
||||
* Default settings for the Site Sync plugin.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $defaults = array(
|
||||
'site_uuid' => '',
|
||||
'shared_key' => '',
|
||||
'peer_site_key' => '',
|
||||
'peer_url' => '',
|
||||
'enabled' => false,
|
||||
'sync_interval' => 'site_sync_5min',
|
||||
'post_meta_keys' => self::DEFAULT_META_KEYS,
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieves the merged plugin settings, applying defaults and normalizing meta keys.
|
||||
*
|
||||
* @return array The merged and normalized settings array.
|
||||
*/
|
||||
public function get_settings(): array {
|
||||
$stored = get_option( self::OPTION_KEY, array() );
|
||||
$settings = is_array( $stored ) ? $stored : array();
|
||||
|
||||
$merged = array_merge( $this->defaults, $settings );
|
||||
$merged['post_meta_keys'] = $this->normalize_meta_keys( $merged['post_meta_keys'] );
|
||||
|
||||
return $merged;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a specific setting value by key.
|
||||
*
|
||||
* @param string $key The setting key to retrieve.
|
||||
* @param mixed $default The default value to return if the key does not exist.
|
||||
* @return mixed The value of the setting or the default value.
|
||||
*/
|
||||
public function get( string $key, $default = null ) { // phpcs:ignore
|
||||
$settings = $this->get_settings();
|
||||
|
||||
return $settings[ $key ] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the plugin settings with provided data, sanitizing and normalizing values.
|
||||
*
|
||||
* @param array $data The settings data to update.
|
||||
* @return void
|
||||
*/
|
||||
public function update( array $data ): void {
|
||||
$settings = $this->get_settings();
|
||||
|
||||
$settings['peer_url'] = isset( $data['peer_url'] ) ? esc_url_raw( $data['peer_url'] ) : '';
|
||||
$settings['peer_site_key'] = isset( $data['peer_site_key'] ) ? sanitize_text_field( $data['peer_site_key'] ) : '';
|
||||
$settings['shared_key'] = isset( $data['shared_key'] ) ? sanitize_text_field( $data['shared_key'] ) : $settings['shared_key'];
|
||||
$settings['site_uuid'] = isset( $data['site_uuid'] ) ? sanitize_text_field( $data['site_uuid'] ) : $settings['site_uuid'];
|
||||
$settings['enabled'] = ! empty( $data['enabled'] );
|
||||
$metaKeys = $data['post_meta_keys'] ?? $settings['post_meta_keys'];
|
||||
$settings['post_meta_keys'] = $this->normalize_meta_keys( $metaKeys );
|
||||
|
||||
$interval = isset( $data['sync_interval'] ) ? sanitize_text_field( $data['sync_interval'] ) : $settings['sync_interval'];
|
||||
$settings['sync_interval'] = $interval ? $interval : 'site_sync_5min';
|
||||
|
||||
update_option( self::OPTION_KEY, $settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that default values for 'site_uuid' and 'shared_key' are set if missing.
|
||||
*
|
||||
* @return array The updated settings array.
|
||||
*/
|
||||
public function ensure_defaults(): array {
|
||||
$settings = $this->get_settings();
|
||||
$changed = false;
|
||||
|
||||
if ( empty( $settings['site_uuid'] ) ) {
|
||||
$settings['site_uuid'] = wp_generate_uuid4();
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ( empty( $settings['shared_key'] ) ) {
|
||||
$settings['shared_key'] = wp_generate_password( 32, false, false );
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ( $changed ) {
|
||||
update_option( self::OPTION_KEY, $settings );
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the post meta keys to be synchronized.
|
||||
*
|
||||
* @return array The array of post meta keys.
|
||||
*/
|
||||
public function get_post_meta_keys(): array {
|
||||
return $this->get_settings()['post_meta_keys'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the post meta keys input into a sanitized array of unique keys.
|
||||
*
|
||||
* @param mixed $value The input value, either a string or array of meta keys.
|
||||
* @return array The sanitized and unique array of meta keys.
|
||||
*/
|
||||
private function normalize_meta_keys( $value ): array {
|
||||
if ( is_string( $value ) ) {
|
||||
$parts = preg_split( '/[\s,]+/', $value );
|
||||
} elseif ( is_array( $value ) ) {
|
||||
$parts = $value;
|
||||
} else {
|
||||
$parts = array();
|
||||
}
|
||||
|
||||
$keys = array();
|
||||
foreach ( $parts as $part ) {
|
||||
$sanitized = ltrim( sanitize_text_field( (string) $part ) );
|
||||
if ( $sanitized !== '' ) {
|
||||
$keys[] = $sanitized;
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty( $keys ) ) {
|
||||
$keys = self::DEFAULT_META_KEYS;
|
||||
}
|
||||
|
||||
return array_values( array_unique( $keys ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user