summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/class-adjacent-post-order.php124
-rw-r--r--src/class-draggable-post-order.php57
-rw-r--r--src/index.js3
3 files changed, 171 insertions, 13 deletions
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 @@
+<?php
+/**
+ * Functions for getting the correct adjacent post
+ *
+ * @package Draggable_Post_Order
+ */
+
+namespace Draggable_Post_Order;
+
+/**
+ * Class Adjacent_Post_Order
+ *
+ * @package Draggable_Post_Order
+ */
+class Adjacent_Post_Order {
+
+ /**
+ * Initialize.
+ */
+ public static function init() {
+ add_filter( 'get_previous_post_join', [ self::class, 'get_post_join' ], 10, 5 );
+ add_filter( 'get_next_post_join', [ self::class, 'get_post_join' ], 10, 5 );
+ add_filter( 'get_previous_post_where', [ self::class, 'get_previous_post_where' ], 10, 5 );
+ add_filter( 'get_next_post_where', [ self::class, 'get_next_post_where' ], 10, 5 );
+ add_filter( 'get_previous_post_sort', [ self::class, 'get_post_sort' ], 10, 3 );
+ add_filter( 'get_next_post_sort', [ self::class, 'get_post_sort' ], 10, 3 );
+ }
+
+ /**
+ * Callback for "get_previous_post_join" and "get_previous_post_join" filters.
+ * Joins in the postmeta table for further use.
+ *
+ * @param string $join The JOIN 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_post_join( $join, $in_same_term, $excluded_terms, $taxonomy, $post ) {
+ if ( ! Draggable_Post_Order::supports( $post->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
@@ -15,10 +15,31 @@ 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' );
- ?><input type="number" name="draggable-post-order" value="<?php echo get_post_meta( $post->ID, 'draggable-post-order', true ); ?>">
+ wp_nonce_field( self::$nonce, self::$nonce );
+ ?><input type="number" name="<?php echo esc_html( self::$meta_key ); ?>" value="<?php echo esc_html( get_post_meta( $post->ID, self::$meta_key, true ) ); ?>">
<?php
}
@@ -105,17 +129,19 @@ class Draggable_Post_Order {
* Callback for "wp_ajax" action.
*/
public static function update_post_order() {
- if ( ! isset( $_POST['page'] ) || ! isset( $_POST['perPage'] ) || ! isset( $_POST['postOrder'] ) ) {
+ if ( ! check_ajax_referer( self::$nonce, 'nonce' ) ||
+ ! isset( $_POST['page'] ) || ! isset( $_POST['perPage'] ) || ! isset( $_POST['postOrder'] ) ) {
return;
}
$page = intval( $_POST['page'] );
$per_page = intval( $_POST['perPage'] );
+ //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
parse_str( $_POST['postOrder'], $post_order );
foreach ( $post_order['post'] as $order => $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' ),