PostCloner.php
8 years ago
PostDataFactory.php
8 years ago
PostFactory.php
8 years ago
PostRepository.php
8 years ago
PostTrashActions.php
8 years ago
PostUpdateRepository.php
7 years ago
PrivatePostParent.php
8 years ago
PostUpdateRepository.php
458 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 | |
| 8 | /** |
| 9 | * Post Create/Update Methods |
| 10 | */ |
| 11 | class PostUpdateRepository |
| 12 | { |
| 13 | /** |
| 14 | * Validation Class |
| 15 | * @var object NP_Validation |
| 16 | */ |
| 17 | protected $validation; |
| 18 | |
| 19 | /** |
| 20 | * Nav Menu Repository |
| 21 | */ |
| 22 | protected $nav_menu_repo; |
| 23 | |
| 24 | /** |
| 25 | * Post Type Repository |
| 26 | */ |
| 27 | protected $post_type_repo; |
| 28 | |
| 29 | /** |
| 30 | * New Post ID |
| 31 | * @var int |
| 32 | */ |
| 33 | protected $new_id; |
| 34 | |
| 35 | public function __construct() |
| 36 | { |
| 37 | $this->validation = new Validation; |
| 38 | $this->nav_menu_repo = new NavMenuRepository; |
| 39 | $this->post_type_repo = new PostTypeRepository; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Update Order |
| 44 | * @param array posts |
| 45 | * @param int parent |
| 46 | * @since 1.0 |
| 47 | */ |
| 48 | public function updateOrder($posts, $parent = 0) |
| 49 | { |
| 50 | $this->validation->validatePostIDs($posts); |
| 51 | global $wpdb; |
| 52 | foreach( $posts as $key => $post ) |
| 53 | { |
| 54 | $post_id = sanitize_text_field($post['id']); |
| 55 | $original_modifed_date = get_post_modified_time('Y-m-d H:i:s', false, $post_id); |
| 56 | $original_modifed_date_gmt = get_post_modified_time('Y-m-d H:i:s', true, $post_id); |
| 57 | |
| 58 | // Reset the modified date to the last modified date |
| 59 | $query = $wpdb->prepare( |
| 60 | "UPDATE $wpdb->posts |
| 61 | SET menu_order = '%d', post_parent = '%d', post_modified = '%s', post_modified_gmt = '%s' |
| 62 | WHERE ID = '%d'", |
| 63 | intval($key), |
| 64 | intval($parent), |
| 65 | $original_modifed_date, |
| 66 | $original_modifed_date_gmt, |
| 67 | intval($post_id) |
| 68 | ); |
| 69 | |
| 70 | $wpdb->query( $query ); |
| 71 | do_action('nestedpages_post_order_updated', $post_id, $parent, $key); |
| 72 | |
| 73 | if ( isset($post['children']) ) $this->updateOrder($post['children'], $post_id); |
| 74 | } |
| 75 | do_action('nestedpages_posts_order_updated', $posts, $parent); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Update Post |
| 81 | * @param array data |
| 82 | * @param boolean append taxonomies - whether to append or overwrite taxonomies |
| 83 | * @since 1.0 |
| 84 | */ |
| 85 | public function updatePost($data, $append_taxonomies = false) |
| 86 | { |
| 87 | $updated_post = [ |
| 88 | 'ID' => sanitize_text_field($data['post_id']) |
| 89 | ]; |
| 90 | |
| 91 | if ( isset($data['post_title']) && $data['post_title'] == "" ){ |
| 92 | $this->validation->checkEmpty($data['post_title'], __('Title', 'wp-nested-pages')); |
| 93 | } elseif ( isset($data['post_title']) ){ |
| 94 | $updated_post['post_title'] = sanitize_text_field($data['post_title']); |
| 95 | } |
| 96 | |
| 97 | if ( isset($data['post_name']) ) |
| 98 | $updated_post['post_name'] = sanitize_text_field($data['post_name']); |
| 99 | |
| 100 | if ( isset($data['post_author']) ) |
| 101 | $updated_post['post_author'] = sanitize_text_field($data['post_author']); |
| 102 | |
| 103 | if ( isset($data['post_password']) ) |
| 104 | $updated_post['post_password'] = sanitize_text_field($data['post_password']); |
| 105 | |
| 106 | if ( !$this->post_type_repo->standardFieldDisabled('allow_comments', sanitize_text_field($data['post_type'])) ){ |
| 107 | $updated_post['comment_status'] = ( isset($data['comment_status']) ) ? 'open' : 'closed'; |
| 108 | } |
| 109 | |
| 110 | if ( isset($data['post_parent']) && $data['post_parent'] != '-1' ){ |
| 111 | $updated_post['post_parent'] = intval(sanitize_text_field($data['post_parent'])); |
| 112 | } |
| 113 | |
| 114 | if ( isset($data['np_date']) ) { |
| 115 | $date = $this->validation->validateDate($data); |
| 116 | $updated_post['post_date'] = $date; |
| 117 | } |
| 118 | |
| 119 | if ( isset($data['mm']) && isset($data['jj']) && isset($data['aa']) ){ |
| 120 | $date = $this->validation->validateDate($data); |
| 121 | $updated_post['post_date'] = $date; |
| 122 | } |
| 123 | |
| 124 | if ( isset($_POST['keep_private']) && $_POST['keep_private'] == 'private' ){ |
| 125 | $updated_post['post_status'] = 'private'; |
| 126 | } else { |
| 127 | if ( isset($data['_status']) ) $updated_post['post_status'] = sanitize_text_field($data['_status']); |
| 128 | } |
| 129 | |
| 130 | wp_update_post($updated_post); |
| 131 | |
| 132 | $this->updateSticky($data); |
| 133 | $this->updateTemplate($data); |
| 134 | $this->updateNestedPagesStatus($data); |
| 135 | |
| 136 | // Taxonomies |
| 137 | $this->updateCategories($data, $append_taxonomies); |
| 138 | $this->updateTaxonomies($data, $append_taxonomies); |
| 139 | |
| 140 | // Menu Options |
| 141 | $this->updateNavStatus($data); |
| 142 | $this->updateNavTitle($data); |
| 143 | $this->updateLinkTarget($data); |
| 144 | $this->updateTitleAttribute($data); |
| 145 | $this->updateNavCSS($data); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Update Page Template |
| 152 | * @param array data |
| 153 | * @since 1.0 |
| 154 | */ |
| 155 | public function updateTemplate($data) |
| 156 | { |
| 157 | if ( isset($data['page_template']) ){ |
| 158 | $template = sanitize_text_field($data['page_template']); |
| 159 | update_post_meta( |
| 160 | $data['post_id'], |
| 161 | '_wp_page_template', |
| 162 | $template |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Update Nav Status (show/hide in nav menu) |
| 169 | * @since 1.0 |
| 170 | * @param array data |
| 171 | */ |
| 172 | public function updateNavStatus($data) |
| 173 | { |
| 174 | $status = ( isset($data['nav_status']) && $data['nav_status'] == 'hide' ) ? 'hide' : 'show'; |
| 175 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 176 | update_post_meta( |
| 177 | $id, |
| 178 | '_np_nav_status', |
| 179 | $status |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Update Nested Pages Visibility (how/hide in Nested Pages interface) |
| 185 | * @since 1.0 |
| 186 | * @param array data |
| 187 | */ |
| 188 | private function updateNestedPagesStatus($data) |
| 189 | { |
| 190 | if ( $this->post_type_repo->standardFieldDisabled('hide_in_np', sanitize_text_field($data['post_type'])) ) return; |
| 191 | |
| 192 | $status = ( isset($data['nested_pages_status']) && $data['nested_pages_status'] == 'hide' ) ? 'hide' : 'show'; |
| 193 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 194 | update_post_meta( |
| 195 | $id, |
| 196 | '_nested_pages_status', |
| 197 | $status |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Update Nested Pages Menu Navigation Label |
| 203 | * @since 1.0 |
| 204 | * @param array data |
| 205 | */ |
| 206 | private function updateNavTitle($data) |
| 207 | { |
| 208 | if ( isset($data['np_nav_title']) ){ |
| 209 | $title = sanitize_text_field($data['np_nav_title']); |
| 210 | update_post_meta( |
| 211 | $data['post_id'], |
| 212 | '_np_nav_title', |
| 213 | $title |
| 214 | ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Update Nested Pages Menu Navigation CSS Classes |
| 220 | * @since 1.0 |
| 221 | * @param array data |
| 222 | */ |
| 223 | private function updateNavCSS($data) |
| 224 | { |
| 225 | if ( isset($data['np_nav_css_classes']) ){ |
| 226 | $css_classes = sanitize_text_field($data['np_nav_css_classes']); |
| 227 | update_post_meta( |
| 228 | $data['post_id'], |
| 229 | '_np_nav_css_classes', |
| 230 | $css_classes |
| 231 | ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Update Nested Pages Menu Title Attribute |
| 237 | * @since 1.0 |
| 238 | * @param array data |
| 239 | */ |
| 240 | private function updateTitleAttribute($data) |
| 241 | { |
| 242 | if ( isset($data['np_title_attribute']) ){ |
| 243 | $title_attr = sanitize_text_field($data['np_title_attribute']); |
| 244 | update_post_meta( |
| 245 | $data['post_id'], |
| 246 | '_np_title_attribute', |
| 247 | $title_attr |
| 248 | ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Update Categories |
| 254 | * @since 1.0 |
| 255 | * @param array data |
| 256 | */ |
| 257 | private function updateCategories($data, $append_taxonomies = false) |
| 258 | { |
| 259 | if ( isset($data['post_category']) ) |
| 260 | { |
| 261 | $this->validation->validateIntegerArray($data['post_category']); |
| 262 | $cats = []; |
| 263 | foreach($data['post_category'] as $cat) { |
| 264 | if ( $cat !== 0 ) $cats[] = (int) $cat; |
| 265 | } |
| 266 | wp_set_post_terms($data['post_id'], $cats, 'category', $append_taxonomies); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Update Hierarchical Taxonomy Terms |
| 272 | * @since 1.0 |
| 273 | * @param array data |
| 274 | */ |
| 275 | private function updateTaxonomies($data, $append_taxonomies) |
| 276 | { |
| 277 | if ( isset($data['tax_input']) ) { |
| 278 | foreach ( $data['tax_input'] as $taxonomy => $term_ids ){ |
| 279 | $tax = get_taxonomy($taxonomy); |
| 280 | if ( $tax->hierarchical ){ |
| 281 | $this->validation->validateIntegerArray($term_ids); |
| 282 | $this->updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies); |
| 283 | } else { |
| 284 | $this->updateFlatTaxonomy($data, $taxonomy, $term_ids, $append_taxonomies); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Update Hierarchical Taxonomy Terms |
| 292 | * @since 1.1.4 |
| 293 | * @param array data |
| 294 | */ |
| 295 | private function updateHierarchicalTaxonomies($data, $taxonomy, $term_ids, $append_taxonomies) |
| 296 | { |
| 297 | $terms = []; |
| 298 | foreach ( $term_ids as $term ){ |
| 299 | if ( $term !== 0 ) $terms[] = (int) $term; |
| 300 | } |
| 301 | wp_set_post_terms($data['post_id'], $terms, $taxonomy, $append_taxonomies); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Update Flat Taxonomy Terms |
| 306 | * @since 1.1.4 |
| 307 | * @param array data |
| 308 | */ |
| 309 | private function updateFlatTaxonomy($data, $taxonomy, $terms, $append_taxonomies) |
| 310 | { |
| 311 | $terms = explode(',', sanitize_text_field($terms)); |
| 312 | $new_terms = array(); |
| 313 | foreach($terms as $term) |
| 314 | { |
| 315 | if ( $term !== "" ) array_push($new_terms, $term); |
| 316 | } |
| 317 | wp_set_post_terms($data['post_id'], $new_terms, $taxonomy, $append_taxonomies); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Update Link Target for Redirects |
| 322 | * @since 1.1 |
| 323 | * @param array data |
| 324 | */ |
| 325 | private function updateLinkTarget($data) |
| 326 | { |
| 327 | $link_target = ( isset($data['link_target']) && $data['link_target'] == "_blank" ) ? "_blank" : ""; |
| 328 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 329 | update_post_meta( |
| 330 | $id, |
| 331 | '_np_link_target', |
| 332 | $link_target |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Update Sticky Posts |
| 338 | * @since 2.0.1 |
| 339 | * @param array data |
| 340 | */ |
| 341 | private function updateSticky($data) |
| 342 | { |
| 343 | if ( $this->post_type_repo->standardFieldDisabled('sticky', sanitize_text_field($data['post_type'])) ) return; |
| 344 | $sticky_posts = get_option('sticky_posts'); |
| 345 | if ( isset($data['sticky']) && $data['sticky'] ){ |
| 346 | $sticky_posts[] = $data['post_id']; |
| 347 | update_option('sticky_posts', $sticky_posts); |
| 348 | return; |
| 349 | } |
| 350 | foreach($sticky_posts as $key => $post){ |
| 351 | if ( $post == intval($data['post_id']) ) unset($sticky_posts[$key]); |
| 352 | } |
| 353 | update_option('sticky_posts', $sticky_posts); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Update Menu Related Meta |
| 358 | * @since 1.4.1 |
| 359 | * @param array $data |
| 360 | */ |
| 361 | private function updateMenuMeta($data) |
| 362 | { |
| 363 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 364 | $link_target = ( isset($data['linkTarget']) ) ? "_blank" : ""; |
| 365 | update_post_meta($id, '_np_link_target', $link_target); |
| 366 | update_post_meta($id, '_np_nav_menu_item_type', sanitize_text_field($data['menuType'])); |
| 367 | update_post_meta($id, '_np_nav_menu_item_object', sanitize_text_field($data['objectType'])); |
| 368 | update_post_meta($id, '_np_nav_menu_item_object_id', sanitize_text_field($data['objectId'])); |
| 369 | if ( isset($data['cssClasses']) ){ |
| 370 | update_post_meta($id, '_np_nav_css_classes', sanitize_text_field($data['cssClasses'])); |
| 371 | } |
| 372 | if ( isset($data['titleAttribute']) ){ |
| 373 | $title_attr = sanitize_text_field($data['titleAttribute']); |
| 374 | update_post_meta($id, '_np_title_attribute', $title_attr); |
| 375 | } |
| 376 | if ( isset($data['navigationLabel']) ){ |
| 377 | $title = sanitize_text_field($data['navigationLabel']); |
| 378 | update_post_meta($id, '_np_nav_title', $title); |
| 379 | } |
| 380 | $this->updateNavStatus($data); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Update a Redirect |
| 385 | * @since 1.1 |
| 386 | * @param array data |
| 387 | */ |
| 388 | public function updateRedirect($data) |
| 389 | { |
| 390 | $menu_order = isset($data['menu_order']) ? $data['menu_order'] : 0; |
| 391 | $updated_post = [ |
| 392 | 'ID' => sanitize_text_field($data['post_id']), |
| 393 | 'post_title' => sanitize_text_field($data['post_title']), |
| 394 | 'post_status' => sanitize_text_field($data['_status']), |
| 395 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 396 | 'menu_order' => $menu_order |
| 397 | ]; |
| 398 | |
| 399 | if ( isset($data['post_content']) && $data['post_content'] !== "" ){ |
| 400 | $updated_post['post_content'] = esc_url($data['post_content']); |
| 401 | } |
| 402 | |
| 403 | $this->new_id = wp_update_post($updated_post); |
| 404 | $this->updateMenuMeta($data); |
| 405 | return $this->new_id; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Save a new Redirect |
| 410 | * @since 1.1 |
| 411 | * @param array data |
| 412 | */ |
| 413 | public function saveRedirect($data) |
| 414 | { |
| 415 | $new_link = [ |
| 416 | 'post_title' => sanitize_text_field($data['menuTitle']), |
| 417 | 'post_status' => sanitize_text_field('publish'), |
| 418 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 419 | 'post_type' => 'np-redirect', |
| 420 | 'post_excerpt' => '' |
| 421 | ]; |
| 422 | if ( isset($data['url']) && $data['url'] !== "" ){ |
| 423 | $new_link['post_content'] = esc_url($data['url']); |
| 424 | } |
| 425 | $this->new_id = wp_insert_post($new_link); |
| 426 | $this->updateMenuMeta($data); |
| 427 | return $this->new_id; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Update a Post to Match Nav menu |
| 432 | * @since 1.3.4 |
| 433 | * @param array of post data |
| 434 | */ |
| 435 | public function updateFromMenuItem($data) |
| 436 | { |
| 437 | $updated_post = [ |
| 438 | 'ID' => sanitize_text_field($data['post_id']), |
| 439 | 'menu_order' => sanitize_text_field($data['menu_order']), |
| 440 | 'post_parent' => sanitize_text_field($data['post_parent']) |
| 441 | ]; |
| 442 | if ( isset($data['content']) ){ |
| 443 | $updated_post['post_content'] = $data['content']; |
| 444 | $updated_post['post_title'] = $data['np_nav_title']; |
| 445 | } |
| 446 | wp_update_post($updated_post); |
| 447 | |
| 448 | // Menu Options |
| 449 | $this->updateLinkTarget($data); |
| 450 | $this->updateNavTitle($data); |
| 451 | $this->updateTitleAttribute($data); |
| 452 | |
| 453 | if ( $data['np_nav_css_classes'][0] !== "" ){ |
| 454 | $data['np_nav_css_classes'] = implode(' ', $data['np_nav_css_classes']); |
| 455 | $this->updateNavCSS($data); |
| 456 | } |
| 457 | } |
| 458 | } |