Skip to content

Commit 71d49f2

Browse files
authored
Merge pull request #9 from omnisend/grouping2
Grouping by type
2 parents 9cef1c1 + 526abf4 commit 71d49f2

File tree

4 files changed

+246
-10
lines changed

4 files changed

+246
-10
lines changed

assets/css/admin.css

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@
5757
margin-top: 20px;
5858
}
5959

60+
/* Post Type Groups */
61+
.stl-post-type-groups {
62+
margin-top: 20px;
63+
}
64+
65+
.stl-post-type-group {
66+
margin-bottom: 30px;
67+
}
68+
69+
.stl-post-type-header {
70+
background: #f0f0f0;
71+
padding: 10px 15px;
72+
margin-bottom: 15px;
73+
border-radius: 4px;
74+
border-left: 5px solid #2271b1;
75+
font-size: 16px;
76+
font-weight: 600;
77+
color: #1d2327;
78+
}
79+
6080
.stl-content-group {
6181
margin-bottom: 15px;
6282
border: 1px solid #ddd;

includes/class-stl-admin.php

+156-5
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,159 @@ private function render_db_changes( $changes ) {
212212
return;
213213
}
214214

215-
// First render content groups (posts and related content)
216-
if ( isset( $changes['content_groups'] ) && ! empty( $changes['content_groups'] ) ) {
215+
// First render content groups organized by post type
216+
if ( isset( $changes['post_type_groups'] ) && ! empty( $changes['post_type_groups'] ) ) {
217+
?>
218+
<h3><?php esc_html_e( 'Content Changes', 'staging2live' ); ?></h3>
219+
<div class="stl-post-type-groups">
220+
<?php foreach ( $changes['post_type_groups'] as $post_type => $groups ) : ?>
221+
<div class="stl-post-type-group">
222+
<h3 class="stl-post-type-header">
223+
<?php echo esc_html( ucfirst( $post_type ) ); ?>
224+
</h3>
225+
<div class="stl-content-groups">
226+
<?php foreach ( $groups as $group ) : ?>
227+
<div class="stl-content-group">
228+
<div class="stl-group-header">
229+
<input type="checkbox" class="stl-select-group" data-group-id="<?php echo esc_attr( $group['group_id'] ); ?>">
230+
<h4>
231+
<?php echo esc_html( $group['title'] ); ?>
232+
<span class="stl-change-type stl-type-<?php echo esc_attr( $group['type'] ); ?>">
233+
<?php echo esc_html( $this->get_status_label( $group['type'] ) ); ?>
234+
</span>
235+
</h4>
236+
<button class="button stl-toggle-group" data-group-id="<?php echo esc_attr( $group['group_id'] ); ?>">
237+
<?php esc_html_e( 'Show Details', 'staging2live' ); ?>
238+
</button>
239+
</div>
240+
<div class="stl-group-content" id="group-content-<?php echo esc_attr( $group['group_id'] ); ?>" style="display:none;">
241+
<?php foreach ( $group['changes'] as $table => $table_changes ) : ?>
242+
<?php
243+
// Special handling for attachments to make them more visual
244+
if ($table === 'attachments') :
245+
?>
246+
<h5><?php esc_html_e( 'Media Attachments', 'staging2live' ); ?> (<?php echo count( $table_changes ); ?>)</h5>
247+
<div class="stl-attachments-grid">
248+
<?php foreach ( $table_changes as $change ) :
249+
$attachment_title = isset($change['details']['post_title']) ? $change['details']['post_title'] : '';
250+
$attachment_mime = isset($change['details']['post_mime_type']) ? $change['details']['post_mime_type'] : '';
251+
$is_image = strpos($attachment_mime, 'image/') === 0;
252+
253+
// Try to get the attachment URL from guid
254+
$attachment_url = isset($change['details']['guid']) ? $change['details']['guid'] : '';
255+
?>
256+
<div class="stl-attachment-item">
257+
<div class="stl-attachment-preview">
258+
<?php if ($is_image && $attachment_url) : ?>
259+
<img src="<?php echo esc_url($attachment_url); ?>" alt="<?php echo esc_attr($attachment_title); ?>">
260+
<?php else : ?>
261+
<div class="stl-attachment-icon dashicons dashicons-media-default"></div>
262+
<?php endif; ?>
263+
</div>
264+
<div class="stl-attachment-details">
265+
<label>
266+
<input
267+
type="checkbox"
268+
class="stl-select-db stl-group-item"
269+
data-group="<?php echo esc_attr( $group['group_id'] ); ?>"
270+
data-table="posts"
271+
value="<?php echo esc_attr( json_encode( array( 'table' => 'posts', 'id' => $change['id'] ) ) ); ?>"
272+
>
273+
<?php echo $attachment_title ? esc_html($attachment_title) : sprintf(esc_html__('Attachment ID: %s', 'staging2live'), $change['id']); ?>
274+
</label>
275+
<span class="stl-change-type stl-type-<?php echo esc_attr( $change['type'] ); ?>">
276+
<?php echo esc_html( $this->get_status_label( $change['type'] ) ); ?>
277+
</span>
278+
<button class="button stl-view-db-diff" data-table="posts" data-id="<?php echo esc_attr( $change['id'] ); ?>">
279+
<?php esc_html_e( 'View Details', 'staging2live' ); ?>
280+
</button>
281+
</div>
282+
</div>
283+
<?php endforeach; ?>
284+
</div>
285+
<?php
286+
// Special handling for child posts
287+
elseif ($table === 'child_posts') :
288+
?>
289+
<h5><?php esc_html_e( 'Child Posts', 'staging2live' ); ?> (<?php echo count( $table_changes ); ?>)</h5>
290+
<div class="stl-child-posts">
291+
<?php foreach ( $table_changes as $change ) :
292+
$post_title = isset($change['details']['post_title']) ? $change['details']['post_title'] : '';
293+
$post_type = isset($change['details']['post_type']) ? $change['details']['post_type'] : 'post';
294+
?>
295+
<div class="stl-child-post-item">
296+
<div class="stl-child-post-header">
297+
<label>
298+
<input
299+
type="checkbox"
300+
class="stl-select-db stl-group-item"
301+
data-group="<?php echo esc_attr( $group['group_id'] ); ?>"
302+
data-table="posts"
303+
value="<?php echo esc_attr( json_encode( array( 'table' => 'posts', 'id' => $change['id'] ) ) ); ?>"
304+
>
305+
<span class="stl-child-post-title">
306+
<?php echo $post_title ? esc_html($post_title) : sprintf(esc_html__('Post ID: %s', 'staging2live'), $change['id']); ?>
307+
</span>
308+
<span class="stl-child-post-type"><?php echo esc_html(ucfirst($post_type)); ?></span>
309+
</label>
310+
<span class="stl-change-type stl-type-<?php echo esc_attr( $change['type'] ); ?>">
311+
<?php echo esc_html( $this->get_status_label( $change['type'] ) ); ?>
312+
</span>
313+
</div>
314+
<div class="stl-child-post-actions">
315+
<button class="button stl-view-db-diff" data-table="posts" data-id="<?php echo esc_attr( $change['id'] ); ?>">
316+
<?php esc_html_e( 'View Details', 'staging2live' ); ?>
317+
</button>
318+
</div>
319+
</div>
320+
<?php endforeach; ?>
321+
</div>
322+
<?php else : ?>
323+
<h5><?php echo esc_html( ucfirst( $table ) ); ?> (<?php echo count( $table_changes ); ?>)</h5>
324+
<table class="widefat stl-changes-table stl-group-table">
325+
<thead>
326+
<tr>
327+
<th><input type="checkbox" class="stl-select-all-table" data-table="<?php echo esc_attr( $table ); ?>" data-group="<?php echo esc_attr( $group['group_id'] ); ?>"></th>
328+
<th><?php esc_html_e( 'Type', 'staging2live' ); ?></th>
329+
<th><?php esc_html_e( 'Changes', 'staging2live' ); ?></th>
330+
<th><?php esc_html_e( 'Action', 'staging2live' ); ?></th>
331+
</tr>
332+
</thead>
333+
<tbody>
334+
<?php foreach ( $table_changes as $change ) : ?>
335+
<tr>
336+
<td>
337+
<input
338+
type="checkbox"
339+
class="stl-select-db stl-group-item"
340+
data-group="<?php echo esc_attr( $group['group_id'] ); ?>"
341+
data-table="<?php echo esc_attr( $table ); ?>"
342+
value="<?php echo esc_attr( json_encode( array( 'table' => $table, 'id' => $change['id'] ) ) ); ?>"
343+
>
344+
</td>
345+
<td><?php echo esc_html( $change['type'] ); ?></td>
346+
<td><?php echo esc_html( $change['summary'] ); ?></td>
347+
<td>
348+
<button class="button stl-view-db-diff" data-table="<?php echo esc_attr( $table ); ?>" data-id="<?php echo esc_attr( $change['id'] ); ?>">
349+
<?php esc_html_e( 'View Details', 'staging2live' ); ?>
350+
</button>
351+
</td>
352+
</tr>
353+
<?php endforeach; ?>
354+
</tbody>
355+
</table>
356+
<?php endif; ?>
357+
<?php endforeach; ?>
358+
</div>
359+
</div>
360+
<?php endforeach; ?>
361+
</div>
362+
</div>
363+
<?php endforeach; ?>
364+
</div>
365+
<?php
366+
} else if ( isset( $changes['content_groups'] ) && ! empty( $changes['content_groups'] ) ) {
367+
// Fallback to old structure if post_type_groups not available
217368
?>
218369
<h3><?php esc_html_e( 'Content Changes', 'staging2live' ); ?></h3>
219370
<div class="stl-content-groups">
@@ -360,7 +511,7 @@ class="stl-select-db stl-group-item"
360511
$standalone_changes = false;
361512
foreach ( $changes as $table => $table_changes ) {
362513
// Skip the content groups as we've already rendered them
363-
if ( $table === 'content_groups' ) {
514+
if ( $table === 'content_groups' || $table === 'post_type_groups' ) {
364515
continue;
365516
}
366517

@@ -386,8 +537,8 @@ class="stl-select-db stl-group-item"
386537
<tbody>
387538
<?php
388539
foreach ( $changes as $table => $table_changes ) :
389-
// Skip the content groups
390-
if ( $table === 'content_groups' ) {
540+
// Skip the content groups and post type groups
541+
if ( $table === 'content_groups' || $table === 'post_type_groups' ) {
391542
continue;
392543
}
393544
foreach ( $table_changes as $change ) :

includes/class-stl-db-comparer.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ private function group_related_changes($changes) {
421421
$child_to_parent_mapping = array();
422422
$attachment_ids = array(); // Store all attachment IDs for later reference
423423
$post_to_attachment_mapping = array(); // Map posts to their attachments
424+
$post_type_groups = array(); // New array to hold post type groups
424425

425426
// First pass: identify posts with parent relationships
426427
if (isset($changes['posts'])) {
@@ -861,10 +862,30 @@ private function group_related_changes($changes) {
861862

862863
// Merge the post groups into the final result
863864
foreach ($post_groups as $group) {
864-
if (!isset($grouped['content_groups'])) {
865-
$grouped['content_groups'] = array();
865+
// Get the post type from the first post in the group's changes
866+
$post_type = 'unknown';
867+
if (isset($group['changes']['posts']) && !empty($group['changes']['posts'])) {
868+
$first_post = $group['changes']['posts'][0];
869+
$post_type = isset($first_post['details']['post_type']) ? $first_post['details']['post_type'] : 'unknown';
870+
} elseif (isset($group['changes']['child_posts']) && !empty($group['changes']['child_posts'])) {
871+
$first_post = $group['changes']['child_posts'][0];
872+
$post_type = isset($first_post['details']['post_type']) ? $first_post['details']['post_type'] : 'unknown';
873+
} elseif (isset($group['changes']['attachments']) && !empty($group['changes']['attachments'])) {
874+
$post_type = 'attachment';
866875
}
867-
$grouped['content_groups'][] = $group;
876+
877+
// Initialize post type group if it doesn't exist
878+
if (!isset($post_type_groups[$post_type])) {
879+
$post_type_groups[$post_type] = array();
880+
}
881+
882+
// Add this group to the appropriate post type group
883+
$post_type_groups[$post_type][] = $group;
884+
}
885+
886+
// Now add the post type groups to the final result
887+
if (!empty($post_type_groups)) {
888+
$grouped['post_type_groups'] = $post_type_groups;
868889
}
869890

870891
return $grouped;

0 commit comments

Comments
 (0)