summaryrefslogtreecommitdiff
path: root/src/class-draggable-post-order.php
blob: ee9156cd813f7f3d62afa64990d5f846ba7fb0f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * Entry point class file
 *
 * @package Draggable_Post_Order
 */

namespace Draggable_Post_Order;

/**
 * Class DraggablePostOrder
 *
 * @package 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( self::$post_type_feature );

		// if there are no post types to order => return.
		if ( empty( $post_types ) ) {
			return;
		}

		foreach ( $post_types as $post_type ) {
			add_action( "save_post_{$post_type}", [ self::class, 'save_post' ], 10, 2 );
		}

		// add meta box.
		add_action( 'add_meta_boxes', [ self::class, 'add_meta_boxes' ] );

		// add ajax hook.
		add_action( 'wp_ajax_update-post-order', [ self::class, 'update_post_order' ] );

		// load scripts.
		add_action( 'admin_enqueue_scripts', [ self::class, 'enqueue_scripts' ] );

		// sort posts by post order.
		add_action( 'pre_get_posts', [ self::class, 'order_posts' ] );

		// sort for adjacent posts.
		Adjacent_Post_Order::init();
	}

	/**
	 * Whether the given post type is allowed to be ordered.
	 *
	 * @param string $post_type The post type to check against.
	 *
	 * @return bool
	 */
	public static function supports( $post_type = null ) {
		$post_type ??= get_current_screen()->post_type ?? '';

		return post_type_supports( $post_type, self::$post_type_feature );
	}

	/**
	 * Callback for "save_post" action.
	 *
	 * @param int      $post_id The post's ID.
	 * @param \WP_Post $post    The post object.
	 */
	public static function save_post( $post_id, $post ) {
		if (
			( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
			! check_admin_referer( self::$nonce, self::$nonce ) ||
			( ! isset( $_POST['post_ID'] ) || intval( $_POST['post_ID'] ) !== intval( $post_id ) ) ||
			! isset( $_POST[ self::$meta_key ] )
		) {
			return;
		}

		$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, self::$meta_key, $post_order );
	}

	/**
	 * Callback for "add_meta_boxes" action.
	 */
	public static function add_meta_boxes() {
		if ( ! self::supports() ) {
			return;
		}

		add_meta_box( 'draggable-post-order', __( 'Post Order', 'draggable-post-order' ), [ self::class, 'add_meta_box' ], '', 'side' );
	}

	/**
	 * Callback for "add_meta_box" function.
	 *
	 * @param \WP_Post $post The post object.
	 */
	public static function add_meta_box( $post ) {
		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
	}

	/**
	 * Callback for "wp_ajax" action.
	 */
	public static function update_post_order() {
		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'] );
		parse_str( sanitize_text_field( wp_unslash( $_POST['postOrder'] ) ), $post_order );

		foreach ( $post_order['post'] as $idx => $post_id ) {
			$order = intval( $idx ) + 1;
			update_post_meta( $post_id, self::$meta_key, ( ( $page - 1 ) * $per_page ) + $order );
		}
	}

	/**
	 * Callback for "admin_enqueue_scripts" action.
	 */
	public static function enqueue_scripts() {
		$current_screen = get_current_screen();
		if ( $current_screen->base !== 'edit' || ! self::supports() ) {
			return;
		}

		list('dependencies' => $dependencies, 'version' => $version) = require draggable_post_order_path() . '/build/index.asset.php';
		$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 ),
			]
		);
	}

	/**
	 * Callback for "pre_get_posts" action.
	 *
	 * @param \WP_Query $query The query object.
	 */
	public static function order_posts( \WP_Query $query ) {
		$post_type = $query->get( 'post_type' );
		if ( ! self::supports( $post_type ) ) {
			return;
		}

		// if no orderby was set previously.
		if ( ! $query->get( 'orderby' ) ) {
			$meta_sub_query = [
				'relation'                           => 'OR',
				'draggable-post-order-clause'        => [
					'key'  => self::$meta_key,
					'type' => 'NUMERIC',
				],
				// get all posts without the meta as well.
				'draggable-post-order-exists-clause' => [
					'key'     => self::$meta_key,
					'compare' => 'NOT EXISTS',
				],
			];
			$meta_query   = $query->get( 'meta_query' ) ?: [];
			$meta_query[] = $meta_sub_query;

			$order   = apply_filters( 'draggable_post_order_order', 'ASC', $post_type, $query );
			$orderby = [
				'draggable-post-order-clause'        => $order,
				'draggable-post-order-exists-clause' => $order,
			];

			$query->set( 'meta_query', $meta_query );
			$query->set( 'orderby', $orderby );
		}
	}
}