InternalOptions.php
4.16 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace AIOSEO\Plugin\Common\Options;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Traits;
/**
* Class that holds all internal options for AIOSEO.
*
* @since 4.0.0
*/
class InternalOptions {
use Traits\Options;
/**
* Holds a list of all the possible deprecated options.
*
* @since 4.0.0
*
* @var array
*/
protected $allDeprecatedOptions = [
'enableSchemaMarkup',
'excludePosts',
'excludeTerms',
'autogenerateDescriptions',
'descriptionFormat',
'badBotBlocker',
'runShortcodesInDescription',
'staticSitemap',
'staticVideoSitemap',
'useContentForAutogeneratedDescriptions',
'googleAnalytics'
];
/**
* All the default options.
*
* @since 4.0.0
*
* @var array
*/
protected $defaults = [
// phpcs:disable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
'internal' => [
'validLicenseKey' => [ 'type' => 'string' ],
'lastActiveVersion' => [ 'type' => 'string', 'default' => '0.0' ],
'migratedVersion' => [ 'type' => 'string' ],
'siteAnalysis' => [
'connectToken' => [ 'type' => 'string' ],
'score' => [ 'type' => 'number', 'default' => 0 ],
'results' => [ 'type' => 'string' ],
'competitors' => [ 'type' => 'array', 'default' => [], 'preserveHtml' => true ]
],
'headlineAnalysis' => [
'headlines' => [ 'type' => 'array', 'default' => [] ]
],
'wizard' => [ 'type' => 'string' ],
'category' => [ 'type' => 'string' ],
'categoryOther' => [ 'type' => 'string' ],
'deprecatedOptions' => [ 'type' => 'array', 'default' => [] ]
],
'integrations' => [
'semrush' => [
'accessToken' => [ 'type' => 'string' ],
'tokenType' => [ 'type' => 'string' ],
'expires' => [ 'type' => 'string' ],
'refreshToken' => [ 'type' => 'string' ]
]
],
'database' => [
'installedTables' => [ 'type' => 'string' ]
]
// phpcs:enable WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound
];
/**
* The Construct method.
*
* @since 4.0.0
*
* @param string $optionsName The options name.
*/
public function __construct( $optionsName = 'aioseo_options_internal' ) {
$this->optionsName = is_network_admin() ? $optionsName . '_network' : $optionsName;
$this->init();
add_action( 'shutdown', [ $this, 'save' ] );
}
/**
* Initializes the options.
*
* @since 4.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 )
);
aioseo()->optionsCache->setOptions( $this->optionsName, apply_filters( 'aioseo_get_options_internal', $options ) );
// Get the localized options.
$dbOptionsLocalized = get_option( $this->optionsName . '_localized' );
if ( empty( $dbOptionsLocalized ) ) {
$dbOptionsLocalized = [];
}
$this->localized = $dbOptionsLocalized;
}
/**
* Get all the deprecated options.
*
* @since 4.0.0
*
* @return void
*/
public function getAllDeprecatedOptions() {
return $this->allDeprecatedOptions;
}
/**
* Sanitizes, then saves the options to the database.
*
* @since 4.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 = aioseo()->optionsCache->getOptions( $this->optionsName );
$dbOptions = array_replace_recursive(
$cachedOptions,
$this->addValueToValuesArray( $cachedOptions, $options, [], true )
);
$dbOptions['internal']['siteAnalysis']['competitors']['value'] = $this->sanitizeField( $options['internal']['siteAnalysis']['competitors'], 'array', true );
aioseo()->optionsCache->setOptions( $this->optionsName, $dbOptions );
// Update localized options.
update_option( $this->optionsName . '_localized', $this->localized );
// Update values.
$this->save( true );
}
}