PostsTerms.php
11.3 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace AIOSEO\Plugin\Common\Api;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Route class for the API.
*
* @since 4.0.0
*/
class PostsTerms {
/**
* Searches for posts or terms by ID/name.
*
* @since 4.0.0
*
* @param \WP_REST_Request $request The REST Request
* @return \WP_REST_Response The response.
*/
public static function searchForObjects( $request ) {
$body = $request->get_json_params();
if ( empty( $body['query'] ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'No search term was provided.'
], 400 );
}
if ( empty( $body['type'] ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'No type was provided.'
], 400 );
}
$searchQuery = esc_sql( aioseo()->db->db->esc_like( $body['query'] ) );
$objects = [];
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
if ( 'posts' === $body['type'] ) {
$postTypes = aioseo()->helpers->getPublicPostTypes( true );
foreach ( $postTypes as $postType ) {
// Check if post type isn't noindexed.
if ( $dynamicOptions->searchAppearance->postTypes->has( $postType ) && ! $dynamicOptions->searchAppearance->postTypes->$postType->show ) {
$postTypes = aioseo()->helpers->unsetValue( $postTypes, $postType );
}
}
$objects = aioseo()->db
->start( 'posts' )
->select( 'ID, post_type, post_title, post_name' )
->whereRaw( "( post_title LIKE '%{$searchQuery}%' OR post_name LIKE '%{$searchQuery}%' OR ID = '{$searchQuery}' )" )
->whereIn( 'post_type', $postTypes )
->whereIn( 'post_status', [ 'publish', 'draft', 'future', 'pending' ] )
->orderBy( 'post_title' )
->limit( 10 )
->run()
->result();
} elseif ( 'terms' === $body['type'] ) {
$taxonomies = aioseo()->helpers->getPublicTaxonomies( true );
foreach ( $taxonomies as $taxonomy ) {
// Check if taxonomy isn't noindexed.
if ( $dynamicOptions->searchAppearance->taxonomies->has( $taxonomy ) && ! $dynamicOptions->searchAppearance->taxonomies->$taxonomy->show ) {
$taxonomies = aioseo()->helpers->unsetValue( $taxonomies, $taxonomy );
}
}
$objects = aioseo()->db
->start( 'terms as t' )
->select( 't.term_id as term_id, t.slug as slug, t.name as name, tt.taxonomy as taxonomy' )
->join( 'term_taxonomy as tt', 't.term_id = tt.term_id', 'INNER' )
->whereRaw( "( t.name LIKE '%{$searchQuery}%' OR t.slug LIKE '%{$searchQuery}%' OR t.term_id = '{$searchQuery}' )" )
->whereIn( 'tt.taxonomy', $taxonomies )
->orderBy( 't.name' )
->limit( 10 )
->run()
->result();
}
if ( empty( $objects ) ) {
return new \WP_REST_Response( [
'success' => true,
'objects' => []
], 200 );
}
$parsed = [];
foreach ( $objects as $object ) {
if ( 'posts' === $body['type'] ) {
$parsed[] = [
'type' => $object->post_type,
'value' => (int) $object->ID,
'slug' => $object->post_name,
'label' => $object->post_title,
'link' => get_permalink( $object->ID )
];
} elseif ( 'terms' === $body['type'] ) {
$parsed[] = [
'type' => $object->taxonomy,
'value' => (int) $object->term_id,
'slug' => $object->slug,
'label' => $object->name,
'link' => get_term_link( $object->term_id )
];
}
}
return new \WP_REST_Response( [
'success' => true,
'objects' => $parsed
], 200 );
}
/**
* Get post data for fetch requests
*
* @since 4.0.0
*
* @param \WP_REST_Request $request The REST Request
* @return \WP_REST_Response The response.
*/
public static function getPostData( $request ) {
$args = $request->get_query_params();
if ( empty( $args['postId'] ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => __( 'No post ID was provided.', 'all-in-one-seo-pack' )
], 400 );
}
$thePost = Models\Post::getPost( $args['postId'] );
return new \WP_REST_Response( [
'success' => true,
'post' => $thePost,
'postData' => [
'parsedTitle' => aioseo()->tags->replaceTags( $thePost->title, $args['postId'] ),
'parsedDescription' => aioseo()->tags->replaceTags( $thePost->description, $args['postId'] ),
'content' => aioseo()->helpers->theContent( self::getAnalysisContent( $args['postId'] ) ),
'slug' => get_post_field( 'post_name', $args['postId'] )
]
], 200 );
}
/**
* Returns the posts custom fields.
*
* @since 4.0.6
*
* @param WP_Post|int $post The post.
* @return string The custom field content.
*/
private static function getAnalysisContent( $post = null ) {
$post = ( $post && is_object( $post ) ) ? $post : aioseo()->helpers->getPost( $post );
$customFieldKeys = aioseo()->dynamicOptions->searchAppearance->postTypes->{$post->post_type}->customFields;
if ( empty( $customFieldKeys ) ) {
return get_post_field( 'post_content', $post->ID );
}
$customFieldKeys = explode( ' ', sanitize_text_field( $customFieldKeys ) );
$customFieldContent = aioseo()->helpers->getCustomFieldsContent( $post->ID, $customFieldKeys );
$analysisContent = $post->post_content . apply_filters( 'aioseo_analysis_content', $customFieldContent );
return sanitize_post_field( 'post_content', $analysisContent, $post->ID, 'display' );
}
/**
* Update post settings.
*
* @since 4.0.0
*
* @param \WP_REST_Request $request The REST Request
* @return \WP_REST_Response The response.
*/
public static function updatePosts( $request ) {
$body = $request->get_json_params();
$postId = ! empty( $body['id'] ) ? intval( $body['id'] ) : null;
if ( ! $postId ) {
return new \WP_REST_Response( [
'success' => false,
'message' => __( 'Post ID is missing.', 'all-in-one-seo-pack' )
], 400 );
}
$body['id'] = $postId;
$body['title'] = ! empty( $body['title'] ) ? sanitize_text_field( $body['title'] ) : null;
$body['description'] = ! empty( $body['description'] ) ? sanitize_text_field( $body['description'] ) : null;
$body['keywords'] = ! empty( $body['keywords'] ) ? sanitize_text_field( $body['keywords'] ) : null;
$body['og_title'] = ! empty( $body['og_title'] ) ? sanitize_text_field( $body['og_title'] ) : null;
$body['og_description'] = ! empty( $body['og_description'] ) ? sanitize_text_field( $body['og_description'] ) : null;
$body['og_article_section'] = ! empty( $body['og_article_section'] ) ? sanitize_text_field( $body['og_article_section'] ) : null;
$body['og_article_tags'] = ! empty( $body['og_article_tags'] ) ? sanitize_text_field( $body['og_article_tags'] ) : null;
$body['twitter_title'] = ! empty( $body['twitter_title'] ) ? sanitize_text_field( $body['twitter_title'] ) : null;
$body['twitter_description'] = ! empty( $body['twitter_description'] ) ? sanitize_text_field( $body['twitter_description'] ) : null;
// @TODO: Refactor this as it's not the best way to look for errors.
$error = Models\Post::savePost( $postId, $body );
if ( ! empty( $error ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'Failed update query: ' . $error
], 401 );
}
return new \WP_REST_Response( [
'success' => true,
'posts' => $postId
], 200 );
}
/**
* Update post settings from Post screen.
*
* @since 4.0.0
*
* @param \WP_REST_Request $request The REST Request
* @return \WP_REST_Response The response.
*/
public static function updatePostFromScreen( $request ) {
$body = $request->get_json_params();
$postId = ! empty( $body['postId'] ) ? intval( $body['postId'] ) : null;
$isMedia = isset( $body['isMedia'] ) ? true : false;
$post = aioseo()->helpers->getPost( $postId );
if ( ! $postId ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'Post ID is missing.'
], 400 );
}
$thePost = Models\Post::getPost( $postId );
if ( $thePost->exists() ) {
$metaTitle = aioseo()->meta->title->getPostTypeTitle( $post->post_type );
if ( empty( $thePost->title ) && ! empty( $body['title'] ) && trim( $body['title'] ) === trim( $metaTitle ) ) {
$body['title'] = null;
}
$thePost->title = ! empty( $body['title'] ) ? sanitize_text_field( $body['title'] ) : null;
$metaDescription = aioseo()->meta->description->getPostTypeDescription( $post->post_type );
if ( empty( $thePost->description ) && ! empty( $body['description'] ) && trim( $body['description'] ) === trim( $metaDescription ) ) {
$body['description'] = null;
}
$thePost->description = ! empty( $body['description'] ) ? sanitize_text_field( $body['description'] ) : '';
$thePost->updated = gmdate( 'Y-m-d H:i:s' );
if ( $isMedia ) {
wp_update_post(
[
'ID' => $postId,
'post_title' => sanitize_text_field( $body['imageTitle'] ),
]
);
update_post_meta( $postId, '_wp_attachment_image_alt', sanitize_text_field( $body['imageAltTag'] ) );
}
} else {
$thePost->post_id = $postId;
$thePost->title = ! empty( $body['title'] ) ? sanitize_text_field( $body['title'] ) : '';
$thePost->description = ! empty( $body['description'] ) ? sanitize_text_field( $body['description'] ) : null;
$thePost->created = gmdate( 'Y-m-d H:i:s' );
$thePost->updated = gmdate( 'Y-m-d H:i:s' );
if ( $isMedia ) {
wp_update_post(
[
'ID' => $postId,
'post_title' => sanitize_text_field( $body['imageTitle'] ),
]
);
update_post_meta( $postId, '_wp_attachment_image_alt', sanitize_text_field( $body['imageAltTag'] ) );
}
}
$thePost->save();
$lastError = aioseo()->db->lastError();
if ( ! empty( $lastError ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'Failed update query: ' . $lastError
], 401 );
}
return new \WP_REST_Response( [
'success' => true,
'posts' => $postId,
'title' => aioseo()->meta->title->getPostTitle( $postId ),
'description' => aioseo()->meta->description->getPostDescription( $postId )
], 200 );
}
/**
* Update post keyphrases.
*
* @since 4.0.0
*
* @param \WP_REST_Request $request The REST Request
* @return \WP_REST_Response The response.
*/
public static function updatePostKeyphrases( $request ) {
$body = $request->get_json_params();
$postId = ! empty( $body['postId'] ) ? intval( $body['postId'] ) : null;
if ( ! $postId ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'Post ID is missing.'
], 400 );
}
$thePost = Models\Post::getPost( $postId );
$thePost->post_id = $postId;
if ( ! empty( $body['keyphrases'] ) ) {
$thePost->keyphrases = wp_json_encode( $body['keyphrases'] );
}
if ( ! empty( $body['page_analysis'] ) ) {
$thePost->page_analysis = wp_json_encode( $body['page_analysis'] );
}
if ( ! empty( $body['seo_score'] ) ) {
$thePost->seo_score = sanitize_text_field( $body['seo_score'] );
}
$thePost->updated = gmdate( 'Y-m-d H:i:s' );
$thePost->save();
$lastError = aioseo()->db->lastError();
if ( ! empty( $lastError ) ) {
return new \WP_REST_Response( [
'success' => false,
'message' => 'Failed update query: ' . $lastError
], 401 );
}
return new \WP_REST_Response( [
'success' => true,
'post' => $postId
], 200 );
}
}