| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
final class DuplicateFilterSet |
| 10 |
{ |
| 11 |
|
| 12 |
public $post_id; |
| 13 |
|
| 14 |
public $new_post_id; |
| 15 |
|
| 16 |
public function __construct($post_id) |
| 17 |
{ |
| 18 |
$this->post_id = $post_id; |
| 19 |
$this->duplicatePost(); |
| 20 |
} |
| 21 |
|
| 22 |
private function duplicatePost() |
| 23 |
{ |
| 24 |
$post = get_post($this->post_id); |
| 25 |
|
| 26 |
if (!$post || $post->post_type !== FLRT_FILTERS_SET_POST_TYPE) { |
| 27 |
wp_die(esc_html__('The post was not found or is not a Filter Set.', 'filter-everything')); |
| 28 |
} |
| 29 |
|
| 30 |
$post_content = maybe_unserialize($post->post_content); |
| 31 |
if(isset($post_content['wp_filter_query'])) |
| 32 |
unset($post_content['wp_filter_query']); |
| 33 |
|
| 34 |
if (!is_serialized($post_content)) { |
| 35 |
$post_content = maybe_serialize($post_content); |
| 36 |
} |
| 37 |
|
| 38 |
$copy_text = esc_html__('copy', 'filter-everything'); |
| 39 |
|
| 40 |
$this->new_post_id = wp_insert_post( |
| 41 |
$this->preparePost($post, |
| 42 |
[ |
| 43 |
'post_title' => flrt_generate_unique_copy_title($post->post_title, FLRT_FILTERS_SET_POST_TYPE, $copy_text), |
| 44 |
'post_content' => $post_content, |
| 45 |
'post_status' => 'draft' |
| 46 |
] |
| 47 |
)); |
| 48 |
$this->copyPostMeta($this->post_id, $this->new_post_id); |
| 49 |
$this->copyFilterFields(); |
| 50 |
} |
| 51 |
|
| 52 |
private function preparePost($post, $options = []) |
| 53 |
{ |
| 54 |
$new_post = [ |
| 55 |
'post_author' => wp_get_current_user()->ID, |
| 56 |
'post_content' => $post->post_content, |
| 57 |
'post_title' => $post->post_title, |
| 58 |
'post_excerpt' => $post->post_excerpt, |
| 59 |
'post_status' => 'publish', |
| 60 |
'comment_status' => $post->comment_status, |
| 61 |
'ping_status' => $post->ping_status, |
| 62 |
'post_password' => $post->post_password, |
| 63 |
'post_name' => $post->post_name, |
| 64 |
'post_parent' => $post->post_parent, |
| 65 |
'post_content_filtered' => $post->post_content_filtered, |
| 66 |
'post_type' => $post->post_type, |
| 67 |
'menu_order' => $post->menu_order, |
| 68 |
'post_mime_type' => $post->post_mime_type, |
| 69 |
]; |
| 70 |
|
| 71 |
if (!empty($options)) { |
| 72 |
foreach ($options as $key => $value) { |
| 73 |
$new_post[$key] = $value; |
| 74 |
} |
| 75 |
} |
| 76 |
return $new_post; |
| 77 |
} |
| 78 |
|
| 79 |
private function copyFilterFields() |
| 80 |
{ |
| 81 |
$fields = get_children([ |
| 82 |
'post_parent' => $this->post_id, |
| 83 |
'post_type' => 'filter-field', |
| 84 |
'numberposts' => -1, |
| 85 |
'post_status' => 'any' |
| 86 |
]); |
| 87 |
|
| 88 |
if ($fields) { |
| 89 |
$parent_filters = []; |
| 90 |
foreach ($fields as $field) { |
| 91 |
$new_field_post_id = wp_insert_post($this->preparePost($field, |
| 92 |
['post_parent' => $this->new_post_id] |
| 93 |
)); |
| 94 |
$post_content = maybe_unserialize($field->post_content); |
| 95 |
if (isset($post_content['parent_filter']) && !empty($post_content['parent_filter']) && (int) $post_content['parent_filter'] > 0) { |
| 96 |
$parent_filters[$new_field_post_id]['post_name'] = $field->post_name; |
| 97 |
$parent_filters[$new_field_post_id]['has_parent_filter_post_id'] = $new_field_post_id; |
| 98 |
} |
| 99 |
$this->copyPostMeta($field->ID, $new_field_post_id); |
| 100 |
} |
| 101 |
if (!empty($parent_filters)) { |
| 102 |
|
| 103 |
foreach ($parent_filters as $parent_filter) { |
| 104 |
if (!empty($parent_filter['post_name']) && !empty($parent_filter['has_parent_filter_post_id'])) { |
| 105 |
$query = new \WP_Query([ |
| 106 |
'post_type' => 'filter-field', |
| 107 |
'post_parent' => $this->post_id, |
| 108 |
'post_status' => 'any', |
| 109 |
'name' => $parent_filter['post_name'], |
| 110 |
'numberposts' => 1 |
| 111 |
]); |
| 112 |
|
| 113 |
if ($query->have_posts()) { |
| 114 |
$parent_filter_posts = $query->posts[0]; |
| 115 |
$filter_parent_id = maybe_unserialize($parent_filter_posts->post_content)['parent_filter']; |
| 116 |
$parent_filter_name = get_post($filter_parent_id); |
| 117 |
if (!empty($parent_filter_name)) { |
| 118 |
$new_query = new \WP_Query([ |
| 119 |
'post_type' => 'filter-field', |
| 120 |
'post_parent' => $this->new_post_id, |
| 121 |
'post_status' => 'any', |
| 122 |
'name' => $parent_filter_name->post_name, |
| 123 |
'numberposts' => 1 |
| 124 |
]); |
| 125 |
if ($new_query->have_posts()) { |
| 126 |
$parent_filter_info = $new_query->posts[0]; |
| 127 |
$post_new = get_post($parent_filter['has_parent_filter_post_id']); |
| 128 |
$post_new->post_content = maybe_unserialize($post_new->post_content); |
| 129 |
$post_new->post_content['parent_filter'] = (string) $parent_filter_info->ID; |
| 130 |
$post_new->post_content = maybe_serialize($post_new->post_content); |
| 131 |
wp_update_post(wp_slash($post_new)); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private function copyPostMeta($old_post_id, $new_post_id) |
| 142 |
{ |
| 143 |
$meta = get_post_meta($old_post_id); |
| 144 |
foreach ($meta as $key => $values) { |
| 145 |
foreach ($values as $value) { |
| 146 |
add_post_meta($new_post_id, $key, maybe_unserialize($value)); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |