This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel.php
298 lines (259 loc) · 8.03 KB
/
model.php
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
<?php
/**
* Module Name: Editor
* Namespace: Editor
*
* @package pattern-manager
*/
declare(strict_types=1);
namespace PatternManager\Editor;
use WP_Post;
use function PatternManager\PatternDataHandlers\get_pattern_by_name;
use function PatternManager\PatternDataHandlers\get_theme_patterns;
use function PatternManager\PatternDataHandlers\delete_pattern;
use function PatternManager\PatternDataHandlers\update_pattern;
/**
* Gets the pattern content and title from the PHP file.
*
* @param WP_Post $post The post to possibly add content to.
*/
function populate_pattern_from_file( $post ) {
if ( get_pattern_post_type() !== $post->post_type ) {
return;
}
if ( ! $post->post_name ) {
return;
}
$pattern = get_pattern_by_name( $post->post_name );
if ( ! $pattern ) {
return;
}
$post->post_content = $pattern['content'];
$post->post_title = $pattern['title'];
}
add_action( 'the_post', __NAMESPACE__ . '\populate_pattern_from_file' );
/**
* Saves the pattern to the .php file.
*
* @param int $post_id The post ID.
* @param WP_Post $post The post.
*/
function save_pattern_to_file( WP_Post $post ) {
if ( get_pattern_post_type() !== $post->post_type ) {
return;
}
$pattern = get_pattern_by_name( $post->post_name );
update_pattern(
array_merge(
$pattern ? $pattern : [],
[
'content' => $post->post_content,
'name' => $post->post_name,
],
$post->post_title
? [ 'title' => $post->post_title ]
: []
)
);
// Prevent an infinite loop.
remove_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' );
// Removes the post content, as it should be saved in the pattern .php file.
wp_update_post(
[
'ID' => $post->ID,
'post_content' => '',
]
);
add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' );
}
add_action( 'rest_after_insert_' . get_pattern_post_type(), __NAMESPACE__ . '\save_pattern_to_file' );
/**
* Saves a meta value to the pattern file, instead of the DB.
*
* @param null|bool $override Whether to override Core's saving of metadata.
* @param int $post_id The post ID.
* @param string $meta_key The meta key to update.
* @param mixed $meta_value The meta value to update.
* @return null|bool Whether to override Core's saving of metadata to the DB.
*/
function save_metadata_to_pattern_file( $override, $post_id, $meta_key, $meta_value ) {
$post = get_post( $post_id );
if ( get_pattern_post_type() !== $post->post_type ) {
return $override;
}
// Only update the pattern if a registered meta key is being updated here (no need for core keys like _edit_lock).
$registered_meta_keys = array_keys( get_registered_meta_keys( 'post', get_pattern_post_type() ) );
if ( ! in_array( $meta_key, $registered_meta_keys, true ) ) {
return $override;
}
$pattern_name = $post->post_name;
$pattern = get_pattern_by_name( $pattern_name );
if ( ! $pattern ) {
return $override;
}
if ( 'name' === $meta_key ) {
wp_update_post(
[
'ID' => $post_id,
'post_name' => $meta_value,
]
);
if ( $pattern_name !== $meta_value ) {
delete_pattern( $pattern_name );
}
}
return update_pattern(
array_merge(
$pattern,
[
$meta_key => $meta_value,
]
)
);
}
add_filter( 'update_post_metadata', __NAMESPACE__ . '\save_metadata_to_pattern_file', 10, 4 );
/**
* Gets the metadata from the pattern file, not the DB.
*
* @param null|mixed $override The filtered metadata, or null to get meta from the DB.
* @param int $post_id The post ID the meta is for.
* @param string $meta_key The meta key to get.
* @param bool $is_single Whether the meta is single.
* @return null|mixed The filtered meta value, or null.
*/
function get_metadata_from_pattern_file( $override, $post_id, $meta_key, $is_single ) {
$post = get_post( $post_id );
if ( ! $post ) {
return $override;
}
if ( get_pattern_post_type() !== $post->post_type ) {
return $override;
}
$pattern = get_pattern_by_name( $post->post_name );
if ( ! $pattern ) {
return $override;
}
if ( isset( $pattern[ $meta_key ] ) ) {
return $is_single ? $pattern[ $meta_key ] : [ $pattern[ $meta_key ] ];
}
return $override;
}
add_filter( 'get_post_metadata', __NAMESPACE__ . '\get_metadata_from_pattern_file', 10, 4 );
/**
* Redirects for pattern actions.
*/
function redirect_pattern_actions() {
if ( get_pattern_post_type() !== filter_input( INPUT_GET, 'post_type' ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( 'edit-pattern' === filter_input( INPUT_GET, 'action' ) ) {
$new_post = wp_insert_post(
[
'post_type' => get_pattern_post_type(),
'post_name' => sanitize_text_field( filter_input( INPUT_GET, 'name' ) ),
'post_status' => 'publish',
]
);
wp_safe_redirect(
get_edit_post_link( $new_post, 'direct_link' )
);
}
if ( 'duplicate' === filter_input( INPUT_GET, 'action' ) ) {
$pattern_to_duplicate = get_pattern_by_name( sanitize_text_field( filter_input( INPUT_GET, 'name' ) ) );
$duplicate_pattern_ids = get_duplicate_pattern_ids( $pattern_to_duplicate['name'], get_theme_patterns() );
if ( ! $duplicate_pattern_ids ) {
return;
}
$new_pattern = array_merge(
$pattern_to_duplicate,
$duplicate_pattern_ids
);
update_pattern( $new_pattern );
$new_post = wp_insert_post(
[
'post_type' => get_pattern_post_type(),
'post_name' => $new_pattern['name'],
'post_status' => 'publish',
]
);
wp_safe_redirect(
get_edit_post_link( $new_post, 'direct_link' )
);
}
}
add_action( 'admin_init', __NAMESPACE__ . '\redirect_pattern_actions' );
/**
* Adds the active theme to the heartbeat response.
*
* @param array $response The Heartbeat response.
* @param array $data The $_POST data sent.
* @param string $screen_id The screen ID.
* @return array The Heartbeat response.
*/
function add_active_theme_to_heartbeat( $response, $data, $screen_id ) {
return get_pattern_post_type() === $screen_id
? array_merge(
$response,
[ 'activeTheme' => basename( get_stylesheet_directory() ) ]
)
: $response;
}
add_filter( 'heartbeat_received', __NAMESPACE__ . '\add_active_theme_to_heartbeat', 10, 3 );
/**
* Deletes all pm_pattern posts.
*/
function delete_pattern_posts() {
$post_ids = get_pm_post_ids();
while ( ! empty( $post_ids ) ) {
foreach ( $post_ids as $post_id ) {
wp_delete_post( $post_id, true );
}
$post_ids = get_pm_post_ids();
}
}
add_action( 'after_switch_theme', __NAMESPACE__ . '\delete_pattern_posts' );
/**
* Gets the title for a new pattern post.
*
* For posts of type 'post', it's fine if the title is empty.
* But the title of a pattern is important,
* as the file name is a slug of that title.
* And it must be unique.
*
* @param string $post_title The post title.
* @param WP_Post $post The post.
* @return string
*/
function get_default_title( $post_title, $post ) {
return isset( $post->post_type ) && get_pattern_post_type() === $post->post_type
? get_new_pattern_title( get_theme_patterns() )
: $post_title;
}
add_filter( 'default_title', __NAMESPACE__ . '\get_default_title', 10, 2 );
/**
* Prevents WP from giving a longer ->post_name to a pattern.
*
* On renaming a pattern, sometimes WP adds a number
* to the ->post_name in order to be unique.
* It can change 'example-pattern' to 'example-pattern-1'.
* The ->post_name is where we store the pattern name.
* So it needs to be in sync with the ->post_title.
* It can't be 'example-pattern-1' if the title is 'Example Pattern'.
*
* @param string $slug The post slug.
* @param int $post_ID Post ID.
* @param string $post_status The post status.
* @param string $post_type Post type.
* @param int $post_parent Post parent ID
* @param string $original_slug The original post slug.
* @return string The slug.
*/
function short_circuit_unique_slug( $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ) {
return get_pattern_post_type() === $post_type
? $original_slug
: $slug;
}
add_filter( 'wp_unique_post_slug', __NAMESPACE__ . '\short_circuit_unique_slug', 10, 6 );