PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.0.4
Adminify – White Label, Admin Menu Editor, Login Customizer v3.0.4
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / Inc / Modules / PostDuplicator / PostDuplicator.php

PostDuplicator.php in Adminify – White Label, Admin Menu Editor, Login Customizer 3.0.4, at Inc/Modules/PostDuplicator/PostDuplicator.php

166 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPAdminify\Inc\Modules\PostDuplicator;
4
5 use WPAdminify\Inc\Utils;
6 use WPAdminify\Inc\Admin\AdminSettings;
7 // no direct access allowed
8 if (!defined('ABSPATH')) exit;
9
10 /**
11 * WP Adminify
12 * @package Duplicate Post/Page/Custom Posts
13 *
14 * @author Jewel Theme <support@jeweltheme.com>
15 */
16
17
18 if (!defined('ABSPATH')) exit; // If this file is called directly, abort.
19
20 class PostDuplicator
21 {
22
23 private $clone_title;
24
25 public function __construct()
26 {
27 $this->clone_title = AdminSettings::get_instance()->get('jltwp_adminify_wl_plugin_menu_label');
28 add_action('admin_action_adminify_duplicate', [$this, 'adminify_duplicate_post_as_draft']);
29 add_filter('post_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
30 add_filter('page_row_actions', [$this, 'jltma_duplicator_row_actions'], 10, 2);
31 }
32
33
34 /*
35 * Add the duplicate link to action list for post_row_actions
36 */
37 public function jltma_duplicator_row_actions($actions, $post)
38 {
39 if (current_user_can('edit_posts')) {
40
41 $adminify_duplicate_link = admin_url('admin.php?action=adminify_duplicate&post=' . $post->ID);
42 $adminify_duplicate_link = wp_nonce_url($adminify_duplicate_link, 'jltma_post_duplicator_nonce');
43
44 $clone_title = !empty($this->clone_title) ? $this->clone_title : 'Adminify';
45 $clone_title = preg_replace('/wp/i', '', $clone_title) . ' Clone';
46 $duplicate_title = 'Duplicate this item ' . $post->post_title;
47 $actions['adminify_duplicate'] = sprintf(__('<a href="%s" title="%s" rel="permalink">%s</a>','adminify'), $adminify_duplicate_link, $duplicate_title, $clone_title);
48 }
49 return $actions;
50 }
51
52
53 /*
54 * Function creates post duplicate as a draft and redirects then to the edit post screen
55 */
56 public function adminify_duplicate_post_as_draft()
57 {
58
59 global $wpdb;
60
61 if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'adminify_duplicate' == $_REQUEST['action']))) {
62 wp_die('No post to duplicate has been supplied!');
63 }
64
65 /*
66 * Nonce verification
67 * Return if nonce is not valid
68 */
69 if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'jltma_post_duplicator_nonce')) {
70 return;
71 }
72
73 /*
74 * get the original post id
75 */
76 $post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
77 /*
78 * and all the original post data then
79 */
80 $post = get_post($post_id);
81
82 /*
83 * if you don't want current user to be the new post author,
84 * then change next couple of lines to this: $new_post_author = $post->post_author;
85 */
86 $current_user = wp_get_current_user();
87 $new_post_author = $current_user->ID;
88
89 /*
90 * if post data exists, create the post duplicate
91 */
92 if (isset($post) && $post != null) {
93
94 /*
95 * new post data array
96 */
97 $args = array(
98 'post_title' => $post->post_title,
99 'post_type' => $post->post_type,
100 'post_content' => $post->post_content,
101 'post_excerpt' => $post->post_excerpt,
102 'post_author' => $new_post_author,
103 'comment_status' => $post->comment_status,
104 'ping_status' => $post->ping_status,
105 'post_name' => $post->post_name,
106 'post_parent' => $post->post_parent,
107 'post_password' => $post->post_password,
108 'post_status' => 'draft',
109 'to_ping' => $post->to_ping,
110 'menu_order' => $post->menu_order
111 );
112
113 /*
114 * insert the post by wp_insert_post() function
115 */
116 $new_post_id = wp_insert_post($args);
117
118 /*
119 * get all current post terms ad set them to the new post draft
120 */
121 $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
122 foreach ($taxonomies as $taxonomy) {
123 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
124 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
125 }
126
127 /*
128 * duplicate all post meta just in two SQL queries
129 */
130 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
131 if (count($post_meta_infos) != 0) {
132 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
133 foreach ($post_meta_infos as $meta_info) {
134 $meta_key = $meta_info->meta_key;
135
136 // Do not copy these values
137 if ($meta_key == '_wp_old_slug') continue;
138 if ($meta_key == '_elementor_css') continue;
139
140 // Delete already added meta data triggered by wp_insert_post hook
141 if ($meta_key == '_elementor_template_type') {
142 delete_post_meta( $new_post_id, '_elementor_template_type' );
143 }
144
145 $meta_value = $meta_info->meta_value;
146 $sql_query_sel[] = $wpdb->prepare("SELECT %d, %s, %s", $new_post_id, $meta_key, $meta_value);
147 }
148 $sql_query .= implode(" UNION ALL ", $sql_query_sel);
149 $wpdb->query($sql_query);
150 }
151
152
153
154 /*
155 * finally, redirect to the edit post screen for the new draft
156 */
157 $redirect_url = admin_url('edit.php?post_type=' . $post->post_type);
158 wp_safe_redirect($redirect_url);
159
160 exit;
161 } else {
162 wp_die('Post creation failed, could not find original post: ' . $post_id);
163 }
164 }
165 }
166