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
PostFactory.php
72 lines
| 1 | <?php namespace NestedPages\Entities\Post; |
| 2 | |
| 3 | use NestedPages\Entities\Post\PostRepository; |
| 4 | use NestedPages\Entities\Post\PostUpdateRepository; |
| 5 | |
| 6 | /** |
| 7 | * Factory class for adding new posts |
| 8 | */ |
| 9 | class PostFactory { |
| 10 | |
| 11 | /** |
| 12 | * Post Repository |
| 13 | * @var object |
| 14 | */ |
| 15 | private $post_repo; |
| 16 | |
| 17 | /** |
| 18 | * Post Repository |
| 19 | * @var object |
| 20 | */ |
| 21 | private $post_update_repo; |
| 22 | |
| 23 | /** |
| 24 | * New Page IDs |
| 25 | * @var array |
| 26 | */ |
| 27 | private $new_ids = array(); |
| 28 | |
| 29 | |
| 30 | public function __construct() |
| 31 | { |
| 32 | $this->post_repo = new PostRepository; |
| 33 | $this->post_update_repo = new PostUpdateRepository; |
| 34 | } |
| 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 = array( |
| 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']) ){ |
| 54 | $this->post_update_repo->updateTemplate($data); |
| 55 | } |
| 56 | $this->new_ids[$key] = $new_page_id; |
| 57 | } |
| 58 | return $this->getNewPosts($post_type); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Get Array of New Pages |
| 64 | */ |
| 65 | private function getNewPosts($post_type) |
| 66 | { |
| 67 | $new_posts = $this->post_repo->postArray($this->new_ids, $post_type); |
| 68 | return $new_posts; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | } |