From 3e0721eb5d64ef49b5e2d99f22195af8aef0fcb8 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Thu, 24 Dec 2020 15:43:29 +0100 Subject: Post-initial commit --- src/class-adjacent-post-order.php | 124 +++++++++++++++++++++++++++++++++++++ src/class-draggable-post-order.php | 57 +++++++++++++---- src/index.js | 3 +- 3 files changed, 171 insertions(+), 13 deletions(-) create mode 100644 src/class-adjacent-post-order.php (limited to 'src') diff --git a/src/class-adjacent-post-order.php b/src/class-adjacent-post-order.php new file mode 100644 index 0000000..3517696 --- /dev/null +++ b/src/class-adjacent-post-order.php @@ -0,0 +1,124 @@ +post_type ) ) { + return $join; + } + + global $wpdb; + + $join .= " INNER JOIN {$wpdb->postmeta} as pm ON p.id = pm.post_id"; + + return $join; + } + + /** + * Helper function to set the WHERE clause. + * + * @param string $where The `WHERE` clause in the SQL. + * @param WP_Post $post WP_Post object. + * @param string $op The comparative operator to use. + * + * @return string + */ + private static function get_post_where( $where, $post, $op ) { + if ( ! Draggable_Post_Order::supports( $post->post_type ) ) { + return $where; + } + + global $wpdb; + + $post_order = get_post_meta( $post->ID, Draggable_Post_Order::$meta_key, true ); + + return $wpdb->prepare( + // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + "WHERE pm.meta_key = %s AND pm.meta_value $op %s AND p.post_type = %s AND p.post_status = 'publish'", + Draggable_Post_Order::$meta_key, + $post_order, + $post->post_type + ); + } + + /** + * Callback for "get_previous_post_where" filter. + * + * @param string $where The `WHERE` clause in the SQL. + * @param bool $in_same_term Whether post should be in a same taxonomy term. + * @param array $excluded_terms Array of excluded term IDs. + * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. + * @param WP_Post $post WP_Post object. + * @return string + */ + public static function get_previous_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) { + return self::get_post_where( $where, $post, '<' ); + } + + /** + * Callback for "get_next_post_where" filter. + * + * @param string $where The `WHERE` clause in the SQL. + * @param bool $in_same_term Whether post should be in a same taxonomy term. + * @param array $excluded_terms Array of excluded term IDs. + * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true. + * @param WP_Post $post WP_Post object. + * @return string + */ + public static function get_next_post_where( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) { + return self::get_post_where( $where, $post, '>' ); + } + + /** + * Callback for "get_previous_post_sort" and "get_next_post_sort" filters. + * + * @param string $order_by The `ORDER BY` clause in the SQL. + * @param WP_Post $post WP_Post object. + * @param string $order Sort order. 'DESC' for previous post, 'ASC' for next. + * + * @return string + */ + public static function get_post_sort( $order_by, $post, $order ) { + if ( ! Draggable_Post_Order::supports( $post->post_type ) ) { + return $order_by; + } + + return "ORDER BY pm.meta_value $order LIMIT 1"; + } +} diff --git a/src/class-draggable-post-order.php b/src/class-draggable-post-order.php index ae9b501..a976816 100644 --- a/src/class-draggable-post-order.php +++ b/src/class-draggable-post-order.php @@ -14,11 +14,32 @@ namespace Draggable_Post_Order; */ class Draggable_Post_Order { + /** + * The meta key. + * + * @var string + */ + public static string $meta_key = 'draggable-post-order'; + + /** + * The post type feature. + * + * @var string + */ + public static string $post_type_feature = 'draggable-post-order'; + + /** + * The nonce name. + * + * @var string + */ + public static string $nonce = 'draggable-post-order'; + /** * Initialize. */ public static function init() { - $post_types = get_post_types_by_support( 'draggable-post-order' ); + $post_types = get_post_types_by_support( self::$post_type_feature ); // if there are no post types to order => return. if ( empty( $post_types ) ) { @@ -40,6 +61,9 @@ class Draggable_Post_Order { // sort posts by post order. add_action( 'pre_get_posts', [ self::class, 'order_posts' ] ); + + // sort for adjacent posts. + Adjacent_Post_Order::init(); } /** @@ -52,7 +76,7 @@ class Draggable_Post_Order { public static function supports( $post_type = null ) { $post_type ??= get_current_screen()->post_type ?? ''; - return post_type_supports( $post_type, 'draggable-post-order' ); + return post_type_supports( $post_type, self::$post_type_feature ); } /** @@ -64,19 +88,19 @@ class Draggable_Post_Order { public static function save_post( $post_id, $post ) { if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || - ! check_admin_referer( 'metaBoxDraggablePostOrder', 'metaBoxDraggablePostOrder' ) || + ! check_admin_referer( self::$nonce, self::$nonce ) || ( ! isset( $_POST['post_ID'] ) || intval( $_POST['post_ID'] ) !== intval( $post_id ) ) || - ! isset( $_POST['draggable-post-order'] ) + ! isset( $_POST[ self::$meta_key ] ) ) { return; } - $post_order = sanitize_text_field( wp_unslash( $_POST['draggable-post-order'] ) ); + $post_order = sanitize_text_field( wp_unslash( $_POST[ self::$meta_key ] ) ); if ( empty( $post_order ) ) { $post_order = wp_count_posts( $post->post_type )->publish; } - update_post_meta( $post_id, 'draggable-post-order', $post_order ); + update_post_meta( $post_id, self::$meta_key, $post_order ); } /** @@ -96,8 +120,8 @@ class Draggable_Post_Order { * @param \WP_Post $post The post object. */ public static function add_meta_box( $post ) { - wp_nonce_field( 'metaBoxDraggablePostOrder', 'metaBoxDraggablePostOrder' ); - ?> + wp_nonce_field( self::$nonce, self::$nonce ); + ?> $post_id ) { $order = intval( $order ) + 1; - update_post_meta( $post_id, 'draggable-post-order', ( ( $page - 1 ) * $per_page ) + $order ); + update_post_meta( $post_id, self::$meta_key, ( ( $page - 1 ) * $per_page ) + $order ); } } @@ -132,6 +158,13 @@ class Draggable_Post_Order { $dependencies[] = 'jquery-ui-sortable'; wp_enqueue_script( 'draggable-post-order', draggable_post_order_assets_url( 'index.js' ), $dependencies, $version, true ); wp_enqueue_style( 'draggable-post-order', draggable_post_order_assets_url( 'index.css' ), [], $version ); + wp_localize_script( + 'draggable-post-order', + 'draggablePostOrder', + [ + 'nonce' => wp_create_nonce( self::$nonce ), + ] + ); } /** @@ -150,12 +183,12 @@ class Draggable_Post_Order { $meta_sub_query = [ 'relation' => 'OR', 'draggable-post-order-clause' => [ - 'key' => 'draggable-post-order', + 'key' => self::$meta_key, 'type' => 'NUMERIC', ], // get all posts without the meta as well. 'draggable-post-order-exists-clause' => [ - 'key' => 'draggable-post-order', + 'key' => self::$meta_key, 'compare' => 'NOT EXISTS', ], ]; diff --git a/src/index.js b/src/index.js index eecabf6..2a2623f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -/* global jQuery ajaxurl */ +/* global jQuery ajaxurl draggablePostOrder */ import './index.scss'; @@ -35,6 +35,7 @@ import './index.scss'; ajaxurl, { action: 'update-post-order', + nonce: draggablePostOrder.nonce, page: $( '#current-page-selector' ).val(), perPage: $( '#edit_post_per_page' ).val(), postOrder: $( '#the-list' ).sortable( 'serialize' ), -- cgit v1.2.3