PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 3.2.1
Adminify – White Label, Admin Menu Editor, Login Customizer v3.2.1
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.2.1, at Inc/Modules/PostDuplicator/PostDuplicator.php

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