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 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/class-adjacent-post-order.php (limited to 'src/class-adjacent-post-order.php') 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"; + } +} -- cgit v1.2.3