Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort posts in the table list #74

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion assets/order.css
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand Down
35 changes: 32 additions & 3 deletions assets/order.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
( function ( $ ) {
$( 'table.posts #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 = `&mdash; ${ l }`;
}
$postId.find( '.row-title' ).html( l + title );
$postId.find( '.column-mbcpt_order' ).removeClass( 'spinner is-active' );
table.sortable( 'enable' );
} );
}
} );
Expand Down
49 changes: 41 additions & 8 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
namespace MBCPT;

use WP_Query;
use MetaBox\Support\Data;

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( 'admin_init', [ $this, 'add_admin_columns' ] );
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' ] );
Expand All @@ -20,10 +22,6 @@ public function setup_for_edit_screen(): void {
return;
}

// Add admin columns
add_filter( "manage_{$screen->post_type}_posts_columns", [ $this, 'add_admin_order_column' ] );
add_action( "manage_{$screen->post_type}_posts_custom_column", [ $this, 'show_admin_order_column' ] );

// Set initial orders
$this->set_initial_orders( $screen->post_type );

Expand All @@ -33,6 +31,17 @@ public function setup_for_edit_screen(): void {
wp_localize_script( 'order', 'MBCPT', [ 'security' => wp_create_nonce( 'order' ) ] );
}

public function add_admin_columns() {
$post_types = Data::get_post_types();
foreach ( $post_types as $slug => $post_type ) {
if ( ! $this->is_enabled_ordering( $slug ) ) {
continue;
}
add_filter( "manage_{$slug}_posts_columns", [ $this, 'add_admin_order_column' ] );
add_action( "manage_{$slug}_posts_custom_column", [ $this, 'show_admin_order_column' ] );
}
}

private function set_initial_orders( string $post_type ): void {
global $wpdb;
$query = $wpdb->prepare(
Expand Down Expand Up @@ -63,15 +72,36 @@ 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 ) {
foreach ( $values as $id ) {
Expand All @@ -98,6 +128,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 {
Expand Down