post_types = [ 'post', 'page' ]; $misc = Plugin::get_settings( 'misc' ); if( isset( $misc['drag_and_drop_ordering'] ) && $misc['drag_and_drop_ordering'] ) { add_action( 'init', [ $this, 'init' ] ); add_action( 'admin_init', [ $this, 'admin_init' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); add_action( 'wp_ajax_update_post_order', [ $this, 'ajax_update_post_order' ] ); add_filter( 'pre_get_posts', [ $this, 'set_post_order' ] ); add_filter( 'post_class', [ $this, 'post_class' ], 10, 3 ); } } /** * Executes the necessary setup by attaching actions and filters. * @return void */ public function init(): void { $this->post_types = apply_filters( 'adminease_drag_and_drop_ordering_post_types', $this->post_types ); foreach( $this->post_types as $post_type ) { add_action( "manage_{$post_type}_posts_custom_column", [ $this, 'render_order_column' ], 10, 2 ); add_filter( "manage_edit-{$post_type}_columns", [ $this, 'add_order_column' ] ); add_filter( "manage_{$post_type}_posts_columns", [ $this, 'add_order_column' ] ); } } /** * Initializes admin-specific actions by enabling page attributes support for all registered post types. * @return void */ public function admin_init(): void { foreach( $this->post_types as $post_type ) { add_post_type_support( $post_type, 'page-attributes' ); } } /** * Modifies the query to set post order for specific post types. * * @param WP_Query $query The query object to be modified. * * @return WP_Query The modified query object. */ public function set_post_order( WP_Query $query ): WP_Query { if( $query->is_main_query() && in_array( $query->get( 'post_type' ), $this->post_types ) ) { $query->set( 'orderby', 'menu_order' ); $query->set( 'order', 'ASC' ); } return $query; } /** * Filters the list of CSS classes for the current post. * * @param array $classes An array of CSS classes for the post element. * @param $class A comma-separated list of additional classes added to the post. * @param int $post_id The ID of the current post. * * @return array An updated array of CSS classes for the post element. */ public function post_class( array $classes, $class, int $post_id ): array { if( is_admin() ) { $classes[] = ADMINEASE_SLUG . '-orderable'; } return $classes; } /** * Enqueues the necessary scripts for drag and drop functionality on specific admin pages. * * @param string $hook The current admin page hook suffix. * * @return void */ public function enqueue_scripts( string $hook ): void { if( !in_array( $hook, [ 'edit.php' ] ) ) { return; } wp_enqueue_script( ADMINEASE_SLUG . '-drag-drop-ordering', plugins_url( 'assets/js/drag-drop.js', ADMINEASE_FILE ), [ 'jquery-ui-sortable' ], ADMINEASE_VERSION, true ); wp_localize_script( ADMINEASE_SLUG . '-drag-drop-ordering', ADMINEASE_SLUG . 'DragDropOrdering', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'update_post_order_nonce' ), 'i18n' => [ 'updating' => esc_html__( 'Updating post order...', 'adminease' ), 'success' => esc_html__( 'Post order updated successfully.', 'adminease' ), ], ] ); } /** * Handles AJAX request to update the order of posts. * Validates nonce and user permissions, processes post IDs, updates the order, and returns success or error response. * @return void */ public function ajax_update_post_order(): void { check_ajax_referer( 'update_post_order_nonce', 'nonce' ); if( !current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Permission denied' ); } $post_ids = isset( $_POST['post_ids'] ) ? array_map( 'absint', $_POST['post_ids'] ) : []; if( empty( $post_ids ) ) { wp_send_json_error( 'No posts to update' ); } $this->update_posts_order( $post_ids ); wp_send_json_success( 'Posts order updated successfully' ); } /** * Updates the menu order of posts based on the provided post IDs and their positions. * * @param array $post_ids An array of post IDs, where the array keys represent the new positions. * * @return void This method does not return a value. */ private function update_posts_order( array $post_ids ): void { foreach( $post_ids as $position => $post_id ) { wp_update_post( [ 'ID' => $post_id, 'menu_order' => $position, ] ); } } /** * Adds an "Order" column to the list of columns. * * @param array $columns An array of existing columns. * * @return array The modified array of columns including the "Order" column. */ public function add_order_column( array $columns ): array { return array_merge( [ 'order' => '' ], $columns ); } /** * Renders the custom order column in the admin posts table. * * @param string $column_name The name of the column being rendered. * @param int $post_id The ID of the current post. * * @return void */ public function render_order_column( string $column_name, int $post_id ): void { if( 'order' === $column_name ) { echo sprintf( '
', esc_attr( $post_id ) ); } } }