Reorder Posts - Version 1.0.2

Version Description

  • Added ability to post type of posts to be reordered
  • Fixed bug in initial order
Download this release

Release Info

Developer ryanhellyer
Plugin Icon 128x128 Reorder Posts
Version 1.0.2
Comparing to
See all releases

Version 1.0.2

Files changed (7) hide show
  1. admin.css +34 -0
  2. class-reorder.php +278 -0
  3. index.php +86 -0
  4. metronet-icon.png +0 -0
  5. readme.txt +61 -0
  6. screenshot1.png +0 -0
  7. scripts/sort.js +36 -0
admin.css ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #post-list {
2
+ margin-top: 20px;
3
+ }
4
+ #post-list li {
5
+ padding: 10px;
6
+ width: 50%;
7
+ font-weight: bold;
8
+ background: #f0f0f0; /* for non-css3 browsers */
9
+ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#f7f7f7', endColorstr='#e7e7e7'); /* for IE 9 or older */
10
+ background: -webkit-gradient(linear, left top, left bottom, from(#f7f7f7), to(#e7e7e7)); /* for WebKit */
11
+ background: -webkit-linear-gradient(top, #f7f7f7, #e7e7e7); /* for newer versions of WebKit */
12
+ background: -moz-linear-gradient(top, #f7f7f7, #e7e7e7); /* for Firefox 3.6+ */
13
+ background: -o-linear-gradient(top, #f7f7f7, #e7e7e7); /* for Opera 11.1+ */
14
+ background: -ms-linear-gradient(top, #f7f7f7, #e7e7e7); /* for IE 10+ */
15
+ cursor: move;
16
+ border: 1px solid #ddd;
17
+ border-raidus: 10px;
18
+ -moz-border-radius: 10px;
19
+ -webkit-border-radius: 10px;
20
+ }
21
+ #post-list li img {
22
+ vertical-align: middle;
23
+ margin: 0 12px 0 0;
24
+ border: 2px solid #ddd;
25
+ }
26
+ .ui-sortable-placeholder {
27
+ border-radius: 0 !important;
28
+ background: transparent !important;
29
+ border: 2px dashed #ddd !important;
30
+ visibility: visible !important;
31
+ }
32
+ #loading-animation {
33
+ display: none;
34
+ }
class-reorder.php ADDED
@@ -0,0 +1,278 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Reorder posts
4
+ *
5
+ * @package WordPress
6
+ * @subpackage Metronet Reorder Posts plugin
7
+ */
8
+
9
+
10
+ /**
11
+ * Reorder posts
12
+ * Adds drag and drop editor for reordering WordPress posts
13
+ *
14
+ * Based on work by Scott Basgaard and Ronald Huereca
15
+ *
16
+ * To use this class, simply instantiate it using an argument to set the post type as follows:
17
+ * new Reorder( array( 'post_type' => 'post', 'order'=> 'ASC' ) );
18
+ *
19
+ * @copyright Copyright (c), Metronet
20
+ * @license http://www.gnu.org/licenses/gpl.html GPL
21
+ * @author Ryan Hellyer <ryan@metronet.no>
22
+ * @since 1.0
23
+ */
24
+ class Reorder {
25
+
26
+ /**
27
+ * @var $post_type
28
+ * @desc Post type to be reordered
29
+ * @access private
30
+ */
31
+ private $post_type;
32
+
33
+ /**
34
+ * @var $direction
35
+ * @desc ASC or DESC
36
+ * @access private
37
+ */
38
+ private $direction;
39
+
40
+ /**
41
+ * @var $heading
42
+ * @desc Admin page heading
43
+ * @access private
44
+ */
45
+ private $heading;
46
+
47
+ /**
48
+ * @var $initial
49
+ * @desc HTML outputted at end of admin page
50
+ * @access private
51
+ */
52
+ private $initial;
53
+
54
+ /**
55
+ * @var $final
56
+ * @desc HTML outputted at end of admin page
57
+ * @access private
58
+ */
59
+ private $final;
60
+
61
+ /**
62
+ * @var $post_statush
63
+ * @desc The post status of posts to be reordered
64
+ * @access private
65
+ */
66
+ private $post_status;
67
+
68
+ /**
69
+ * @var $menu_label
70
+ * @desc Admin page menu label
71
+ * @access private
72
+ */
73
+ private $menu_label;
74
+
75
+ /**
76
+ * @var $icon
77
+ * @desc Admin page icon
78
+ * @access private
79
+ */
80
+ private $icon;
81
+
82
+ /**
83
+ * Class constructor
84
+ *
85
+ * Sets definitions
86
+ * Adds methods to appropriate hooks
87
+ *
88
+ * @author Ryan Hellyer <ryan@metronet.no>
89
+ * @since Reorder 1.0
90
+ * @access public
91
+ * @param array $args If not set, then uses $defaults instead
92
+ */
93
+ public function __construct( $args = array() ) {
94
+
95
+ // Parse arguments
96
+ $defaults = array(
97
+ 'post_type' => 'post', // Setting the post type to be reordered
98
+ 'order' => 'ASC', // Setting the order of the posts
99
+ 'heading' => __( 'Reorder', 'reorder' ), // Default text for heading
100
+ 'initial' => '', // Initial text displayed before sorting code
101
+ 'final' => '', // Initial text displayed before sorting code
102
+ 'post_status' => 'publish', // Post status of posts to be reordered
103
+ );
104
+ extract( wp_parse_args( $args, $defaults ) );
105
+
106
+ // Set variables
107
+ $this->post_type = $post_type;
108
+ $this->order = $order;
109
+ $this->heading = $heading;
110
+ $this->initial = $initial;
111
+ $this->final = $final;
112
+ $this->menu_label = $menu_label;
113
+ $this->icon = $icon;
114
+ $this->post_status = $post_status;
115
+
116
+ // Add actions
117
+ add_action( 'wp_ajax_post_sort', array( $this, 'save_post_order' ) );
118
+ add_action( 'admin_print_styles', array( $this, 'print_styles' ) );
119
+ add_action( 'admin_print_scripts', array( $this, 'print_scripts' ) );
120
+ add_action( 'admin_menu', array( $this, 'enable_post_sort' ), 10, 'page' );
121
+ add_action( 'admin_print_styles', array( $this, 'create_nonce' ) );
122
+ }
123
+
124
+ /**
125
+ * Creating the nonce value used within sort.js
126
+ *
127
+ * @author Ryan Hellyer <ryan@metronet.no>
128
+ * @since Reorder 1.0
129
+ * @access public
130
+ */
131
+ public function create_nonce() {
132
+ echo "<script>sortnonce = '" . wp_create_nonce( 'sortnonce' ) . "';</script>";
133
+ }
134
+
135
+ /**
136
+ * Saving the post oder for later use
137
+ *
138
+ * @author Ryan Hellyer <ryan@metronet.no> and Ronald Huereca <ronald@metronet.no>
139
+ * @since Reorder 1.0
140
+ * @access public
141
+ * @global object $wpdb The primary global database object used internally by WordPress
142
+ */
143
+ public function save_post_order() {
144
+ global $wpdb;
145
+
146
+ // Verify nonce value, for security purposes
147
+ wp_verify_nonce( json_encode( array( $_POST['nonce'] ) ), 'sortnonce' );
148
+
149
+ // Split post output
150
+ $order = explode( ',', $_POST['order'] );
151
+
152
+ // Loop through blocks and stash in DB accordingly
153
+ $counter = count( $order );
154
+ foreach ( $order as $post_id ) {
155
+ $wpdb->update(
156
+ $wpdb->posts,
157
+ array( 'menu_order' => $counter ),
158
+ array( 'ID' => $post_id )
159
+ );
160
+ $counter = $counter - 1;
161
+ }
162
+
163
+ die( 1 );
164
+ }
165
+
166
+ /**
167
+ * Print styles to admin page
168
+ *
169
+ * @author Ryan Hellyer <ryan@metronet.no>
170
+ * @since Reorder 1.0
171
+ * @access public
172
+ * @global string $pagenow Used internally by WordPress to designate what the current page is in the admin panel
173
+ */
174
+ public function print_styles() {
175
+ global $pagenow;
176
+
177
+ $pages = array( 'edit.php' );
178
+
179
+ if ( in_array( $pagenow, $pages ) )
180
+ wp_enqueue_style( 'reorderpages_style', REORDER_URL . '/admin.css' );
181
+
182
+ }
183
+
184
+ /**
185
+ * Print scripts to admin page
186
+ *
187
+ * @author Ryan Hellyer <ryan@metronet.no>
188
+ * @since Reorder 1.0
189
+ * @access public
190
+ * @global string $pagenow Used internally by WordPress to designate what the current page is in the admin panel
191
+ */
192
+ public function print_scripts() {
193
+ global $pagenow;
194
+
195
+ $pages = array( 'edit.php' );
196
+ if ( in_array( $pagenow, $pages ) ) {
197
+ wp_enqueue_script( 'jquery-ui-sortable' );
198
+ wp_enqueue_script( 'levert_posts', REORDER_URL . '/scripts/sort.js' );
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Add submenu
204
+ *
205
+ * @author Ryan Hellyer <ryan@metronet.no>
206
+ * @since Reorder 1.0
207
+ * @access public
208
+ */
209
+ public function enable_post_sort() {
210
+ $post_type = $this->post_type;
211
+ if ( 'post' != $post_type ) {
212
+ add_submenu_page(
213
+ 'edit.php?post_type=' . $post_type, // Parent slug
214
+ $this->heading, // Page title (unneeded since specified directly)
215
+ $this->menu_label, // Menu title
216
+ 'edit_posts', // Capability
217
+ 'reorder-' . $post_type, // Menu slug
218
+ array( $this, 'sort_posts' ) // Callback function
219
+ );
220
+ }
221
+ else {
222
+ add_posts_page(
223
+ $this->heading, // Page title (unneeded since specified directly)
224
+ $this->menu_label, // Menu title
225
+ 'edit_posts', // Capability
226
+ 'reorder-posts', // Menu slug
227
+ array( $this, 'sort_posts' ) // Callback function
228
+ );
229
+ }
230
+ }
231
+
232
+ /**
233
+ * HTML output
234
+ *
235
+ * @author Ryan Hellyer <ryan@metronet.no>
236
+ * @since Reorder 1.0
237
+ * @access public
238
+ * @global string $post_type
239
+ */
240
+ public function sort_posts() {
241
+ $posts = new WP_Query(
242
+ array(
243
+ 'post_type' => $this->post_type,
244
+ 'posts_per_page' => -1,
245
+ 'orderby' => 'menu_order',
246
+ 'order' => $this->order,
247
+ 'post_status' => $this->post_status,
248
+ )
249
+ );
250
+ ?>
251
+ <style type="text/css">
252
+ #icon-reorder-posts {
253
+ background:url(<?php echo $this->icon; ?>) no-repeat;
254
+ }
255
+ </style>
256
+ <div class="wrap">
257
+ <?php screen_icon( 'reorder-posts' ); ?>
258
+ <h2>
259
+ <?php echo $this->heading; ?>
260
+ <img src="<?php echo admin_url( 'images/loading.gif' ); ?>" id="loading-animation" />
261
+ </h2>
262
+ <div id="reorder-error"></div>
263
+ <?php echo $this->initial; ?>
264
+ <ul id="post-list"><?php
265
+
266
+ // Looping through all the posts
267
+ while ( $posts->have_posts() ) {
268
+ $posts->the_post();
269
+ ?><li id="<?php the_id(); ?>"><?php the_title(); ?></li><?php
270
+ }
271
+ ?>
272
+
273
+ </ul><?php
274
+ echo $this->final; ?>
275
+ </div><?php
276
+ }
277
+
278
+ }
index.php ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Metronet Reorder Posts
4
+ Plugin URI: http://metronet.no/
5
+ Description: Reorder posts
6
+ Version: 1.0.2
7
+ Author: Ryan Hellyer / Metronet
8
+ Author URI: http://metronet.no/
9
+
10
+ ------------------------------------------------------------------------
11
+ Copyright Metronet AS
12
+
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License as published by
15
+ the Free Software Foundation; either version 2 of the License, or
16
+ (at your option) any later version.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
+ */
27
+
28
+
29
+ /**
30
+ * Do not continue processing since file was called directly
31
+ *
32
+ * @since 1.0
33
+ * @author Ryan Hellyer <ryan@metronet.no>
34
+ */
35
+ if ( !defined( 'ABSPATH' ) )
36
+ die( 'Eh! What you doin in here?' );
37
+
38
+ /**
39
+ * Load classes
40
+ *
41
+ * @since 1.0
42
+ * @author Ryan Hellyer <ryan@metronet.no>
43
+ */
44
+ require( 'class-reorder.php' );
45
+
46
+ /**
47
+ * Define constants
48
+ *
49
+ * @since 1.0
50
+ * @author Ryan Hellyer <ryan@metronet.no>
51
+ */
52
+ define( 'REORDER_DIR', dirname( __FILE__ ) . '/' ); // Plugin folder DIR
53
+ define( 'REORDER_URL', WP_PLUGIN_URL . '/' . basename( REORDER_DIR ) . '' ); // Plugin folder URL
54
+
55
+ /**
56
+ * Instantiate admin panel
57
+ * Iterate through each specified post type and instantiate it's organiser
58
+ *
59
+ * @since 1.0
60
+ * @author Ryan Hellyer <ryan@metronet.no>
61
+ */
62
+ add_action( 'wp_loaded', 'mt_reorder_posts_init', 100 ); //Load low priority in init for other plugins to generate their post types
63
+ function mt_reorder_posts_init() {
64
+ $post_types = get_post_types( '','names' );
65
+ foreach ( $post_types as $post_type ) {
66
+
67
+ // Don't bother with hierarchical post types
68
+ if ( is_post_type_hierarchical( $post_type ) )
69
+ continue;
70
+
71
+ // Instantiate new reordering
72
+ new Reorder(
73
+ array(
74
+ 'post_type' => $post_type,
75
+ 'order' => 'DESC',
76
+ 'heading' => __( 'Metronet reorder posts', 'reorder' ),
77
+ 'final' => '',
78
+ 'initial' => '',
79
+ 'menu_label' => __( 'Reorder', 'reorder' ),
80
+ 'icon' => REORDER_URL . '/metronet-icon.png',
81
+ 'post_status' => 'publish',
82
+ )
83
+ );
84
+ }
85
+ } //end mt_reorder_posts_init
86
+
metronet-icon.png ADDED
Binary file
readme.txt ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Metronet Reorder Posts ===
2
+ Contributors: ryanhellyer, metronet, ronalfy
3
+ Author URI: http://metronet.no/
4
+ Plugin URL: http://metronet.no/
5
+ Requires at Least: 3.3
6
+ Tags: reorder, posts, wordpress, post-type
7
+ Stable tag: 1.0.2
8
+
9
+ A simple and easy way to reorder your posts in WordPress.
10
+
11
+ == Description ==
12
+
13
+ A simple and easy way to reorder your posts in WordPress. Adds drag and drop functionality for post ordering in the WordPress admin panel. Works with posts, pages and custom post-types.
14
+
15
+ == Installation ==
16
+
17
+ Either install the plugin via the WordPress admin panel, or ...
18
+
19
+ 1. Upload `metronet-reorder-posts` to the `/wp-content/plugins/` directory.
20
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
21
+
22
+ There are no configuration options in this plugin. Simply navigate to "Posts" > "Reorder" to change the order of your posts. Changes are saved immediately, there is no need to click a save or update button.
23
+
24
+ == Frequently Asked Questions ==
25
+
26
+ = Where's the settings page? =
27
+
28
+ There is no settings page per se. The plugin adds an ordering page as a submenu under each post type.
29
+
30
+ = Where is the "save" button? =
31
+
32
+ There isn't one. The changes are saved automatically .
33
+
34
+ = Can I use this on a single post type? =
35
+
36
+ If you want to use the plugin on a single post type, then we recommend extracting the "Reorder" class from the plugin and using it directly. We have attempted to make the class as user-friendly and adaptable as possible.
37
+
38
+ = Why doesn't it work with pages? =
39
+
40
+ Because pages are hierarchical and that makes things a whole lot more complicated. If need this functionality in future, we may add it, but in the mean time the plugin only works with non-hierarchical post-types.
41
+
42
+ = Does it work in older versions of WordPress? =
43
+
44
+ Probably, but we only support the latest version of WordPress.
45
+
46
+ == Screenshots ==
47
+
48
+ 1. Metronet Reorder Posts allows you to easily drag and drop posts to change their order
49
+
50
+ == Changelog ==
51
+
52
+ = 1.0.2 =
53
+ * Added ability to post type of posts to be reordered
54
+ * Fixed bug in initial order
55
+
56
+ = 1.0.1 =
57
+ * Added ability to change menu name via class argument
58
+ * Removed support for non-hierarchical post-types
59
+
60
+ = 1.0 =
61
+ * Initial plugin release
screenshot1.png ADDED
Binary file
scripts/sort.js ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function($) {
2
+ var postList = $('#post-list');
3
+
4
+ postList.sortable({
5
+ update: function(event, ui) {
6
+ $('#loading-animation').show(); // Show the animate loading gif while waiting
7
+
8
+ opts = {
9
+ url: ajaxurl, // ajaxurl is defined by WordPress and points to /wp-admin/admin-ajax.php
10
+ type: 'POST',
11
+ async: true,
12
+ cache: false,
13
+ dataType: 'json',
14
+ data:{
15
+ action: 'post_sort', // Tell WordPress how to handle this ajax request
16
+ order: postList.sortable('toArray').toString(), // Passes ID's of list items in 1,3,2 format
17
+ nonce: sortnonce,
18
+ },
19
+ success: function(response) {
20
+ $('#loading-animation').hide(); // Hide the loading animation
21
+ $('#reorder-error').html('There was an error worked'); // Error message
22
+ $('#reorder-error').html(response); // Error message
23
+
24
+ console.log(response);
25
+ return;
26
+ },
27
+ error: function(xhr,textStatus,e) { // This can be expanded to provide more information
28
+ alert('There was an error saving the updates');
29
+ $('#loading-animation').hide(); // Hide the loading animation
30
+ return;
31
+ }
32
+ };
33
+ $.ajax(opts);
34
+ }
35
+ });
36
+ });