PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.2.3
Nested Pages v3.2.3
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 3 years ago PrivatePostParent.php 4 years ago
PostUpdateRepository.php
546 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 if ( !current_user_can('edit_post', $data['post_id']) ) continue;
318 update_post_meta(
319 $data['post_id'],
320 $field_key,
321 sanitize_text_field($data[$key])
322 );
323 }
324 }
325 }
326
327 /**
328 * Update Categories
329 * @since 1.0
330 * @param array data
331 */
332 private function updateCategories($data, $append_taxonomies = false)
333 {
334 if ( !current_user_can('edit_post', $data['post_id']) ) return;
335 if ( isset($data['post_category']) )
336 {
337 $this->validation->validateIntegerArray($data['post_category']);
338 $cats = [];
339 foreach($data['post_category'] as $cat) {
340 if ( $cat !== 0 ) $cats[] = (int) $cat;
341 }
342 wp_set_post_terms($data['post_id'], $cats, 'category', $append_taxonomies);
343 }
344 }
345
346 /**
347 * Update Hierarchical Taxonomy Terms
348 * @since 1.0
349 * @param array data
350 */
351 private function updateTaxonomies($data, $append_taxonomies)
352 {
353 if ( !current_user_can('edit_post', $data['post_id']) ) return;
354 if ( isset($data['tax_input']) ) {
355 foreach ( $data['tax_input'] as $taxonomy => $term_ids ){
356 $tax = get_taxonomy($taxonomy);
357 if ( $tax->hierarchical ){
358 $this->validation->validateIntegerArray($term_ids);
359 $this->updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies);
360 } else {
361 $this->updateFlatTaxonomy($data, $taxonomy, $term_ids, $append_taxonomies);
362 }
363 }
364 }
365 }
366
367 /**
368 * Update Hierarchical Taxonomy Terms
369 * @since 1.1.4
370 * @param array data
371 */
372 private function updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies)
373 {
374 if ( !current_user_can('edit_post', $data['post_id']) ) return;
375 $terms = [];
376 foreach ( $term_ids as $term ){
377 if ( $term !== 0 ) $terms[] = (int) $term;
378 }
379 wp_set_post_terms($data['post_id'], $terms, $taxonomy, $append_taxonomies);
380 }
381
382 /**
383 * Update Flat Taxonomy Terms
384 * @since 1.1.4
385 * @param array data
386 */
387 private function updateFlatTaxonomy($data, $taxonomy, $terms, $append_taxonomies)
388 {
389 if ( !current_user_can('edit_post', $data['post_id']) ) return;
390 $terms = explode(',', sanitize_text_field($terms));
391 $new_terms = array();
392 foreach($terms as $term)
393 {
394 if ( $term !== "" ) array_push($new_terms, $term);
395 }
396 wp_set_post_terms($data['post_id'], $new_terms, $taxonomy, $append_taxonomies);
397 }
398
399 /**
400 * Update Link Target for Redirects
401 * @since 1.1
402 * @param array data
403 */
404 private function updateLinkTarget($data)
405 {
406 if ( !current_user_can('edit_post', $data['post_id']) ) return;
407 $link_target = ( isset($data['link_target']) && $data['link_target'] == "_blank" ) ? "_blank" : "";
408 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
409 update_post_meta(
410 $id,
411 '_np_link_target',
412 $link_target
413 );
414 }
415
416 /*
417 * Update Sticky Posts
418 * @since 2.0.1
419 * @param array data
420 */
421 private function updateSticky($data)
422 {
423 if ( !current_user_can('manage_options') ) return;
424 if ( $this->post_type_repo->standardFieldDisabled('sticky', sanitize_text_field($data['post_type'])) ) return;
425 $sticky_posts = get_option('sticky_posts');
426 if ( isset($data['sticky']) && $data['sticky'] ){
427 $sticky_posts[] = $data['post_id'];
428 update_option('sticky_posts', $sticky_posts);
429 return;
430 }
431 foreach($sticky_posts as $key => $post){
432 if ( $post == intval($data['post_id']) ) unset($sticky_posts[$key]);
433 }
434 update_option('sticky_posts', $sticky_posts);
435 }
436
437 /**
438 * Update Menu Related Meta
439 * @since 1.4.1
440 * @param array $data
441 */
442 private function updateMenuMeta($data)
443 {
444 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
445 $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id;
446 $link_target = ( isset($data['linkTarget']) ) ? "_blank" : "";
447 update_post_meta($id, '_np_link_target', $link_target);
448 update_post_meta($id, '_np_nav_menu_item_type', sanitize_text_field($data['menuType']));
449 update_post_meta($id, '_np_nav_menu_item_object', sanitize_text_field($data['objectType']));
450 update_post_meta($id, '_np_nav_menu_item_object_id', sanitize_text_field($data['objectId']));
451 if ( isset($data['cssClasses']) ){
452 update_post_meta($id, '_np_nav_css_classes', sanitize_text_field($data['cssClasses']));
453 }
454 if ( isset($data['titleAttribute']) ){
455 $title_attr = sanitize_text_field($data['titleAttribute']);
456 update_post_meta($id, '_np_title_attribute', $title_attr);
457 }
458 if ( isset($data['navigationLabel']) ){
459 $title = sanitize_text_field($data['navigationLabel']);
460 update_post_meta($id, '_np_nav_title', $title);
461 }
462 $this->updateNavStatus($data);
463 }
464
465 /**
466 * Update a Redirect
467 * @since 1.1
468 * @param array data
469 */
470 public function updateRedirect($data)
471 {
472 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
473 $menu_order = isset($data['menu_order']) ? $data['menu_order'] : 0;
474 $updated_post = [
475 'ID' => sanitize_text_field($data['post_id']),
476 'post_title' => sanitize_text_field($data['post_title']),
477 'post_status' => sanitize_text_field($data['_status']),
478 'post_parent' => sanitize_text_field($data['parent_id']),
479 'menu_order' => $menu_order
480 ];
481
482 if ( isset($data['post_content']) && $data['post_content'] !== "" ){
483 $updated_post['post_content'] = esc_url($data['post_content']);
484 }
485
486 $this->new_id = wp_update_post($updated_post);
487 $this->updateMenuMeta($data);
488 return $this->new_id;
489 }
490
491 /**
492 * Save a new Redirect
493 * @since 1.1
494 * @param array data
495 */
496 public function saveRedirect($data)
497 {
498 if ( !$this->user_repo->canSortPosts('page') ) return;
499 $new_link = [
500 'post_title' => sanitize_text_field($data['menuTitle']),
501 'post_status' => sanitize_text_field('publish'),
502 'post_parent' => sanitize_text_field($data['parent_id']),
503 'post_type' => 'np-redirect',
504 'post_excerpt' => ''
505 ];
506 if ( isset($data['url']) && $data['url'] !== "" ){
507 $new_link['post_content'] = esc_url($data['url']);
508 }
509 $this->new_id = wp_insert_post($new_link);
510 $data['post_id'] = $this->new_id;
511 $parent_post_type = ( isset($data['parent_post_type']) ) ? sanitize_text_field($data['parent_post_type']) : 'page';
512 add_post_meta($this->new_id, '_np_parent_post_type', $parent_post_type);
513 $this->updateMenuMeta($data);
514 return $this->new_id;
515 }
516
517 /**
518 * Update a Post to Match Nav menu
519 * @since 1.3.4
520 * @param array of post data
521 */
522 public function updateFromMenuItem($data)
523 {
524 if ( !current_user_can( 'edit_post', $data['post_id'] ) ) return false;
525 $updated_post = [
526 'ID' => sanitize_text_field($data['post_id']),
527 'menu_order' => sanitize_text_field($data['menu_order']),
528 'post_parent' => sanitize_text_field($data['post_parent'])
529 ];
530 if ( isset($data['content']) ){
531 $updated_post['post_content'] = $data['content'];
532 $updated_post['post_title'] = $data['np_nav_title'];
533 }
534 wp_update_post($updated_post);
535
536 // Menu Options
537 $this->updateLinkTarget($data);
538 $this->updateNavTitle($data);
539 $this->updateTitleAttribute($data);
540
541 if ( $data['np_nav_css_classes'][0] !== "" ){
542 $data['np_nav_css_classes'] = implode(' ', $data['np_nav_css_classes']);
543 $this->updateNavCSS($data);
544 }
545 }
546 }