Commit inicial powerservice

This commit is contained in:
Gabriel
2026-10-19 12:45:45 -06:00
commit ad85486fe2
10158 changed files with 2736804 additions and 0 deletions
@@ -0,0 +1,93 @@
<?php
namespace AIOSEO\TableOfContents\Options;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that holds all the cache for the options.
*
* @since 1.0.0
*/
class Cache {
/**
* The DB options cache.
*
* @since 1.0.0
*
* @var array
*/
private static $db = [];
/**
* The options cache.
*
* @since 1.0.0
*
* @var array
*/
private static $options = [];
/**
* Sets the cache for the DB option.
*
* @since 1.0.0
*
* @param string $name The cache name.
* @param array $value The value.
* @return void
*/
public function setDb( $name, $value ) {
self::$db[ $name ] = $value;
}
/**
* Gets the cache for the DB option.
*
* @since 1.0.0
*
* @param string $name The cache name.
* @return array The data from the cache.
*/
public function getDb( $name ) {
return ! empty( self::$db[ $name ] ) ? self::$db[ $name ] : [];
}
/**
* Sets the cache for the options.
*
* @since 1.0.0
*
* @param string $name The cache name.
* @param array $value The value.
* @return void
*/
public function setOptions( $name, $value ) {
self::$options[ $name ] = $value;
}
/**
* Gets the cache for the options.
*
* @since 1.0.0
*
* @param string $name The cache name.
* @return array The data from the cache.
*/
public function getOptions( $name ) {
return ! empty( self::$options[ $name ] ) ? self::$options[ $name ] : [];
}
/**
* Resets the DB cache.
*
* @since 1.0.0
*
* @return void
*/
public function resetDb() {
self::$db = [];
}
}
@@ -0,0 +1,119 @@
<?php
namespace AIOSEO\TableOfContents\Options;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\TableOfContents\Traits;
/**
* Class that holds all internal options for Table of Contents Plus.
*
* @since 1.0.0
*/
class InternalOptions {
use Traits\Options;
/**
* Holds a list of all the possible deprecated options.
*
* @since 1.0.0
*
* @var array
*/
protected $allDeprecatedOptions = [];
/**
* All the default options.
*
* @since 1.0.0
*
* @var array
*/
protected $defaults = [
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
'internal' => [
'firstActivated' => [ 'type' => 'number', 'default' => 0 ],
'lastActiveVersion' => [ 'type' => 'string', 'default' => '0.0' ],
'lastSchemaVersion' => [ 'type' => 'string', 'default' => '0.0' ],
'migratedAppearanceDefaults' => [ 'type' => 'boolean', 'default' => false ],
]
// phpcs:enable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
];
/**
* Class constructor.
*
* @since 1.0.0
*
* @param string $optionsName The options name.
*/
public function __construct( $optionsName = 'aioseo_table_of_contents_options_internal' ) {
$this->optionsName = is_network_admin() ? $optionsName . '_network' : $optionsName;
$this->init();
add_action( 'shutdown', [ $this, 'save' ] );
}
/**
* Initializes the options.
*
* @since 1.0.0
*
* @return void
*/
protected function init() {
// Options from the DB.
$dbOptions = $this->getDbOptions( $this->optionsName );
// Refactor options.
$this->defaultsMerged = array_replace_recursive( $this->defaults, $this->defaultsMerged );
$options = array_replace_recursive(
$this->defaultsMerged,
$this->addValueToValuesArray( $this->defaultsMerged, $dbOptions )
);
aioseoTableOfContents()->core->optionsCache->setOptions( $this->optionsName, apply_filters( 'aioseo_table_of_contents_get_options_internal', $options ) );
}
/**
* Get all the deprecated options.
*
* @since 1.0.0
*
* @return void
*/
public function getAllDeprecatedOptions() {
return $this->allDeprecatedOptions;
}
/**
* Sanitizes, then saves the options to the database.
*
* @since 1.0.0
*
* @param array $options An array of options to sanitize, then save.
* @return void
*/
public function sanitizeAndSave( $options ) {
if ( ! is_array( $options ) ) {
return;
}
// Refactor options.
$cachedOptions = aioseoTableOfContents()->core->optionsCache->getOptions( $this->optionsName );
$dbOptions = array_replace_recursive(
$cachedOptions,
$this->addValueToValuesArray( $cachedOptions, $options, [], true )
);
aioseoTableOfContents()->core->optionsCache->setOptions( $this->optionsName, $dbOptions );
// Update values.
$this->save( true );
}
}
@@ -0,0 +1,124 @@
<?php
namespace AIOSEO\TableOfContents\Options;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\TableOfContents\Traits;
/**
* Handles the main options.
*
* @since 1.0.0
*/
class Options {
use Traits\Options;
/**
* All the default options.
*
* @since 1.0.0
*
* @var array
*/
protected $defaults = [
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
'general' => [
// Define options here.
]
// phpcs:enable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
];
/**
* The Construct method.
*
* @since 1.0.0
*
* @param string $optionsName An array of options.
*/
public function __construct( $optionsName = 'aioseo_table_of_contents_options' ) {
$this->optionsName = $optionsName;
$this->init();
add_action( 'shutdown', [ $this, 'save' ] );
}
/**
* Initializes the options.
*
* @since 1.0.0
*
* @return void
*/
protected function init() {
$options = $this->getTableOfContentsDbOptions();
aioseoTableOfContents()->core->optionsCache->setOptions( $this->optionsName, apply_filters( 'aioseo_table_of_contents_get_options', $options ) );
}
/**
* Get the DB options.
*
* @since 1.0.0
*
* @return array An array of options.
*/
public function getTableOfContentsDbOptions() {
// Options from the DB.
$dbOptions = $this->getDbOptions( $this->optionsName );
// Refactor options.
$this->defaultsMerged = array_replace_recursive( $this->defaults, $this->defaultsMerged );
return array_replace_recursive(
$this->defaultsMerged,
$this->addValueToValuesArray( $this->defaultsMerged, $dbOptions )
);
}
/**
* Sanitizes, then saves the options to the database.
*
* @since 1.0.0
*
* @param array $newOptions An array of options to sanitize, then save.
* @return void
*/
public function sanitizeAndSave( $newOptions ) {
$this->init();
if ( ! is_array( $newOptions ) ) {
return;
}
// First, recursively replace the new options into the cached state.
// It's important we use the helper method since we want to replace populated arrays with empty ones if needed (when a setting was cleared out).
$cachedOptions = aioseoTableOfContents()->core->optionsCache->getOptions( $this->optionsName );
$dbOptions = aioseoTableOfContents()->helpers->arrayReplaceRecursive(
$cachedOptions,
$this->addValueToValuesArray( $cachedOptions, $newOptions, [], true )
);
// Now, we must also intersect both arrays to delete any individual keys that were unset.
// We must do this because, while arrayReplaceRecursive will update the values for keys or empty them out,
// it will keys that aren't present in the replacement array unaffected in the target array.
$dbOptions = aioseoTableOfContents()->helpers->arrayIntersectRecursive(
$dbOptions,
$this->addValueToValuesArray( $cachedOptions, $newOptions, [], true ),
'value'
);
if ( isset( $newOptions['advanced']['excludeDomains'] ) ) {
$dbOptions['advanced']['excludeDomains'] = preg_replace( '/\h/', "\n", (string) $newOptions['advanced']['excludeDomains'] );
}
// Update the cache state.
aioseoTableOfContents()->core->optionsCache->setOptions( $this->optionsName, $dbOptions );
// Finally, save the new values to the DB.
$this->save( true );
}
}