-
Notifications
You must be signed in to change notification settings - Fork 63
/
template-merge-post-details.php
303 lines (260 loc) · 12.8 KB
/
template-merge-post-details.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/*
* Name: Merge Post Type Details
*/
dt_please_log_in();
// Redirect if unable to determine post type
// phpcs:disable
$post_type = dt_get_post_type();
// phpcs:enable
if ( empty( $post_type ) ) {
return wp_redirect( '/' );
}
// Determine permission status for given post type
if ( ! current_user_can( 'access_' . $post_type ) ) {
wp_die( esc_html( sprintf( 'You do not have permission to access %s', $post_type ) ), 'Permission denied', 403 );
}
// Ensure required parameter ids are present
if ( ! isset( $_GET['currentid'], $_GET['dupeid'] ) ) {
return wp_redirect( '/' . $post_type );
}
// Extract post ids
$dt_current_id = sanitize_text_field( wp_unslash( $_GET['currentid'] ) );
$dt_dupe_id = sanitize_text_field( wp_unslash( $_GET['dupeid'] ) );
// Ensure this is not a self-merge
if ( $dt_current_id === $dt_dupe_id ) {
wp_die( esc_html( 'Self-merges not allowed!' ), 'Self-merge denied', 403 );
}
// Grab handles to various objects and test validity
$dt_current_post = DT_Posts::get_post( $post_type, $dt_current_id );
$dt_duplicate_post = DT_Posts::get_post( $post_type, $dt_dupe_id );
if ( is_wp_error( $dt_current_post ) || is_wp_error( $dt_duplicate_post ) ) {
get_template_part( '403', null, is_wp_error( $dt_current_post ) ? $dt_current_post : $dt_duplicate_post );
die();
}
// Ensure oldest of the two records, defaults to primary role.
if ( isset( $dt_current_post['post_date']['timestamp'], $dt_duplicate_post['post_date']['timestamp'] ) ) {
if ( $dt_duplicate_post['post_date']['timestamp'] < $dt_current_post['post_date']['timestamp'] ) {
$dt_temp_post = $dt_current_post;
$dt_current_post = $dt_duplicate_post;
$dt_duplicate_post = $dt_temp_post;
}
}
$post_settings = DT_Posts::get_post_settings( $post_type );
$post_settings_fields = DT_Posts::get_post_field_settings( $post_type, false );
// Determine fields to be displayed
$fields_to_display = determine_post_fields_to_display( $post_settings_fields, $dt_current_post, $dt_duplicate_post );
// Load supporting merge scripts
function merge_post_details_scripts( $args ) {
$dependencies = [ 'jquery', 'lodash', 'shared-functions', 'typeahead-jquery' ];
if ( DT_Mapbox_API::get_key() ) {
DT_Mapbox_API::load_mapbox_header_scripts();
DT_Mapbox_API::load_mapbox_search_widget();
$dependencies[] = 'mapbox-cookie';
$dependencies[] = 'mapbox-search-widget';
$dependencies[] = 'mapbox-gl';
}
dt_theme_enqueue_script( 'merge-post-details', 'dt-assets/js/merge-post-details.js', $dependencies, true );
wp_localize_script( 'merge-post-details', 'merge_post_details', $args );
}
add_action( 'wp_enqueue_scripts', function () use ( $dt_current_post, $dt_duplicate_post, $post_settings, $fields_to_display ) {
merge_post_details_scripts( [
'posts' => [
$dt_current_post['ID'] => [
'record' => $dt_current_post,
'html' => render_post_fields_html( $dt_current_post, $fields_to_display, $post_settings['fields'], $dt_current_post['ID'] . '_', true )
],
$dt_duplicate_post['ID'] => [
'record' => $dt_duplicate_post,
'html' => render_post_fields_html( $dt_duplicate_post, $fields_to_display, $post_settings['fields'], $dt_duplicate_post['ID'] . '_', true )
]
],
'post_settings' => $post_settings,
'post_fields_default_html' => render_post_fields_html( $dt_current_post, $fields_to_display, $post_settings['fields'], '', false ),
'site_url' => esc_url( site_url( '/' ) ),
'url_root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'mapbox' => [
'map_key' => DT_Mapbox_API::get_key(),
'google_map_key' => Disciple_Tools_Google_Geocode_API::get_key(),
'translations' => [
'search_location' => __( 'Search Location', 'disciple_tools' ),
'delete_location' => __( 'Delete Location', 'disciple_tools' ),
'use' => __( 'Use', 'disciple_tools' ),
'open_modal' => __( 'Open Modal', 'disciple_tools' )
]
],
'translations' => [
'regions_of_focus' => __( 'Regions of Focus', 'disciple_tools' ),
'all_locations' => __( 'All Locations', 'disciple_tools' ),
'error_msg' => __( 'Sorry, something went wrong', 'disciple_tools' )
]
] );
}, 10 );
// Render dt header
get_header();
?>
<div id="content" class="template-merge-post-details">
<div id="inner-content" class="grid-x grid-margin-x">
<!-- Merge Header-->
<main id="main_header" class="large-12 medium-12 cell" role="main">
<div class="bordered-box">
<h2 class="center"><?php echo esc_html( sprintf( __( 'Merge Duplicate %s', 'disciple_tools' ), $post_settings['label_plural'] ) )?></h2>
<p class="center"
style="max-width: 75%; margin-left:auto; margin-right:auto;"><?php esc_html_e( 'When you merge, the primary record is updated with the values you choose, and relationships to other items are shifted to the primary record; which can be switched below.', 'disciple_tools' ) ?></p>
<label>
<strong><?php esc_html_e( 'Copy comments to updated primary record', 'disciple_tools' ); ?></strong>
<input type="checkbox" id="merge_comments"
name="merge_comments" <?php checked( ! isset( $_GET['comments'] ) ) ?>>
</label>
<button class='button loader submit-merge' type='button'
value='Merge'><?php esc_html_e( 'Merge', 'disciple_tools' ); ?></button>
<br>
<span class="merge_errors"></span>
</div>
</main>
<br>
<!-- Archiving Post Record -->
<main id="main_archiving" class="large-4 medium-4 cell" role="main">
<div class="bordered-box">
<h2 class="center"><?php esc_html_e( 'Archiving', 'disciple_tools' ) ?> - <a
id="main_archiving_post_id_title_link" target="_blank"><span
id="main_archiving_post_id_title"></span></a>
</h2>
<div class="main-archiving-primary-switch-but-div">
<button id="main_archiving_primary_switch_but"
style="text-align: center;"
class="button center"><?php esc_html_e( 'Use as Primary', 'disciple_tools' ) ?>
</button>
</div>
<input type="hidden" id="main_archiving_current_post_id"
value="<?php echo esc_html( $dt_current_post['ID'] ) ?>"/>
<div id="main_archiving_fields_div"></div>
</div>
</main>
<!-- Primary Post Record -->
<main id="main_primary" class="large-4 medium-4 cell" role="main">
<div class="bordered-box">
<h2 class="center"><?php esc_html_e( 'Primary', 'disciple_tools' ) ?> - <a
id="main_primary_post_id_title_link" target="_blank"><span
id="main_primary_post_id_title"></span></a>
</h2>
<input type="hidden" id="main_primary_current_post_id"
value="<?php echo esc_html( $dt_duplicate_post['ID'] ) ?>"/>
<div id="main_primary_fields_div" style="margin-top: 60px;"></div>
</div>
</main>
<!-- Updated Post Record -->
<main id="main_updated" class="large-4 medium-4 cell" role="main">
<div class="bordered-box">
<h2 class="center"><?php esc_html_e( 'Updated', 'disciple_tools' ) ?> - #<span
id="main_updated_post_id_title"></span></h2>
<div id="main_updated_fields_div" style="margin-top: 60px;"></div>
</div>
</main>
<!-- Merge Footer -->
<main id="main_footer" class="large-12 medium-12 cell" role="main">
<div class="bordered-box">
<button class='button loader submit-merge' type='button'
value='Merge'><?php esc_html_e( 'Merge', 'disciple_tools' ); ?></button>
<br>
<span class='merge_errors'></span>
</div>
</main>
</div>
</div>
<?php
function determine_post_fields_to_display( $settings_fields, $current_post, $duplicate_post ) {
$fields_to_display = [];
foreach ( $settings_fields ?? [] as $key => $field ) {
// Ignore/Hide fields not present within both post records
if ( ! empty( $current_post[ $key ] ) || ! empty( $duplicate_post[ $key ] ) ) {
$fields_to_display[] = $key;
}
}
return $fields_to_display;
}
function render_post_fields_html( $post, $fields, $settings_fields, $field_id_prefix, $show_field_select ) {
ob_start();
render_post_fields( $post, $fields, $settings_fields, $field_id_prefix, $show_field_select );
$rendered_post_fields_html = ob_get_contents();
ob_end_clean();
return $rendered_post_fields_html;
}
function render_post_fields( $post, $fields, $settings_fields, $field_id_prefix, $show_field_select = true ) {
$merge_capable_field_types = list_merge_capable_field_types();
?>
<table>
<tbody>
<?php
foreach ( $fields as $field ) {
// Capture rendered field html
ob_start();
render_field_for_display( $field, $settings_fields, $post, true, false, $field_id_prefix );
$rendered_field_html = ob_get_contents();
ob_end_clean();
$merge_field_id = $field_id_prefix . $field;
$merge_field_type = $settings_fields[ $field ]['type'];
// Only display if valid html content has been generated
if ( ! empty( $rendered_field_html ) ) {
?>
<tr>
<?php
if ( $show_field_select ) {
if ( in_array( $settings_fields[ $field ]['type'], $merge_capable_field_types ) ) {
?>
<td class="td-field-select">
<input type="checkbox" class="field-select"
data-merge_update_field_id="<?php echo esc_html( $field ) ?>"
data-merge_field_id="<?php echo esc_html( $merge_field_id ) ?>"
data-merge_field_type="<?php echo esc_html( $merge_field_type ) ?>">
</td>
<?php
} else {
?>
<td class="td-field-select">
<input type="radio" class="field-select" name="<?php echo esc_html( $field ) ?>"
data-merge_update_field_id="<?php echo esc_html( $field ) ?>"
data-merge_field_id="<?php echo esc_html( $merge_field_id ) ?>"
data-merge_field_type="<?php echo esc_html( $merge_field_type ) ?>">
</td>
<?php
}
}
?>
<td class="td-field-input">
<input type="hidden" id="post_field_id"
value="<?php echo esc_html( $field ) ?>"/>
<input type="hidden" id="merge_field_id"
value="<?php echo esc_html( $merge_field_id ) ?>"/>
<input type="hidden" id="merge_field_type"
value="<?php echo esc_html( $merge_field_type ) ?>"/>
<input type="hidden" id="field_meta" value=""/>
<?php
// phpcs:disable
echo $rendered_field_html;
// phpcs:enable
?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php
}
function list_merge_capable_field_types(): array {
return [
'communication_channel',
'multi_select',
'location_meta',
'location',
'tags',
'connection',
'link'
];
}
// Render dt footer; which should contain the majority of scripts
get_footer();