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
PostFactory.php
112 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\Post; |
| 3 | |
| 4 | use NestedPages\Entities\Post\PostRepository; |
| 5 | use NestedPages\Entities\Post\PostUpdateRepository; |
| 6 | |
| 7 | /** |
| 8 | * Factory class for adding new posts |
| 9 | */ |
| 10 | class PostFactory |
| 11 | { |
| 12 | /** |
| 13 | * Post Repository |
| 14 | * @var object |
| 15 | */ |
| 16 | private $post_repo; |
| 17 | |
| 18 | /** |
| 19 | * Post Repository |
| 20 | * @var object |
| 21 | */ |
| 22 | private $post_update_repo; |
| 23 | |
| 24 | /** |
| 25 | * New Page IDs |
| 26 | * @var array |
| 27 | */ |
| 28 | private $new_ids = []; |
| 29 | |
| 30 | |
| 31 | public function __construct() |
| 32 | { |
| 33 | $this->post_repo = new PostRepository; |
| 34 | $this->post_update_repo = new PostUpdateRepository; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Create New Child Pages |
| 39 | */ |
| 40 | public function createChildPosts($data) |
| 41 | { |
| 42 | foreach($data['post_title'] as $key => $title){ |
| 43 | $post_type = sanitize_text_field($data['post_type']); |
| 44 | $post = [ |
| 45 | 'post_title' => sanitize_text_field($title), |
| 46 | 'post_status' => sanitize_text_field($data['_status']), |
| 47 | 'post_author' => sanitize_text_field($data['post_author']), |
| 48 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 49 | 'post_type' => $post_type |
| 50 | ]; |
| 51 | $new_page_id = wp_insert_post($post); |
| 52 | $data['post_id'] = $new_page_id; |
| 53 | if ( isset($data['page_template']) ) $this->post_update_repo->updateTemplate($data); |
| 54 | if ( isset($data['nav_status']) ) $this->post_update_repo->updateNavStatus($data); |
| 55 | $this->new_ids[$key] = $new_page_id; |
| 56 | } |
| 57 | return $this->getNewPosts($post_type); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Create new Posts before/after a specified post |
| 62 | */ |
| 63 | public function createBeforeAfterPosts($data) |
| 64 | { |
| 65 | global $wpdb; |
| 66 | $menu_order = 0; |
| 67 | $parent = false; |
| 68 | $post_type = sanitize_text_field($data['post_type']); |
| 69 | $after = ( isset($data['after_id']) && $data['after_id'] !== '' ) ? true : false; |
| 70 | $reference_post = ( $after ) ? intval($data['after_id']) : intval($data['before_id']); |
| 71 | |
| 72 | // Get the source post, so the reference points for menu order can be determined |
| 73 | $pq = new \WP_Query([ |
| 74 | 'post_type' => $post_type, |
| 75 | 'posts_per_page' => 1, |
| 76 | 'p' => $reference_post |
| 77 | ]); |
| 78 | if ( $pq->have_posts() ) : |
| 79 | $parent = intval($pq->posts[0]->post_parent); |
| 80 | $menu_order = $pq->posts[0]->menu_order; |
| 81 | endif; wp_reset_postdata(); |
| 82 | |
| 83 | if ( $parent ) $data['parent_id'] = $parent; |
| 84 | $new_posts = $this->createChildPosts($data); |
| 85 | |
| 86 | if ( $after ) $menu_order = $menu_order + 1; |
| 87 | $new_post_count = count($new_posts); |
| 88 | $first_new_id = $new_posts[0]['id']; |
| 89 | $last_new_id = $new_posts[count($new_posts) - 1]['id']; |
| 90 | |
| 91 | $sql = "UPDATE `$wpdb->posts` SET menu_order = menu_order+%d WHERE post_parent = %d AND (post_status = 'publish' OR post_status = 'draft') AND (post_type = '%s'"; |
| 92 | if ( $post_type == 'page' ) $sql .= " OR post_type = 'np-redirect'"; |
| 93 | $sql .= ") AND (menu_order >= %d) ORDER BY menu_order;"; |
| 94 | |
| 95 | // Reorder All posts after the new ones |
| 96 | $wpdb->query($wpdb->prepare($sql, [$new_post_count, $parent, $post_type, $menu_order])); |
| 97 | // Reorder the new posts menu_order |
| 98 | $wpdb->query($wpdb->prepare("SET @start_order := %d;", [$menu_order-1])); |
| 99 | $wpdb->query($wpdb->prepare("UPDATE `$wpdb->posts` SET menu_order = (@start_order:=@start_order+1) WHERE post_parent = %d AND (post_type = '%s') AND (ID BETWEEN %d AND %d) ORDER BY menu_order;", [$parent, $post_type, $first_new_id, $last_new_id])); |
| 100 | |
| 101 | return $new_posts; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get Array of New Pages |
| 106 | */ |
| 107 | private function getNewPosts($post_type) |
| 108 | { |
| 109 | $new_posts = $this->post_repo->postArray($this->new_ids, $post_type); |
| 110 | return $new_posts; |
| 111 | } |
| 112 | } |