settings = Plugin::get_settings( 'taxonomies' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !current_user_can( 'edit_posts' ) ) { return; } if( empty( $this->settings['drag_and_drop_ordering_taxonomies'] ) ) { return; } add_action( 'init', [ $this, 'init' ] ); if( is_admin() ) { add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'wp_ajax_adminease_update_term_order', [ $this, 'ajax_update_term_order' ] ); add_filter( 'adminease_global_inline_css', [ $this, 'add_global_css' ] ); } add_filter( 'get_terms', [ $this, 'sort_terms_by_menu_order' ], 10, 4 ); } /** * Modifies the settings fields to include a drag-and-drop ordering option. * * @param array $fields An array of existing settings fields. * * @return array The modified settings fields array, including the drag-and-drop ordering option. */ public function adminease_settings_fields( array $fields ): array { $fields['taxonomies']['fields'][] = [ 'type' => 'switch', 'id' => 'drag-and-drop-ordering-taxonomies', 'name' => 'adminease[taxonomies][drag_and_drop_ordering_taxonomies]', 'value' => $this->settings['drag_and_drop_ordering_taxonomies'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Order Taxonomies using Drag and Drop', 'adminease' ), 'description' => __( "Rearrange items on your site exactly how you want just click, drag, and drop! Instantly organize taxonomies with a simple, intuitive interface. No coding, no hassle, just smooth, visual control over your site’s order. Make your WordPress experience truly yours!", 'adminease' ), ]; return $fields; } /** * Executes the necessary setup by attaching actions and filters. * @return void */ public function init(): void { $this->taxonomies = apply_filters( 'adminease_drag_and_drop_ordering_taxonomies_taxonomies', Utils::get_taxonomies( [ 'base' ] ) ); foreach( $this->taxonomies as $taxonomy_key => $taxonomy ) { add_action( "manage_{$taxonomy_key}_custom_column", [ $this, 'render_term_order_column' ], 10, 3 ); add_filter( "manage_edit-{$taxonomy_key}_columns", [ $this, 'add_term_order_column' ] ); } } /** * Enqueues the necessary scripts for drag-and-drop ordering in the admin interface. * * @param string $hook The current admin page hook suffix. * * @return void */ public function admin_enqueue_scripts( string $hook ): void { if( 'edit-tags.php' !== $hook ) { return; } wp_enqueue_script( ADMINEASE_NAME . 'DragDropOrderingTaxonomies', ADMINEASE_PLUGIN_URL . 'assets/js/AdminEaseDragAndDropOrderingTaxonomies.js', [ 'jquery-ui-sortable' ], filemtime( ADMINEASE_DIR . 'assets/js/AdminEaseDragAndDropOrderingTaxonomies.js' ), true ); wp_localize_script( ADMINEASE_NAME . 'DragDropOrderingTaxonomies', ADMINEASE_NAME . 'DragDropOrderingTaxonomiesAjaxObj', [ 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'security' => [ 'termsNonce' => wp_create_nonce( 'adminease_update_term_order_nonce' ), ], 'i18n' => [ 'updating' => esc_html__( 'Updating taxonomy order...', 'adminease' ), 'success' => esc_html__( 'Taxonomy order updated successfully.', 'adminease' ), 'updateFailed' => esc_html__( 'The new term order could not be saved. Refresh the page and try again.', 'adminease' ), 'networkError' => esc_html__( 'The new term order could not be saved because the server did not respond. Refresh the page and try again.', 'adminease' ), ], ] ); } /** * Sorts terms by their 'menu_order' meta value, prioritizing those with menu order set. * * @param array $terms Array of found terms. * @param array|null $taxonomies An array of taxonomies if known. * @param array $query_vars An array of get_terms() arguments. * @param WP_Term_Query $term_query The WP_Term_Query object. * * @return array The sorted array of terms by 'menu_order' and name fallback. */ public function sort_terms_by_menu_order( array $terms, ?array $taxonomies, array $query_vars, WP_Term_Query $term_query ): array { if( empty( $terms ) || is_wp_error( $terms ) ) { return $terms; } if( null === $taxonomies ) { return $terms; } $fields = $query_vars['fields'] ?? 'all'; if( 'all' !== $fields && 'all_with_object_id' !== $fields ) { return $terms; } $target_taxonomies = array_intersect_key( $this->taxonomies, array_flip( $taxonomies ) ); if( empty( $target_taxonomies ) ) { return $terms; } $term_ids = array_filter( wp_list_pluck( $terms, 'term_id' ) ); if( !empty( $term_ids ) ) { update_termmeta_cache( $term_ids ); } $order_map = []; foreach( $terms as $term ) { if( is_a( $term, 'WP_Term' ) ) { $order_map[ $term->term_id ] = get_term_meta( $term->term_id, 'menu_order', true ); } } usort( $terms, function( $a, $b ) use ( $order_map ) { if( !is_a( $a, 'WP_Term' ) || !is_a( $b, 'WP_Term' ) ) { return 0; } $order_a = $order_map[ $a->term_id ] ?? ''; $order_b = $order_map[ $b->term_id ] ?? ''; if( $order_a !== '' && $order_b !== '' ) { return (int) $order_a - (int) $order_b; } if( $order_a !== '' ) { return - 1; } if( $order_b !== '' ) { return 1; } return strcmp( $a->name, $b->name ); } ); remove_filter( 'get_terms', [ $this, 'sort_terms_by_menu_order' ], 10 ); return $terms; } /** * Adds drag and drop CSS to the global stylesheet * * @param string $css Existing CSS * * @return string Modified CSS with drag and drop styles */ public function add_global_css( string $css ): string { $drag_drop_css = ' .column-order { width: 30px !important; text-align: center; } .column-order > .ae-icon { padding: 0 5px; } .adminease-orderable .ui-draggable-handle, .adminease-orderable .ui-sortable-handle { cursor: grab; } tbody tr.adminease-orderable { transition: background-color 0.3s ease; } /* The spinner and the save feedback had no rules at all, so the icon never turned and success and failure looked identical. The colours are the WordPress admin palette rather than --pwp-* tokens, because .pwp-app is only applied on the AdminEase dashboard screen. */ .drag-handle { transition: color 0.3s ease; } .drag-handle .ae-icon-spin { animation: adminease-drag-handle-spin 1s infinite linear; } .drag-handle.success-flash { color: #00a32a; } .drag-handle.error-flash { color: #d63638; } @keyframes adminease-drag-handle-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } '; return $css . $drag_drop_css; } /** * Handles AJAX request to update the order of taxonomy terms. * Validates nonce and user permissions, processes term IDs, updates the order, and returns success or error response. * @return void */ public function ajax_update_term_order(): void { if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_update_term_order_nonce' ) ) { wp_send_json_error( new WP_Error( 'security_error', esc_html__( 'Security check failed. Refresh the page and try again.', 'adminease' ) ), 403 ); } if( !current_user_can( 'edit_posts' ) ) { wp_send_json_error( new WP_Error( 'permissions', esc_html__( 'You do not have sufficient permissions to perform this action', 'adminease' ) ) ); } $term_ids = isset( $_POST['term_ids'] ) ? array_map( 'absint', $_POST['term_ids'] ) : []; if( empty( $term_ids ) ) { wp_send_json_error( esc_html__( 'No terms to update', 'adminease' ) ); } $this->update_terms_order( $term_ids ); wp_send_json_success( esc_html__( 'Terms order updated successfully', 'adminease' ) ); } /** * Updates the order of taxonomy terms using term meta. * * @param array $term_ids An array of term IDs, where the array keys represent the new positions. * * @return void This method does not return a value. */ private function update_terms_order( array $term_ids ): void { foreach( $term_ids as $position => $term_id ) { update_term_meta( $term_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' => Icons::lucide( 'align-justify' ) ], $columns ); } /** * Adds an "Order" column to taxonomy term columns. * * @param array $columns An array of existing columns. * * @return array The modified array of columns including the "Order" column. */ public function add_term_order_column( array $columns ): array { return array_merge( [ 'order' => Icons::lucide( 'align-justify' ) ], $columns ); } /** * Renders the custom order column in the admin taxonomies 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 ) { printf( '
%s
', esc_attr( $post_id ), Icons::lucide( 'grip-horizontal' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted SVG markup. ); } } /** * Renders the custom order column in the admin taxonomy terms table. * * @param string $content Column content (empty by default). * @param string $column_name The name of the column being rendered. * @param int $term_id The ID of the current term. * * @return void */ public function render_term_order_column( string $content, string $column_name, int $term_id ): void { if( 'order' === $column_name ) { printf( '
%s
', esc_attr( $term_id ), Icons::lucide( 'grip-horizontal' ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted SVG markup. ); } } }