PluginProbe ʕ •ᴥ•ʔ
Yoast Duplicate Post / 0.3
Yoast Duplicate Post v0.3
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 18 years ago gpl-3.0.txt 18 years ago make_duplicate_post.php 18 years ago readme.txt 18 years ago save_as_new_page.php 18 years ago save_as_new_post.php 18 years ago todo.txt 18 years ago
duplicate-post.php
474 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.3
7 Author: Enrico Battocchi
8 Author URI: http://www.lopo.it
9 */
10
11 /* Copyright 2007 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 return $post[0];
335 }
336
337 /**
338 * Get a post from the database
339 */
340 function duplicate_post_get_post($id) {
341 global $wpdb;
342 $post = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE ID=$id");
343 return $post[0];
344 }
345
346 /**
347 * Copy the categories of a post to another post
348 */
349 function duplicate_post_copy_post_categories($id, $new_id) {
350 global $wpdb;
351 if (isset($wpdb->terms)) {
352 // WordPress 2.3
353 $taxonomies = get_object_taxonomies('post'); //array("category", "post_tag");
354 foreach ($taxonomies as $taxonomy) {
355 $post_terms = wp_get_object_terms($id, $taxonomy);
356 for ($i=0; $i<count($post_terms); $i++) {
357 wp_set_object_terms($new_id, $post_terms[$i]->slug, $taxonomy, true);
358 }
359 }
360 } else {
361 $post_categories = $wpdb->get_results("SELECT category_id FROM $wpdb->post2cat WHERE post_id=$id");
362 if (count($post_categories)!=0) {
363 $sql_query = "INSERT INTO $wpdb->post2cat (post_id, category_id) ";
364
365 for ($i=0; $i<count($post_categories); $i++) {
366 $post_category = $post_categories[$i]->category_id;
367
368 if ($i<count($post_categories)-1) {
369 $sql_query .= "SELECT $new_id, $post_category UNION ALL ";
370 } else {
371 $sql_query .= "SELECT $new_id, $post_category";
372 }
373 }
374
375 $wpdb->query($sql_query);
376 }
377 }
378 }
379
380 /**
381 * Copy the meta information of a post to another post
382 */
383 function duplicate_post_copy_post_meta_info($id, $new_id) {
384 global $wpdb;
385 $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$id");
386
387 if (count($post_meta_infos)!=0) {
388 $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
389
390 for ($i=0; $i<count($post_meta_infos); $i++) {
391 $meta_info = $post_meta_infos[$i];
392
393 if ($i<count($post_meta_infos)-1) {
394 $sql_query .= "SELECT $new_id, '$meta_info->meta_key', '$meta_info->meta_value' UNION ALL ";
395 } else {
396 $sql_query .= "SELECT $new_id, '$meta_info->meta_key', '$meta_info->meta_value'";
397 }
398 }
399
400 $wpdb->query($sql_query);
401 }
402 }
403
404 /**
405 * Create a duplicate from a post
406 */
407 function duplicate_post_create_duplicate_from_post($post) {
408 global $wpdb;
409 $new_post_type = 'post';
410 $new_post_author = duplicate_post_get_current_user();
411 $new_post_date = current_time('mysql');
412 $new_post_date_gmt = get_gmt_from_date($new_post_date);
413
414 $post_content = str_replace("'", "''", $post->post_content);
415 $post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
416 $post_excerpt = str_replace("'", "''", $post->post_excerpt);
417 $post_title = str_replace("'", "''", $post->post_title);
418 $post_status = str_replace("'", "''", $post->post_status);
419 $comment_status = str_replace("'", "''", $post->comment_status);
420 $ping_status = str_replace("'", "''", $post->ping_status);
421
422 // Insert the new template in the post table
423 $wpdb->query(
424 "INSERT INTO $wpdb->posts
425 (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)
426 VALUES
427 ('$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')");
428
429 $new_post_id = $wpdb->insert_id;
430
431 // Copy the categories
432 duplicate_post_copy_post_categories($post->ID, $new_post_id);
433
434 // Copy the meta information
435 duplicate_post_copy_post_meta_info($post->ID, $new_post_id);
436
437 return $new_post_id;
438 }
439
440 /**
441 * Create a duplicate from a page
442 */
443 function duplicate_post_create_duplicate_from_page($post) {
444 global $wpdb;
445 $new_post_type = 'page';
446 $new_post_date = current_time('mysql');
447 $new_post_date_gmt = get_gmt_from_date($new_post_date);
448
449 $post_content = str_replace("'", "''", $post->post_content);
450 $post_content_filtered = str_replace("'", "''", $post->post_content_filtered);
451 $post_excerpt = str_replace("'", "''", $post->post_excerpt);
452 $post_title = str_replace("'", "''", $post->post_title);
453 $post_status = str_replace("'", "''", $post->post_status);
454 $post_name = str_replace("'", "''", $post->post_name);
455 $comment_status = str_replace("'", "''", $post->comment_status);
456 $ping_status = str_replace("'", "''", $post->ping_status);
457
458 // Insert the new template in the post table
459 $wpdb->query(
460 "INSERT INTO $wpdb->posts
461 (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)
462 VALUES
463 ('$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')");
464
465 $new_page_id = $wpdb->insert_id;
466
467 // Copy the meta information
468 duplicate_post_copy_post_meta_info($post->ID, $new_page_id);
469
470 return $new_page_id;
471 }
472
473
474 ?>