cap->edit_others_posts ) ) {
return;
}
$sorted_other = isset( $_GET['orderby'] ) && 'menu_order title' !== $_GET['orderby'] && 'menu_order' !== $_GET['orderby']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$filtered = ! empty( $_GET['s'] ) || ! empty( $_GET['m'] ) || ! empty( $_GET['cat'] ) || ! empty( $_GET['author'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
add_action(
'admin_enqueue_scripts',
function () use ( $sorted_other, $filtered, $type ) {
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'wp-api-fetch' );
wp_register_script( 'svabg-ordering', '', array( 'jquery', 'jquery-ui-sortable', 'wp-api-fetch' ), SOFTVOLT_AI_BODYGUARD_VERSION, true );
wp_enqueue_script( 'svabg-ordering' );
wp_add_inline_script( 'svabg-ordering', 'window.svabgOrdering = ' . wp_json_encode( array(
'path' => '/' . Softvolt_Ai_Bodyguard_Rest::NS . '/ordering',
'type' => $type,
'active' => ! $sorted_other && ! $filtered,
'saved' => __( 'Order saved.', 'ai-guardwall' ),
'failed' => __( 'The order could not be saved. Reload the page and try again.', 'ai-guardwall' ),
'hint' => __( 'Drag the rows to change the order. It is saved at once.', 'ai-guardwall' ),
'reset' => __( 'Sorted by another column or filtered: drag-and-drop works in the default order.', 'ai-guardwall' ),
'resetUrl' => admin_url( 'edit.php?post_type=' . $type ),
) ) . ';' . self::script(), 'after' );
wp_register_style( 'svabg-ordering', false, array(), SOFTVOLT_AI_BODYGUARD_VERSION );
wp_enqueue_style( 'svabg-ordering' );
wp_add_inline_style( 'svabg-ordering', '.svabg-sortable #the-list tr{cursor:move}.svabg-sortable #the-list tr.ui-sortable-helper{background:#fff;box-shadow:0 6px 18px rgba(0,0,0,.15);display:table}.svabg-sortable #the-list tr.svabg-placeholder td{background:#f0f6fc;border:1px dashed #2271b1}.svabg-order-note{margin:8px 0 0;color:#50575e}.svabg-order-note .dashicons{color:#2271b1}' );
}
);
}
private static function script() {
return <<<'JS'
jQuery( function ( $ ) {
var cfg = window.svabgOrdering || {};
var $list = $( '#the-list' );
if ( ! $list.length || ! $list.children( 'tr' ).not( '.no-items' ).length ) {
return;
}
var note = $( '
' );
if ( ! cfg.active ) {
note.append( document.createTextNode( cfg.reset + ' ' ) ).append( $( '' ).attr( 'href', cfg.resetUrl ).text( '↺' ) );
$( '.tablenav.top' ).after( note );
return;
}
note.append( document.createTextNode( cfg.hint ) );
$( '.tablenav.top' ).after( note );
$( 'body' ).addClass( 'svabg-sortable' );
$list.sortable( {
items: '> tr:not(.inline-edit-row):not(.no-items)',
axis: 'y',
cursor: 'move',
placeholder: 'svabg-placeholder',
helper: function ( e, tr ) {
tr.children().each( function () {
$( this ).width( $( this ).width() );
} );
return tr;
},
start: function ( e, ui ) {
ui.placeholder.html( ' | ' );
},
update: function () {
var ids = $list.children( 'tr' ).map( function () {
var m = ( this.id || '' ).match( /^post-(\d+)$/ );
return m ? parseInt( m[ 1 ], 10 ) : null;
} ).get();
var paged = parseInt( ( new URLSearchParams( window.location.search ) ).get( 'paged' ) || '1', 10 );
var perPage = $list.children( 'tr' ).length;
window.wp.apiFetch( { path: cfg.path, method: 'POST', data: { type: cfg.type, ids: ids, offset: ( paged - 1 ) * perPage } } ).then( function ( res ) {
note.find( '.svabg-order-msg' ).remove();
note.append( $( '' ).text( ' ' + ( res && res.ok ? cfg.saved : cfg.failed ) ) );
} ).catch( function () {
window.alert( cfg.failed );
} );
}
} );
} );
JS;
}
/**
* Sort the ordered lists by menu_order (admin), and on the site for the types that should follow it.
*/
public static function pre_get_posts( $query ) {
if ( ! $query->is_main_query() || $query->get( 'orderby' ) ) {
return;
}
$type = $query->get( 'post_type' );
$type = is_array( $type ) ? ( 1 === count( $type ) ? reset( $type ) : '' ) : (string) $type;
if ( is_admin() ) {
global $pagenow;
if ( 'edit.php' === $pagenow && '' !== $type && in_array( $type, self::types(), true ) && ! isset( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$query->set( 'orderby', 'menu_order title' );
$query->set( 'order', 'ASC' );
}
return;
}
$front = (array) Softvolt_Ai_Bodyguard_Modules::get( 'ordering_front' );
if ( ! $front ) {
return;
}
if ( '' === $type && ( $query->is_home() || $query->is_category() || $query->is_tag() ) ) {
$type = 'post';
}
if ( '' !== $type && in_array( $type, $front, true ) && ( $query->is_archive() || $query->is_home() ) ) {
$query->set( 'orderby', 'menu_order date' );
$query->set( 'order', 'ASC' );
}
}
/**
* Save the dragged order (REST).
*
* @param string $type Post type.
* @param array $ids Post ids in their new order.
* @param int $offset Position of the first id (rows on earlier pages).
* @return array|WP_Error
*/
public static function save( $type, array $ids, $offset = 0 ) {
global $wpdb;
$obj = get_post_type_object( $type );
if ( ! $obj || ! in_array( $type, self::types(), true ) || ! current_user_can( $obj->cap->edit_others_posts ) ) {
return new WP_Error( 'svabg_ordering', __( 'Ordering is not switched on for this type.', 'ai-guardwall' ) );
}
$n = 0;
foreach ( array_values( array_map( 'intval', $ids ) ) as $i => $id ) {
$post = get_post( $id );
if ( ! $post || $post->post_type !== $type || ! current_user_can( 'edit_post', $id ) ) {
continue;
}
$order = (int) $offset + $i;
if ( (int) $post->menu_order !== $order ) {
$wpdb->update( $wpdb->posts, array( 'menu_order' => $order ), array( 'ID' => $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
clean_post_cache( $id );
$n++;
}
}
return array( 'message' => __( 'Order saved.', 'ai-guardwall' ), 'changed' => $n );
}
}