PostCloner.php
3 years ago
PostDataFactory.php
3 years ago
PostFactory.php
2 days ago
PostRepository.php
5 years ago
PostSaveActions.php
3 years ago
PostTrashActions.php
8 years ago
PostUpdateRepository.php
2 days ago
PrivatePostParent.php
3 years ago
PostCloner.php
159 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\Post; |
| 3 | |
| 4 | class PostCloner |
| 5 | { |
| 6 | /** |
| 7 | * Original Post ID |
| 8 | * @var int |
| 9 | */ |
| 10 | private $original_id; |
| 11 | |
| 12 | /** |
| 13 | * Original Post Object |
| 14 | * @var int |
| 15 | */ |
| 16 | private $original_post; |
| 17 | |
| 18 | /** |
| 19 | * The New Post ID |
| 20 | * @var int |
| 21 | */ |
| 22 | private $new_id; |
| 23 | |
| 24 | /** |
| 25 | * The New Post |
| 26 | * @var object |
| 27 | */ |
| 28 | private $new_post; |
| 29 | |
| 30 | /** |
| 31 | * Clone Options |
| 32 | * @var array |
| 33 | */ |
| 34 | private $clone_options = []; |
| 35 | |
| 36 | /** |
| 37 | * Clone the post |
| 38 | * @param int $id - The ID of the original post to clone |
| 39 | * @param int $quantity - The number of copies to make |
| 40 | * @param str $status - The post status |
| 41 | * @param int $author - The author id to assign the new post(s) to |
| 42 | * @param bool $clone_children - Whether to clone the post tree (hierarchical only) |
| 43 | * @param int $parent_id - The parent post to assign the clone to |
| 44 | * @param bool $original_clone - Whether this is the original clone. Used in post tree cloning only |
| 45 | */ |
| 46 | public function clonePost($id, $quantity = 1, $status = 'publish', $author = null, $clone_children = false, $parent_id = null, $original_clone = true) |
| 47 | { |
| 48 | if ( !current_user_can('edit_post', $id) ) return; |
| 49 | $this->original_id = $id; |
| 50 | $this->original_post = get_post( $id ); |
| 51 | $this->clone_options['quantity'] = $quantity; |
| 52 | $this->clone_options['status'] = $status; |
| 53 | $this->clone_options['author'] = $author; |
| 54 | $this->clone_options['clone_children'] = $clone_children; |
| 55 | $this->clone_options['parent_id'] = $parent_id; |
| 56 | $this->clone_options['original_clone'] = $original_clone; |
| 57 | $this->loopClone(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Clone Post for quantity specified |
| 62 | */ |
| 63 | private function loopClone() |
| 64 | { |
| 65 | $quantity = ( $this->clone_options['original_clone'] ) ? $this->clone_options['quantity'] : 1; |
| 66 | for ( $i = 0; $i < $quantity; $i++ ){ |
| 67 | $this->clonePostData(); |
| 68 | $this->cloneTaxonomies(); |
| 69 | $this->cloneMeta(); |
| 70 | $this->cloneChildren(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Loop through and clone the children if set to do so |
| 76 | */ |
| 77 | private function cloneChildren() |
| 78 | { |
| 79 | if ( !$this->clone_options['clone_children'] ) return; |
| 80 | $post_type = $this->original_post->post_type; |
| 81 | if ( $post_type = 'page' ) $post_type = ['page', 'np-redirect']; |
| 82 | $child_ids = []; |
| 83 | $children = new \WP_Query([ |
| 84 | 'post_type' => $post_type, |
| 85 | 'post_parent' => $this->original_id, |
| 86 | 'posts_per_page' => -1, |
| 87 | 'fields' => 'ids' |
| 88 | ]); |
| 89 | if ( $children->have_posts() ) $child_ids = $children->posts; |
| 90 | wp_reset_postdata(); |
| 91 | if ( empty($child_ids) ) return; |
| 92 | foreach ( $child_ids as $child_id ){ |
| 93 | $cloner = (new PostCloner) |
| 94 | ->clonePost( |
| 95 | $child_id, |
| 96 | $this->clone_options['quantity'], |
| 97 | $this->clone_options['status'], |
| 98 | $this->clone_options['author'], |
| 99 | $this->clone_options['clone_children'], |
| 100 | $this->new_id, |
| 101 | false |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Clone the standard post data |
| 108 | */ |
| 109 | private function clonePostData() |
| 110 | { |
| 111 | $parent = ( $this->clone_options['parent_id'] ) ? $this->clone_options['parent_id'] : $this->original_post->post_parent; |
| 112 | $args = [ |
| 113 | 'comment_status' => $this->original_post->comment_status, |
| 114 | 'ping_status' => $this->original_post->ping_status, |
| 115 | 'post_author' => $this->clone_options['author'], |
| 116 | 'post_content' => $this->original_post->post_content, |
| 117 | 'post_excerpt' => $this->original_post->post_excerpt, |
| 118 | 'post_name' => $this->original_post->post_name, |
| 119 | 'post_parent' => $parent, |
| 120 | 'post_password' => $this->original_post->post_password, |
| 121 | 'post_status' => $this->clone_options['status'], |
| 122 | 'post_title' => $this->original_post->post_title, |
| 123 | 'post_type' => $this->original_post->post_type, |
| 124 | 'to_ping' => $this->original_post->to_ping, |
| 125 | 'menu_order' => $this->original_post->menu_order |
| 126 | ]; |
| 127 | $this->new_id = wp_insert_post(wp_slash($args)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Clone the taxonomies |
| 132 | */ |
| 133 | private function cloneTaxonomies() |
| 134 | { |
| 135 | $taxonomies = get_object_taxonomies($this->original_post->post_type); |
| 136 | foreach ($taxonomies as $taxonomy) { |
| 137 | $post_terms = wp_get_object_terms($this->original_id, $taxonomy, ['fields' => 'slugs']); |
| 138 | wp_set_object_terms($this->new_id, $post_terms, $taxonomy, false); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Clone the post meta |
| 144 | */ |
| 145 | private function cloneMeta() |
| 146 | { |
| 147 | $original_id = $this->original_id; |
| 148 | $new_id = $this->new_id; |
| 149 | $meta_keys = get_post_custom_keys($original_id); |
| 150 | foreach ( $meta_keys as $meta_key ) { |
| 151 | $meta_values = \get_post_custom_values($meta_key, $original_id); |
| 152 | delete_post_meta( $new_id, $meta_key ); |
| 153 | foreach ( $meta_values as $meta_value ) { |
| 154 | $meta_value = \maybe_unserialize($meta_value ); |
| 155 | add_post_meta( $new_id, $meta_key, wp_slash( $meta_value ) ); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |