Options.php
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace AIOSEO\Plugin\Lite\Traits;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Options trait.
*
* @since 4.0.0
*/
trait Options {
/**
* Initialize the options.
*
* @since 4.1.4
*
* @return void
*/
public function init() {
parent::init();
$dbOptions = $this->getDbOptions( $this->optionsName . '_lite' );
// Refactor options.
$this->defaultsMerged = array_replace_recursive( $this->defaults, $this->liteDefaults );
$mergedDefaults = array_replace_recursive(
$this->liteDefaults,
$this->addValueToValuesArray( $this->liteDefaults, $dbOptions )
);
$cachedOptions = aioseo()->core->optionsCache->getOptions( $this->optionsName );
$dbOptions = array_replace_recursive(
$cachedOptions,
$mergedDefaults
);
aioseo()->core->optionsCache->setOptions( $this->optionsName, $dbOptions );
}
/**
* Merge defaults with liteDefaults.
*
* @since 4.1.4
*
* @return array An array of dafults.
*/
public function getDefaults() {
return array_replace_recursive( parent::getDefaults(), $this->liteDefaults );
}
/**
* Updates the options in the database.
*
* @since 4.1.4
*
* @param string $optionsName An optional option name to update.
* @param string $defaults The defaults to filter the options by.
* @param array|null $options An optional options array.
* @return void
*/
public function update( $optionsName = null, $defaults = null, $options = null ) {
$optionsName = empty( $optionsName ) ? $this->optionsName . '_lite' : $optionsName;
$defaults = empty( $defaults ) ? $this->liteDefaults : $defaults;
// We're creating a new array here because it was setting it by reference.
$cachedOptions = aioseo()->core->optionsCache->getOptions( $this->optionsName );
$optionsBefore = json_decode( wp_json_encode( $cachedOptions ), true );
parent::update( $this->optionsName, $options );
parent::update( $optionsName, $defaults, $optionsBefore );
}
/**
* Updates the options in the database.
*
* @since 4.1.4
*
* @param boolean $force Whether or not to force an immediate save.
* @param string $optionsName An optional option name to update.
* @param string $defaults The defaults to filter the options by.
* @return void
*/
public function save( $force = false, $optionsName = null, $defaults = null ) {
if ( ! $this->shouldSave && ! $force ) {
return;
}
$optionsName = empty( $optionsName ) ? $this->optionsName . '_lite' : $optionsName;
$defaults = empty( $defaults ) ? $this->liteDefaults : $defaults;
parent::save( $force, $this->optionsName );
parent::save( $force, $optionsName, $defaults );
}
}