Simple Custom Post Order - Version 1.0

Version Description

Download this release

Release Info

Developer hsameerc
Plugin Icon wp plugin Simple Custom Post Order
Version 1.0
Comparing to
See all releases

Version 1.0

assets/scporder.css ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ .ui-sortable tr:hover {
2
+ cursor: move;
3
+ }
4
+ .ui-sortable tr.alternate {
5
+ background-color: #F9F9F9;
6
+ }
7
+ .ui-sortable tr.ui-sortable-helper {
8
+ background-color: #F9F9F9;
9
+ border-top: 1px solid #DFDFDF;
10
+ }
assets/scporder.js ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function() {
2
+ jQuery("#the-list").sortable({
3
+ 'items': 'tr',
4
+ 'axis': 'y',
5
+ 'helper': fixHelper,
6
+ 'update' : function(e, ui) {
7
+ jQuery.post( ajaxurl, {
8
+ action: 'update-menu-order',
9
+ order: jQuery("#the-list").sortable("serialize"),
10
+ });
11
+ }
12
+ });
13
+
14
+ });
15
+
16
+ var fixHelper = function(e, ui) {
17
+ ui.children().children().each(function() {
18
+ jQuery(this).width(jQuery(this).width());
19
+ });
20
+ return ui;
21
+ };
readme.txt ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Simple Custom Post Order ===
2
+ Contributors: hsameerc
3
+ Tags: custom post order, post order, custom post type order, order post, order posts, post order, posts order
4
+ Requires at least: 3.0.0
5
+ Tested up to: 3.5.1
6
+ Stable tag: 3.5.1
7
+ License: GPLv2 or later
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary.
11
+
12
+ == Description ==
13
+
14
+ Order posts(posts, any custom post types) using a Drag and Drop Sortable JavaScript. Configuration is unnecessary. You can do directly on default WordPress administration.
15
+ Excluding custom query which uses order or orderby parameters, in get_posts or query_posts and so on.
16
+
17
+ == Installation ==
18
+
19
+ 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
20
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
21
+ 1. Place `<?php do_action('plugin_name_hook'); ?>` in your templates
22
+
23
+ == Frequently asked questions ==
24
+
25
+ = A question that someone might have =
26
+
27
+ An answer to that question.
28
+
29
+ == Screenshots ==
30
+
31
+ 1. screenshot-1
32
+ 2. screenshot-2
33
+
34
+ == Changelog ==
35
+
36
+
37
+
38
+ == Upgrade notice ==
39
+
40
+
41
+
42
+ == Arbitrary section 1 ==
screenshot-1.png ADDED
Binary file
screenshot-2.png ADDED
Binary file
settings.php ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $scporder_options = get_option('scporder_options');
4
+ $objects = $scporder_options['objects'];
5
+
6
+ ?>
7
+
8
+ <div class="wrapper">
9
+
10
+ <?php screen_icon( 'plugins' ); ?>
11
+
12
+ <h2><?php _e('Simple Custom Post Order Settings', 'scporder'); ?></h2>
13
+
14
+ <?php if ( isset($_GET['msg'] )) : ?>
15
+ <div id="message" class="updated below-h2">
16
+ <?php if ( $_GET['msg'] == 'update') : ?>
17
+ <p><?php _e('Settings saved.', 'scporder'); ?></p>
18
+ <?php endif; ?>
19
+ </div>
20
+ <?php endif; ?>
21
+
22
+ <form method="post">
23
+
24
+ <?php if ( function_exists( 'wp_nonce_field' ) ) wp_nonce_field( 'nonce_scporder' ); ?>
25
+
26
+ <table class="form-table">
27
+ <tbody>
28
+ <tr valign="top">
29
+ <th scope="row"><label for="blogname"><?php _e('Sortable Objects', 'scporder') ?></label></th>
30
+ <td>
31
+ <?php
32
+ $post_types = get_post_types( array (
33
+ 'public' => true
34
+ ), 'objects' );
35
+
36
+ foreach ($post_types as $post_type ) {
37
+ if ( $post_type->name != 'attachment' ) {
38
+ ?>
39
+ <label><input type="checkbox" name="objects[]" value="<?php echo $post_type->name; ?>" <?php if ( isset($objects) && is_array($objects) ) { if ( in_array($post_type->name, $objects )) { echo 'checked="checked"'; } } ?> />&nbsp;<?php echo $post_type->label; ?></label><br />
40
+ <?php
41
+ }
42
+ }
43
+ ?>
44
+ </td>
45
+ </tr>
46
+ </tbody>
47
+ </table>
48
+
49
+ <p class="submit">
50
+ <input type="submit" class="button-primary" name="scporder_submit" value="<?php _e('Update', 'cptg'); ?>" />
51
+ </p>
52
+
53
+ </form>
54
+
55
+ </div>
simple-custom-post-order.php ADDED
@@ -0,0 +1,372 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Simple Custom Post Order
4
+ Plugin URI: http://hsameer.com.np/simple-custom-post-order/
5
+ Description: Order Items (Posts, Pages, and Custom Post Types) using a Drag and Drop Sortable JavaScript.
6
+ Version: 1.0
7
+ Author: Sameer Humagain
8
+ Author URI: http://hsameer.com.np/
9
+ */
10
+
11
+
12
+ /*====================================================================
13
+ Define
14
+ ====================================================================*/
15
+
16
+ define( 'SCPO_URL', plugins_url('', __FILE__) );
17
+ define( 'SCPO_DIR', plugin_dir_path(__FILE__) );
18
+
19
+ /*====================================================================
20
+ Class & Method
21
+ ====================================================================*/
22
+
23
+ $scporder = new SCPO_Engine();
24
+
25
+ class SCPO_Engine
26
+ {
27
+ function __construct()
28
+ {
29
+ if ( !get_option('scporder_options') ) $this->scporder_install();
30
+
31
+ add_action( 'admin_menu', array( &$this, 'admin_menu') );
32
+
33
+ add_action( 'admin_init', array( &$this, 'refresh' ) );
34
+ add_action( 'admin_init', array( &$this, 'update_options') );
35
+ add_action( 'init', array( &$this, 'enable_objects' ) );
36
+
37
+ add_action( 'wp_ajax_update-menu-order', array( &$this, 'update_menu_order' ) );
38
+
39
+
40
+ add_filter( 'pre_get_posts', array( &$this, 'scporder_filter_active' ) );
41
+ add_filter( 'pre_get_posts', array( &$this, 'scporder_pre_get_posts' ) );
42
+
43
+
44
+ add_filter( 'get_previous_post_where', array( &$this, 'scporder_previous_post_where' ) );
45
+ add_filter( 'get_previous_post_sort', array( &$this, 'scporder_previous_post_sort' ) );
46
+ add_filter( 'get_next_post_where', array( &$this, 'scporder_next_post_where' ) );
47
+ add_filter( 'get_next_post_sort', array( &$this, 'scporder_next_post_sort' ) );
48
+ }
49
+
50
+ function scporder_install()
51
+ {
52
+ global $wpdb;
53
+
54
+ //Initialize Options
55
+
56
+ $post_types = get_post_types( array (
57
+ 'public' => true
58
+ ), 'objects' );
59
+
60
+ foreach ($post_types as $post_type ) {
61
+ $init_objects[] = $post_type->name;
62
+ }
63
+ $input_options = array( 'objects' => $init_objects );
64
+
65
+ update_option( 'scporder_options', $input_options );
66
+
67
+
68
+ // Initialize : menu_order from date_post
69
+
70
+ $scporder_options = get_option( 'scporder_options' );
71
+ $objects = $scporder_options['objects'];
72
+
73
+ foreach( $objects as $object) {
74
+ $sql = "SELECT
75
+ ID
76
+ FROM
77
+ $wpdb->posts
78
+ WHERE
79
+ post_type = '".$object."'
80
+ AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
81
+ ORDER BY
82
+ post_date DESC
83
+ ";
84
+
85
+ $results = $wpdb->get_results($sql);
86
+
87
+ foreach( $results as $key => $result ) {
88
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
89
+ }
90
+ }
91
+ }
92
+
93
+ function admin_menu()
94
+ {
95
+ add_options_page( __('SCPOrder', 'scporder'), __('SCPOrder', 'scporder'), 'manage_options', 'scporder-settings', array( &$this,'admin_page' ));
96
+ }
97
+
98
+ function admin_page()
99
+ {
100
+ require SCPO_DIR.'settings.php';
101
+ }
102
+
103
+ function enable_objects()
104
+ {
105
+ $scporder_options = get_option( 'scporder_options' );
106
+ $objects = $scporder_options['objects'];
107
+
108
+ if ( is_array( $objects ) ) {
109
+ $active = false;
110
+
111
+ // for Pages or Custom Post Types
112
+ if ( isset($_GET['post_type']) ) {
113
+ if ( in_array( $_GET['post_type'], $objects ) ) {
114
+ $active = true;
115
+ }
116
+ // for Posts
117
+ } else {
118
+ $post_list = strstr( $_SERVER["REQUEST_URI"], 'wp-admin/edit.php' );
119
+ if ( $post_list && in_array( 'post', $objects ) ) {
120
+ $active = true;
121
+ }
122
+ }
123
+
124
+ if ( $active ) {
125
+ $this->load_script_css();
126
+ }
127
+ }
128
+ }
129
+
130
+ function load_script_css() {
131
+ // load JavaScript
132
+ wp_enqueue_script( 'jQuery' );
133
+ wp_enqueue_script( 'jquery-ui-sortable' );
134
+ wp_enqueue_script( 'scporderjs', SCPO_URL.'/assets/scporder.js', array( 'jquery' ), null, true );
135
+ // load CSS
136
+ wp_enqueue_style( 'scporder', SCPO_URL.'/assets/scporder.css', array(), null );
137
+ }
138
+
139
+ function refresh()
140
+ {
141
+
142
+ global $wpdb;
143
+
144
+ $scporder_options = get_option( 'scporder_options' );
145
+ $objects = $scporder_options['objects'];
146
+
147
+ if ( is_array( $objects ) ) {
148
+ foreach( $objects as $object) {
149
+ $sql = "SELECT
150
+ ID
151
+ FROM
152
+ $wpdb->posts
153
+ WHERE
154
+ post_type = '".$object."'
155
+ AND post_status IN ('publish', 'pending', 'draft', 'private', 'future')
156
+ ORDER BY
157
+ menu_order ASC
158
+ ";
159
+
160
+ $results = $wpdb->get_results($sql);
161
+
162
+ foreach( $results as $key => $result ) {
163
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $key+1 ), array( 'ID' => $result->ID ) );
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ function update_menu_order()
170
+ {
171
+ global $wpdb;
172
+
173
+ parse_str($_POST['order'], $data);
174
+
175
+ if ( is_array($data) ) {
176
+
177
+
178
+ $id_arr = array();
179
+ foreach( $data as $key => $values ) {
180
+ foreach( $values as $position => $id ) {
181
+ $id_arr[] = $id;
182
+ }
183
+ }
184
+
185
+
186
+ $menu_order_arr = array();
187
+ foreach( $id_arr as $key => $id ) {
188
+ $results = $wpdb->get_results("SELECT menu_order FROM $wpdb->posts WHERE ID = ".$id);
189
+ foreach( $results as $result ) {
190
+ $menu_order_arr[] = $result->menu_order;
191
+ }
192
+ }
193
+
194
+ sort($menu_order_arr);
195
+
196
+ foreach( $data as $key => $values ) {
197
+ foreach( $values as $position => $id ) {
198
+ $wpdb->update( $wpdb->posts, array( 'menu_order' => $menu_order_arr[$position] ), array( 'ID' => $id ) );
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ function update_options()
205
+ {
206
+ if ( isset( $_POST['scporder_submit'] ) ) {
207
+
208
+ check_admin_referer( 'nonce_scporder' );
209
+
210
+ if ( isset( $_POST['objects'] ) ) {
211
+ $input_options = array( 'objects' => $_POST['objects'] );
212
+ } else {
213
+ $input_options = array( 'objects' => '' );
214
+ }
215
+
216
+ update_option( 'scporder_options', $input_options );
217
+ wp_redirect( 'admin.php?page=scporder-settings&msg=update' );
218
+ }
219
+ }
220
+
221
+ function scporder_previous_post_where( $where )
222
+ {
223
+ global $post;
224
+
225
+ $scporder_options = get_option('scporder_options');
226
+ $objects = $scporder_options['objects'];
227
+
228
+ if ( in_array( $post->post_type, $objects ) ) {
229
+ $current_menu_order = $post->menu_order;
230
+ $where = "WHERE p.menu_order > '".$current_menu_order."' AND p.post_type = '". $post->post_type ."' AND p.post_status = 'publish'";
231
+ }
232
+ return $where;
233
+ }
234
+
235
+ function scporder_previous_post_sort( $orderby )
236
+ {
237
+ global $post;
238
+
239
+ $scporder_options = get_option('scporder_options');
240
+ $objects = $scporder_options['objects'];
241
+
242
+ if ( in_array( $post->post_type, $objects ) ) {
243
+ $orderby = 'ORDER BY p.menu_order ASC LIMIT 1';
244
+ }
245
+ return $orderby;
246
+ }
247
+
248
+ function scporder_next_post_where( $where )
249
+ {
250
+ global $post;
251
+
252
+ $scporder_options = get_option('scporder_options');
253
+ $objects = $scporder_options['objects'];
254
+
255
+ if ( in_array( $post->post_type, $objects ) ) {
256
+ $current_menu_order = $post->menu_order;
257
+ $where = "WHERE p.menu_order < '".$current_menu_order."' AND p.post_type = '". $post->post_type ."' AND p.post_status = 'publish'";
258
+ }
259
+ return $where;
260
+ }
261
+
262
+ function scporder_next_post_sort( $orderby )
263
+ {
264
+ global $post;
265
+
266
+ $scporder_options = get_option('scporder_options');
267
+ $objects = $scporder_options['objects'];
268
+
269
+ if ( in_array( $post->post_type, $objects ) ) {
270
+ $orderby = 'ORDER BY p.menu_order DESC LIMIT 1';
271
+ }
272
+ return $orderby;
273
+ }
274
+
275
+ function scporder_filter_active( $wp_query )
276
+ {
277
+
278
+ if ( isset($wp_query->query['suppress_filters']) ) $wp_query->query['suppress_filters'] = false;
279
+ if ( isset($wp_query->query_vars['suppress_filters']) ) $wp_query->query_vars['suppress_filters'] = false;
280
+ return $wp_query;
281
+ }
282
+
283
+ function scporder_pre_get_posts( $wp_query )
284
+ {
285
+ $scporder_options = get_option('scporder_options');
286
+ $objects = $scporder_options['objects'];
287
+
288
+ if ( is_array( $objects ) ) {
289
+
290
+
291
+
292
+ if ( is_admin() && !defined( 'DOING_AJAX' ) ) {
293
+
294
+
295
+ if ( isset( $wp_query->query['post_type'] ) ) {
296
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
297
+ $wp_query->set( 'orderby', 'menu_order' );
298
+ $wp_query->set( 'order', 'ASC' );
299
+ }
300
+ }
301
+
302
+
303
+
304
+ } else {
305
+
306
+ $active = false;
307
+
308
+
309
+
310
+ if ( empty( $wp_query->query ) ) {
311
+ if ( in_array( 'post', $objects ) ) {
312
+ $active = true;
313
+ }
314
+ } else {
315
+
316
+
317
+
318
+ if ( isset($wp_query->query['suppress_filters']) ) {
319
+
320
+
321
+ if ( is_array( $wp_query->query['post_type'] ) ) {
322
+ $post_types = $wp_query->query['post_type'];
323
+ foreach( $post_types as $post_type ) {
324
+ if ( in_array( $post_type, $objects ) ) {
325
+ $active = true;
326
+ }
327
+ }
328
+ } else {
329
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
330
+ $active = true;
331
+ }
332
+ }
333
+
334
+
335
+ } else {
336
+
337
+
338
+ if ( isset( $wp_query->query['post_type'] ) ) {
339
+
340
+
341
+ if ( is_array( $wp_query->query['post_type'] ) ) {
342
+ $post_types = $wp_query->query['post_type'];
343
+ foreach( $post_types as $post_type ) {
344
+ if ( in_array( $post_type, $objects ) ) {
345
+ $active = true;
346
+ }
347
+ }
348
+ } else {
349
+ if ( in_array( $wp_query->query['post_type'], $objects ) ) {
350
+ $active = true;
351
+ }
352
+ }
353
+
354
+ } else {
355
+ if ( in_array( 'post', $objects ) ) {
356
+ $active = true;
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ if ( $active ) {
363
+ if ( !isset( $wp_query->query['orderby'] ) || $wp_query->query['orderby'] == 'post_date' ) $wp_query->set( 'orderby', 'menu_order' );
364
+ if ( !isset( $wp_query->query['order'] ) || $wp_query->query['order'] == 'DESC' ) $wp_query->set( 'order', 'ASC' );
365
+ }
366
+ }
367
+ }
368
+ }
369
+ }
370
+
371
+
372
+ ?>