Share this diff
2 lines added. 2 lines deleted.
1<?php 1<?php
2/** 2/**
3 Plugin Name: Simple Page Ordering 3 Plugin Name: Simple Page Ordering
4 Plugin URI: http://www.get10up.com/plugins/simple-page-ordering-wordpress/ 4 Plugin URI: http://www.get10up.com/plugins/simple-page-ordering-wordpress/
5 Description: Order your pages and hierarchical post types using drag and drop on the built in page list. Also adds a filter for items to show per page. For further instructions, open the "Help" tab on the Pages screen. 5 Description: Order your pages and hierarchical post types using drag and drop on the built in page list. Also adds a filter for items to show per page. For further instructions, open the "Help" tab on the Pages screen.
6 Version: 1.0 6 Version: 1.0
7 Author: Jake Goldman (10up) 7 Author: Jake Goldman (10up)
8 Author URI: http://www.get10up.com 8 Author URI: http://www.get10up.com
9 9
10 Plugin: Copyright 2011 10up (email : jake@get10up.com) 10 Plugin: Copyright 2011 10up (email : jake@get10up.com)
11 11
12 This program is free software; you can redistribute it and/or modify 12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by 13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or 14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version. 15 (at your option) any later version.
16 16
17 This program is distributed in the hope that it will be useful, 17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details. 20 GNU General Public License for more details.
21 21
22 You should have received a copy of the GNU General Public License 22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software 23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25*/ 25*/
26 26
27class simple_page_ordering { 27class simple_page_ordering {
28 28
29 function simple_page_ordering() { 29 function simple_page_ordering() {
30 add_action( 'admin_init', array( $this, 'admin_init' ) ); 30 add_action( 'admin_init', array( $this, 'admin_init' ) );
31 add_action( 'load-edit.php', array( $this, 'load_edit_screen' ) ); 31 add_action( 'load-edit.php', array( $this, 'load_edit_screen' ) );
32 add_action( 'wp_ajax_simple_page_ordering', array( $this, 'ajax_simple_page_ordering' ) ); 32 add_action( 'wp_ajax_simple_page_ordering', array( $this, 'ajax_simple_page_ordering' ) );
33 } 33 }
34 34
35 function admin_init() { 35 function admin_init() {
36 load_plugin_textdomain( 'simple-page-ordering', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' ); 36 load_plugin_textdomain( 'simple-page-ordering', false, dirname( plugin_basename( __FILE__ ) ) . '/localization/' );
37 } 37 }
38 38
39 function load_edit_screen() { 39 function load_edit_screen() {
40 add_action( 'wp', array( $this, 'wp_edit' ) ); 40 add_action( 'wp', array( $this, 'wp_edit' ) );
41 } 41 }
42 42
43 function wp_edit() { 43 function wp_edit() {
44 global $post_type, $wp_query; 44 global $post_type, $wp_query;
45 45
46 if ( ! current_user_can('edit_others_pages') || ( ! post_type_supports( $post_type, 'page-attributes' ) && ! is_post_type_hierarchical( $post_type ) ) ) // check permission 46 if ( ! current_user_can('edit_others_pages') || ( ! post_type_supports( $post_type, 'page-attributes' ) && ! is_post_type_hierarchical( $post_type ) ) ) // check permission
47 return; 47 return;
48 48
49 add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) ); // posts per page drop down UI 49 add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) ); // posts per page drop down UI
50 50
51 if ( isset( $_GET['per_page'] ) && ( $_GET['per_page'] == 99999 || ( $_GET['per_page']%10 ) == 0 ) ) 51 if ( isset( $_GET['per_page'] ) && ( $_GET['per_page'] == 99999 || ( $_GET['per_page']%10 ) == 0 ) )
52 update_user_option( get_current_user_id(), 'edit_' . $post_type . '_per_page', (int) $_GET['per_page'] ); 52 update_user_option( get_current_user_id(), 'edit_' . $post_type . '_per_page', (int) $_GET['per_page'] );
53 53
54 add_filter( 'views_' . get_current_screen()->id, array( $this, 'sort_by_order_link' ) ); // add view by menu order to views 54 add_filter( 'views_' . get_current_screen()->id, array( $this, 'sort_by_order_link' ) ); // add view by menu order to views
55 add_filter( 'contextual_help', array( $this, 'contextual_help' ) ); // add contextual help to hierarchical post screens 55 add_filter( 'contextual_help', array( $this, 'contextual_help' ) ); // add contextual help to hierarchical post screens
56 56
57 if ( $wp_query->query['orderby'] == 'menu_order title' ) { // we can only sort if we're organized by menu order; WP 3.2 and 3.1 versions57 if ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) { // we can only sort if we're organized by menu order; WP 3.2 and 3.1 versions
58 58
59 wp_enqueue_script( 'simple-page-ordering', plugin_dir_url( __FILE__ ) . 'simple-page-ordering.js', array('jquery-ui-sortable'), '0.9.7', true ); 59 wp_enqueue_script( 'simple-page-ordering', plugin_dir_url( __FILE__ ) . 'simple-page-ordering.js', array('jquery-ui-sortable'), '0.9.7', true );
60 $js_trans = array( 60 $js_trans = array(
61 'RepositionTree' => __("Items can only be repositioned within their current branch in the page tree / hierarchy (next to pages with the same parent).\n\nIf you want to move this item into a different part of the page tree, use the Quick Edit feature to change the parent before continuing.", 'simple-page-ordering') 61 'RepositionTree' => __("Items can only be repositioned within their current branch in the page tree / hierarchy (next to pages with the same parent).\n\nIf you want to move this item into a different part of the page tree, use the Quick Edit feature to change the parent before continuing.", 'simple-page-ordering')
62 ); 62 );
63 wp_localize_script( 'simple-page-ordering', 'simple_page_ordering_l10n', $js_trans ); 63 wp_localize_script( 'simple-page-ordering', 'simple_page_ordering_l10n', $js_trans );
64 64
65 } 65 }
66 } 66 }
67 67
68 function restrict_manage_posts() 68 function restrict_manage_posts()
69 { 69 {
70 global $per_page; 70 global $per_page;
71 71
72 $per_page = isset( $_GET['per_page'] ) ? (int) $_GET['per_page'] : $per_page; 72 $per_page = isset( $_GET['per_page'] ) ? (int) $_GET['per_page'] : $per_page;
73 ?> 73 ?>
74 <select name="per_page" style="width: 110px;"> 74 <select name="per_page" style="width: 110px;">
75 <option <?php selected( $per_page, 99999 ); ?> value="99999"><?php _e( 'Show all', 'simple-page-ordering' ); ?></option> 75 <option <?php selected( $per_page, 99999 ); ?> value="99999"><?php _e( 'Show all', 'simple-page-ordering' ); ?></option>
76 <?php for( $i=10;$i<=100;$i+=10 ) : ?> 76 <?php for( $i=10;$i<=100;$i+=10 ) : ?>
77 <option <?php selected( $per_page, $i ); ?> value="<?php echo $i; ?>"><?php echo $i; ?> <?php _e( 'per page', 'simple-page-ordering' ); ?></option> 77 <option <?php selected( $per_page, $i ); ?> value="<?php echo $i; ?>"><?php echo $i; ?> <?php _e( 'per page', 'simple-page-ordering' ); ?></option>
78 <?php endfor; ?> 78 <?php endfor; ?>
79 <?php if ( $per_page != -1 && $per_page != 99999 && ( $per_page%10 != 0 || $per_page > 100 ) ) : ?> 79 <?php if ( $per_page != -1 && $per_page != 99999 && ( $per_page%10 != 0 || $per_page > 100 ) ) : ?>
80 <option <?php selected( true ); ?> value="<?php echo (int) $per_page; ?>"><?php echo (int) $per_page; ?> <?php _e( 'per page', 'simple-page-ordering' ); ?></option> 80 <option <?php selected( true ); ?> value="<?php echo (int) $per_page; ?>"><?php echo (int) $per_page; ?> <?php _e( 'per page', 'simple-page-ordering' ); ?></option>
81 <?php endif; ?> 81 <?php endif; ?>
82 </select> 82 </select>
83 <?php 83 <?php
84 } 84 }
85 85
86 function contextual_help( $help ) 86 function contextual_help( $help )
87 { 87 {
88 return $help . ' 88 return $help . '
89 <p><strong>'. __( 'Simple Page Ordering', 'simple_page_ordering' ) . '</strong></p> 89 <p><strong>'. __( 'Simple Page Ordering', 'simple_page_ordering' ) . '</strong></p>
90 <p><a href="http://www.get10up.com/plugins/simple-page-ordering-wordpress/" target="_blank">' . __( 'Simple Page Ordering', 'simple_page_ordering' ) . '</a> ' . __( 'is a plug-in by', 'simple-page-ordering' ) . ' <a href="http://www.get10up.com" target="_blank">Jake Goldman (10up)</a> ' . __( 'that allows you to order pages and other hierarchical post types with drag and drop.', 'simple-page-ordering' ) . '</p> 90 <p><a href="http://www.get10up.com/plugins/simple-page-ordering-wordpress/" target="_blank">' . __( 'Simple Page Ordering', 'simple_page_ordering' ) . '</a> ' . __( 'is a plug-in by', 'simple-page-ordering' ) . ' <a href="http://www.get10up.com" target="_blank">Jake Goldman (10up)</a> ' . __( 'that allows you to order pages and other hierarchical post types with drag and drop.', 'simple-page-ordering' ) . '</p>
91 <p>' . __( 'To reposition an item, simply drag and drop the row by "clicking and holding" it anywhere (outside of the links and form controls) and moving it to its new position.', 'simple-page-ordering' ) . '</p> 91 <p>' . __( 'To reposition an item, simply drag and drop the row by "clicking and holding" it anywhere (outside of the links and form controls) and moving it to its new position.', 'simple-page-ordering' ) . '</p>
92 <p>' . __( 'If you have a large number of pages, it may be helpful to adjust the new "items per page" filter located above the table and before the filter button.', 'simple-page-ordering' ) . '</p> 92 <p>' . __( 'If you have a large number of pages, it may be helpful to adjust the new "items per page" filter located above the table and before the filter button.', 'simple-page-ordering' ) . '</p>
93 <p>' . __( 'To keep things relatively simple, the current version only allows you to reposition items within their current tree / hierarchy (next to pages with the same parent). If you want to move an item into or out of a different part of the page tree, use the "quick edit" feature to change the parent.', 'simple-page-ordering' ) . '</p> 93 <p>' . __( 'To keep things relatively simple, the current version only allows you to reposition items within their current tree / hierarchy (next to pages with the same parent). If you want to move an item into or out of a different part of the page tree, use the "quick edit" feature to change the parent.', 'simple-page-ordering' ) . '</p>
94 '; 94 ';
95 } 95 }
96 96
97 function ajax_simple_page_ordering() { 97 function ajax_simple_page_ordering() {
98 // check permissions again and make sure we have what we need 98 // check permissions again and make sure we have what we need
99 if ( ! current_user_can('edit_others_pages') || empty( $_POST['id'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) ) 99 if ( ! current_user_can('edit_others_pages') || empty( $_POST['id'] ) || ( ! isset( $_POST['previd'] ) && ! isset( $_POST['nextid'] ) ) )
100 die(-1); 100 die(-1);
101 101
102 // real post? 102 // real post?
103 if ( ! $post = get_post( $_POST['id'] ) ) 103 if ( ! $post = get_post( $_POST['id'] ) )
104 die(-1); 104 die(-1);
105 105
106 $previd = isset( $_POST['previd'] ) ? $_POST['previd'] : false; 106 $previd = isset( $_POST['previd'] ) ? $_POST['previd'] : false;
107 $nextid = isset( $_POST['nextid'] ) ? $_POST['nextid'] : false; 107 $nextid = isset( $_POST['nextid'] ) ? $_POST['nextid'] : false;
108 $new_pos = array(); // store new positions for ajax 108 $new_pos = array(); // store new positions for ajax
109 109
110 $siblings = get_posts(array( 110 $siblings = get_posts(array(
111 'depth' => 1, 111 'depth' => 1,
112 'numberposts' => -1, 112 'numberposts' => -1,
113 'post_type' => $post->post_type, 113 'post_type' => $post->post_type,
114 'post_status' => 'publish,pending,draft,future,private', 114 'post_status' => 'publish,pending,draft,future,private',
115 'post_parent' => $post->post_parent, 115 'post_parent' => $post->post_parent,
116 'orderby' => 'menu_order title', 116 'orderby' => 'menu_order title',
117 'order' => 'ASC', 117 'order' => 'ASC',
118 'exclude' => $post->ID 118 'exclude' => $post->ID
119 )); // fetch all the siblings (relative ordering) 119 )); // fetch all the siblings (relative ordering)
120 120
121 $menu_order = 0; 121 $menu_order = 0;
122 122
123 foreach( $siblings as $sibling ) : 123 foreach( $siblings as $sibling ) :
124 124
125 // if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order 125 // if this is the post that comes after our repositioned post, set our repositioned post position and increment menu order
126 if ( $nextid == $sibling->ID ) { 126 if ( $nextid == $sibling->ID ) {
127 wp_update_post(array( 'ID' => $post->ID, 'menu_order' => $menu_order )); 127 wp_update_post(array( 'ID' => $post->ID, 'menu_order' => $menu_order ));
128 $new_pos[$post->ID] = $menu_order; 128 $new_pos[$post->ID] = $menu_order;
129 $menu_order++; 129 $menu_order++;
130 } 130 }
131 131
132 // if repositioned post has been set, and new items are already in the right order, we can stop 132 // if repositioned post has been set, and new items are already in the right order, we can stop
133 if ( isset( $new_pos[$post->ID] ) && $sibling->menu_order >= $menu_order ) 133 if ( isset( $new_pos[$post->ID] ) && $sibling->menu_order >= $menu_order )
134 break; 134 break;
135 135
136 // set the menu order of the current sibling and increment the menu order 136 // set the menu order of the current sibling and increment the menu order
137 wp_update_post(array( 'ID' => $sibling->ID, 'menu_order' => $menu_order )); 137 wp_update_post(array( 'ID' => $sibling->ID, 'menu_order' => $menu_order ));
138 $new_pos[$sibling->ID] = $menu_order; 138 $new_pos[$sibling->ID] = $menu_order;
139 $menu_order++; 139 $menu_order++;
140 140
141 if ( ! $nextid && $previd == $sibling->ID ) { 141 if ( ! $nextid && $previd == $sibling->ID ) {
142 wp_update_post(array( 'ID' => $post->ID, 'menu_order' => $menu_order )); 142 wp_update_post(array( 'ID' => $post->ID, 'menu_order' => $menu_order ));
143 $new_pos[$post->ID] = $menu_order; 143 $new_pos[$post->ID] = $menu_order;
144 $menu_order++; 144 $menu_order++;
145 } 145 }
146 146
147 endforeach; 147 endforeach;
148 148
149 // if the moved post has children, we need to refresh the page 149 // if the moved post has children, we need to refresh the page
150 $children = get_posts(array( 'depth' => 1, 'numberposts' => 1, 'post_type' => $post->post_type, 'post_status' => 'publish,pending,draft,future,private', 'post_parent' => $post->ID )); 150 $children = get_posts(array( 'depth' => 1, 'numberposts' => 1, 'post_type' => $post->post_type, 'post_status' => 'publish,pending,draft,future,private', 'post_parent' => $post->ID ));
151 if ( ! empty( $children ) ) 151 if ( ! empty( $children ) )
152 die('children'); 152 die('children');
153 153
154 die( json_encode($new_pos) ); 154 die( json_encode($new_pos) );
155 } 155 }
156 156
157 function sort_by_order_link( $views ) { 157 function sort_by_order_link( $views ) {
158 global $post_type, $wp_query; 158 global $post_type, $wp_query;
159 $class = ( $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';159 $class = ( isset( $wp_query->query['orderby'] ) && $wp_query->query['orderby'] == 'menu_order title' ) ? 'current' : '';
160 $query_string = remove_query_arg(array( 'orderby', 'order' )); 160 $query_string = remove_query_arg(array( 'orderby', 'order' ));
161 $query_string = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string ); 161 $query_string = add_query_arg( 'orderby', urlencode('menu_order title'), $query_string );
162 $views['byorder'] = '<a href="'. $query_string . '" class="' . $class . '">Sort by Order</a>'; 162 $views['byorder'] = '<a href="'. $query_string . '" class="' . $class . '">Sort by Order</a>';
163 return $views; 163 return $views;
164 } 164 }
165} 165}
166 166
167if ( is_admin() ) 167if ( is_admin() )
168 $simple_page_ordering = new simple_page_ordering;168 $simple_page_ordering = new simple_page_ordering;
ORIGINAL TEXT
CHANGED TEXT