PostSettings.php
10.7 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
namespace AIOSEO\Plugin\Common\Admin;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Abstract class that Pro and Lite both extend.
*
* @since 4.0.0
*/
class PostSettings {
/**
* Initialize the admin.
*
* @since 4.0.0
*
* @return void
*/
public function __construct() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Clear the Post Type Overview cache.
add_action( 'save_post', [ $this, 'clearPostTypeOverviewCache' ], 100 );
add_action( 'delete_post', [ $this, 'clearPostTypeOverviewCache' ], 100 );
add_action( 'wp_trash_post', [ $this, 'clearPostTypeOverviewCache' ], 100 );
if ( wp_doing_ajax() || wp_doing_cron() || ! is_admin() ) {
return;
}
// Load Vue APP.
add_action( 'admin_enqueue_scripts', [ $this, 'enqueuePostSettingsAssets' ] );
// Add metabox.
add_action( 'add_meta_boxes', [ $this, 'addPostSettingsMetabox' ] );
// Add metabox to terms on init hook.
add_action( 'init', [ $this, 'init' ], 1000 );
// Save metabox.
add_action( 'save_post', [ $this, 'saveSettingsMetabox' ] );
add_action( 'edit_attachment', [ $this, 'saveSettingsMetabox' ] );
add_action( 'add_attachment', [ $this, 'saveSettingsMetabox' ] );
// Filter the sql clauses to show posts filtered by our params.
add_filter( 'posts_clauses', [ $this, 'changeClausesToFilterPosts' ], 10, 2 );
}
/**
* Enqueues the JS/CSS for the on page/posts settings.
*
* @since 4.0.0
*
* @return void
*/
public function enqueuePostSettingsAssets() {
if (
aioseo()->helpers->isScreenBase( 'event-espresso' ) ||
aioseo()->helpers->isScreenBase( 'post' ) ||
aioseo()->helpers->isScreenBase( 'term' ) ||
aioseo()->helpers->isScreenBase( 'edit-tags' ) ||
aioseo()->helpers->isScreenBase( 'site-editor' )
) {
$page = null;
if (
aioseo()->helpers->isScreenBase( 'event-espresso' ) ||
aioseo()->helpers->isScreenBase( 'post' )
) {
$page = 'post';
}
aioseo()->core->assets->load( 'src/vue/standalone/post-settings/main.js', [], aioseo()->helpers->getVueData( $page ) );
aioseo()->core->assets->load( 'src/vue/standalone/link-format/main.js', [], aioseo()->helpers->getVueData( $page ) );
aioseo()->admin->enqueueAioseoModalPortal();
}
$screen = get_current_screen();
if ( 'attachment' === $screen->id ) {
wp_enqueue_media();
}
}
/**
* Check whether or not we can add the metabox.
*
* @since 4.1.7
*
* @param string $postType The post type to check.
* @return boolean Whether or not can add the Metabox.
*/
public function canAddPostSettingsMetabox( $postType ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
$pageAnalysisSettingsCapability = aioseo()->access->hasCapability( 'aioseo_page_analysis' );
$generalSettingsCapability = aioseo()->access->hasCapability( 'aioseo_page_general_settings' );
$socialSettingsCapability = aioseo()->access->hasCapability( 'aioseo_page_social_settings' );
$schemaSettingsCapability = aioseo()->access->hasCapability( 'aioseo_page_schema_settings' );
$linkAssistantCapability = aioseo()->access->hasCapability( 'aioseo_page_link_assistant_settings' );
$redirectsCapability = aioseo()->access->hasCapability( 'aioseo_page_redirects_manage' );
$advancedSettingsCapability = aioseo()->access->hasCapability( 'aioseo_page_advanced_settings' );
if (
$dynamicOptions->searchAppearance->postTypes->has( $postType ) &&
$dynamicOptions->searchAppearance->postTypes->$postType->advanced->showMetaBox &&
! (
empty( $pageAnalysisSettingsCapability ) &&
empty( $generalSettingsCapability ) &&
empty( $socialSettingsCapability ) &&
empty( $schemaSettingsCapability ) &&
empty( $linkAssistantCapability ) &&
empty( $redirectsCapability ) &&
empty( $advancedSettingsCapability )
)
) {
return true;
}
return false;
}
/**
* Adds a meta box to page/posts screens.
*
* @since 4.0.0
*
* @return void
*/
public function addPostSettingsMetabox() {
$screen = get_current_screen();
$postType = $screen->post_type;
if ( $this->canAddPostSettingsMetabox( $postType ) ) {
// Translators: 1 - The plugin short name ("AIOSEO").
$aioseoMetaboxTitle = sprintf( esc_html__( '%1$s Settings', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_SHORT_NAME );
add_meta_box(
'aioseo-settings',
$aioseoMetaboxTitle,
[ $this, 'postSettingsMetabox' ],
[ $postType ],
'normal',
apply_filters( 'aioseo_post_metabox_priority', 'high' )
);
}
}
/**
* Render the on page/posts settings metabox with Vue App wrapper.
*
* @since 4.0.0
*
* @param WP_Post $post The current post.
* @return void
*/
public function postSettingsMetabox() {
$this->postSettingsHiddenField();
?>
<div id="aioseo-post-settings-metabox">
<?php aioseo()->templates->getTemplate( 'parts/loader.php' ); ?>
</div>
<?php
}
/**
* Adds the hidden field where all the metabox data goes.
*
* @since 4.0.17
*
* @return void
*/
public function postSettingsHiddenField() {
static $fieldExists = false;
if ( $fieldExists ) {
return;
}
$fieldExists = true;
?>
<div id="aioseo-post-settings-field">
<input type="hidden" name="aioseo-post-settings" id="aioseo-post-settings" value=""/>
<?php wp_nonce_field( 'aioseoPostSettingsNonce', 'PostSettingsNonce' ); ?>
</div>
<?php
}
/**
* Handles metabox saving.
*
* @since 4.0.3
*
* @param int $postId Post ID.
* @return void
*/
public function saveSettingsMetabox( $postId ) {
if ( ! aioseo()->helpers->isValidPost( $postId, [ 'all' ] ) ) {
return;
}
// Security check.
if ( ! isset( $_POST['PostSettingsNonce'] ) || ! wp_verify_nonce( $_POST['PostSettingsNonce'], 'aioseoPostSettingsNonce' ) ) {
return;
}
// If we don't have our post settings input, we can safely skip.
if ( ! isset( $_POST['aioseo-post-settings'] ) ) {
return;
}
// Check user permissions.
if ( ! current_user_can( 'edit_post', $postId ) ) {
return;
}
$currentPost = json_decode( stripslashes( $_POST['aioseo-post-settings'] ), true ); // phpcs:ignore HM.Security.ValidatedSanitizedInput
// If there is no data, there likely was an error, e.g. if the hidden field wasn't populated on load and the user saved the post without making changes in the metabox.
// In that case we should return to prevent a complete reset of the data.
if ( empty( $currentPost ) ) {
return;
}
Models\Post::savePost( $postId, $currentPost );
}
/**
* Clear the Post Type Overview cache from our cache table.
*
* @since 4.2.0
*
* @param int $postId The Post ID being updated/deleted.
* @return void
*/
public function clearPostTypeOverviewCache( $postId ) {
$postType = get_post_type( $postId );
if ( empty( $postType ) ) {
return;
}
aioseo()->core->cache->delete( $postType . '_overview_data' );
}
/**
* Get a list of post types with an overview showing how many posts are good, okay and so on.
*
* @since 4.2.0
*
* @return array The list of post types with the overview.
*/
public function getPostTypesOverview() {
$postTypes = [];
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
foreach ( aioseo()->helpers->getPublicPostTypes( true ) as $postType ) {
if (
! $dynamicOptions->searchAppearance->postTypes->has( $postType ) ||
! $dynamicOptions->searchAppearance->postTypes->$postType->show ||
! $dynamicOptions->searchAppearance->postTypes->$postType->advanced->showMetaBox ||
'attachment' === $postType ||
aioseo()->helpers->isBBPressPostType( $postType )
) {
continue;
}
$postTypes[ $postType ] = $this->getPostTypeOverview( $postType );
}
return $postTypes;
}
/**
* Get how many posts are good, okay, needs improvement or are missing the focus keyphrase for the given post type.
*
* @since 4.2.0
*
* @param string $postType The post type name.
* @return array The overview for the given post type.
*/
public function getPostTypeOverview( $postType ) {
$overview = aioseo()->core->cache->get( $postType . '_overview_data' );
if ( null !== $overview ) {
return $overview;
}
$posts = aioseo()->core->db->start( 'posts as p' )
->select( 'ap.seo_score, ap.keyphrases' )
->leftJoin( 'aioseo_posts as ap', 'ap.post_id = p.ID' )
->where( 'p.post_status', 'publish' )
->where( 'p.post_type', $postType )
->run()
->result();
$overview = [
'total' => count( $posts ),
'needsImprovement' => 0,
'okay' => 0,
'good' => 0,
'withoutFocusKeyphrase' => 0,
];
foreach ( $posts as $post ) {
if ( empty( $post->keyphrases ) || strpos( $post->keyphrases, '{"focus":[]' ) === 0 ) {
$overview['withoutFocusKeyphrase']++;
// We skip to the next since we will just consider posts with focus keyphrase in the counts.
continue;
}
if ( 50 > $post->seo_score ) {
$overview['needsImprovement']++;
continue;
}
if ( 50 <= $post->seo_score && 80 >= $post->seo_score ) {
$overview['okay']++;
continue;
}
if ( 80 < $post->seo_score ) {
$overview['good']++;
}
}
aioseo()->core->cache->update( $postType . '_overview_data', $overview, WEEK_IN_SECONDS );
return $overview;
}
/**
* Change the JOIN and WHERE clause to filter just the posts we need to show depending on the query string.
*
* @since 4.2.0
*
* @param array $clauses Associative array of the clauses for the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
* @return array The clauses array updated.
*/
public function changeClausesToFilterPosts( $clauses, $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return $clauses;
}
$filter = filter_input( INPUT_GET, 'aioseo-filter' );
if ( empty( $filter ) ) {
return $clauses;
}
$whereClause = '';
$noKeyphrasesClause = " (aioseo_p.keyphrases = '' OR aioseo_p.keyphrases IS NULL OR aioseo_p.keyphrases LIKE '{\"focus\":[]%') ";
switch ( $filter ) {
case 'withoutFocusKeyphrase':
$whereClause = " AND $noKeyphrasesClause ";
break;
case 'needsImprovement':
$whereClause = " AND aioseo_p.seo_score < 50 AND NOT $noKeyphrasesClause ";
break;
case 'okay':
$whereClause = " AND aioseo_p.seo_score BETWEEN 50 AND 80 AND NOT $noKeyphrasesClause ";
break;
case 'good':
$whereClause = " AND aioseo_p.seo_score > 80 AND NOT $noKeyphrasesClause ";
break;
}
$prefix = aioseo()->core->db->prefix;
$postsTable = aioseo()->core->db->db->posts;
$clauses['join'] .= " LEFT JOIN {$prefix}aioseo_posts AS aioseo_p ON ({$postsTable}.ID = aioseo_p.post_id) ";
$clauses['where'] .= $whereClause;
return $clauses;
}
}