Keywords.php
7.04 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
237
238
239
240
241
242
243
<?php
namespace AIOSEO\Plugin\Common\Meta;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the keywords.
*
* @since 4.0.0
*/
class Keywords {
/**
* Get the keywords for the meta output.
*
* @since 4.0.0
*
* @return string The keywords as a string.
*/
public function getKeywords() {
if ( ! aioseo()->options->searchAppearance->advanced->useKeywords ) {
return '';
}
$isStaticArchive = aioseo()->helpers->isWooCommerceShopPage() || aioseo()->helpers->isStaticPostsPage();
$dynamicContent = is_archive() || is_post_type_archive() || is_home() || aioseo()->helpers->isWooCommerceShopPage() || is_category() || is_tag() || is_tax();
$generate = aioseo()->options->searchAppearance->advanced->dynamicallyGenerateKeywords;
if ( $dynamicContent && $generate ) {
return $this->prepareKeywords( $this->getGeneratedKeywords() );
}
if ( is_front_page() && ! aioseo()->helpers->isStaticHomePage() ) {
$keywords = $this->extractMetaKeywords( aioseo()->options->searchAppearance->global->keywords );
return $this->prepareKeywords( $keywords );
}
if ( $dynamicContent && ! $isStaticArchive ) {
if ( is_date() ) {
$keywords = $this->extractMetaKeywords( aioseo()->options->searchAppearance->archives->date->advanced->keywords );
return $this->prepareKeywords( $keywords );
}
if ( is_author() ) {
$keywords = $this->extractMetaKeywords( aioseo()->options->searchAppearance->archives->author->advanced->keywords );
return $this->prepareKeywords( $keywords );
}
if ( is_search() ) {
$keywords = $this->extractMetaKeywords( aioseo()->options->searchAppearance->archives->search->advanced->keywords );
return $this->prepareKeywords( $keywords );
}
$postType = get_queried_object();
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( $postType && $dynamicOptions->searchAppearance->archives->has( $postType->name ) ) {
$keywords = $this->extractMetaKeywords( aioseo()->dynamicOptions->searchAppearance->archives->{ $postType->name }->advanced->keywords );
return $this->prepareKeywords( $keywords );
}
return '';
}
return $this->prepareKeywords( $this->getAllKeywords() );
}
/**
* Get generated keywords for an archive page.
*
* @since 4.0.0
*
* @return array An array of generated keywords.
*/
private function getGeneratedKeywords() {
global $posts, $wp_query;
$keywords = [];
$isStaticArchive = aioseo()->helpers->isWooCommerceShopPage() || aioseo()->helpers->isStaticPostsPage();
if ( $isStaticArchive ) {
$keywords = $this->getAllKeywords();
} elseif ( is_front_page() && ! aioseo()->helpers->isStaticHomePage() ) {
$keywords = $this->extractMetaKeywords( aioseo()->options->searchAppearance->global->keywords );
} elseif ( is_category() || is_tag() || is_tax() ) {
$metaData = aioseo()->meta->metaData->getMetaData();
if ( ! empty( $metaData->keywords ) ) {
$keywords = $this->extractMetaKeywords( $metaData->keywords );
}
}
$wpPosts = $posts;
if ( empty( $posts ) ) {
$wpPosts = array_filter( [ aioseo()->helpers->getPost() ] );
}
// Turn off current query so we can get specific post data.
$originalTag = $wp_query->is_tag;
$originalTax = $wp_query->is_tax;
$originalCategory = $wp_query->is_category;
$wp_query->is_tag = false;
$wp_query->is_tax = false;
$wp_query->is_category = false;
foreach ( $wpPosts as $post ) {
$metaData = aioseo()->meta->metaData->getMetaData( $post );
$tmpKeywords = $this->extractMetaKeywords( $metaData->keywords );
if ( count( $tmpKeywords ) ) {
foreach ( $tmpKeywords as $keyword ) {
$keywords[] = $keyword;
}
}
}
$wp_query->is_tag = $originalTag;
$wp_query->is_tax = $originalTax;
$wp_query->is_category = $originalCategory;
return $keywords;
}
/**
* Returns the keywords.
*
* @since 4.0.0
*
* @return string A comma-separated list of unique keywords.
*/
public function getAllKeywords() {
$keywords = [];
$post = aioseo()->helpers->getPost();
$metaData = aioseo()->meta->metaData->getMetaData();
if ( ! empty( $metaData->keywords ) ) {
$keywords = $this->extractMetaKeywords( $metaData->keywords );
}
if ( $post ) {
if ( aioseo()->options->searchAppearance->advanced->useTagsForMetaKeywords ) {
$keywords = array_merge( $keywords, aioseo()->helpers->getAllTags( $post->ID ) );
}
if ( aioseo()->options->searchAppearance->advanced->useCategoriesForMetaKeywords && ! is_page() ) {
$keywords = array_merge( $keywords, aioseo()->helpers->getAllCategories( $post->ID ) );
}
}
return $keywords;
}
/**
* Prepares the keywords for display.
*
* @since 4.0.0
*
* @param array $keywords Raw keywords.
* @return string A list of prepared keywords, comma-separated.
*/
protected function prepareKeywords( $keywords ) {
$keywords = $this->getUniqueKeywords( $keywords );
$keywords = trim( $keywords );
$keywords = aioseo()->helpers->internationalize( $keywords );
$keywords = stripslashes( $keywords );
$keywords = str_replace( '"', '', $keywords );
$keywords = wp_filter_nohtml_kses( $keywords );
return apply_filters( 'aioseo_keywords', $keywords );
}
/**
* Returns an array of keywords, based on a stringified list separated by commas.
*
* @since 4.0.0
*
* @param string $keywords The keywords string.
* @return array The keywords.
*/
public function keywordStringToList( $keywords ) {
$keywords = str_replace( '"', '', $keywords );
return ! empty( $keywords ) ? explode( ',', $keywords ) : [];
}
/**
* Returns a stringified list of unique keywords, separated by commas.
*
* @since 4.0.0
*
* @param array $keywords The keywords.
* @param boolean $toString Whether or not to turn it into a comma separated string.
* @return string The keywords string.
*/
public function getUniqueKeywords( $keywords, $toString = true ) {
$keywords = $this->keywordsToLowerCase( $keywords );
return $toString ? implode( ',', $keywords ) : $keywords;
}
/**
* Returns the keywords in lowercase.
*
* @since 4.0.0
*
* @param array $keywords The keywords.
* @return array The formatted keywords.
*/
private function keywordsToLowerCase( $keywords ) {
$smallKeywords = [];
if ( ! is_array( $keywords ) ) {
$keywords = $this->keywordStringToList( $keywords );
}
if ( ! empty( $keywords ) ) {
foreach ( $keywords as $keyword ) {
$smallKeywords[] = trim( aioseo()->helpers->toLowercase( $keyword ) );
}
}
return array_unique( $smallKeywords );
}
/**
* Extract keywords and then return as a string.
*
* @param string $keywords A json encoded string of keywords.
* @return string A string of keywords that were extracted.
*/
public function extractMetaKeywords( $keywords ) {
$extracted = [];
$keywords = ! empty( $keywords ) ? json_decode( $keywords ) : null;
if ( ! empty( $keywords ) ) {
foreach ( $keywords as $keyword ) {
$extracted[] = trim( $keyword->value );
}
}
return $extracted;
}
}