PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.2.5
Nested Pages v3.2.5
3.3.1 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 4 years ago PostFactory.php 4 years ago PostRepository.php 5 years ago PostSaveActions.php 3 years ago PostTrashActions.php 8 years ago PostUpdateRepository.php 2 years ago PrivatePostParent.php 4 years ago
PostUpdateRepository.php
551 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 do_action('nestedpages_post_order_updated', $post_id, $parent, $key, $filtered);
100 if ( isset($post['children']) ) $this->updateOrder($post['children'], $post_id);
101 }
102 do_action('nestedpages_posts_order_updated', $posts, $parent);
103 return true;
104 }
105
106 /**
107 * Update Post
108 * @param array data
109 * @param boolean append taxonomies - whether to append or overwrite taxonomies
110 * @since 1.0
111 */
112 public function updatePost($data, $append_taxonomies = false)
113 {
114 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
115 $updated_post = [
116 'ID' => sanitize_text_field($data['post_id'])
117 ];
118
119 $this->validation->validateCustomFields($data);
120
121 if ( isset($data['post_title']) && $data['post_title'] == "" ){
122 $this->validation->checkEmpty($data['post_title'], __('Title', 'wp-nested-pages'));
123 } elseif ( isset($data['post_title']) ){
124 $updated_post['post_title'] = sanitize_text_field($data['post_title']);
125 }
126
127 if ( isset($data['post_name']) )
128 $updated_post['post_name'] = sanitize_text_field($data['post_name']);
129
130 if ( isset($data['post_author']) )
131 $updated_post['post_author'] = sanitize_text_field($data['post_author']);
132
133 if ( isset($data['post_password']) )
134 $updated_post['post_password'] = sanitize_text_field($data['post_password']);
135
136 if ( !$this->post_type_repo->standardFieldDisabled('allow_comments', sanitize_text_field($data['post_type'])) ){
137 $updated_post['comment_status'] = ( isset($data['comment_status']) ) ? 'open' : 'closed';
138 }
139
140 if ( isset($data['post_parent']) && $data['post_parent'] != '-1' ){
141 $updated_post['post_parent'] = intval(sanitize_text_field($data['post_parent']));
142 }
143
144 if ( isset($data['np_date']) ) {
145 $date = $this->validation->validateDate($data);
146 $updated_post['post_date'] = $date;
147 }
148
149 if ( isset($data['mm']) && isset($data['jj']) && isset($data['aa']) ){
150 $date = $this->validation->validateDate($data);
151 $updated_post['post_date'] = $date;
152 }
153
154 if ( isset($_POST['keep_private']) && $_POST['keep_private'] == 'private' ){
155 $updated_post['post_status'] = 'private';
156 } else {
157 if ( isset($data['_status']) ) $updated_post['post_status'] = sanitize_text_field($data['_status']);
158 }
159
160 wp_update_post($updated_post);
161
162 $this->updateSticky($data);
163 $this->updateTemplate($data);
164 $this->updateNestedPagesStatus($data);
165 $this->updateCustomFields($data);
166
167 // Taxonomies
168 $this->updateCategories($data, $append_taxonomies);
169 $this->updateTaxonomies($data, $append_taxonomies);
170
171 // Menu Options
172 $this->updateNavStatus($data);
173 $this->updateNavTitle($data);
174 $this->updateLinkTarget($data);
175 $this->updateTitleAttribute($data);
176 $this->updateNavCSS($data);
177 $this->updateNavCustomUrl($data);
178
179 return true;
180 }
181
182 /**
183 * Update Page Template
184 * @param array data
185 * @since 1.0
186 */
187 public function updateTemplate($data)
188 {
189 if ( isset($data['page_template']) && current_user_can('edit_post', $data['post_id']) ){
190 $template = sanitize_text_field($data['page_template']);
191 update_post_meta(
192 $data['post_id'],
193 '_wp_page_template',
194 $template
195 );
196 }
197 }
198
199 /**
200 * Update Nav Status (show/hide in nav menu)
201 * @since 1.0
202 * @param array data
203 */
204 public function updateNavStatus($data)
205 {
206 if ( !current_user_can('edit_post', $data['post_id']) ) return;
207 $status = ( isset($data['nav_status']) && $data['nav_status'] == 'hide' ) ? 'hide' : 'show';
208 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
209 update_post_meta(
210 $id,
211 '_np_nav_status',
212 $status
213 );
214 }
215
216 /**
217 * Update Nested Pages Visibility (how/hide in Nested Pages interface)
218 * @since 1.0
219 * @param array data
220 */
221 private function updateNestedPagesStatus($data)
222 {
223 if ( !current_user_can('edit_post', $data['post_id']) ) return;
224 if ( $this->post_type_repo->standardFieldDisabled('hide_in_np', sanitize_text_field($data['post_type'])) ) return;
225
226 $status = ( isset($data['nested_pages_status']) && $data['nested_pages_status'] == 'hide' ) ? 'hide' : 'show';
227 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
228 update_post_meta(
229 $id,
230 '_nested_pages_status',
231 $status
232 );
233 }
234
235 /**
236 * Update Nested Pages Menu Navigation Label
237 * @since 1.0
238 * @param array data
239 */
240 private function updateNavTitle($data)
241 {
242 if ( !current_user_can('edit_post', $data['post_id']) ) return;
243 if ( isset($data['np_nav_title']) ){
244 $title = sanitize_text_field($data['np_nav_title']);
245 update_post_meta(
246 $data['post_id'],
247 '_np_nav_title',
248 $title
249 );
250 }
251 }
252
253 /**
254 * Update Nested Pages Menu Navigation CSS Classes
255 * @since 1.0
256 * @param array data
257 */
258 private function updateNavCSS($data)
259 {
260 if ( !current_user_can('edit_post', $data['post_id']) ) return;
261 if ( isset($data['np_nav_css_classes']) ){
262 $css_classes = sanitize_text_field($data['np_nav_css_classes']);
263 update_post_meta(
264 $data['post_id'],
265 '_np_nav_css_classes',
266 $css_classes
267 );
268 }
269 }
270
271 /**
272 * Update Nested Pages Menu Custom URL
273 * @since 3.2.0
274 * @param array data
275 */
276 private function updateNavCustomUrl($data)
277 {
278 if ( !current_user_can('edit_post', $data['post_id']) ) return;
279 if ( isset($data['np_nav_custom_url']) ){
280 $url_input = $data['np_nav_custom_url'];
281 $url = ( $url_input == '#' ) ? '#' : esc_url($data['np_nav_custom_url']);
282 update_post_meta(
283 $data['post_id'],
284 '_np_nav_custom_url',
285 $url
286 );
287 }
288 }
289
290 /**
291 * Update Nested Pages Menu Title Attribute
292 * @since 1.0
293 * @param array data
294 */
295 private function updateTitleAttribute($data)
296 {
297 if ( !current_user_can('edit_post', $data['post_id']) ) return;
298 if ( isset($data['np_title_attribute']) ){
299 $title_attr = sanitize_text_field($data['np_title_attribute']);
300 update_post_meta(
301 $data['post_id'],
302 '_np_title_attribute',
303 $title_attr
304 );
305 }
306 }
307
308 /**
309 * Update Custom Fields, Available through filters
310 * @param array data
311 */
312 private function updateCustomFields($data)
313 {
314 foreach ( $data as $key => $value ){
315 if ( strpos($key, 'np_custom_') !== false) {
316 $field_key = str_replace('np_custom_', '', $key);
317 $matches = preg_match('/nptype_(.*)_nptype/', $field_key, $output_array);
318 $field_type = ( $output_array && isset($output_array[1]) ) ? $output_array[1] : null;
319 $value = sanitize_text_field($data[$key]);
320 $field_key = preg_replace('/nptype_(.*)_nptype_/', '', $field_key);
321 if ( $field_type == 'url' ) $value = esc_url($value);
322 if ( !current_user_can('edit_post', $data['post_id']) ) continue;
323 update_post_meta(
324 $data['post_id'],
325 $field_key,
326 $value
327 );
328 }
329 }
330 }
331
332 /**
333 * Update Categories
334 * @since 1.0
335 * @param array data
336 */
337 private function updateCategories($data, $append_taxonomies = false)
338 {
339 if ( !current_user_can('edit_post', $data['post_id']) ) return;
340 if ( isset($data['post_category']) )
341 {
342 $this->validation->validateIntegerArray($data['post_category']);
343 $cats = [];
344 foreach($data['post_category'] as $cat) {
345 if ( $cat !== 0 ) $cats[] = (int) $cat;
346 }
347 wp_set_post_terms($data['post_id'], $cats, 'category', $append_taxonomies);
348 }
349 }
350
351 /**
352 * Update Hierarchical Taxonomy Terms
353 * @since 1.0
354 * @param array data
355 */
356 private function updateTaxonomies($data, $append_taxonomies)
357 {
358 if ( !current_user_can('edit_post', $data['post_id']) ) return;
359 if ( isset($data['tax_input']) ) {
360 foreach ( $data['tax_input'] as $taxonomy => $term_ids ){
361 $tax = get_taxonomy($taxonomy);
362 if ( $tax->hierarchical ){
363 $this->validation->validateIntegerArray($term_ids);
364 $this->updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies);
365 } else {
366 $this->updateFlatTaxonomy($data, $taxonomy, $term_ids, $append_taxonomies);
367 }
368 }
369 }
370 }
371
372 /**
373 * Update Hierarchical Taxonomy Terms
374 * @since 1.1.4
375 * @param array data
376 */
377 private function updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies)
378 {
379 if ( !current_user_can('edit_post', $data['post_id']) ) return;
380 $terms = [];
381 foreach ( $term_ids as $term ){
382 if ( $term !== 0 ) $terms[] = (int) $term;
383 }
384 wp_set_post_terms($data['post_id'], $terms, $taxonomy, $append_taxonomies);
385 }
386
387 /**
388 * Update Flat Taxonomy Terms
389 * @since 1.1.4
390 * @param array data
391 */
392 private function updateFlatTaxonomy($data, $taxonomy, $terms, $append_taxonomies)
393 {
394 if ( !current_user_can('edit_post', $data['post_id']) ) return;
395 $terms = explode(',', sanitize_text_field($terms));
396 $new_terms = array();
397 foreach($terms as $term)
398 {
399 if ( $term !== "" ) array_push($new_terms, $term);
400 }
401 wp_set_post_terms($data['post_id'], $new_terms, $taxonomy, $append_taxonomies);
402 }
403
404 /**
405 * Update Link Target for Redirects
406 * @since 1.1
407 * @param array data
408 */
409 private function updateLinkTarget($data)
410 {
411 if ( !current_user_can('edit_post', $data['post_id']) ) return;
412 $link_target = ( isset($data['link_target']) && $data['link_target'] == "_blank" ) ? "_blank" : "";
413 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
414 update_post_meta(
415 $id,
416 '_np_link_target',
417 $link_target
418 );
419 }
420
421 /*
422 * Update Sticky Posts
423 * @since 2.0.1
424 * @param array data
425 */
426 private function updateSticky($data)
427 {
428 if ( !current_user_can('manage_options') ) return;
429 if ( $this->post_type_repo->standardFieldDisabled('sticky', sanitize_text_field($data['post_type'])) ) return;
430 $sticky_posts = get_option('sticky_posts');
431 if ( isset($data['sticky']) && $data['sticky'] ){
432 $sticky_posts[] = $data['post_id'];
433 update_option('sticky_posts', $sticky_posts);
434 return;
435 }
436 foreach($sticky_posts as $key => $post){
437 if ( $post == intval($data['post_id']) ) unset($sticky_posts[$key]);
438 }
439 update_option('sticky_posts', $sticky_posts);
440 }
441
442 /**
443 * Update Menu Related Meta
444 * @since 1.4.1
445 * @param array $data
446 */
447 private function updateMenuMeta($data)
448 {
449 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
450 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
451 $link_target = ( isset($data['linkTarget']) ) ? "_blank" : "";
452 update_post_meta($id, '_np_link_target', $link_target);
453 update_post_meta($id, '_np_nav_menu_item_type', sanitize_text_field($data['menuType']));
454 update_post_meta($id, '_np_nav_menu_item_object', sanitize_text_field($data['objectType']));
455 update_post_meta($id, '_np_nav_menu_item_object_id', sanitize_text_field($data['objectId']));
456 if ( isset($data['cssClasses']) ){
457 update_post_meta($id, '_np_nav_css_classes', sanitize_text_field($data['cssClasses']));
458 }
459 if ( isset($data['titleAttribute']) ){
460 $title_attr = sanitize_text_field($data['titleAttribute']);
461 update_post_meta($id, '_np_title_attribute', $title_attr);
462 }
463 if ( isset($data['navigationLabel']) ){
464 $title = sanitize_text_field($data['navigationLabel']);
465 update_post_meta($id, '_np_nav_title', $title);
466 }
467 $this->updateNavStatus($data);
468 }
469
470 /**
471 * Update a Redirect
472 * @since 1.1
473 * @param array data
474 */
475 public function updateRedirect($data)
476 {
477 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
478 $menu_order = isset($data['menu_order']) ? $data['menu_order'] : 0;
479 $updated_post = [
480 'ID' => sanitize_text_field($data['post_id']),
481 'post_title' => sanitize_text_field($data['post_title']),
482 'post_status' => sanitize_text_field($data['_status']),
483 'post_parent' => sanitize_text_field($data['parent_id']),
484 'menu_order' => $menu_order
485 ];
486
487 if ( isset($data['post_content']) && $data['post_content'] !== "" ){
488 $updated_post['post_content'] = esc_url($data['post_content']);
489 }
490
491 $this->new_id = wp_update_post($updated_post);
492 $this->updateMenuMeta($data);
493 return $this->new_id;
494 }
495
496 /**
497 * Save a new Redirect
498 * @since 1.1
499 * @param array data
500 */
501 public function saveRedirect($data)
502 {
503 if ( !$this->user_repo->canSortPosts('page') ) return;
504 $new_link = [
505 'post_title' => sanitize_text_field($data['menuTitle']),
506 'post_status' => sanitize_text_field('publish'),
507 'post_parent' => sanitize_text_field($data['parent_id']),
508 'post_type' => 'np-redirect',
509 'post_excerpt' => ''
510 ];
511 if ( isset($data['url']) && $data['url'] !== "" ){
512 $new_link['post_content'] = esc_url($data['url']);
513 }
514 $this->new_id = wp_insert_post($new_link);
515 $data['post_id'] = $this->new_id;
516 $parent_post_type = ( isset($data['parent_post_type']) ) ? sanitize_text_field($data['parent_post_type']) : 'page';
517 add_post_meta($this->new_id, '_np_parent_post_type', $parent_post_type);
518 $this->updateMenuMeta($data);
519 return $this->new_id;
520 }
521
522 /**
523 * Update a Post to Match Nav menu
524 * @since 1.3.4
525 * @param array of post data
526 */
527 public function updateFromMenuItem($data)
528 {
529 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
530 $updated_post = [
531 'ID' => sanitize_text_field($data['post_id']),
532 'menu_order' => sanitize_text_field($data['menu_order']),
533 'post_parent' => sanitize_text_field($data['post_parent'])
534 ];
535 if ( isset($data['content']) ){
536 $updated_post['post_content'] = $data['content'];
537 $updated_post['post_title'] = $data['np_nav_title'];
538 }
539 wp_update_post($updated_post);
540
541 // Menu Options
542 $this->updateLinkTarget($data);
543 $this->updateNavTitle($data);
544 $this->updateTitleAttribute($data);
545
546 if ( $data['np_nav_css_classes'][0] !== "" ){
547 $data['np_nav_css_classes'] = implode(' ', $data['np_nav_css_classes']);
548 $this->updateNavCSS($data);
549 }
550 }
551 }