| @@ -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,26 +28,25 @@ | ||
| 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. | |
| 40 | - | |
| 41 | 36 | // Check Access for User roles |
| 42 | - add_action('admin_init', [$this, 'post_duplicator_init']); | |
| 37 | + $restrict_for = !empty($this->options['adminify_clone_post_user_roles']) ? $this->options['adminify_clone_post_user_roles'] : ''; | |
| 38 | + if ($restrict_for) { | |
| 39 | + return; | |
| 40 | + } | |
| 41 | + add_action('admin_init', [$this, 'adminify_post_duplicator_init']); | |
| 43 | 42 | } |
| 44 | 43 | |
| 45 | - public function post_duplicator_init() | |
| 44 | + public function adminify_post_duplicator_init() | |
| 46 | 45 | { |
| 47 | 46 | $post_types = []; |
| 48 | - if (!empty($this->options['post_duplicator']['post_types'])) { | |
| 49 | - $post_types = $this->options['post_duplicator']['post_types']; | |
| 47 | + if (!empty($this->options['adminify_clone_post_posts'])) { | |
| 48 | + $post_types = $this->options['adminify_clone_post_posts']; | |
| 50 | 49 | } |
| 51 | 50 | $all_post_types = get_post_types(); |
| 52 | 51 | |
| 53 | 52 | if (empty($post_types) || empty(array_intersect($all_post_types, $post_types))) { |
| @@ -52,19 +51,15 @@ | ||
| 52 | 51 | |
| 53 | 52 | if (empty($post_types) || empty(array_intersect($all_post_types, $post_types))) { |
| 54 | 53 | return; |
| 55 | 54 | } |
| 56 | - | |
| 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); | |
| 60 | 55 | |
| 61 | - | |
| 62 | - // foreach ($post_types as $key => $post_type) { | |
| 63 | - // if (in_array($post_type, $all_post_types)) { | |
| 64 | - // add_filter($post_type . '_row_actions', [$this, 'duplicator_row_actions'], 10, 2); | |
| 65 | - // } | |
| 66 | - // } | |
| 56 | + add_action('admin_action_adminify_duplicate', [$this, 'adminify_duplicate_post_as_draft']); | |
| 57 | + foreach ($post_types as $key => $post_type) { | |
| 58 | + if (in_array($post_type, $all_post_types)) { | |
| 59 | + add_filter($post_type . '_row_actions', [$this, 'jltwp_adminify_duplicator_row_actions'], 10, 2); | |
| 60 | + } | |
| 61 | + } | |
| 67 | 62 | } |
| 68 | 63 | |
| 69 | 64 | |
| 70 | 65 | /** |
| @@ -96,26 +91,20 @@ | ||
| 96 | 91 | |
| 97 | 92 | /* |
| 98 | 93 | * Add the duplicate link to action list for post_row_actions |
| 99 | 94 | */ |
| 100 | - public function duplicator_row_actions($actions, $post) | |
| 95 | + public function jltwp_adminify_duplicator_row_actions($actions, $post) | |
| 101 | 96 | { |
| 102 | - $post_type = $post->post_type; | |
| 103 | - $allowed_post_types = $post_types = $this->options['post_duplicator']['post_types']; | |
| 104 | - if( !in_array($post_type, $allowed_post_types)){ | |
| 97 | + if (!$this->user_can_clone($post)) { | |
| 105 | 98 | return $actions; |
| 106 | 99 | } |
| 107 | - if (!$this->user_can_clone($post)) { | |
| 108 | - return $actions; | |
| 109 | - } | |
| 110 | - | |
| 100 | + | |
| 111 | 101 | $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'); | |
| 102 | + $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'jltwp_adminify_post_duplicator_nonce'); | |
| 113 | 103 | |
| 114 | 104 | $clone_title = !empty($this->clone_title) ? $this->clone_title : 'Adminify'; |
| 115 | 105 | $clone_title = preg_replace('/wp/i', '', $clone_title) . ' Clone'; |
| 116 | 106 | $duplicate_title = 'Duplicate this item ' . $post->post_title; |
| 117 | - /* translators: 1: Duplicate post URL, 2: Link title attribute, 3: Link text */ | |
| 118 | 107 | $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 | 108 | return $actions; |
| 120 | 109 | } |
| 121 | 110 | |
| @@ -122,9 +111,9 @@ | ||
| 122 | 111 | |
| 123 | 112 | /* |
| 124 | 113 | * Function creates post duplicate as a draft and redirects then to the edit post screen |
| 125 | 114 | */ |
| 126 | - public function duplicate_post_as_draft() | |
| 115 | + public function adminify_duplicate_post_as_draft() | |
| 127 | 116 | { |
| 128 | 117 | global $wpdb; |
| 129 | 118 | |
| 130 | 119 | if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) { |
| @@ -134,9 +123,9 @@ | ||
| 134 | 123 | /* |
| 135 | 124 | * Nonce verification |
| 136 | 125 | * Return if nonce is not valid |
| 137 | 126 | */ |
| 138 | - if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'pxlbsadminify_post_duplicator_nonce')) { | |
| 127 | + if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'jltwp_adminify_post_duplicator_nonce')) { | |
| 139 | 128 | return; |
| 140 | 129 | } |
| 141 | 130 | |
| 142 | 131 | /* |
| @@ -146,15 +135,9 @@ | ||
| 146 | 135 | /* |
| 147 | 136 | * and all the original post data then |
| 148 | 137 | */ |
| 149 | 138 | $post = get_post($post_id); |
| 150 | - if( isset( $_SERVER['HTTP_REFERER']) && str_contains( esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ), 'action=adminify_duplicate' )) { | |
| 151 | - $redirect_url = admin_url('edit.php?post_type=' . $post->post_type); | |
| 152 | - wp_safe_redirect($redirect_url); | |
| 153 | 139 | |
| 154 | - // exit; | |
| 155 | - } | |
| 156 | - | |
| 157 | 140 | if (!$this->user_can_clone($post)) { |
| 158 | 141 | return; |
| 159 | 142 | } |
| 160 | 143 | |
| @@ -198,10 +181,9 @@ | ||
| 198 | 181 | |
| 199 | 182 | /* |
| 200 | 183 | * duplicate all post meta just in two SQL queries |
| 201 | 184 | */ |
| 202 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- direct query required for copying all post meta rows; not cached intentionally. | |
| 203 | - $post_meta_infos = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=%d", $post_id)); | |
| 185 | + $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"); | |
| 204 | 186 | if (count($post_meta_infos) != 0) { |
| 205 | 187 | $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) "; |
| 206 | 188 | foreach ($post_meta_infos as $meta_info) { |
| 207 | 189 | $meta_key = $meta_info->meta_key; |
| @@ -222,9 +204,8 @@ | ||
| 222 | 204 | $meta_value = $meta_info->meta_value; |
| 223 | 205 | $sql_query_sel[] = $wpdb->prepare('SELECT %d, %s, %s', $new_post_id, $meta_key, $meta_value); |
| 224 | 206 | } |
| 225 | 207 | $sql_query .= implode(' UNION ALL ', $sql_query_sel); |
| 226 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter -- $sql_query is built from $wpdb->prepare() fragments above; direct query required for bulk meta insert, not cached intentionally. | |
| 227 | 208 | $wpdb->query($sql_query); |
| 228 | 209 | } |
| 229 | 210 | |
| 230 | 211 | /* |