-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-custom-post-order.php
311 lines (242 loc) · 8.14 KB
/
simple-custom-post-order.php
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
$mt_order = new MT_Sort_Engine();
class MT_Sort_Engine {
function __construct() {
add_action( 'admin_init', array( $this, 'refresh' ) );
add_action( 'admin_init', array( $this, 'update_options' ) );
add_action( 'admin_init', array( $this, 'load_script_css' ) );
add_action( 'wp_ajax_update-menu-order', array( $this, 'update_menu_order' ) );
add_action( 'pre_get_posts', array( $this, 'mt_order_pre_get_posts' ) );
add_filter( 'get_previous_post_where', array( $this, 'mt_order_previous_post_where' ) );
add_filter( 'get_previous_post_sort', array( $this, 'mt_order_previous_post_sort' ) );
add_filter( 'get_next_post_where', array( $this, 'mt_order_next_post_where' ) );
add_filter( 'get_next_post_sort', array( $this, 'mt_order_next_post_sort' ) );
}
function _check_load_script_css() {
$active = false;
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return false;
}
if ( isset( $_GET['orderby'] ) || strstr( $_SERVER['REQUEST_URI'], 'action=edit' ) || strstr( $_SERVER['REQUEST_URI'], 'wp-admin/post-new.php' ) ) {
return false;
}
if ( ! empty( $objects ) ) {
if ( isset( $_GET['post_type'] ) && ! isset( $_GET['taxonomy'] ) && in_array( $_GET['post_type'], $objects ) ) { // if page or custom post types
$active = true;
}
if ( ! isset( $_GET['post_type'] ) && strstr( $_SERVER['REQUEST_URI'], 'wp-admin/edit.php' ) && in_array( 'post', $objects ) ) { // if post
$active = true;
}
}
return $active;
}
function load_script_css() {
if ( $this->_check_load_script_css() ) {
$core_path = plugin_dir_url(__FILE__);
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-sortable' );
wp_enqueue_script( 'mt_orderjs', $core_path . 'js/scporder.js', array( 'jquery' ), null, true );
wp_enqueue_style( 'mt_order', $core_path . 'css/scporder.css', array(), null );
}
}
function refresh() {
global $wpdb;
$objects = $this->get_mt_order_options_objects();
if ( ! empty( $objects ) ) {
foreach ( $objects as $object ) {
$result = $wpdb->get_results( "
SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
FROM $wpdb->posts
WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
" );
if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
continue;
}
$results = $wpdb->get_results( "
SELECT ID
FROM $wpdb->posts
WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
ORDER BY menu_order ASC
" );
foreach ( $results as $key => $result ) {
$wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => $result->ID ) );
}
}
}
}
function update_menu_order() {
global $wpdb;
parse_str( $_POST['order'], $data );
if ( ! is_array( $data ) ) {
return false;
}
$id_arr = array();
foreach ( $data as $key => $values ) {
foreach ( $values as $position => $id ) {
$id_arr[] = $id;
}
}
$menu_order_arr = array();
foreach ( $id_arr as $key => $id ) {
$results = $wpdb->get_results( "SELECT menu_order FROM $wpdb->posts WHERE ID = " . intval( $id ) );
foreach ( $results as $result ) {
$menu_order_arr[] = $result->menu_order;
}
}
sort( $menu_order_arr );
foreach ( $data as $key => $values ) {
foreach ( $values as $position => $id ) {
$wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_order_arr[ $position ] ), array( 'ID' => intval( $id ) ) );
}
}
}
function update_options() {
global $wpdb;
if ( ! isset( $_POST['mt_order_submit'] ) ) {
return false;
}
check_admin_referer( 'nonce_mt_order' );
$input_options = array();
$input_options['objects'] = isset( $_POST['objects'] ) ? $_POST['objects'] : '';
update_option( 'mt_order_options', $input_options );
$objects = $this->get_mt_order_options_objects();
if ( ! empty( $objects ) ) {
foreach ( $objects as $object ) {
$result = $wpdb->get_results( "
SELECT count(*) as cnt, max(menu_order) as max, min(menu_order) as min
FROM $wpdb->posts
WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
" );
if ( $result[0]->cnt == 0 || $result[0]->cnt == $result[0]->max ) {
continue;
}
if ( $object == 'page' ) {
$results = $wpdb->get_results( "
SELECT ID
FROM $wpdb->posts
WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
ORDER BY post_title ASC
" );
} else {
$results = $wpdb->get_results( "
SELECT ID
FROM $wpdb->posts
WHERE post_type = '" . $object . "' AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
ORDER BY post_date DESC
" );
}
foreach ( $results as $key => $result ) {
$wpdb->update( $wpdb->posts, array( 'menu_order' => $key + 1 ), array( 'ID' => $result->ID ) );
}
}
}
wp_redirect( 'admin.php?page=mt_order-settings&msg=update' );
}
function mt_order_previous_post_where( $where ) {
global $post;
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return $where;
}
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
$current_menu_order = $post->menu_order;
$where = "WHERE p.menu_order > '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'";
}
return $where;
}
function mt_order_previous_post_sort( $orderby ) {
global $post;
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return $orderby;
}
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
$orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
}
return $orderby;
}
function mt_order_next_post_where( $where ) {
global $post;
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return $where;
}
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
$current_menu_order = $post->menu_order;
$where = "WHERE p.menu_order < '" . $current_menu_order . "' AND p.post_type = '" . $post->post_type . "' AND p.post_status = 'publish'";
}
return $where;
}
function mt_order_next_post_sort( $orderby ) {
global $post;
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return $orderby;
}
if ( isset( $post->post_type ) && in_array( $post->post_type, $objects ) ) {
$orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
}
return $orderby;
}
function mt_order_pre_get_posts( $wp_query ) {
$objects = $this->get_mt_order_options_objects();
if ( empty( $objects ) ) {
return false;
}
if ( is_admin() ) {
if ( isset( $wp_query->query['post_type'] ) && ! isset( $_GET['orderby'] ) ) {
if ( in_array( $wp_query->query['post_type'], $objects ) ) {
$wp_query->set( 'orderby', 'menu_order' );
$wp_query->set( 'order', 'ASC' );
}
}
} else {
$active = false;
if ( isset( $wp_query->query['post_type'] ) ) {
if ( ! is_array( $wp_query->query['post_type'] ) ) {
if ( in_array( $wp_query->query['post_type'], $objects ) ) {
$active = true;
}
}
} else {
if ( in_array( 'post', $objects ) ) {
$active = true;
}
}
if ( ! $active ) {
return false;
}
if ( isset( $wp_query->query['suppress_filters'] ) ) {
if ( $wp_query->get( 'orderby' ) == 'date' ) {
$wp_query->set( 'orderby', 'menu_order' );
}
if ( $wp_query->get( 'order' ) == 'DESC' ) {
$wp_query->set( 'order', 'ASC' );
}
} else {
if ( ! $wp_query->get( 'orderby' ) ) {
$wp_query->set( 'orderby', 'menu_order' );
}
if ( ! $wp_query->get( 'order' ) ) {
$wp_query->set( 'order', 'ASC' );
}
}
}
}
function get_mt_order_options_objects() {
/**
* Custom Post Types this is supposed to be working for.
*/
$objects = array(
'cpo_slide',
'cpo_feature',
'cpo_portfolio',
'cpo_service',
'cpo_team',
'cpo_testimonial',
'cpo_client'
);
return $objects;
}
}