This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpost-merge.php
221 lines (176 loc) · 7.54 KB
/
post-merge.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
Plugin Name: Post Merge
Plugin URI: http://github.com/ibotty/post-merge
Description: A plugin to merge two post.
Version: 0.1
Author: Tobias Florek <[email protected]>
Author URI: http://github.com/ibotty
License: BSD
*/
?>
<?php
/*
Copyright (c) 2011, Tobias Florek. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
require_once(ABSPATH . '/wp-admin/includes/template.php');
if (! class_exists("PostMerge")) {
class PostMerge {
function __construct() {
$this->init();
}
function init() {
$this->merge_page_slug = 'pm-merge';
register_activation_hook (__FILE__, array ($this, 'install'));
register_deactivation_hook (__FILE__, array ($this, 'deinstall'));
add_action('admin_init', array($this, 'admin_init'));
add_filter("admin_head", array($this, "admin_head"), 10, 2);
add_filter("admin_menu", array($this, "admin_menu"));
add_filter("post_row_actions", array($this, "row_actions"), 10, 2);
add_filter("page_row_actions", array($this, "row_actions"), 10, 2);
}
function install() {
}
function deinstall() {
}
function admin_init() {
wp_register_script('jquery.fn.autoresize',
plugins_url('/vendor/jquery.fn.autoResize/jquery.autoresize.js', __FILE__));
wp_register_script('pm-merge-script', plugins_url('/pm-merge.js', __FILE__));
wp_register_style('pm-merge-style', plugins_url('/pm-merge.css', __FILE__));
}
function admin_head() {
// add css to highlight selected candidate row
if (isset($_GET['pm-candidate'])) {
$candidate = intval($_GET['pm-candidate']);
echo "<style type='text/css'> #post-$candidate {background:rgba(255,0,0,0.2);} </style>";
}
}
function row_actions($actions, $post) {
$cur_url = $_SERVER['REQUEST_URI'];
$merge_url = menu_page_url($this->merge_page_slug, false);
# if a merge candidate is already set
if (isset($_GET['pm-candidate'])) {
$candidate = intval($_GET['pm-candidate']);
# remove candidate status if same post
if ($post->ID === $candidate) {
$link = remove_query_arg('pm-candidate', $cur_url);
$displaytext = 'Cancel merge';
} else { # merge
$link = esc_url(add_query_arg(array(
'pm-one'=>$candidate, 'pm-another'=>$post->ID), $merge_url));
$displaytext = 'Merge with selected '.$_GET['post_type'];
}
} else { # no merge candidate set
$link = esc_url(add_query_arg('pm-candidate', $post->ID, $cur_url));
$displaytext = 'Merge';
}
$str = '<a class="pm-merge" href="'.$link.'">'.__($displaytext).'</a>';
$actions["posts_merge"] = $str;
return $actions;
}
function admin_menu() {
$page = add_management_page(__('Merge Posts'), __('Merge Posts'),
'edit_published_posts', $this->merge_page_slug, array($this, 'tools_page'));
add_action("admin_print_styles-$page",
array($this, 'merge_styles_register'));
}
function merge_styles_register() {
wp_enqueue_script('jquery.fn.autoresize');
wp_enqueue_script('pm-merge-script');
wp_enqueue_style('pm-merge-style');
}
function tools_page() {
global $wpdb;
if (! isset($_REQUEST['pm-one']) || ! isset($_REQUEST['pm-another']))
wp_die(__('Please select two posts to merge in the post overview.'));
$one = get_post(intval($_REQUEST['pm-one']));
$another = get_post(intval($_REQUEST['pm-another']));
if (! current_user_can('edit_others_posts') &&
($one->post_author != $another->post_author || $one-post_author != get_current_user_id()))
wp_die(__('You do not have sufficient permissions to access this page.'));
// compare and select for merge
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
do_action('pm_merge');
$one_data = array_filter(apply_filters("pm_change_data", (array) $one, $one));
$another_data = array_filter(apply_filters("pm_change_data", (array) $another, $another));
// somehow array_unique is required...
$fields = array_unique(array_merge(array_keys($one_data), array_keys($another_data)));
$fields = apply_filters("pm_merge_fields", $fields, $one, $another);
// can (and should) use $fields, $one, $one_data and $another, $another_data
include "includes/merge.php";
}
// POST: save merged post
else {
check_admin_referer('pm-nonce');
$old_post_ids = array($_REQUEST["pm-one"], $_REQUEST["pm-another"]);
$merged_post = array();
foreach ($_POST as $key=>$val)
// add fields that begin with 'pmp-' to the post
if (substr($key, 0, strlen('pmp-')) == 'pmp-')
$merged_post[substr($key, strlen('pmp-'))] = $val;
$merged_post = (object) $merged_post;
$merged_post = apply_filters('pm_prepare_merged_post' , $merged_post);
$wp_post_cols = array('ID', 'post_author', 'post_date',
'post_date_gmt', 'post_content', 'post_content_filtered',
'post_title', 'post_excerpt', 'post_status', 'post_type',
'comment_count', 'comment_status', 'ping_status', 'post_password',
'post_name', 'to_ping', 'pinged', 'post_modified',
'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type',
'guid');
// the "main" part of the new post
$wp_post = array();
foreach ($wp_post_cols as $key)
if (isset($merged_post->$key)) {
$wp_post[$key] = $merged_post->$key;
unset($merged_post->$key);
}
$new_id = $wp_post["ID"];
if ($new_id == "new")
$new_id = wp_insert_post($wp_post);
else {
// check, whether someone did something bad first (changed ids)
if (! in_array($new_id, $old_post_ids))
wp_die("tsetsetse! nice try though.");
// don't delete updated post
$old_post_ids = array_diff($old_post_ids, array($new_id));
wp_update_post($wp_post);
}
/**
* pm_save post is an action taking
* - the new id
* - the to be trashed ids
* - the changed wp_posts' fields
* - the changed metadata fields (or anything else set)
*/
do_action('pm_save_post', $new_id, $old_post_ids, $wp_post, (array) $merged_post);
foreach ($old_post_ids as $oldid)
wp_trash_post($oldid);
include 'includes/saved.php'; // can (and should use) $old_post_ids and $new_id
}
}
function tools_styles() {
wp_enqueue_style('pm_tools.css');
}
}
}
new PostMerge();
?>