PostCloner.php
3 years ago
PostDataFactory.php
3 years ago
PostFactory.php
3 days ago
PostRepository.php
5 years ago
PostSaveActions.php
3 years ago
PostTrashActions.php
8 years ago
PostUpdateRepository.php
3 days ago
PrivatePostParent.php
3 years ago
PostFactory.php
131 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 | if ( !current_user_can('publish_posts') ) return; |
| 43 | $post_type = sanitize_text_field($data['post_type']); |
| 44 | |
| 45 | // Set the initial menu order |
| 46 | $pq = new \WP_Query([ |
| 47 | 'post_type' => $post_type, |
| 48 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 49 | 'posts_per_page' => -1, |
| 50 | 'fields' => 'ids' |
| 51 | ]); |
| 52 | $menu_order = ( $pq->have_posts() ) ? count($pq->posts) : 0; |
| 53 | wp_reset_postdata(); |
| 54 | |
| 55 | foreach($data['post_title'] as $key => $title){ |
| 56 | $post = [ |
| 57 | 'post_title' => esc_attr($title), |
| 58 | 'post_status' => sanitize_text_field($data['_status']), |
| 59 | 'post_author' => sanitize_text_field($data['post_author']), |
| 60 | 'post_parent' => sanitize_text_field($data['parent_id']), |
| 61 | 'post_type' => $post_type, |
| 62 | 'menu_order' => $menu_order |
| 63 | ]; |
| 64 | $post = apply_filters('nestedpages_new_post', $post, $data); |
| 65 | $new_page_id = wp_insert_post($post); |
| 66 | $data['post_id'] = $new_page_id; |
| 67 | if ( isset($data['page_template']) ) $this->post_update_repo->updateTemplate($data); |
| 68 | if ( isset($data['nav_status']) ) $this->post_update_repo->updateNavStatus($data); |
| 69 | $this->new_ids[$key] = $new_page_id; |
| 70 | |
| 71 | $new_post = get_post($new_page_id); |
| 72 | $new_post->post_content = (string) apply_filters('default_content', $new_post->post_content, $new_post); |
| 73 | wp_update_post($new_post); |
| 74 | } |
| 75 | return $this->getNewPosts($post_type); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Create new Posts before/after a specified post |
| 80 | */ |
| 81 | public function createBeforeAfterPosts($data) |
| 82 | { |
| 83 | if ( !current_user_can('publish_posts') ) return; |
| 84 | global $wpdb; |
| 85 | $menu_order = 0; |
| 86 | $parent = false; |
| 87 | $post_type = sanitize_text_field($data['post_type']); |
| 88 | $after = ( isset($data['after_id']) && $data['after_id'] !== '' ) ? true : false; |
| 89 | $reference_post = ( $after ) ? intval($data['after_id']) : intval($data['before_id']); |
| 90 | |
| 91 | // Get the source post, so the reference points for menu order can be determined |
| 92 | $pq = new \WP_Query([ |
| 93 | 'post_type' => $post_type, |
| 94 | 'posts_per_page' => 1, |
| 95 | 'p' => $reference_post |
| 96 | ]); |
| 97 | if ( $pq->have_posts() ) : |
| 98 | $parent = intval($pq->posts[0]->post_parent); |
| 99 | $menu_order = $pq->posts[0]->menu_order; |
| 100 | endif; wp_reset_postdata(); |
| 101 | |
| 102 | if ( $parent ) $data['parent_id'] = $parent; |
| 103 | $new_posts = $this->createChildPosts($data); |
| 104 | |
| 105 | if ( $after ) $menu_order = $menu_order + 1; |
| 106 | $new_post_count = count($new_posts); |
| 107 | $first_new_id = $new_posts[0]['id']; |
| 108 | $last_new_id = $new_posts[count($new_posts) - 1]['id']; |
| 109 | |
| 110 | $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'"; |
| 111 | if ( $post_type == 'page' ) $sql .= " OR post_type = 'np-redirect'"; |
| 112 | $sql .= ") AND (menu_order >= %d) ORDER BY menu_order;"; |
| 113 | |
| 114 | // Reorder All posts after the new ones |
| 115 | $wpdb->query($wpdb->prepare($sql, [$new_post_count, $parent, $post_type, $menu_order])); |
| 116 | // Reorder the new posts menu_order |
| 117 | $wpdb->query($wpdb->prepare("SET @start_order := %d;", [$menu_order-1])); |
| 118 | $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])); |
| 119 | |
| 120 | return $new_posts; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get Array of New Pages |
| 125 | */ |
| 126 | private function getNewPosts($post_type) |
| 127 | { |
| 128 | $new_posts = $this->post_repo->postArray($this->new_ids, $post_type); |
| 129 | return $new_posts; |
| 130 | } |
| 131 | } |