From 7cf94f3c96addb08f82a2c1c685eed20ad6cb2f8 Mon Sep 17 00:00:00 2001 From: Bao Date: Wed, 25 Sep 2024 17:14:16 +0700 Subject: [PATCH 1/2] Fix sort posts in the table list --- assets/order.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/order.js b/assets/order.js index 430b955..2142f1e 100644 --- a/assets/order.js +++ b/assets/order.js @@ -1,5 +1,5 @@ ( function ( $ ) { - $( 'table.posts #the-list' ).sortable( { + $( 'table.posts #the-list, table.pages #the-list' ).sortable( { 'items': 'tr', 'axis': 'y', 'handle': '.mbcpt_order', From 9d542e8c8ab9810d27b4a8f29052acd51058c390 Mon Sep 17 00:00:00 2001 From: Bao Date: Mon, 21 Oct 2024 14:41:50 +0700 Subject: [PATCH 2/2] Update order items --- assets/order.css | 14 +++++++++++++- assets/order.js | 35 ++++++++++++++++++++++++++++++++--- src/Order.php | 32 ++++++++++++++++++++++++++++---- 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/assets/order.css b/assets/order.css index cec4b0d..ebc6979 100644 --- a/assets/order.css +++ b/assets/order.css @@ -1,10 +1,18 @@ .wp-list-table .column-mbcpt_order { - width: 20px; + width: 22px; padding-top: 10px; padding-right: 0; padding-left: 4px; } +.column-mbcpt_order.spinner.is-active { + background-size: 18px 18px; + position: relative; + top: 10px; + left: 4px; + margin: 0; +} + .column-mbcpt_order svg { display: none; width: 18px; @@ -17,6 +25,10 @@ tr:hover .column-mbcpt_order svg { display: inline-block; } +tr:hover .column-mbcpt_order.spinner.is-active svg { + display: none; +} + .column-mbcpt_order+.check-column.check-column { padding-left: 0; } diff --git a/assets/order.js b/assets/order.js index 2142f1e..e0bfedf 100644 --- a/assets/order.js +++ b/assets/order.js @@ -1,13 +1,42 @@ ( function ( $ ) { - $( 'table.posts #the-list, table.pages #the-list' ).sortable( { + const table = jQuery( ".wp-list-table tbody" ); + table.sortable( { 'items': 'tr', 'axis': 'y', 'handle': '.mbcpt_order', - 'update': function () { + 'update': function ( e, o ) { + const id = o.item[ 0 ].id.substr( 5 ); + let prevId = ''; + if ( o.item.prev().length > 0 ) { + prevId = o.item.prev().attr( "id" ).substr( 5 ); + } + let nextId = ''; + if ( o.item.next().length > 0 ) { + nextId = o.item.next().attr( "id" ).substr( 5 ); + } + let $postId = $( `#post-${ id }` ); + $postId.find( '.column-mbcpt_order' ).addClass( 'spinner is-active' ); + table.sortable( 'disable' ); $.post( ajaxurl, { - action: 'mbcpt_update_menu_order', + action: 'mbcpt_update_order_items', + id: id, + prev_id: prevId, + next_id: nextId, order: $( '#the-list' ).sortable( 'serialize' ), security: MBCPT.security + }, response => { + if ( !response.success ) { + alert( response.data ); + window.location.reload(); + } + let title = $( `#inline_${ id }` ).find( '.post_title' ).text(); + let l = ''; + for ( let s = 0; s < response.data; s++ ) { + l = `— ${ l }`; + } + $postId.find( '.row-title' ).html( l + title ); + $postId.find( '.column-mbcpt_order' ).removeClass( 'spinner is-active' ); + table.sortable( 'enable' ); } ); } } ); diff --git a/src/Order.php b/src/Order.php index 0259506..6b4064e 100644 --- a/src/Order.php +++ b/src/Order.php @@ -6,7 +6,7 @@ class Order { public function __construct() { add_action( 'load-edit.php', [ $this, 'setup_for_edit_screen' ] ); - add_action( 'wp_ajax_mbcpt_update_menu_order', [ $this, 'update_menu_order' ] ); + add_action( 'wp_ajax_mbcpt_update_order_items', [ $this, 'update_order_items' ] ); add_action( 'pre_get_posts', [ $this, 'set_orderby_menu_order' ] ); add_filter( 'get_previous_post_where', [ $this, 'order_previous_post_where' ] ); add_filter( 'get_previous_post_sort', [ $this, 'order_previous_post_sort' ] ); @@ -63,14 +63,35 @@ private function set_initial_orders( string $post_type ): void { ); } - public function update_menu_order(): void { + public function update_order_items(): void { check_ajax_referer( 'order', 'security' ); - global $wpdb; parse_str( $_POST['order'], $data ); if ( ! is_array( $data ) ) { - return; + wp_send_json_error( __( 'Error: Invalid data!', 'mb-custom-post-type' ) ); + } + + $post_id = empty( $_POST['id'] ) ? false : (int) $_POST['id']; + $prev_id = empty( $_POST['prev_id'] ) ? false : (int) $_POST['prev_id']; + $next_id = empty( $_POST['next_id'] ) ? false : (int) $_POST['next_id']; + if ( ! $post_id ) { + wp_send_json_error( __( 'Missing mandatory parameters.', 'mb-custom-post-type' ) ); } + $parent_id = wp_get_post_parent_id( $post_id ); + $next_post_parent = $next_id ? wp_get_post_parent_id( $next_id ) : false; + if ( $prev_id === $next_post_parent ) { + $parent_id = $next_post_parent; + } + if ( $next_post_parent !== $parent_id ) { + $prev_post_parent = $prev_id ? wp_get_post_parent_id( $prev_id ) : false; + if ( $prev_post_parent !== $parent_id ) { + $parent_id = ( false !== $prev_post_parent ) ? $prev_post_parent : $next_post_parent; + } + } + wp_update_post([ + 'ID' => $post_id, + 'post_parent' => $parent_id, + ]); $id_arr = []; foreach ( $data as $values ) { @@ -98,6 +119,9 @@ public function update_menu_order(): void { ); } } + + $ancestors = get_post_ancestors( $post_id ); + wp_send_json_success( count( $ancestors ) ); } public function set_orderby_menu_order( WP_Query $query ): void {