PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.2.15
Nested Pages v3.2.15
3.2.15 3.2.14 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Entities / Post / PostUpdateRepository.php
wp-nested-pages / app / Entities / Post Last commit date
PostCloner.php 3 years ago PostDataFactory.php 3 years ago PostFactory.php 3 days ago PostRepository.php 5 years ago PostSaveActions.php 3 years ago PostTrashActions.php 8 years ago PostUpdateRepository.php 3 days ago PrivatePostParent.php 3 years ago
PostUpdateRepository.php
553 lines
1 <?php
2 namespace NestedPages\Entities\Post;
3
4 use NestedPages\Form\Validation\Validation;
5 use NestedPages\Entities\NavMenu\NavMenuRepository;
6 use NestedPages\Entities\PostType\PostTypeRepository;
7 use NestedPages\Entities\User\UserRepository;
8
9 /**
10 * Post Create/Update Methods
11 */
12 class PostUpdateRepository
13 {
14 /**
15 * Validation Class
16 * @var object NP_Validation
17 */
18 protected $validation;
19
20 /**
21 * Nav Menu Repository
22 */
23 protected $nav_menu_repo;
24
25 /**
26 * Post Type Repository
27 */
28 protected $post_type_repo;
29
30 /**
31 * User Repository
32 */
33 protected $user_repo;
34
35 /**
36 * New Post ID
37 * @var int
38 */
39 protected $new_id;
40
41 public function __construct()
42 {
43 $this->validation = new Validation;
44 $this->nav_menu_repo = new NavMenuRepository;
45 $this->post_type_repo = new PostTypeRepository;
46 $this->user_repo = new UserRepository;
47 }
48
49 /**
50 * Update Order
51 * @param array posts
52 * @param int parent
53 * @since 1.0
54 */
55 public function updateOrder($posts, $parent = 0, $filtered = false)
56 {
57 $this->validation->validatePostIDs($posts);
58 $post_type = get_post_type($posts[0]['id']);
59 $update_post_hook = apply_filters('nestedpages_use_update_post', false);
60 if ( !$this->user_repo->canSortPosts($post_type) ) return;
61 global $wpdb;
62 foreach( $posts as $key => $post )
63 {
64 $post_id = sanitize_text_field($post['id']);
65 $original_modifed_date = get_post_modified_time('Y-m-d H:i:s', false, $post_id);
66 $original_modifed_date_gmt = get_post_modified_time('Y-m-d H:i:s', true, $post_id);
67
68 if ( !$filtered ) $args['post_parent'] = intval($parent);
69
70 // Update post hook causes server timeout on large sites, but may be required by some users
71 if ( $update_post_hook ) wp_update_post(['ID' => $post_id]);
72
73 if ( !$filtered ) :
74 $query = $wpdb->prepare(
75 "UPDATE $wpdb->posts
76 SET menu_order = '%d', post_parent = '%d', post_modified = '%s', post_modified_gmt = '%s'
77 WHERE ID = '%d'",
78 intval($key),
79 intval($parent),
80 $original_modifed_date,
81 $original_modifed_date_gmt,
82 intval($post_id)
83 );
84 else : // The posts are filtered, don't update the parent
85 $query = $wpdb->prepare(
86 "UPDATE $wpdb->posts
87 SET menu_order = '%d', post_modified = '%s', post_modified_gmt = '%s'
88 WHERE ID = '%d'",
89 intval($key),
90 $original_modifed_date,
91 $original_modifed_date_gmt,
92 intval($post_id)
93 );
94 endif;
95
96 $wpdb->query( $query );
97 do_action('nestedpages_post_order_updated', $post_id, $parent, $key, $filtered);
98
99 wp_cache_delete( $post_id, 'posts' );
100
101 if ( isset($post['children']) ) $this->updateOrder($post['children'], $post_id);
102 }
103 do_action('nestedpages_posts_order_updated', $posts, $parent);
104 return true;
105 }
106
107 /**
108 * Update Post
109 * @param array data
110 * @param boolean append taxonomies - whether to append or overwrite taxonomies
111 * @since 1.0
112 */
113 public function updatePost($data, $append_taxonomies = false)
114 {
115 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
116 $updated_post = [
117 'ID' => sanitize_text_field($data['post_id'])
118 ];
119
120 $this->validation->validateCustomFields($data);
121
122 if ( isset($data['post_title']) && $data['post_title'] == "" ){
123 $this->validation->checkEmpty($data['post_title'], __('Title', 'wp-nested-pages'));
124 } elseif ( isset($data['post_title']) ){
125 $updated_post['post_title'] = esc_attr($data['post_title']);
126 }
127
128 if ( isset($data['post_name']) )
129 $updated_post['post_name'] = sanitize_text_field($data['post_name']);
130
131 if ( isset($data['post_author']) )
132 $updated_post['post_author'] = sanitize_text_field($data['post_author']);
133
134 if ( isset($data['post_password']) )
135 $updated_post['post_password'] = sanitize_text_field($data['post_password']);
136
137 if ( !$this->post_type_repo->standardFieldDisabled('allow_comments', sanitize_text_field($data['post_type'])) ){
138 $updated_post['comment_status'] = ( isset($data['comment_status']) ) ? 'open' : 'closed';
139 }
140
141 if ( isset($data['post_parent']) && $data['post_parent'] != '-1' ){
142 $updated_post['post_parent'] = intval(sanitize_text_field($data['post_parent']));
143 }
144
145 if ( isset($data['np_date']) ) {
146 $date = $this->validation->validateDate($data);
147 $updated_post['post_date'] = $date;
148 }
149
150 if ( isset($data['mm']) && isset($data['jj']) && isset($data['aa']) ){
151 $date = $this->validation->validateDate($data);
152 $updated_post['post_date'] = $date;
153 }
154
155 if ( isset($_POST['keep_private']) && $_POST['keep_private'] == 'private' ){
156 $updated_post['post_status'] = 'private';
157 } else {
158 if ( isset($data['_status']) ) $updated_post['post_status'] = sanitize_text_field($data['_status']);
159 }
160
161 wp_update_post($updated_post);
162
163 $this->updateSticky($data);
164 $this->updateTemplate($data);
165 $this->updateNestedPagesStatus($data);
166 $this->updateCustomFields($data);
167
168 // Taxonomies
169 $this->updateCategories($data, $append_taxonomies);
170 $this->updateTaxonomies($data, $append_taxonomies);
171
172 // Menu Options
173 $this->updateNavStatus($data);
174 $this->updateNavTitle($data);
175 $this->updateLinkTarget($data);
176 $this->updateTitleAttribute($data);
177 $this->updateNavCSS($data);
178 $this->updateNavCustomUrl($data);
179
180 return true;
181 }
182
183 /**
184 * Update Page Template
185 * @param array data
186 * @since 1.0
187 */
188 public function updateTemplate($data)
189 {
190 if ( isset($data['page_template']) && current_user_can('edit_post', $data['post_id']) ){
191 $template = sanitize_text_field($data['page_template']);
192 update_post_meta(
193 $data['post_id'],
194 '_wp_page_template',
195 $template
196 );
197 }
198 }
199
200 /**
201 * Update Nav Status (show/hide in nav menu)
202 * @since 1.0
203 * @param array data
204 */
205 public function updateNavStatus($data)
206 {
207 if ( !current_user_can('edit_post', $data['post_id']) ) return;
208 $status = ( isset($data['nav_status']) && $data['nav_status'] == 'hide' ) ? 'hide' : 'show';
209 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
210 update_post_meta(
211 $id,
212 '_np_nav_status',
213 $status
214 );
215 }
216
217 /**
218 * Update Nested Pages Visibility (how/hide in Nested Pages interface)
219 * @since 1.0
220 * @param array data
221 */
222 private function updateNestedPagesStatus($data)
223 {
224 if ( !current_user_can('edit_post', $data['post_id']) ) return;
225 if ( $this->post_type_repo->standardFieldDisabled('hide_in_np', sanitize_text_field($data['post_type'])) ) return;
226
227 $status = ( isset($data['nested_pages_status']) && $data['nested_pages_status'] == 'hide' ) ? 'hide' : 'show';
228 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
229 update_post_meta(
230 $id,
231 '_nested_pages_status',
232 $status
233 );
234 }
235
236 /**
237 * Update Nested Pages Menu Navigation Label
238 * @since 1.0
239 * @param array data
240 */
241 private function updateNavTitle($data)
242 {
243 if ( !current_user_can('edit_post', $data['post_id']) ) return;
244 if ( isset($data['np_nav_title']) ){
245 $title = sanitize_text_field($data['np_nav_title']);
246 update_post_meta(
247 $data['post_id'],
248 '_np_nav_title',
249 $title
250 );
251 }
252 }
253
254 /**
255 * Update Nested Pages Menu Navigation CSS Classes
256 * @since 1.0
257 * @param array data
258 */
259 private function updateNavCSS($data)
260 {
261 if ( !current_user_can('edit_post', $data['post_id']) ) return;
262 if ( isset($data['np_nav_css_classes']) ){
263 $css_classes = sanitize_text_field($data['np_nav_css_classes']);
264 update_post_meta(
265 $data['post_id'],
266 '_np_nav_css_classes',
267 $css_classes
268 );
269 }
270 }
271
272 /**
273 * Update Nested Pages Menu Custom URL
274 * @since 3.2.0
275 * @param array data
276 */
277 private function updateNavCustomUrl($data)
278 {
279 if ( !current_user_can('edit_post', $data['post_id']) ) return;
280 if ( isset($data['np_nav_custom_url']) ){
281 $url_input = $data['np_nav_custom_url'];
282 $url = ( $url_input == '#' ) ? '#' : esc_url($data['np_nav_custom_url']);
283 update_post_meta(
284 $data['post_id'],
285 '_np_nav_custom_url',
286 $url
287 );
288 }
289 }
290
291 /**
292 * Update Nested Pages Menu Title Attribute
293 * @since 1.0
294 * @param array data
295 */
296 private function updateTitleAttribute($data)
297 {
298 if ( !current_user_can('edit_post', $data['post_id']) ) return;
299 if ( isset($data['np_title_attribute']) ){
300 $title_attr = sanitize_text_field($data['np_title_attribute']);
301 update_post_meta(
302 $data['post_id'],
303 '_np_title_attribute',
304 $title_attr
305 );
306 }
307 }
308
309 /**
310 * Update Custom Fields, Available through filters
311 * @param array data
312 */
313 private function updateCustomFields($data)
314 {
315 foreach ( $data as $key => $value ){
316 if ( strpos($key, 'np_custom_') !== false) {
317 $field_key = str_replace('np_custom_', '', $key);
318 $matches = preg_match('/nptype_(.*)_nptype/', $field_key, $output_array);
319 $field_type = ( $output_array && isset($output_array[1]) ) ? $output_array[1] : null;
320 $value = sanitize_text_field($data[$key]);
321 $field_key = preg_replace('/nptype_(.*)_nptype_/', '', $field_key);
322 if ( $field_type == 'url' ) $value = esc_url($value);
323 if ( !current_user_can('edit_post', $data['post_id']) ) continue;
324 update_post_meta(
325 $data['post_id'],
326 $field_key,
327 $value
328 );
329 }
330 }
331 }
332
333 /**
334 * Update Categories
335 * @since 1.0
336 * @param array data
337 */
338 private function updateCategories($data, $append_taxonomies = false)
339 {
340 if ( !current_user_can('edit_post', $data['post_id']) ) return;
341 if ( isset($data['post_category']) )
342 {
343 $this->validation->validateIntegerArray($data['post_category']);
344 $cats = [];
345 foreach($data['post_category'] as $cat) {
346 if ( $cat !== 0 ) $cats[] = (int) $cat;
347 }
348 wp_set_post_terms($data['post_id'], $cats, 'category', $append_taxonomies);
349 }
350 }
351
352 /**
353 * Update Hierarchical Taxonomy Terms
354 * @since 1.0
355 * @param array data
356 */
357 private function updateTaxonomies($data, $append_taxonomies)
358 {
359 if ( !current_user_can('edit_post', $data['post_id']) ) return;
360 if ( isset($data['tax_input']) ) {
361 foreach ( $data['tax_input'] as $taxonomy => $term_ids ){
362 $tax = get_taxonomy($taxonomy);
363 if ( $tax->hierarchical ){
364 $this->validation->validateIntegerArray($term_ids);
365 $this->updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies);
366 } else {
367 $this->updateFlatTaxonomy($data, $taxonomy, $term_ids, $append_taxonomies);
368 }
369 }
370 }
371 }
372
373 /**
374 * Update Hierarchical Taxonomy Terms
375 * @since 1.1.4
376 * @param array data
377 */
378 private function updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies)
379 {
380 if ( !current_user_can('edit_post', $data['post_id']) ) return;
381 $terms = [];
382 foreach ( $term_ids as $term ){
383 if ( $term !== 0 ) $terms[] = (int) $term;
384 }
385 wp_set_post_terms($data['post_id'], $terms, $taxonomy, $append_taxonomies);
386 }
387
388 /**
389 * Update Flat Taxonomy Terms
390 * @since 1.1.4
391 * @param array data
392 */
393 private function updateFlatTaxonomy($data, $taxonomy, $terms, $append_taxonomies)
394 {
395 if ( !current_user_can('edit_post', $data['post_id']) ) return;
396 $terms = explode(',', sanitize_text_field($terms));
397 $new_terms = array();
398 foreach($terms as $term)
399 {
400 if ( $term !== "" ) array_push($new_terms, $term);
401 }
402 wp_set_post_terms($data['post_id'], $new_terms, $taxonomy, $append_taxonomies);
403 }
404
405 /**
406 * Update Link Target for Redirects
407 * @since 1.1
408 * @param array data
409 */
410 private function updateLinkTarget($data)
411 {
412 if ( !current_user_can('edit_post', $data['post_id']) ) return;
413 $link_target = ( isset($data['link_target']) && $data['link_target'] == "_blank" ) ? "_blank" : "";
414 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
415 update_post_meta(
416 $id,
417 '_np_link_target',
418 $link_target
419 );
420 }
421
422 /*
423 * Update Sticky Posts
424 * @since 2.0.1
425 * @param array data
426 */
427 private function updateSticky($data)
428 {
429 if ( !current_user_can('manage_options') ) return;
430 if ( $this->post_type_repo->standardFieldDisabled('sticky', sanitize_text_field($data['post_type'])) ) return;
431 $sticky_posts = get_option('sticky_posts');
432 if ( isset($data['sticky']) && $data['sticky'] ){
433 $sticky_posts[] = $data['post_id'];
434 update_option('sticky_posts', $sticky_posts);
435 return;
436 }
437 foreach($sticky_posts as $key => $post){
438 if ( $post == intval($data['post_id']) ) unset($sticky_posts[$key]);
439 }
440 update_option('sticky_posts', $sticky_posts);
441 }
442
443 /**
444 * Update Menu Related Meta
445 * @since 1.4.1
446 * @param array $data
447 */
448 private function updateMenuMeta($data)
449 {
450 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
451 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
452 $link_target = ( isset($data['linkTarget']) ) ? "_blank" : "";
453 update_post_meta($id, '_np_link_target', $link_target);
454 update_post_meta($id, '_np_nav_menu_item_type', sanitize_text_field($data['menuType']));
455 update_post_meta($id, '_np_nav_menu_item_object', sanitize_text_field($data['objectType']));
456 update_post_meta($id, '_np_nav_menu_item_object_id', sanitize_text_field($data['objectId']));
457 if ( isset($data['cssClasses']) ){
458 update_post_meta($id, '_np_nav_css_classes', sanitize_text_field($data['cssClasses']));
459 }
460 if ( isset($data['titleAttribute']) ){
461 $title_attr = sanitize_text_field($data['titleAttribute']);
462 update_post_meta($id, '_np_title_attribute', $title_attr);
463 }
464 if ( isset($data['navigationLabel']) ){
465 $title = sanitize_text_field($data['navigationLabel']);
466 update_post_meta($id, '_np_nav_title', $title);
467 }
468 $this->updateNavStatus($data);
469 }
470
471 /**
472 * Update a Redirect
473 * @since 1.1
474 * @param array data
475 */
476 public function updateRedirect($data)
477 {
478 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
479 $menu_order = isset($data['menu_order']) ? $data['menu_order'] : 0;
480 $updated_post = [
481 'ID' => sanitize_text_field($data['post_id']),
482 'post_title' => sanitize_text_field($data['post_title']),
483 'post_status' => sanitize_text_field($data['_status']),
484 'post_parent' => sanitize_text_field($data['parent_id']),
485 'menu_order' => $menu_order
486 ];
487
488 if ( isset($data['post_content']) && $data['post_content'] !== "" ){
489 $updated_post['post_content'] = esc_url($data['post_content']);
490 }
491
492 $this->new_id = wp_update_post($updated_post);
493 $this->updateMenuMeta($data);
494 return $this->new_id;
495 }
496
497 /**
498 * Save a new Redirect
499 * @since 1.1
500 * @param array data
501 */
502 public function saveRedirect($data)
503 {
504 if ( !$this->user_repo->canSortPosts('page') ) return;
505 $new_link = [
506 'post_title' => sanitize_text_field($data['menuTitle']),
507 'post_status' => sanitize_text_field('publish'),
508 'post_parent' => sanitize_text_field($data['parent_id']),
509 'post_type' => 'np-redirect',
510 'post_excerpt' => ''
511 ];
512 if ( isset($data['url']) && $data['url'] !== "" ){
513 $new_link['post_content'] = esc_url($data['url']);
514 }
515 $this->new_id = wp_insert_post($new_link);
516 $data['post_id'] = $this->new_id;
517 $parent_post_type = ( isset($data['parent_post_type']) ) ? sanitize_text_field($data['parent_post_type']) : 'page';
518 add_post_meta($this->new_id, '_np_parent_post_type', $parent_post_type);
519 $this->updateMenuMeta($data);
520 return $this->new_id;
521 }
522
523 /**
524 * Update a Post to Match Nav menu
525 * @since 1.3.4
526 * @param array of post data
527 */
528 public function updateFromMenuItem($data)
529 {
530 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
531 $updated_post = [
532 'ID' => sanitize_text_field($data['post_id']),
533 'menu_order' => sanitize_text_field($data['menu_order']),
534 'post_parent' => sanitize_text_field($data['post_parent'])
535 ];
536 if ( isset($data['content']) ){
537 $updated_post['post_content'] = $data['content'];
538 $updated_post['post_title'] = $data['np_nav_title'];
539 }
540 wp_update_post($updated_post);
541
542 // Menu Options
543 $this->updateLinkTarget($data);
544 $this->updateNavTitle($data);
545 $this->updateTitleAttribute($data);
546
547 if ( $data['np_nav_css_classes'][0] !== "" ){
548 $data['np_nav_css_classes'] = implode(' ', $data['np_nav_css_classes']);
549 $this->updateNavCSS($data);
550 }
551 }
552 }
553