LimitModifiedDate.php
6.02 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
<?php
namespace AIOSEO\Plugin\Common\Standalone;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
use AIOSEO\Plugin\Common\Models;
/**
* Limit Modified Date class.
*
* @since 4.1.8
*/
class LimitModifiedDate {
/**
* Class constructor.
*
* @since 4.1.8
*
* @return void
*/
public function __construct() {
if ( apply_filters( 'aioseo_last_modified_date_disable', false ) ) {
return;
}
// Reset modified date when the post is updated.
add_filter( 'wp_insert_post_data', [ $this, 'resetModifiedDate' ], 99999, 2 );
add_filter( 'wp_insert_attachment_data', [ $this, 'resetModifiedDate' ], 99999, 2 );
add_action( 'rest_api_init', [ $this, 'registerRestHooks' ] );
if ( ! is_admin() ) {
return;
}
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ], 20 );
add_action( 'post_submitbox_misc_actions', [ $this, 'classicEditorField' ] );
}
/**
* Register the REST API hooks.
*
* @since 4.1.8
*
* @return void
*/
public function registerRestHooks() {
// Prevent REST API from dropping limit modified date value before updating the post.
foreach ( aioseo()->helpers->getPublicPostTypes( true ) as $postType ) {
add_filter( "rest_pre_insert_$postType", [ $this, 'addLimitModifiedDateValue' ], 10, 2 );
}
}
/**
* Enqueues the scripts for the Limited Modified Date functionality.
*
* @since 4.1.8
*
* @return void
*/
public function enqueueScripts() {
if ( ! $this->isAllowed() || ! aioseo()->helpers->isScreenBase( 'post' ) ) {
return;
}
// Only enqueue this script if the post-settings-metabox is already enqueued.
if ( wp_script_is( 'aioseo/js/src/vue/standalone/post-settings/main.js', 'enqueued' ) ) {
aioseo()->core->assets->load( 'src/vue/standalone/limit-modified-date/main.js' );
}
}
/**
* Adds the Limit Modified Date field to the post object to prevent it from being dropped.
*
* @since 4.1.8
*
* @param Object $preparedPost The post data.
* @param WP_REST_Request $restRequest The request.
* @return Object The modified post data.
*/
public function addLimitModifiedDateValue( $preparedPost, $restRequest ) {
if ( 'PUT' !== $restRequest->get_method() ) {
return $preparedPost;
}
$params = $restRequest->get_json_params();
if ( empty( $params ) || ! isset( $params['aioseo_limit_modified_date'] ) ) {
return $preparedPost;
}
$preparedPost->aioseo_limit_modified_date = $params['aioseo_limit_modified_date'];
return $preparedPost;
}
/**
* Resets the modified date when a post is updated if the Limit Modified Date option is enabled.
*
* @since 4.1.8
*
* @param array $sanitizedData The sanitized post data.
* @param array $unsanitizedData The unsanitized post data.
* @return array The modified sanitized post data.
*/
public function resetModifiedDate( $sanitizedData, $unsanitizedData ) {
// If the ID isn't set, a new post is being inserted.
if ( ! isset( $unsanitizedData['ID'] ) ) {
return $sanitizedData;
}
$shouldReset = false;
// Handle the REST API request from the Block Editor.
if ( aioseo()->helpers->isRestApiRequest() ) {
// If the value isn't set, then the value wasn't changed in the editor, and we can grab it from the post.
if ( ! isset( $unsanitizedData['aioseo_limit_modified_date'] ) ) {
$aioseoPost = Models\Post::getPost( $unsanitizedData['ID'] );
if ( $aioseoPost->exists() && $aioseoPost->limit_modified_date ) {
$shouldReset = true;
}
} else {
if ( $unsanitizedData['aioseo_limit_modified_date'] ) {
$shouldReset = true;
}
}
}
// Handle the POST request.
if ( isset( $unsanitizedData['aioseo-post-settings'] ) ) {
$aioseoData = json_decode( stripslashes( $unsanitizedData['aioseo-post-settings'] ) );
if ( ! empty( $aioseoData->limit_modified_date ) ) {
$shouldReset = true;
}
}
// Handle post revision.
if ( ! empty( $GLOBALS['action'] ) && 'restore' === $GLOBALS['action'] ) {
$aioseoPost = Models\Post::getPost( $unsanitizedData['ID'] );
if ( $aioseoPost->exists() && $aioseoPost->limit_modified_date ) {
$shouldReset = true;
}
}
if ( $shouldReset ) {
$sanitizedData['post_modified'] = $unsanitizedData['post_modified'];
$sanitizedData['post_modified_gmt'] = $unsanitizedData['post_modified_gmt'];
}
return $sanitizedData;
}
/**
* Add the checkbox in the Classic Editor.
*
* @since 4.1.8
*
* @param WP_Post $post The post object.
* @return void
*/
public function classicEditorField( $post ) {
if ( ! $this->isAllowed( $post->post_type ) ) {
return;
}
?>
<div class="misc-pub-section">
<div id="aioseo-limit-modified-date"></div>
</div>
<?php
}
/**
* Check if the Limit Modified Date functionality is allowed to run.
*
* @since 4.1.8
*
* @param string $postType The current post type.
* @return bool Whether the functionality is allowed.
*/
private function isAllowed( $postType = false ) {
if ( empty( $postType ) ) {
$postType = get_post_type();
}
if ( class_exists( 'Limit_Modified_Date', false ) ) {
return false;
}
if ( ! $this->isAllowedPostType( $postType ) ) {
return false;
}
if ( ! aioseo()->access->hasCapability( 'aioseo_page_general_settings' ) ) {
return false;
}
return true;
}
/**
* Check if the given post type is allowed to limit the modified date.
*
* @since 4.1.8
*
* @param string $postType The post type name.
* @return bool Whether the post type is allowed.
*/
private function isAllowedPostType( $postType ) {
$dynamicOptions = aioseo()->dynamicOptions->noConflict();
$postTypes = aioseo()->helpers->getPublicPostTypes( true );
$postTypes = apply_filters( 'aioseo_limit_modified_date_post_types', $postTypes );
if ( ! in_array( $postType, $postTypes, true ) ) {
return false;
}
if ( ! $dynamicOptions->searchAppearance->postTypes->has( $postType ) || ! $dynamicOptions->searchAppearance->postTypes->$postType->advanced->showMetaBox ) {
return false;
}
return true;
}
}