Priority.php
7.33 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace AIOSEO\Plugin\Common\Sitemap;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Determines the priority/frequency.
*
* @since 4.0.0
*/
class Priority {
/**
* Whether the advanced settings are enabled for the sitemap.
*
* @since 4.0.0
*
* @var boolean
*/
private static $advanced;
/**
* The global priority for the page type.
*
* @since 4.0.0
*
* @var boolean
*/
private static $globalPriority = [];
/**
* The global frequency for the page type.
*
* @since 4.0.0
*
* @var boolean
*/
private static $globalFrequency = [];
/**
* Whether or not we have grouped our settings.
*
* @since 4.0.0
*
* @var array
*/
private static $grouped = [];
/**
* The current object type priority.
*
* @since 4.0.0
*
* @var array
*/
private static $objectTypePriority = [];
/**
* The current object type frequency.
*
* @since 4.0.0
*
* @var array
*/
private static $objectTypeFrequency = [];
/**
* Returns the sitemap priority for a given page.
*
* @since 4.0.0
*
* @param string $pageType The type of page (e.g. homepage, blog, post, taxonomies, etc.).
* @param stdClass $object The post/term object (optional).
* @param string $objectType The post/term object type (optional).
* @return float $priority The priority.
*/
public function priority( $pageType, $object = false, $objectType = null ) {
// Store setting values in static properties so that we can cache them.
// Otherwise this has a significant impact on the load time of the sitemap.
if ( ! self::$advanced ) {
self::$advanced = aioseo()->options->sitemap->general->advancedSettings->enable;
}
if ( ! isset( self::$globalPriority[ $pageType . $objectType ] ) ) {
$options = aioseo()->options->noConflict();
$pageTypeConditional = 'date' === $pageType ? 'archive' : $pageType;
self::$globalPriority[ $pageType . $objectType ] = self::$advanced && $options->sitemap->general->advancedSettings->priority->has( $pageTypeConditional )
? json_decode( $options->sitemap->general->advancedSettings->priority->$pageTypeConditional->priority )
: false;
}
if ( ! isset( self::$grouped[ $pageType . $objectType ] ) ) {
$options = aioseo()->options->noConflict();
self::$grouped[ $pageType . $objectType ] = self::$advanced &&
$options->sitemap->general->advancedSettings->priority->has( $pageType ) &&
$options->sitemap->general->advancedSettings->priority->$pageType->has( 'grouped' )
? $options->sitemap->general->advancedSettings->priority->$pageType->grouped
: true;
}
if ( empty( self::$grouped[ $pageType . $objectType ] ) && self::$advanced ) {
if ( ! isset( self::$objectTypePriority[ $pageType . $objectType ] ) ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
self::$objectTypePriority[ $pageType . $objectType ] = $dynamicOptions->sitemap->priority->has( $pageType ) && $dynamicOptions->sitemap->priority->$pageType->has( $objectType )
? json_decode( $dynamicOptions->sitemap->priority->$pageType->$objectType->priority )
: false;
}
}
$priority = $this->defaultPriority( $pageType );
if ( self::$globalPriority[ $pageType . $objectType ] ) {
$defaultValue = ! self::$grouped[ $pageType . $objectType ] &&
self::$advanced &&
! empty( self::$objectTypePriority[ $pageType . $objectType ] )
? self::$objectTypePriority[ $pageType . $objectType ]
: self::$globalPriority[ $pageType . $objectType ];
$priority = 'default' === $defaultValue->value ? $priority : $defaultValue->value;
}
return $priority;
}
/**
* Returns the sitemap frequency for a given page.
*
* @since 4.0.0
*
* @param string $pageType The type of page (e.g. homepage, blog, post, taxonomies, etc.).
* @param stdClass $object The post/term object (optional).
* @param string $objectType The post/term object type (optional).
* @return float $frequency The frequency.
*/
public function frequency( $pageType, $object = false, $objectType = null ) {
// Store setting values in static properties so that we can cache them.
// Otherwise this has a significant impact on the load time of the sitemap.
if ( ! self::$advanced ) {
self::$advanced = aioseo()->options->sitemap->general->advancedSettings->enable;
}
if ( ! isset( self::$globalFrequency[ $pageType . $objectType ] ) ) {
$options = aioseo()->options->noConflict();
$pageTypeConditional = 'date' === $pageType ? 'archive' : $pageType;
self::$globalFrequency[ $pageType . $objectType ] = self::$advanced && $options->sitemap->general->advancedSettings->priority->has( $pageTypeConditional )
? json_decode( $options->sitemap->general->advancedSettings->priority->$pageTypeConditional->frequency )
: false;
}
if ( ! isset( self::$grouped[ $pageType . $objectType ] ) ) {
$options = aioseo()->options->noConflict();
self::$grouped[ $pageType . $objectType ] = self::$advanced &&
$options->sitemap->general->advancedSettings->priority->has( $pageType ) &&
$options->sitemap->general->advancedSettings->priority->$pageType->has( 'grouped' )
? $options->sitemap->general->advancedSettings->priority->$pageType->grouped
: true;
}
if ( empty( self::$grouped[ $pageType . $objectType ] ) && self::$advanced ) {
if ( ! isset( self::$objectTypeFrequency[ $pageType . $objectType ] ) ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
self::$objectTypeFrequency[ $pageType . $objectType ] = $dynamicOptions->sitemap->priority->has( $pageType ) && $dynamicOptions->sitemap->priority->$pageType->has( $objectType )
? json_decode( $dynamicOptions->sitemap->priority->$pageType->$objectType->frequency )
: false;
}
}
$frequency = $this->defaultFrequency( $pageType );
if ( self::$globalFrequency[ $pageType . $objectType ] ) {
$defaultValue = ! self::$grouped[ $pageType . $objectType ] &&
self::$advanced &&
! empty( self::$objectTypeFrequency[ $pageType . $objectType ] )
? self::$objectTypeFrequency[ $pageType . $objectType ]
: self::$globalFrequency[ $pageType . $objectType ];
$frequency = 'default' === $defaultValue->value ? $frequency : $defaultValue->value;
}
return $frequency;
}
/**
* Returns the default priority for the page.
*
* @since 4.0.0
*
* @param string $pageType The type of page.
* @return float The default priority.
*/
private function defaultPriority( $pageType ) {
$defaults = [
'homePage' => 1.0,
'blog' => 0.9,
'sitemap' => 0.8,
'postTypes' => 0.7,
'archive' => 0.5,
'author' => 0.3,
'taxonomies' => 0.3,
'other' => 0.5,
];
if ( array_key_exists( $pageType, $defaults ) ) {
return $defaults[ $pageType ];
}
return $defaults['other'];
}
/**
* Returns the default frequency for the page.
*
* @since 4.0.0
*
* @param string $pageType The type of page.
* @return float The default frequency.
*/
private function defaultFrequency( $pageType ) {
$defaults = [
'homePage' => 'always',
'sitemap' => 'hourly',
'blog' => 'daily',
'postTypes' => 'weekly',
'author' => 'weekly',
'archive' => 'monthly',
'taxonomies' => 'monthly',
'other' => 'weekly'
];
if ( array_key_exists( $pageType, $defaults ) ) {
return $defaults[ $pageType ];
}
return $defaults['other'];
}
}