PluginProbe ʕ •ᴥ•ʔ
WP Post Page Clone / trunk
WP Post Page Clone vtrunk
trunk 1.0 1.1 1.2 1.3
wp-post-page-clone / wp-post-page-clone.php
wp-post-page-clone Last commit date
languages 1 year ago index.php 9 years ago readme.txt 1 month ago wp-post-page-clone.php 1 year ago
wp-post-page-clone.php
194 lines
1 <?php
2 /*
3 Plugin Name: WP Post Page Clone
4 Plugin URI: https://wordpress.org/plugins/wp-post-page-clone
5 Description: A plugin to generate duplicate post or page with contents and it's meta fields and other required settings.
6 Version: 1.3
7 Author: Gaurang Sondagar
8 Author URI: http://gaurangsondagar99.wordpress.com/
9 License: GPLv2 or later
10 License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 Text Domain: wp-post-page-clone
12 /**
13 * Exit if accessed directly
14 */
15 if (!defined('ABSPATH')) {
16 exit;
17 }
18
19 /**
20 * Define Constant variables
21 */
22 if (!defined('WP_POST_PAGE_CLONE_URL')) {
23 define('WP_POST_PAGE_CLONE_URL', plugins_url() . '/wp-post-page-clone');
24 }
25
26 if (!defined('WP_POST_PAGE_CLONE_PLUGIN_DIRNAME')) {
27 define('WP_POST_PAGE_CLONE_PLUGIN_DIRNAME', plugin_basename(dirname(__FILE__)));
28 }
29
30 if(!function_exists('wp_post_page_clone_translate')) {
31 /**
32 * Function for language translations
33 */
34 function wp_post_page_clone_translate() {
35
36 load_plugin_textdomain('wp-post-page-clone', false, WP_POST_PAGE_CLONE_PLUGIN_DIRNAME . '/languages' );
37
38 }
39
40 }
41
42 add_action( 'plugins_loaded', 'wp_post_page_clone_translate' );
43
44
45 if(!function_exists('wp_post_page_clone')) {
46 /**
47 * Function for post / page clone and redirect that post
48 * @global type $wpdb
49 */
50 function wp_post_page_clone(){
51
52 global $wpdb;
53
54 /*
55 * get Nonce value
56 */
57 $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ) : '';
58
59 // Safely retrieve post ID
60 if ( isset( $_GET['post'] ) ) {
61 $post_id = intval( $_GET['post'] );
62 } elseif ( isset( $_POST['post'] ) ) {
63 $post_id = intval( $_POST['post'] );
64 } else {
65 $post_id = 0; // Or handle as invalid
66 }
67
68 // check access permissions to even consider the cloning....
69 if( ! wp_verify_nonce( $nonce, 'wp-post-page-clone-'.$post_id) || ! current_user_can( 'edit_posts' )) {
70 wp_die( esc_html__( 'You do not have permission to be here', 'wp-post-page-clone' ) );
71 }
72
73 if ( !isset( $_GET['post']) || (!isset($_REQUEST['action']) && 'wp_post_page_clone' != $_REQUEST['action'] ) ) {
74 wp_die( esc_html__( 'No post or page to clone has been supplied!, Please try again!', 'wp-post-page-clone' ) );
75 }
76
77 $post = get_post( $post_id );
78 $current_user = wp_get_current_user();
79 $post_author = $current_user->ID;
80
81 $allowed_roles = array( 'editor', 'administrator' );
82 if ($post->post_author == $current_user->ID || array_intersect( $allowed_roles, $current_user->roles ) || (current_user_can( 'edit_post', $post->ID ))) {
83
84 if (isset( $post ) && $post != null) {
85
86 $args = array(
87 'comment_status' => $post->comment_status,
88 'ping_status' => $post->ping_status,
89 'post_author' => $post_author,
90 'post_content' => $post->post_content,
91 'post_excerpt' => $post->post_excerpt,
92 'post_name' => $post->post_name,
93 'post_parent' => $post->post_parent,
94 'post_password' => $post->post_password,
95 'post_status' => 'draft',
96 'post_title' => $post->post_title,
97 'post_type' => $post->post_type,
98 'to_ping' => $post->to_ping,
99 'menu_order' => $post->menu_order
100 );
101
102 $clone_post_id = wp_insert_post( $args );
103 if ( is_wp_error( $clone_post_id ) ) {
104 wp_die( esc_html( $clone_post_id->get_error_message() ) );
105 }
106
107
108 /*
109 * get and set terms to the new post draft
110 */
111 $taxonomies = array_map('sanitize_text_field',get_object_taxonomies($post->post_type));
112 if (!empty($taxonomies) && is_array($taxonomies)){
113 foreach ($taxonomies as $taxonomy) {
114 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
115 wp_set_object_terms($clone_post_id, $post_terms, $taxonomy, false);
116 }
117 }
118
119 /*
120 * clone all post meta
121 */
122 $post_meta_keys = get_post_custom_keys( $post_id );
123 if(!empty($post_meta_keys)){
124 foreach ( $post_meta_keys as $meta_key ) {
125 $meta_values = get_post_custom_values( $meta_key, $post_id );
126 foreach ( $meta_values as $meta_value ) {
127 $meta_value = maybe_unserialize( $meta_value );
128 update_post_meta( $clone_post_id, $meta_key, wp_slash( $meta_value ) );
129 }
130 }
131 }
132
133 /**
134 * Added plugin's compatibility with Elementor plugin
135 */
136 if(is_plugin_active( 'elementor/elementor.php' )){
137 $elm = Elementor\Core\Files\CSS\Post::create( $clone_post_id );
138 $elm->update();
139 }
140
141 wp_redirect(admin_url('edit.php?post_type='.$post->post_type));
142 exit;
143
144 } else {
145
146 wp_die( esc_html__( 'Post or Page creation failed, could not find original post:', 'wp-post-page-clone' ) . ' ' . esc_html( $post_id ) );
147
148
149 }
150
151 } else {
152 wp_die( esc_html__( 'Security issue occure, Please try again!.', 'wp-post-page-clone' ) );
153
154 }
155
156 }
157
158 }
159
160 add_action( 'admin_action_wp_post_page_clone', 'wp_post_page_clone' );
161
162
163 if(!function_exists('wp_post_page_link')) {
164
165 /**
166 * Add the duplicate link to action list for post_row_actions
167 * @param string $actions
168 * @param type $post
169 * @return string
170 */
171 function wp_post_page_link( $actions, $post ) {
172
173 // Remove support for acf-field-group post type
174 if($post->post_type == 'acf-field-group'){
175 return $actions;
176 }
177
178 $current_user = wp_get_current_user();
179 $post_author = $current_user->ID;
180 $allowed_roles = array( 'editor', 'administrator' );
181 if ($post->post_author == $current_user->ID || array_intersect( $allowed_roles, $current_user->roles ) || (current_user_can( 'edit_post', $post->ID )) ) {
182 $actions['clone'] = '<a '.$post_author.'==='.$post->post_author.' href="admin.php?action=wp_post_page_clone&amp;post=' . $post->ID . '&amp;nonce='.wp_create_nonce( 'wp-post-page-clone-'.$post->ID ).'" title="'.__('Clone Post and Page', 'wp-post-page-clone').'" rel="permalink">'.__('Click To Clone', 'wp-post-page-clone').'</a>';
183 }
184
185 return $actions;
186 }
187
188 }
189
190 /**
191 * Filter for post / page row actions
192 */
193 add_filter( 'post_row_actions', 'wp_post_page_link', 10, 2 );
194 add_filter('page_row_actions', 'wp_post_page_link', 10, 2);