PostDataFactory.php
11 years ago
PostFactory.php
11 years ago
PostRepository.php
11 years ago
PostTrashActions.php
11 years ago
PostUpdateRepository.php
11 years ago
PostUpdateRepository.php
385 lines
| 1 | <?php namespace NestedPages\Entities\Post; |
| 2 | |
| 3 | use NestedPages\Form\Validation\Validation; |
| 4 | use NestedPages\Entities\NavMenu\NavMenuRepository; |
| 5 | /** |
| 6 | * Post Create/Update Methods |
| 7 | */ |
| 8 | class PostUpdateRepository { |
| 9 | |
| 10 | /** |
| 11 | * Validation Class |
| 12 | * @var object NP_Validation |
| 13 | */ |
| 14 | protected $validation; |
| 15 | |
| 16 | /** |
| 17 | * Nav Menu Repository |
| 18 | */ |
| 19 | protected $nav_menu_repo; |
| 20 | |
| 21 | /** |
| 22 | * New Post ID |
| 23 | * @var int |
| 24 | */ |
| 25 | protected $new_id; |
| 26 | |
| 27 | |
| 28 | public function __construct() |
| 29 | { |
| 30 | $this->validation = new Validation; |
| 31 | $this->nav_menu_repo = new NavMenuRepository; |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Update Order |
| 37 | * @param array posts |
| 38 | * @param int parent |
| 39 | * @since 1.0 |
| 40 | */ |
| 41 | public function updateOrder($posts, $parent = 0) |
| 42 | { |
| 43 | $this->validation->validatePostIDs($posts); |
| 44 | foreach( $posts as $key => $post ) |
| 45 | { |
| 46 | wp_update_post(array( |
| 47 | 'ID' => sanitize_text_field($post['id']), |
| 48 | 'menu_order' => $key, |
| 49 | 'post_parent' => $parent |
| 50 | )); |
| 51 | |
| 52 | if ( isset($post['children']) ){ |
| 53 | $this->updateOrder($post['children'], $post['id']); |
| 54 | } |
| 55 | } |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Update Post |
| 61 | * @param array data |
| 62 | * @since 1.0 |
| 63 | */ |
| 64 | public function updatePost($data) |
| 65 | { |
| 66 | $this->validation->checkEmpty($data['post_title'], __('Title', 'nestedpages')); |
| 67 | $date = $this->validation->validateDate($data); |
| 68 | if ( !isset($_POST['comment_status']) ) $data['comment_status'] = 'closed'; |
| 69 | |
| 70 | if ( isset($_POST['keep_private']) && $_POST['keep_private'] == 'private' ){ |
| 71 | $status = 'private'; |
| 72 | } else { |
| 73 | $status = ( isset($data['_status']) ) ? sanitize_text_field($data['_status']) : 'publish'; |
| 74 | } |
| 75 | |
| 76 | $updated_post = array( |
| 77 | 'ID' => sanitize_text_field($data['post_id']), |
| 78 | 'post_title' => sanitize_text_field($data['post_title']), |
| 79 | 'post_author' => sanitize_text_field($data['post_author']), |
| 80 | 'post_name' => sanitize_text_field($data['post_name']), |
| 81 | 'post_date' => $date, |
| 82 | 'comment_status' => sanitize_text_field($data['comment_status']), |
| 83 | 'post_status' => $status, |
| 84 | 'post_password' => sanitize_text_field($data['post_password']) |
| 85 | ); |
| 86 | wp_update_post($updated_post); |
| 87 | |
| 88 | $this->updateTemplate($data); |
| 89 | $this->updateNestedPagesStatus($data); |
| 90 | |
| 91 | // Taxonomies |
| 92 | $this->updateCategories($data); |
| 93 | $this->updateTaxonomies($data); |
| 94 | |
| 95 | // Menu Options |
| 96 | $this->updateNavStatus($data); |
| 97 | $this->updateNavTitle($data); |
| 98 | $this->updateLinkTarget($data); |
| 99 | $this->updateTitleAttribute($data); |
| 100 | $this->updateNavCSS($data); |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Update Page Template |
| 108 | * @param array data |
| 109 | * @since 1.0 |
| 110 | */ |
| 111 | public function updateTemplate($data) |
| 112 | { |
| 113 | if ( isset($data['page_template']) ){ |
| 114 | $template = sanitize_text_field($data['page_template']); |
| 115 | update_post_meta( |
| 116 | $data['post_id'], |
| 117 | '_wp_page_template', |
| 118 | $template |
| 119 | ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * Update Nav Status (show/hide in nav menu) |
| 126 | * @since 1.0 |
| 127 | * @param array data |
| 128 | */ |
| 129 | public function updateNavStatus($data) |
| 130 | { |
| 131 | $status = ( isset($data['nav_status']) ) ? 'hide' : 'show'; |
| 132 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 133 | update_post_meta( |
| 134 | $id, |
| 135 | 'np_nav_status', |
| 136 | $status |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Update Nested Pages Visibility (how/hide in Nested Pages interface) |
| 143 | * @since 1.0 |
| 144 | * @param array data |
| 145 | */ |
| 146 | private function updateNestedPagesStatus($data) |
| 147 | { |
| 148 | $status = ( isset($data['nested_pages_status']) ) ? 'hide' : 'show'; |
| 149 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 150 | update_post_meta( |
| 151 | $id, |
| 152 | 'nested_pages_status', |
| 153 | $status |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * Update Nested Pages Menu Navigation Label |
| 160 | * @since 1.0 |
| 161 | * @param array data |
| 162 | */ |
| 163 | private function updateNavTitle($data) |
| 164 | { |
| 165 | if ( isset($data['np_nav_title']) ){ |
| 166 | $title = sanitize_text_field($data['np_nav_title']); |
| 167 | update_post_meta( |
| 168 | $data['post_id'], |
| 169 | 'np_nav_title', |
| 170 | $title |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Update Nested Pages Menu Navigation CSS Classes |
| 178 | * @since 1.0 |
| 179 | * @param array data |
| 180 | */ |
| 181 | private function updateNavCSS($data) |
| 182 | { |
| 183 | if ( isset($data['np_nav_css_classes']) ){ |
| 184 | $css_classes = sanitize_text_field($data['np_nav_css_classes']); |
| 185 | update_post_meta( |
| 186 | $data['post_id'], |
| 187 | 'np_nav_css_classes', |
| 188 | $css_classes |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Update Nested Pages Menu Title Attribute |
| 196 | * @since 1.0 |
| 197 | * @param array data |
| 198 | */ |
| 199 | private function updateTitleAttribute($data) |
| 200 | { |
| 201 | if ( isset($data['np_title_attribute']) ){ |
| 202 | $title_attr = sanitize_text_field($data['np_title_attribute']); |
| 203 | update_post_meta( |
| 204 | $data['post_id'], |
| 205 | 'np_title_attribute', |
| 206 | $title_attr |
| 207 | ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /** |
| 213 | * Update Categories |
| 214 | * @since 1.0 |
| 215 | * @param array data |
| 216 | */ |
| 217 | private function updateCategories($data) |
| 218 | { |
| 219 | if ( isset($data['post_category']) ) |
| 220 | { |
| 221 | $this->validation->validateIntegerArray($data['post_category']); |
| 222 | $cats = array(); |
| 223 | foreach($data['post_category'] as $cat) { |
| 224 | if ( $cat !== 0 ) $cats[] = (int) $cat; |
| 225 | } |
| 226 | wp_set_post_terms($data['post_id'], $cats, 'category'); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Update Hierarchical Taxonomy Terms |
| 233 | * @since 1.0 |
| 234 | * @param array data |
| 235 | */ |
| 236 | private function updateTaxonomies($data) |
| 237 | { |
| 238 | if ( isset($data['tax_input']) ) { |
| 239 | foreach ( $data['tax_input'] as $taxonomy => $term_ids ){ |
| 240 | $tax = get_taxonomy($taxonomy); |
| 241 | if ( $tax->hierarchical ){ |
| 242 | $this->validation->validateIntegerArray($term_ids); |
| 243 | $this->updateHierarchicalTaxonomies($data, $taxonomy, $term_ids); |
| 244 | } else { |
| 245 | $this->updateFlatTaxonomy($data, $taxonomy, $term_ids); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /** |
| 253 | * Update Hierarchical Taxonomy Terms |
| 254 | * @since 1.1.4 |
| 255 | * @param array data |
| 256 | */ |
| 257 | private function updateHierarchicalTaxonomies($data, $taxonomy, $term_ids) |
| 258 | { |
| 259 | $terms = array(); |
| 260 | foreach ( $term_ids as $term ){ |
| 261 | if ( $term !== 0 ) $terms[] = (int) $term; |
| 262 | } |
| 263 | wp_set_post_terms($data['post_id'], $terms, $taxonomy); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Update Flat Taxonomy Terms |
| 269 | * @since 1.1.4 |
| 270 | * @param array data |
| 271 | */ |
| 272 | private function updateFlatTaxonomy($data, $taxonomy, $terms) |
| 273 | { |
| 274 | $terms = explode(',', sanitize_text_field($terms)); |
| 275 | $new_terms = array(); |
| 276 | foreach($terms as $term) |
| 277 | { |
| 278 | if ( $term !== "" ) array_push($new_terms, $term); |
| 279 | } |
| 280 | wp_set_post_terms($data['post_id'], $new_terms, $taxonomy); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /** |
| 285 | * Update Link Target for Redirects |
| 286 | * @since 1.1 |
| 287 | * @param array data |
| 288 | */ |
| 289 | private function updateLinkTarget($data) |
| 290 | { |
| 291 | $link_target = ( isset($data['link_target']) && $data['link_target'] == "_blank" ) ? "_blank" : ""; |
| 292 | $id = ( isset($data['post_id']) ) ? $data['post_id'] : $this->new_id; |
| 293 | update_post_meta( |
| 294 | $id, |
| 295 | 'np_link_target', |
| 296 | $link_target |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * Update a Redirect |
| 303 | * @since 1.1 |
| 304 | * @param array data |
| 305 | */ |
| 306 | public function updateRedirect($data) |
| 307 | { |
| 308 | $this->validation->checkEmpty($data['post_title'], __('Label', 'nestedpages')); |
| 309 | $menu_order = isset($data['menu_order']) ? $data['menu_order'] : 0; |
| 310 | $updated_post = array( |
| 311 | 'ID' => sanitize_text_field($data['post_id']), |
| 312 | 'post_title' => sanitize_text_field($data['post_title']), |
| 313 | 'post_status' => sanitize_text_field($data['_status']), |
| 314 | 'post_content' => sanitize_text_field($data['post_content']), |
| 315 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 316 | 'menu_order' => $menu_order |
| 317 | ); |
| 318 | $this->new_id = wp_update_post($updated_post); |
| 319 | |
| 320 | $this->updateNavStatus($data); |
| 321 | $this->updateNestedPagesStatus($data); |
| 322 | $this->updateLinkTarget($data); |
| 323 | $this->updateTitleAttribute($data); |
| 324 | $this->updateNavCSS($data); |
| 325 | |
| 326 | return $this->new_id; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | /** |
| 331 | * Save a new Redirect |
| 332 | * @since 1.1 |
| 333 | * @param array data |
| 334 | */ |
| 335 | public function saveRedirect($data) |
| 336 | { |
| 337 | $this->validation->validateRedirect($data); |
| 338 | $new_link = array( |
| 339 | 'post_title' => sanitize_text_field($data['np_link_title']), |
| 340 | 'post_status' => sanitize_text_field($data['_status']), |
| 341 | 'post_content' => sanitize_text_field($data['np_link_content']), |
| 342 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 343 | 'post_type' => 'np-redirect', |
| 344 | 'post_excerpt' => '' |
| 345 | ); |
| 346 | $this->new_id = wp_insert_post($new_link); |
| 347 | |
| 348 | $this->updateNavStatus($data); |
| 349 | $this->updateNestedPagesStatus($data); |
| 350 | $this->updateLinkTarget($data); |
| 351 | return $this->new_id; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | /** |
| 357 | * Update a Post to Match Nav menu |
| 358 | * @since 1.3.4 |
| 359 | * @param array of post data |
| 360 | */ |
| 361 | public function updateFromMenuItem($data) |
| 362 | { |
| 363 | $updated_post = array( |
| 364 | 'ID' => sanitize_text_field($data['post_id']), |
| 365 | 'menu_order' => sanitize_text_field($data['menu_order']), |
| 366 | 'post_parent' => sanitize_text_field($data['post_parent']) |
| 367 | ); |
| 368 | if ( isset($data['content']) ){ |
| 369 | $updated_post['post_content'] = $data['content']; |
| 370 | $updated_post['post_title'] = $data['np_nav_title']; |
| 371 | } |
| 372 | wp_update_post($updated_post); |
| 373 | |
| 374 | // Menu Options |
| 375 | $this->updateLinkTarget($data); |
| 376 | $this->updateNavTitle($data); |
| 377 | $this->updateTitleAttribute($data); |
| 378 | |
| 379 | if ( $data['np_nav_css_classes'][0] !== "" ){ |
| 380 | $data['np_nav_css_classes'] = implode(' ', $data['np_nav_css_classes']); |
| 381 | $this->updateNavCSS($data); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | } |