| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace PXLBSAdminify\Inc\Modules\PostDuplicator; | |
| 3 | +namespace WPAdminify\Inc\Modules\PostDuplicator; | |
| 4 | 4 | |
| 5 | -use PXLBSAdminify\Inc\Admin\AdminSettings; | |
| 5 | +use WPAdminify\Inc\Admin\AdminSettings; | |
| 6 | 6 | // no direct access allowed |
| 7 | 7 | if (!defined('ABSPATH')) { |
| 8 | 8 | exit; |
| 9 | 9 | } |
| @@ -28,22 +28,19 @@ | ||
| 28 | 28 | public $options = []; |
| 29 | 29 | |
| 30 | 30 | public function __construct() |
| 31 | 31 | { |
| 32 | - $this->clone_title = AdminSettings::get_instance()->get('pxlbsadminify_wl_plugin_menu_label'); | |
| 32 | + $this->clone_title = AdminSettings::get_instance()->get('jltwp_adminify_wl_plugin_menu_label'); | |
| 33 | 33 | |
| 34 | 34 | $this->options = (array) AdminSettings::get_instance()->get(); |
| 35 | 35 | |
| 36 | - // Taxonomy duplicator (reads post_duplicator.taxonomies) is a | |
| 37 | - // Pro-only feature. Pro/Classes/Filters.php registers the | |
| 38 | - // admin_action and *_row_actions filters when the Pro plugin | |
| 39 | - // is active. | |
| 36 | + new TaxonomyDuplicator(); | |
| 40 | 37 | |
| 41 | 38 | // Check Access for User roles |
| 42 | - add_action('admin_init', [$this, 'post_duplicator_init']); | |
| 39 | + add_action('admin_init', [$this, 'adminify_post_duplicator_init']); | |
| 43 | 40 | } |
| 44 | 41 | |
| 45 | - public function post_duplicator_init() | |
| 42 | + public function adminify_post_duplicator_init() | |
| 46 | 43 | { |
| 47 | 44 | $post_types = []; |
| 48 | 45 | if (!empty($this->options['post_duplicator']['post_types'])) { |
| 49 | 46 | $post_types = $this->options['post_duplicator']['post_types']; |
| @@ -53,16 +50,16 @@ | ||
| 53 | 50 | if (empty($post_types) || empty(array_intersect($all_post_types, $post_types))) { |
| 54 | 51 | return; |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | - add_action('admin_action_adminify_duplicate', [$this, 'duplicate_post_as_draft']); | |
| 58 | - add_filter('post_row_actions', [$this, 'duplicator_row_actions'], 10, 2); | |
| 59 | - add_filter('page_row_actions', [$this, 'duplicator_row_actions'], 10, 2); | |
| 54 | + add_action('admin_action_adminify_duplicate', [$this, 'adminify_duplicate_post_as_draft']); | |
| 55 | + add_filter('post_row_actions', [$this, 'jltwp_adminify_duplicator_row_actions'], 10, 2); | |
| 56 | + add_filter('page_row_actions', [$this, 'jltwp_adminify_duplicator_row_actions'], 10, 2); | |
| 60 | 57 | |
| 61 | 58 | |
| 62 | 59 | // foreach ($post_types as $key => $post_type) { |
| 63 | 60 | // if (in_array($post_type, $all_post_types)) { |
| 64 | - // add_filter($post_type . '_row_actions', [$this, 'duplicator_row_actions'], 10, 2); | |
| 61 | + // add_filter($post_type . '_row_actions', [$this, 'jltwp_adminify_duplicator_row_actions'], 10, 2); | |
| 65 | 62 | // } |
| 66 | 63 | // } |
| 67 | 64 | } |
| 68 | 65 | |
| @@ -96,9 +93,9 @@ | ||
| 96 | 93 | |
| 97 | 94 | /* |
| 98 | 95 | * Add the duplicate link to action list for post_row_actions |
| 99 | 96 | */ |
| 100 | - public function duplicator_row_actions($actions, $post) | |
| 97 | + public function jltwp_adminify_duplicator_row_actions($actions, $post) | |
| 101 | 98 | { |
| 102 | 99 | $post_type = $post->post_type; |
| 103 | 100 | $allowed_post_types = $post_types = $this->options['post_duplicator']['post_types']; |
| 104 | 101 | if( !in_array($post_type, $allowed_post_types)){ |
| @@ -108,14 +105,13 @@ | ||
| 108 | 105 | return $actions; |
| 109 | 106 | } |
| 110 | 107 | |
| 111 | 108 | $adminify_duplicate_link = admin_url('admin.php?action=adminify_duplicate&post=' . $post->ID); |
| 112 | - $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'pxlbsadminify_post_duplicator_nonce'); | |
| 109 | + $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'jltwp_adminify_post_duplicator_nonce'); | |
| 113 | 110 | |
| 114 | 111 | $clone_title = !empty($this->clone_title) ? $this->clone_title : 'Adminify'; |
| 115 | 112 | $clone_title = preg_replace('/wp/i', '', $clone_title) . ' Clone'; |
| 116 | 113 | $duplicate_title = 'Duplicate this item ' . $post->post_title; |
| 117 | - /* translators: 1: Duplicate post URL, 2: Link title attribute, 3: Link text */ | |
| 118 | 114 | $actions['adminify_duplicate'] = sprintf(__('<a href="%1$s" title="%2$s" rel="permalink">%3$s</a>', 'adminify'), $adminify_duplicate_link, $duplicate_title, $clone_title); |
| 119 | 115 | return $actions; |
| 120 | 116 | } |
| 121 | 117 | |
| @@ -122,9 +118,9 @@ | ||
| 122 | 118 | |
| 123 | 119 | /* |
| 124 | 120 | * Function creates post duplicate as a draft and redirects then to the edit post screen |
| 125 | 121 | */ |
| 126 | - public function duplicate_post_as_draft() | |
| 122 | + public function adminify_duplicate_post_as_draft() | |
| 127 | 123 | { |
| 128 | 124 | global $wpdb; |
| 129 | 125 | |
| 130 | 126 | if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) { |
| @@ -134,9 +130,9 @@ | ||
| 134 | 130 | /* |
| 135 | 131 | * Nonce verification |
| 136 | 132 | * Return if nonce is not valid |
| 137 | 133 | */ |
| 138 | - if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'pxlbsadminify_post_duplicator_nonce')) { | |
| 134 | + if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'jltwp_adminify_post_duplicator_nonce')) { | |
| 139 | 135 | return; |
| 140 | 136 | } |
| 141 | 137 | |
| 142 | 138 | /* |
| @@ -146,13 +142,13 @@ | ||
| 146 | 142 | /* |
| 147 | 143 | * and all the original post data then |
| 148 | 144 | */ |
| 149 | 145 | $post = get_post($post_id); |
| 150 | - if( isset( $_SERVER['HTTP_REFERER']) && str_contains( esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ), 'action=adminify_duplicate' )) { | |
| 146 | + if( isset( $_SERVER['HTTP_REFERER']) && str_contains( $_SERVER['HTTP_REFERER'], 'action=adminify_duplicate' )) { | |
| 151 | 147 | $redirect_url = admin_url('edit.php?post_type=' . $post->post_type); |
| 152 | 148 | wp_safe_redirect($redirect_url); |
| 153 | 149 | |
| 154 | - // exit; | |
| 150 | + exit; | |
| 155 | 151 | } |
| 156 | 152 | |
| 157 | 153 | if (!$this->user_can_clone($post)) { |
| 158 | 154 | return; |
| @@ -196,20 +192,23 @@ | ||
| 196 | 192 | wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); |
| 197 | 193 | } |
| 198 | 194 | |
| 199 | 195 | /* |
| 200 | - * duplicate all post meta | |
| 201 | - * get_post_meta() without a key returns raw (still serialized) values, | |
| 202 | - * so rows are copied byte-for-byte via parameterized $wpdb->insert(). | |
| 196 | + * duplicate all post meta just in two SQL queries | |
| 203 | 197 | */ |
| 204 | - $new_post_id = absint($new_post_id); | |
| 205 | - $post_metas = $new_post_id ? get_post_meta($post_id) : []; | |
| 206 | - if (!empty($post_metas) && is_array($post_metas)) { | |
| 207 | - foreach ($post_metas as $meta_key => $meta_values) { | |
| 198 | + $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"); | |
| 199 | + if (count($post_meta_infos) != 0) { | |
| 200 | + $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; | |
| 201 | + foreach ($post_meta_infos as $meta_info) { | |
| 202 | + $meta_key = $meta_info->meta_key; | |
| 203 | + | |
| 208 | 204 | // Do not copy these values |
| 209 | - if (in_array($meta_key, ['_wp_old_slug', '_elementor_css'], true)) { | |
| 205 | + if ($meta_key == '_wp_old_slug') { | |
| 210 | 206 | continue; |
| 211 | 207 | } |
| 208 | + if ($meta_key == '_elementor_css') { | |
| 209 | + continue; | |
| 210 | + } | |
| 212 | 211 | |
| 213 | 212 | // Delete already added meta data triggered by wp_insert_post hook |
| 214 | 213 | if ($meta_key == '_elementor_template_type') { |
| 215 | 214 | delete_post_meta($new_post_id, '_elementor_template_type'); |
| @@ -214,22 +213,13 @@ | ||
| 214 | 213 | if ($meta_key == '_elementor_template_type') { |
| 215 | 214 | delete_post_meta($new_post_id, '_elementor_template_type'); |
| 216 | 215 | } |
| 217 | 216 | |
| 218 | - foreach ((array) $meta_values as $meta_value) { | |
| 219 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- raw copy keeps serialized values intact; $wpdb->insert() is parameterized. | |
| 220 | - $wpdb->insert( | |
| 221 | - $wpdb->postmeta, | |
| 222 | - [ | |
| 223 | - 'post_id' => $new_post_id, | |
| 224 | - 'meta_key' => $meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_query_meta_key | |
| 225 | - 'meta_value' => $meta_value, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_query_meta_value | |
| 226 | - ], | |
| 227 | - ['%d', '%s', '%s'] | |
| 228 | - ); | |
| 229 | - } | |
| 217 | + $meta_value = $meta_info->meta_value; | |
| 218 | + $sql_query_sel[] = $wpdb->prepare('SELECT %d, %s, %s', $new_post_id, $meta_key, $meta_value); | |
| 230 | 219 | } |
| 231 | - wp_cache_delete($new_post_id, 'post_meta'); | |
| 220 | + $sql_query .= implode(' UNION ALL ', $sql_query_sel); | |
| 221 | + $wpdb->query($sql_query); | |
| 232 | 222 | } |
| 233 | 223 | |
| 234 | 224 | /* |
| 235 | 225 | * finally, redirect to the edit post screen for the new draft |