PluginProbe ʕ •ᴥ•ʔ
Yoast Duplicate Post / 0.4
Yoast Duplicate Post v0.4
trunk 0.3 0.4 0.5 0.6 0.6.1 1.0 1.1 1.1.1 1.1.2 2.0 2.0.1 2.0.2 2.1 2.1.1 2.2 2.3 2.4 2.4.1 2.5 2.6 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.2 3.2 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 4.0 4.0.1 4.0.2 4.1 4.1.1 4.1.2 4.2 4.3 4.4 4.5 4.6
duplicate-post / duplicate-post.php
duplicate-post Last commit date
duplicate-post.php 17 years ago gpl-3.0.txt 18 years ago make_duplicate_post.php 17 years ago readme.txt 17 years ago save_as_new_page.php 17 years ago save_as_new_post.php 17 years ago todo.txt 18 years ago
duplicate-post.php
482 lines
1 <?php
2 /*
3 Plugin Name: Duplicate Post
4 Plugin URI: http://www.lopo.it/duplicate-post.tar.gz
5 Description: Create a copy of a post.
6 Version: 0.4
7 Author: Enrico Battocchi
8 Author URI: http://www.lopo.it
9 */
10
11 /* Copyright 2008 Enrico Battocchi (email : enrico.battocchi@gmail.com)
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 // Version of the plugin
29 define('DUPLICATE_POST_CURRENT_VERSION', '0.3' );
30 define('DUPLICATE_POST_COLUMN', 'control_duplicate_post');
31 define('DUPLICATE_POST_VIEW_USER_LEVEL_OPTION', 'duplicate_post_view_user_level');
32 define('DUPLICATE_POST_CREATE_USER_LEVEL_OPTION', 'duplicate_post_create_user_level');
33 define('DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION', 'duplicate_post_admin_user_level');
34
35 // i18n plugin domain
36 define('DUPLICATE_POST_I18N_DOMAIN', 'duplicate-post');
37
38 /**
39 * Initialise the internationalisation domain
40 */
41 $duplicate_post_is_i18n_setup = false;
42 function duplicate_post_init_i18n() {
43 global $duplicate_post_is_i18n_setup;
44
45 if ($duplicate_post_is_i18n_setup == false) {
46 load_plugin_textdomain(DUPLICATE_POST_I18N_DOMAIN, 'wp-content/plugins/duplicate_post');
47 $duplicate_post_is_i18n_setup = true;
48 }
49 }
50
51 /**
52 * Plugin activation
53 */
54 add_action('activate_duplicate-post/duplicate-post.php','duplicate_post_plugin_activation');
55
56
57 function duplicate_post_plugin_activation() {
58 $installed_version = duplicate_post_get_installed_version();
59
60 if ( $installed_version==duplicate_post_get_current_version() ) {
61 // do nothing
62 } else if ( $installed_version=='' ) {
63 // Add all options, nothing already installed
64 add_option(
65 DUPLICATE_POST_VIEW_USER_LEVEL_OPTION,
66 '2',
67 'Default user level to copy posts' );
68 add_option(
69 DUPLICATE_POST_CREATE_USER_LEVEL_OPTION,
70 '5',
71 'Default user level to create the templates' );
72 add_option(
73 DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION,
74 '8',
75 'Default user level to change the plugin options' );
76 }
77
78 // Update version number
79 update_option( 'duplicate_post_version', duplicate_post_get_current_version() );
80 }
81
82
83 /**
84 * Add a column in the post listing
85 */
86 add_filter('manage_posts_columns', 'duplicate_post_add_duplicate_post_column');
87 add_action('manage_posts_custom_column', 'duplicate_post_make_duplicate_link', 10, 2);
88
89 function duplicate_post_add_duplicate_post_column($columns) {
90 if (duplicate_post_is_current_user_allowed_to_create()) {
91 $columns[DUPLICATE_POST_COLUMN] = '';
92 }
93 return $columns;
94 }
95
96 function duplicate_post_make_duplicate_link($column_name, $id) {
97 if (duplicate_post_is_current_user_allowed_to_create()) {
98 if ($column_name == DUPLICATE_POST_COLUMN) {
99 echo "<a href='edit.php?page=duplicate-post/make_duplicate_post.php&amp;post=" . $id
100 . "' title='" . __("Make a duplicate from this post", DUPLICATE_POST_I18N_DOMAIN)
101 . "' class='edit'>" . __("Duplicate", DUPLICATE_POST_I18N_DOMAIN) . "</a>";
102 }
103 }
104 }
105
106
107
108 /**
109 * Add a button in the post edit form to create a clone
110 */
111 add_action( 'edit_form_advanced', 'duplicate_post_add_duplicate_post_button' );
112
113
114
115 function duplicate_post_add_duplicate_post_button() {
116 if ( isset( $_GET['post'] ) && duplicate_post_is_current_user_allowed_to_create()) {
117 $notifyUrl = "edit.php?page=duplicate-post/save_as_new_post.php&post=" . $_GET['post'];
118 ?>
119 <script language="JavaScript">
120 <!--
121 function save_as_copy( thisForm ) {
122 thisForm.referredby.value = "<?php echo $notifyUrl; ?>";
123 thisForm.action = "edit.php?page=duplicate-post/save_as_new_post.php";
124 thisForm.submit();
125 }
126 // -->
127 </script>
128 <input type="hidden" name="post" value="<?php echo $_GET['post']; ?>" />
129 <p class="submit">
130 <span style="font-weight: bold; color: red;">
131 <?php _e('Click here to create a copy of this post.', DUPLICATE_POST_I18N_DOMAIN); ?>
132 </span>
133 <input type="submit" name="SubmitNotification"
134 value="<?php echo __('Make Copy', DUPLICATE_POST_I18N_DOMAIN) . ' &raquo;'; ?>"
135 onclick="save_as_copy( this.form )" />
136 </p>
137 <?php
138 }
139 }
140
141 /**
142 * Add a button in the page edit form to create a clone
143 */
144 add_action( 'edit_page_form', 'duplicate_post_add_duplicate_page_button' );
145
146 function duplicate_post_add_duplicate_page_button() {
147 if ( isset( $_GET['post'] ) && duplicate_post_is_current_user_allowed_to_create()) {
148 $notifyUrl = "edit.php?page=duplicate-post/save_as_new_page.php&post=" . $_GET['post'];
149 ?>
150 <script language="JavaScript">
151 <!--
152 function save_as_copy( thisForm ) {
153 thisForm.referredby.value = "<?php echo $notifyUrl; ?>";
154 thisForm.action = "edit.php?page=duplicate-post/save_as_new_page.php";
155 thisForm.submit();
156 }
157 // -->
158 </script>
159 <input type="hidden" name="post" value="<?php echo $_GET['post']; ?>" />
160 <p class="submit">
161 <span style="font-weight: bold; color: red;">
162 <?php _e('Click here to create a copy of this page.', DUPLICATE_POST_I18N_DOMAIN); ?>
163 </span>
164 <input type="submit" name="SubmitNotification"
165 value="<?php echo __('Make Copy', DUPLICATE_POST_I18N_DOMAIN) . ' &raquo;'; ?>"
166 onclick="save_as_copy( this.form )" />
167 </p>
168 <?php
169 }
170 }
171
172 /**
173 * Wrapper for the option 'duplicate_post_view_user_level'
174 */
175 function duplicate_post_get_view_user_level() {
176 return get_option( DUPLICATE_POST_VIEW_USER_LEVEL_OPTION );
177 }
178
179 /**
180 * Wrapper for the option 'duplicate_post_view_user_level'
181 */
182 function duplicate_post_set_view_user_level($new_level) {
183 return update_option( DUPLICATE_POST_VIEW_USER_LEVEL_OPTION, $new_level );
184 }
185
186 /**
187 * Wrapper for the option 'duplicate_post_create_user_level'
188 */
189 function duplicate_post_get_create_user_level() {
190 return get_option( DUPLICATE_POST_CREATE_USER_LEVEL_OPTION );
191 }
192
193 /**
194 * Wrapper for the option 'duplicate_post_create_user_level'
195 */
196 function duplicate_post_set_create_user_level($new_level) {
197 return update_option( DUPLICATE_POST_CREATE_USER_LEVEL_OPTION, $new_level );
198 }
199
200 /**
201 * Wrapper for the option 'duplicate_post_admin_user_level'
202 */
203 function duplicate_post_get_admin_user_level() {
204 return get_option( DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION );
205 }
206
207 /**
208 * Wrapper for the option 'duplicate_post_admin_user_level'
209 */
210 function duplicate_post_set_admin_user_level($new_level) {
211 return update_option( DUPLICATE_POST_ADMIN_USER_LEVEL_OPTION, $new_level );
212 }
213
214 /**
215 * Wrapper for the option 'duplicate_post_version'
216 */
217 function duplicate_post_get_installed_version() {
218 return get_option( 'duplicate_post_version' );
219 }
220
221 /**
222 * Wrapper for the defined constant DUPLICATE_POST_CURRENT_VERSION
223 */
224 function duplicate_post_get_current_version() {
225 return DUPLICATE_POST_CURRENT_VERSION;
226 }
227
228 /**
229 * Test if the user is allowed to view the templates & create posts
230 */
231 function duplicate_post_is_current_user_allowed_to_view() {
232 return current_user_can("level_" . duplicate_post_get_view_user_level());
233 }
234
235 /**
236 * Test if the user is allowed to create templates
237 */
238 function duplicate_post_is_current_user_allowed_to_create() {
239 return current_user_can("level_" . duplicate_post_get_create_user_level());
240 }
241
242 /**
243 * Test if the user is allowed to administrate the plugin
244 */
245 function duplicate_post_is_current_user_allowed_to_admin() {
246 return current_user_can("level_" . duplicate_post_get_admin_user_level());
247 }
248
249 /**
250 * Get a level given a role
251 */
252 function duplicate_post_get_level_from_role($role) {
253 switch ($role) {
254 case 0: // subscribers 0
255 return 0;
256 case 1: // contributors 1
257 return 1;
258 case 2: // authors 2..4
259 return 2;
260 case 3: // editors 5..7
261 return 5;
262 case 4: // administrators 8..10
263 return 8;
264 default: // error
265 return 0;
266 }
267 }
268
269 /**
270 * Get a role given a level
271 */
272 function duplicate_post_get_role_from_level($level) {
273 if ($level<=0) {
274 // subscribers 0
275 return 0;
276 } else if ($level==1) {
277 // contributors 1
278 return 1;
279 } else if ($level>=2 && $level<=4) {
280 // authors 2..4
281 return 2;
282 } else if ($level>=5 && $level<=7) {
283 // editors 5..7
284 return 3;
285 } else if ($level>=8) {
286 // admins 8..10
287 return 4;
288 }
289 return 0;
290 }
291
292 /**
293 * Get the currently registered user
294 */
295 function duplicate_post_get_current_user() {
296 if (function_exists('wp_get_current_user')) {
297 return wp_get_current_user();
298 } else if (function_exists('get_currentuserinfo')) {
299 global $userdata;
300 get_currentuserinfo();
301 return $userdata;
302 } else {
303 $user_login = $_COOKIE[USER_COOKIE];
304 $current_user = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_login='$user_login'");
305 return $current_user;
306 }
307 }
308
309 /**
310 * Escape single quotes, specialchar double quotes, and fix line endings.
311 */
312 function duplicate_post_js_escape($text) {
313 if (function_exists('js_escape')) {
314 return js_escape($text);
315 } else {
316 $safe_text = str_replace('&&', '&#038;&', $text);
317 $safe_text = str_replace('&&', '&#038;&', $safe_text);
318 $safe_text = preg_replace('/&(?:$|([^#])(?![a-z1-4]{1,8};))/', '&#038;$1', $safe_text);
319 $safe_text = str_replace('<', '&lt;', $safe_text);
320 $safe_text = str_replace('>', '&gt;', $safe_text);
321 $safe_text = str_replace('"', '&quot;', $safe_text);
322 $safe_text = str_replace('&#039;', "'", $safe_text);
323 $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
324 return safe_text;
325 }
326 }
327
328 /**
329 * Get a page from the database
330 */
331 function duplicate_post_get_page($id) {
332 global $wpdb;
333 $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
334 if ($post->post_type == "revision"){
335 $id = $post->post_parent;
336 $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
337 }
338 return $post[0];
339 }
340
341 /**
342 * Get a post from the database
343 */
344 function duplicate_post_get_post($id) {
345 global $wpdb;
346 $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
347 if ($post->post_type == "revision"){
348 $id = $post->post_parent;
349 $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
350 }
351 return $post[0];
352 }
353
354 /**
355 * Copy the categories of a post to another post
356 */
357 function duplicate_post_copy_post_categories($id, $new_id) {
358 global $wpdb;
359 if (isset($wpdb->terms)) {
360 // WordPress 2.3
361 $taxonomies = get_object_taxonomies('post'); //array("category", "post_tag");
362 foreach ($taxonomies as $taxonomy) {
363 $post_terms = wp_get_object_terms($id, $taxonomy);
364 for ($i=0; $i<count($post_terms); $i++) {
365 wp_set_object_terms($new_id, $post_terms[$i]->slug, $taxonomy, true);
366 }
367 }
368 } else {
369 $post_categories = $wpdb->get_results("SELECT category_id FROM $wpdb->post2cat WHERE post_id=$id");
370 if (count($post_categories)!=0) {
371 $sql_query = "INSERT INTO $wpdb->post2cat (post_id, category_id) ";
372
373 for ($i=0; $i<count($post_categories); $i++) {
374 $post_category = $post_categories[$i]->category_id;
375
376 if ($i<count($post_categories)-1) {
377 $sql_query .= "SELECT $new_id, $post_category UNION ALL ";
378 } else {
379 $sql_query .= "SELECT $new_id, $post_category";
380 }
381 }
382
383 $wpdb->query($sql_query);
384 }
385 }
386 }
387
388 /**
389 * Copy the meta information of a post to another post
390 */
391 function duplicate_post_copy_post_meta_info($id, $new_id) {
392 global $wpdb;
393 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id");
394
395 if (count($post_meta_infos)!=0) {
396 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
397
398 for ($i=0; $i<count($post_meta_infos); $i++) {
399 $meta_info = $post_meta_infos[$i];
400
401 if ($i<count($post_meta_infos)-1) {
402 $sql_query .= "SELECT $new_id, '$meta_info->meta_key', '$meta_info->meta_value' UNION ALL ";
403 } else {
404 $sql_query .= "SELECT $new_id, '$meta_info->meta_key', '$meta_info->meta_value'";
405 }
406 }
407
408 $wpdb->query($sql_query);
409 }
410 }
411
412 /**
413 * Create a duplicate from a post
414 */
415 function duplicate_post_create_duplicate_from_post($post) {
416 global $wpdb;
417 $new_post_type = 'post';
418 $new_post_author = duplicate_post_get_current_user();
419 $new_post_date = current_time('mysql');
420 $new_post_date_gmt = get_gmt_from_date($new_post_date);
421
422 $post_content = str_replace("'", "''", $post->post_content);
423 $post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
424 $post_excerpt = str_replace("'", "''", $post->post_excerpt);
425 $post_title = str_replace("'", "''", $post->post_title);
426 $post_status = str_replace("'", "''", $post->post_status);
427 $comment_status = str_replace("'", "''", $post->comment_status);
428 $ping_status = str_replace("'", "''", $post->ping_status);
429
430 // Insert the new template in the post table
431 $wpdb->query(
432 "INSERT INTO $wpdb->posts
433 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
434 VALUES
435 ('$new_post_author->ID', '', '', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', 'draft', '$new_post_type', '$comment_status', '$ping_status', '$post->post_password', '$post->to_ping', '$post->pinged', '', '', '$post->post_parent', '$post->menu_order', '$post->post_mime_type')");
436
437 $new_post_id = $wpdb->insert_id;
438
439 // Copy the categories
440 duplicate_post_copy_post_categories($post->ID, $new_post_id);
441
442 // Copy the meta information
443 duplicate_post_copy_post_meta_info($post->ID, $new_post_id);
444
445 return $new_post_id;
446 }
447
448 /**
449 * Create a duplicate from a page
450 */
451 function duplicate_post_create_duplicate_from_page($post) {
452 global $wpdb;
453 $new_post_type = 'page';
454 $new_post_date = current_time('mysql');
455 $new_post_date_gmt = get_gmt_from_date($new_post_date);
456
457 $post_content = str_replace("'", "''", $post->post_content);
458 $post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
459 $post_excerpt = str_replace("'", "''", $post->post_excerpt);
460 $post_title = str_replace("'", "''", $post->post_title);
461 $post_status = str_replace("'", "''", $post->post_status);
462 $post_name = str_replace("'", "''", $post->post_name);
463 $comment_status = str_replace("'", "''", $post->comment_status);
464 $ping_status = str_replace("'", "''", $post->ping_status);
465
466 // Insert the new template in the post table
467 $wpdb->query(
468 "INSERT INTO $wpdb->posts
469 (post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)
470 VALUES
471 ('$post->post_author', '', '', '$post_content', '$post_content_filtered', '$post_title', '$post_excerpt', 'draft', '$new_post_type', '$comment_status', '$ping_status', '$post->post_password', '$post_name', '$post->to_ping', '$post->pinged', '', '', '$post->post_parent', '$post->menu_order', '$post->post_mime_type')");
472
473 $new_page_id = $wpdb->insert_id;
474
475 // Copy the meta information
476 duplicate_post_copy_post_meta_info($post->ID, $new_page_id);
477
478 return $new_page_id;
479 }
480
481
482 ?>