PluginProbe ʕ •ᴥ•ʔ
Duplicate Page / 2.5
Duplicate Page v2.5
trunk 1.1 2.5 4.4.9 4.5
duplicate-page / duplicatepage.php
duplicate-page Last commit date
languages 8 years ago admin-settings.php 8 years ago duplicatepage.php 8 years ago readme.txt 8 years ago
duplicatepage.php
242 lines
1 <?php
2 /*
3 Plugin Name: Duplicate Page
4 Plugin URI: https://wordpress.org/plugins/duplicate-page/
5 Description: Duplicate Posts, Pages and Custom Posts using single click.
6 Author: mndpsingh287
7 Version: 2.5
8 Author URI: https://profiles.wordpress.org/mndpsingh287/
9 License: GPLv2
10 Text Domain: duplicate-page
11 */
12 if (!defined("DUPLICATE_PAGE_PLUGIN_DIRNAME")) define("DUPLICATE_PAGE_PLUGIN_DIRNAME", plugin_basename(dirname(__FILE__)));
13 if(!class_exists('duplicate_page')):
14 class duplicate_page
15 {
16 /*
17 * AutoLoad Hooks
18 */
19 public function __construct(){
20 register_activation_hook(__FILE__, array(&$this, 'duplicate_page_install'));
21 add_action('admin_menu', array(&$this, 'duplicate_page_options_page'));
22 add_filter( 'plugin_action_links', array(&$this, 'duplicate_page_plugin_action_links'), 10, 2 );
23 add_action( 'admin_action_dt_duplicate_post_as_draft', array(&$this,'dt_duplicate_post_as_draft') );
24 add_filter( 'post_row_actions', array(&$this,'dt_duplicate_post_link'), 10, 2);
25 add_filter( 'page_row_actions', array(&$this,'dt_duplicate_post_link'), 10, 2);
26 add_action( 'post_submitbox_misc_actions', array(&$this,'duplicate_page_custom_button'));
27 add_action( 'wp_before_admin_bar_render', array(&$this, 'duplicate_page_admin_bar_link'));
28 add_action('init', array(&$this, 'duplicate_page_load_text_domain'));
29 }
30 /*
31 * Localization - 19-dec-2016
32 */
33 public function duplicate_page_load_text_domain(){
34 load_plugin_textdomain('duplicate-page', false, DUPLICATE_PAGE_PLUGIN_DIRNAME . "/languages");
35 }
36 /*
37 * Activation Hook
38 */
39 public function duplicate_page_install(){
40 $defaultsettings = array(
41 'duplicate_post_status' => 'draft',
42 'duplicate_post_redirect' => 'to_list',
43 'duplicate_post_suffix' => ''
44 );
45 $opt = get_option('duplicate_page_options');
46 if(!$opt['duplicate_post_status']) {
47 update_option('duplicate_page_options', $defaultsettings);
48 }
49 }
50 /*
51 Action Links
52 */
53 public function duplicate_page_plugin_action_links($links, $file){
54 if ( $file == plugin_basename( __FILE__ ) ) {
55 $duplicate_page_links = '<a href="'.get_admin_url().'options-general.php?page=duplicate_page_settings">'.__('Settings', 'duplicate-page').'</a>';
56 $duplicate_page_donate = '<a href="http://www.webdesi9.com/donate/?plugin=duplicate-page" title="Donate Now" target="_blank" style="font-weight:bold">'.__('Donate', 'duplicate-page').'</a>';
57 array_unshift( $links, $duplicate_page_donate );
58 array_unshift( $links, $duplicate_page_links );
59 }
60
61 return $links;
62 }
63 /*
64 * Admin Menu
65 */
66 public function duplicate_page_options_page(){
67 add_options_page( __( 'Duplicate Page', 'duplicate-page' ), __( 'Duplicate Page', 'duplicate-page' ), 'manage_options', 'duplicate_page_settings',array(&$this, 'duplicate_page_settings'));
68 }
69 /*
70 * Duplicate Page Admin Settings
71 */
72 public function duplicate_page_settings(){
73 if(current_user_can( 'manage_options' )){
74 include('admin-settings.php');
75 }
76 }
77 /*
78 * Main function
79 */
80 public function dt_duplicate_post_as_draft(){
81 global $wpdb;
82 $opt = get_option('duplicate_page_options');
83 $suffix = !empty($opt['duplicate_post_suffix']) ? ' -- '.$opt['duplicate_post_suffix'] : '';
84 $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
85 $redirectit = !empty($opt['duplicate_post_redirect']) ? $opt['duplicate_post_redirect'] : 'to_list';
86 if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'dt_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
87 wp_die('No post to duplicate has been supplied!');
88 }
89 $returnpage = '';
90 /*
91 * get the original post id
92 */
93 $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
94 /*
95 * and all the original post data then
96 */
97 $post = get_post( $post_id );
98 /*
99 * if you don't want current user to be the new post author,
100 * then change next couple of lines to this: $new_post_author = $post->post_author;
101 */
102 $current_user = wp_get_current_user();
103 $new_post_author = $current_user->ID;
104 /*
105 * if post data exists, create the post duplicate
106 */
107 if (isset( $post ) && $post != null) {
108 /*
109 * new post data array
110 */
111 $args = array(
112 'comment_status' => $post->comment_status,
113 'ping_status' => $post->ping_status,
114 'post_author' => $new_post_author,
115 'post_content' => $post->post_content,
116 'post_excerpt' => $post->post_excerpt,
117 //'post_name' => $post->post_name,
118 'post_parent' => $post->post_parent,
119 'post_password' => $post->post_password,
120 'post_status' => $post_status,
121 'post_title' => $post->post_title.$suffix,
122 'post_type' => $post->post_type,
123 'to_ping' => $post->to_ping,
124 'menu_order' => $post->menu_order
125 );
126 /*
127 * insert the post by wp_insert_post() function
128 */
129 $new_post_id = wp_insert_post( $args );
130 /*
131 * get all current post terms ad set them to the new post draft
132 */
133 $taxonomies = get_object_taxonomies($post->post_type);
134 if(!empty($taxonomies) && is_array($taxonomies)):
135 foreach ($taxonomies as $taxonomy) {
136 $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
137 wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
138 }
139 endif;
140 /*
141 * duplicate all post meta
142 */
143 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
144 if (count($post_meta_infos)!=0) {
145 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
146 foreach ($post_meta_infos as $meta_info) {
147 $meta_key = $meta_info->meta_key;
148 $meta_value = addslashes($meta_info->meta_value);
149 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
150 }
151 $sql_query.= implode(" UNION ALL ", $sql_query_sel);
152 $wpdb->query($sql_query);
153 }
154 /*
155 * finally, redirecting to your choice
156 */
157 if($post->post_type != 'post'):
158 $returnpage = '?post_type='.$post->post_type;
159 endif;
160 if(!empty($redirectit) && $redirectit == 'to_list'):
161 wp_redirect( admin_url( 'edit.php'.$returnpage ) );
162 elseif(!empty($redirectit) && $redirectit == 'to_page'):
163 wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
164 else:
165 wp_redirect( admin_url( 'edit.php'.$returnpage ) );
166 endif;
167 exit;
168 } else {
169 wp_die('Error! Post creation failed, could not find original post: ' . $post_id);
170 }
171 }
172 /*
173 * Add the duplicate link to action list for post_row_actions
174 */
175 public function dt_duplicate_post_link( $actions, $post ) {
176 $opt = get_option('duplicate_page_options');
177 $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
178 if (current_user_can('edit_posts')) {
179 $actions['duplicate'] = '<a href="admin.php?action=dt_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this as '.$post_status.'" rel="permalink">'.__( "Duplicate This", "duplicate-page" ).'</a>';
180 }
181 return $actions;
182 }
183 /*
184 * Add the duplicate link to edit screen
185 */
186 public function duplicate_page_custom_button(){
187 global $post;
188 $opt = get_option('duplicate_page_options');
189 $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
190 $html = '<div id="major-publishing-actions">';
191 $html .= '<div id="export-action">';
192 $html .= '<a href="admin.php?action=dt_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this as '.$post_status.'" rel="permalink">'.__( "Duplicate This", "duplicate-page" ).'</a>';
193 $html .= '</div>';
194 $html .= '</div>';
195 echo $html;
196 }
197 /*
198 * Admin Bar Duplicate This Link
199 */
200 public function duplicate_page_admin_bar_link(){
201 global $wp_admin_bar, $post;
202 $opt = get_option('duplicate_page_options');
203 $post_status = !empty($opt['duplicate_post_status']) ? $opt['duplicate_post_status'] : 'draft';
204 $current_object = get_queried_object();
205 if ( empty($current_object) )
206 return;
207 if ( ! empty( $current_object->post_type )
208 && ( $post_type_object = get_post_type_object( $current_object->post_type ) )
209 && ( $post_type_object->show_ui || $current_object->post_type == 'attachment') )
210 {
211 $wp_admin_bar->add_menu( array(
212 'parent' => 'edit',
213 'id' => 'duplicate_this',
214 'title' => __("Duplicate this as ".$post_status."", 'duplicate-page'),
215 'href' => admin_url().'admin.php?action=dt_duplicate_post_as_draft&amp;post='. $post->ID
216 ) );
217 }
218 }
219 public function duplicate_page_adsense() {
220 $API = "http://www.webdesi9.com/adsense/";
221 $curl = curl_init();
222 curl_setopt($curl, CURLOPT_URL, $API);
223 curl_setopt($curl, CURLOPT_POST, 1);
224 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
225 curl_setopt($curl, CURLOPT_POSTFIELDS, "ad_token=DUP_3951m8635u6542n3819i69130s9372h5602");
226 $result = curl_exec ($curl);
227 $data = json_decode($result, true);
228 curl_close ($curl);
229 if(!empty($data) && $data['status'] == 1 && !empty($data['image'])) {
230 return '<a href="'.$data['link'].'" target="_blank" title="Click here"><img src="'.$data['image'].'" width="100%"></a>';
231 }
232 }
233 /*
234 * Redirect function
235 */
236 static function dp_redirect($url){
237 echo '<script>window.location.href="'.$url.'"</script>';
238 }
239 }
240 new duplicate_page;
241 endif;
242 ?>