Admin Post Navigation - Version 1.6

Version Description

  • Add support for navigation in other post types
    • Add filter 'c2c_admin_post_navigation_post_types' for customizing valid post_types for search
    • Enable navigation for all post types by default
    • Allow per-post_type sort order for navigation by adding $post_type argument when applying filters for 'c2c_admin_post_navigation_orderby'
    • Pass additional arguments ($post_type and $post) to functions hooking 'c2c_admin_post_navigation_post_statuses'
  • Ensure post navigation only appears on posts of the appropriate post_status
  • For hierarchical post types, order by 'post_title', otherwise order by 'ID' (filterable)
  • Move application of filters from admin_init() into new do_meta_box(), which is hooking 'do_meta_box' action, so they only fire when actually being used
  • Output JavaScript via 'admin_print_footer_scripts' action rather than 'admin_footer'
  • Rename class from 'AdminPostNavigation' to 'c2c_AdminPostNavigation'
  • Switch from object instantiation to direct class invocation
  • Explicitly declare all functions public static and class variables private static
  • Documentation tweaks
  • Note compatibility through WP 3.1+
  • Update copyright date (2011)
Download this release

Release Info

Developer coffee2code
Plugin Icon 128x128 Admin Post Navigation
Version 1.6
Comparing to
See all releases

Code changes from version 1.5 to 1.6

Files changed (2) hide show
  1. admin-post-navigation.php +74 -44
  2. readme.txt +53 -6
admin-post-navigation.php CHANGED
@@ -2,17 +2,17 @@
2
  /**
3
  * @package Admin_Post_Navigation
4
  * @author Scott Reilly
5
- * @version 1.5
6
  */
7
  /*
8
  Plugin Name: Admin Post Navigation
9
- Version: 1.5
10
  Plugin URI: http://coffee2code.com/wp-plugins/admin-post-navigation/
11
  Author: Scott Reilly
12
  Author URI: http://coffee2code.com
13
  Description: Adds links to the next and previous posts when editing a post in the WordPress admin.
14
 
15
- Compatible with WordPress 2.8+, 2.9+, 3.0+.
16
 
17
  =>> Read the accompanying readme.txt file for instructions and documentation.
18
  =>> Also, visit the plugin's homepage for additional information and updates.
@@ -21,7 +21,7 @@ Compatible with WordPress 2.8+, 2.9+, 3.0+.
21
  */
22
 
23
  /*
24
- Copyright (c) 2008-2010 by Scott Reilly (aka coffee2code)
25
 
26
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
27
  files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
@@ -36,38 +36,57 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRA
36
  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
  */
38
 
39
- if ( is_admin() && !class_exists( 'AdminPostNavigation' ) ) :
40
 
41
- class AdminPostNavigation {
42
- var $prev_text = '';
43
- var $next_text = '';
44
-
45
- var $orderby = 'ID'; // Filterable later
46
- var $post_statuses = array( 'draft', 'future', 'pending', 'private', 'publish' ); // Filterable later
47
 
48
  /**
49
  * Class constructor: initializes class variables and adds actions and filters.
50
  */
51
- function AdminPostNavigation() {
52
  global $pagenow;
53
  if ( 'post.php' == $pagenow ) {
54
- $this->prev_text = __( '« Previous' );
55
- $this->next_text = __( 'Next »' );
56
 
57
- add_action( 'admin_init', array( &$this,'admin_init' ) );
58
- add_action( 'admin_head', array( &$this, 'add_css' ) );
59
- add_action( 'admin_footer', array( &$this, 'add_js' ) );
60
  }
61
  }
62
 
63
  /**
64
  * Initialize variables and meta_box
65
  */
66
- function admin_init() {
67
- $this->orderby = esc_sql( apply_filters( 'c2c_admin_post_navigation_orderby', $this->orderby ) ); // pre-1.5 this used to order by 'post_date'
68
- $this->post_statuses = apply_filters( 'c2c_admin_post_navigation_post_statuses', $this->post_statuses );
69
- $this->post_statuses = "'" . implode( "', '", array_map( 'esc_sql', $this->post_statuses ) ) . "'";
70
- add_meta_box( 'adminpostnav', 'Post Navigation', array( &$this, 'add_meta_box' ), 'post', 'side', 'core' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
72
 
73
  /**
@@ -77,23 +96,23 @@ class AdminPostNavigation {
77
  * @param array $box
78
  * @return void (Text is echoed.)
79
  */
80
- function add_meta_box( $object, $box ) {
81
  global $post_ID;
82
  $display = '';
83
  $context = $object->post_type;
84
- $prev = $this->previous_post();
85
  if ( $prev ) {
86
- $post_title = esc_attr( strip_tags( get_the_title( $prev->ID ) ) );
87
  $display .= '<a href="' . get_edit_post_link( $prev->ID ) .
88
- "\" id='admin-post-nav-prev' title='Previous $context: $post_title' class='admin-post-nav-prev'>{$this->prev_text}</a>";
89
  }
90
- $next = $this->next_post();
91
  if ( $next ) {
92
  if ( !empty( $display ) )
93
  $display .= ' | ';
94
- $post_title = esc_attr( strip_tags( get_the_title( $next->ID ) ) );
95
  $display .= '<a href="' . get_edit_post_link( $next->ID ) .
96
- "\" id='admin-post-nav-next' title='Next $context: $post_title' class='admin-post-nav-next'>{$this->next_text}</a>";
97
  }
98
  $display = '<span id="admin-post-nav">' . $display . '</span>';
99
  $display = apply_filters( 'admin_post_nav', $display ); /* Deprecated as of v1.5 */
@@ -103,7 +122,7 @@ class AdminPostNavigation {
103
  /**
104
  * Outputs CSS within style tags
105
  */
106
- function add_css() {
107
  echo <<<CSS
108
  <style type="text/css">
109
  #admin-post-nav {margin-left:20px;}
@@ -121,7 +140,7 @@ CSS;
121
  * for non-JS people is that the plugin's meta_box is shown and the
122
  * navigation links can be found there.
123
  */
124
- function add_js() {
125
  echo <<<JS
126
  <script type="text/javascript">
127
  jQuery(document).ready(function($) {
@@ -147,21 +166,32 @@ JS;
147
  * @param int $limit (optional) Limit. Default is 15.
148
  * @return string
149
  */
150
- function query( $type = '<', $offset = 0, $limit = 15 ) {
151
  global $post_ID, $wpdb;
152
 
153
  if ( $type != '<' )
154
  $type = '>';
155
  $offset = (int) $offset;
156
- $limit = (int) $limit;
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
- $sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status IN ({$this->post_statuses}) ";
159
- if ( $post_ID )
160
- $sql .= "AND ID $type $post_ID ";
161
  $sort = $type == '<' ? 'DESC' : 'ASC';
162
- $sql .= "ORDER BY {$this->orderby} $sort LIMIT $offset, $limit";
163
 
164
  // Find the first one the user can actually edit
 
 
165
  $posts = $wpdb->get_results( $sql );
166
  $result = false;
167
  if ( $posts ) {
@@ -175,7 +205,7 @@ JS;
175
  $offset += $limit;
176
  // Double the limit each time (if haven't found a post yet, chances are we may not, so try to get through posts quicker)
177
  $limit += $limit;
178
- return $this->query( $type, $offset, $limit );
179
  }
180
  }
181
  return $result;
@@ -188,8 +218,8 @@ JS;
188
  *
189
  * @return object The next post object.
190
  */
191
- function next_post() {
192
- return $this->query( '>' );
193
  }
194
 
195
  /**
@@ -199,13 +229,13 @@ JS;
199
  *
200
  * @return object The previous post object.
201
  */
202
- function previous_post() {
203
- return $this->query( '<' );
204
  }
205
 
206
- } // end AdminPostNavigation
207
 
208
- $GLOBALS['c2c_admin_post_navigation'] = new AdminPostNavigation();
209
 
210
  endif; // end if !class_exists()
211
 
2
  /**
3
  * @package Admin_Post_Navigation
4
  * @author Scott Reilly
5
+ * @version 1.6
6
  */
7
  /*
8
  Plugin Name: Admin Post Navigation
9
+ Version: 1.6
10
  Plugin URI: http://coffee2code.com/wp-plugins/admin-post-navigation/
11
  Author: Scott Reilly
12
  Author URI: http://coffee2code.com
13
  Description: Adds links to the next and previous posts when editing a post in the WordPress admin.
14
 
15
+ Compatible with WordPress 2.8+, 2.9+, 3.0+, 3.1+.
16
 
17
  =>> Read the accompanying readme.txt file for instructions and documentation.
18
  =>> Also, visit the plugin's homepage for additional information and updates.
21
  */
22
 
23
  /*
24
+ Copyright (c) 2008-2011 by Scott Reilly (aka coffee2code)
25
 
26
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
27
  files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
36
  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
  */
38
 
39
+ if ( is_admin() && !class_exists( 'c2c_AdminPostNavigation' ) ) :
40
 
41
+ class c2c_AdminPostNavigation {
42
+ private static $prev_text = '';
43
+ private static $next_text = '';
44
+ private static $post_statuses = array( 'draft', 'future', 'pending', 'private', 'publish' ); // Filterable later
45
+ private static $post_statuses_sql = '';
 
46
 
47
  /**
48
  * Class constructor: initializes class variables and adds actions and filters.
49
  */
50
+ public static function init() {
51
  global $pagenow;
52
  if ( 'post.php' == $pagenow ) {
53
+ self::$prev_text = __( '&laquo; Previous' );
54
+ self::$next_text = __( 'Next &raquo;' );
55
 
56
+ add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
57
+ add_action( 'admin_head', array( __CLASS__, 'add_css' ) );
58
+ add_action( 'admin_print_footer_scripts', array( __CLASS__, 'add_js' ) );
59
  }
60
  }
61
 
62
  /**
63
  * Initialize variables and meta_box
64
  */
65
+ public static function admin_init() {
66
+ add_action( 'do_meta_boxes', array( __CLASS__, 'do_meta_box' ), 10, 3 ); /* For WP 3.0+ only support, change this to hook 'add_meta_boxes' */
67
+ }
68
+
69
+ /**
70
+ * Register meta box
71
+ *
72
+ * By default, the navigation is present for all post types. Filter
73
+ * 'c2c_admin_post_navigation_post_types' to limit its use.
74
+ *
75
+ * @param string $post_type The post type
76
+ * @param string $type The mode for the meta box (normal, advanced, or side)
77
+ * @param WP_Post $post The post
78
+ * @return void
79
+ */
80
+ public static function do_meta_box( $post_type, $type, $post ) {
81
+ $post_statuses = apply_filters( 'c2c_admin_post_navigation_post_statuses', self::$post_statuses, $post_type, $post );
82
+
83
+ $post_types = apply_filters( 'c2c_admin_post_navigation_post_types', get_post_types() );
84
+ if ( !in_array( $post_type, $post_types ) )
85
+ return;
86
+
87
+ self::$post_statuses_sql = "'" . implode( "', '", array_map( 'esc_sql', $post_statuses ) ) . "'";
88
+ if ( in_array( $post->post_status, $post_statuses ) )
89
+ add_meta_box( 'adminpostnav', sprintf( '%s Navigation', strtoupper( $post_type ) ), array( __CLASS__, 'add_meta_box' ), $post_type, 'side', 'core' );
90
  }
91
 
92
  /**
96
  * @param array $box
97
  * @return void (Text is echoed.)
98
  */
99
+ public static function add_meta_box( $object, $box ) {
100
  global $post_ID;
101
  $display = '';
102
  $context = $object->post_type;
103
+ $prev = self::previous_post();
104
  if ( $prev ) {
105
+ $post_title = esc_attr( strip_tags( get_the_title( $prev->ID ) ) ); /* If only the_title_attribute() accepted post ID as arg */
106
  $display .= '<a href="' . get_edit_post_link( $prev->ID ) .
107
+ "\" id='admin-post-nav-prev' title='Previous $context: $post_title' class='admin-post-nav-prev'>" . self::$prev_text . '</a>';
108
  }
109
+ $next = self::next_post();
110
  if ( $next ) {
111
  if ( !empty( $display ) )
112
  $display .= ' | ';
113
+ $post_title = esc_attr( strip_tags( get_the_title( $next->ID ) ) ); /* If only the_title_attribute() accepted post ID as arg */
114
  $display .= '<a href="' . get_edit_post_link( $next->ID ) .
115
+ "\" id='admin-post-nav-next' title='Next $context: $post_title' class='admin-post-nav-next'>" . self::$next_text . '</a>';
116
  }
117
  $display = '<span id="admin-post-nav">' . $display . '</span>';
118
  $display = apply_filters( 'admin_post_nav', $display ); /* Deprecated as of v1.5 */
122
  /**
123
  * Outputs CSS within style tags
124
  */
125
+ public static function add_css() {
126
  echo <<<CSS
127
  <style type="text/css">
128
  #admin-post-nav {margin-left:20px;}
140
  * for non-JS people is that the plugin's meta_box is shown and the
141
  * navigation links can be found there.
142
  */
143
+ public static function add_js() {
144
  echo <<<JS
145
  <script type="text/javascript">
146
  jQuery(document).ready(function($) {
166
  * @param int $limit (optional) Limit. Default is 15.
167
  * @return string
168
  */
169
+ public static function query( $type = '<', $offset = 0, $limit = 15 ) {
170
  global $post_ID, $wpdb;
171
 
172
  if ( $type != '<' )
173
  $type = '>';
174
  $offset = (int) $offset;
175
+ $limit = (int) $limit;
176
+
177
+ $post_type = esc_sql( get_post_type( $post_ID ) );
178
+ $sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status IN (" . self::$post_statuses_sql . ') ';
179
+
180
+ // Determine order
181
+ if ( function_exists( 'is_post_type_hierarchical' ) && is_post_type_hierarchical( $post_type ) )
182
+ $orderby = 'post_title';
183
+ else
184
+ $orderby = 'ID';
185
+ $orderby = esc_sql( apply_filters( 'c2c_admin_post_navigation_orderby', $orderby, $post_type ) );
186
+ $post = get_post( $post_ID );
187
+ $sql .= "AND $orderby $type '{$post->$orderby}' ";
188
 
 
 
 
189
  $sort = $type == '<' ? 'DESC' : 'ASC';
190
+ $sql .= "ORDER BY $orderby $sort LIMIT $offset, $limit";
191
 
192
  // Find the first one the user can actually edit
193
+ //echo "<p>query $type : ($sql)</p>";
194
+ //die();
195
  $posts = $wpdb->get_results( $sql );
196
  $result = false;
197
  if ( $posts ) {
205
  $offset += $limit;
206
  // Double the limit each time (if haven't found a post yet, chances are we may not, so try to get through posts quicker)
207
  $limit += $limit;
208
+ return self::query( $type, $offset, $limit );
209
  }
210
  }
211
  return $result;
218
  *
219
  * @return object The next post object.
220
  */
221
+ public static function next_post() {
222
+ return self::query( '>' );
223
  }
224
 
225
  /**
229
  *
230
  * @return object The previous post object.
231
  */
232
+ public static function previous_post() {
233
+ return self::query( '<' );
234
  }
235
 
236
+ } // end c2c_AdminPostNavigation
237
 
238
+ c2c_AdminPostNavigation::init();
239
 
240
  endif; // end if !class_exists()
241
 
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Admin Post Navigation ===
2
  Contributors: coffee2code
3
  Donate link: http://coffee2code.com/donate
4
- Tags: admin, navigation, post, next, previous, edit, coffee2code
5
  Requires at least: 2.8
6
- Tested up to: 3.0.1
7
- Stable tag: 1.5
8
- Version: 1.5
9
 
10
  Adds links to the next and previous posts when editing a post in the WordPress admin.
11
 
@@ -20,11 +20,14 @@ Currently, a previous/next post is determined by the next lower/higher valid pos
20
 
21
  NOTE: Be sure to save the post currently being edited before navigating away to the previous/next post.
22
 
 
 
23
 
24
  == Installation ==
25
 
26
  1. Unzip `admin-post-navigation.zip` inside the `/wp-content/plugins/` directory for your site (or install via the built-in WordPress plugin installer)
27
  1. Activate the plugin through the 'Plugins' admin menu in WordPress
 
28
 
29
 
30
  == Screenshots ==
@@ -39,7 +42,7 @@ The plugin is further customizable via three filters. Typically, these customiza
39
 
40
  = c2c_admin_post_navigation_orderby =
41
 
42
- The 'c2c_admin_post_navigation_orderby' filter allows you to change the post field used in the ORDER BY clause for the SQL to find the previous/next post. By default this is 'ID'. If you wish to change this, hook this filter. This is not typical usage for most users.
43
 
44
  Arguments:
45
 
@@ -69,6 +72,30 @@ function change_apn_post_status( $post_statuses ) {
69
  return $post_statuses;
70
  }`
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  = c2c_admin_post_navigation_display =
73
 
74
  The 'c2c_admin_post_navigation_display' filter allows you to customize the output links for the post navigation.
@@ -88,6 +115,23 @@ function override_apn_display( $text ) {
88
 
89
  == Changelog ==
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  = 1.5 =
92
  * Change post search ORDER BY from 'post_date' to 'ID'
93
  * Add filter 'c2c_admin_post_navigation_orderby' for customizing search ORDER BY field
@@ -129,5 +173,8 @@ function override_apn_display( $text ) {
129
 
130
  == Upgrade Notice ==
131
 
 
 
 
132
  = 1.5 =
133
- Recommended update. Highlights: find prev/next post by ID rather than post_date, added verified WP 3.0 compatibility.
1
  === Admin Post Navigation ===
2
  Contributors: coffee2code
3
  Donate link: http://coffee2code.com/donate
4
+ Tags: admin, navigation, post, next, previous, edit, post types, coffee2code
5
  Requires at least: 2.8
6
+ Tested up to: 3.1
7
+ Stable tag: 1.6
8
+ Version: 1.6
9
 
10
  Adds links to the next and previous posts when editing a post in the WordPress admin.
11
 
20
 
21
  NOTE: Be sure to save the post currently being edited before navigating away to the previous/next post.
22
 
23
+ Links: [Plugin Homepage]:(http://coffee2code.com/wp-plugins/admin-post-navigation/) | [Author Homepage]:(http://coffee2code.com)
24
+
25
 
26
  == Installation ==
27
 
28
  1. Unzip `admin-post-navigation.zip` inside the `/wp-content/plugins/` directory for your site (or install via the built-in WordPress plugin installer)
29
  1. Activate the plugin through the 'Plugins' admin menu in WordPress
30
+ 1. See documentation for available customizations, if so inclined
31
 
32
 
33
  == Screenshots ==
42
 
43
  = c2c_admin_post_navigation_orderby =
44
 
45
+ The 'c2c_admin_post_navigation_orderby' filter allows you to change the post field used in the ORDER BY clause for the SQL to find the previous/next post. By default this is 'ID' for non-hierarchical post types (such as posts) and 'post_title' for hierarchical post types (such as pages). If you wish to change this, hook this filter. This is not typical usage for most users.
46
 
47
  Arguments:
48
 
72
  return $post_statuses;
73
  }`
74
 
75
+ = c2c_admin_post_navigation_post_types =
76
+
77
+ The 'c2c_admin_post_navigation_post_types' filter allows you to modify the list of post_types used as part of the search for the prev/next post. By default this array includes all available post types. If you wish to change this, hook this filter.
78
+
79
+ Arguments:
80
+
81
+ * $post_types (array) The array of valid post_types
82
+
83
+ Examples:
84
+
85
+ `// Modify Admin Post Navigation to only allow navigating strictly for posts.
86
+ add_filter( 'c2c_admin_post_navigation_post_types', 'change_apn_post_types' );
87
+ function change_apn_post_types( $post_types ) {
88
+ return array( 'post' );
89
+ }`
90
+
91
+ `// Modify Admin Post Navigation to disallow navigation for the 'recipe' post type
92
+ add_filter( 'c2c_admin_post_navigation_post_types', 'remove_recipe_apn_post_types' );
93
+ function remove_recipe_apn_post_types( $post_types ) {
94
+ if ( isset( $post_types['recipe'] ) )
95
+ unset( $post_types['recipe'] ); // Removing a post type
96
+ return $post_types;
97
+ }`
98
+
99
  = c2c_admin_post_navigation_display =
100
 
101
  The 'c2c_admin_post_navigation_display' filter allows you to customize the output links for the post navigation.
115
 
116
  == Changelog ==
117
 
118
+ = 1.6 =
119
+ * Add support for navigation in other post types
120
+ * Add filter 'c2c_admin_post_navigation_post_types' for customizing valid post_types for search
121
+ * Enable navigation for all post types by default
122
+ * Allow per-post_type sort order for navigation by adding $post_type argument when applying filters for 'c2c_admin_post_navigation_orderby'
123
+ * Pass additional arguments ($post_type and $post) to functions hooking 'c2c_admin_post_navigation_post_statuses'
124
+ * Ensure post navigation only appears on posts of the appropriate post_status
125
+ * For hierarchical post types, order by 'post_title', otherwise order by 'ID' (filterable)
126
+ * Move application of filters from admin_init() into new do_meta_box(), which is hooking 'do_meta_box' action, so they only fire when actually being used
127
+ * Output JavaScript via 'admin_print_footer_scripts' action rather than 'admin_footer'
128
+ * Rename class from 'AdminPostNavigation' to 'c2c_AdminPostNavigation'
129
+ * Switch from object instantiation to direct class invocation
130
+ * Explicitly declare all functions public static and class variables private static
131
+ * Documentation tweaks
132
+ * Note compatibility through WP 3.1+
133
+ * Update copyright date (2011)
134
+
135
  = 1.5 =
136
  * Change post search ORDER BY from 'post_date' to 'ID'
137
  * Add filter 'c2c_admin_post_navigation_orderby' for customizing search ORDER BY field
173
 
174
  == Upgrade Notice ==
175
 
176
+ = 1.6 =
177
+ Feature update: added support for non-'post' post types; fixed so navigation only appears for posts of appropriate post status; implementation changes; renamed class; updated copyright date; other minor code changes.
178
+
179
  = 1.5 =
180
+ Recommended update. Highlights: find prev/next post by ID rather than post_date, fix navigation logic, added numerous filters to allow for customizations, miscellaneous improvements, dropped pre-WP 2.8 compatibility, added verified WP 3.0 compatibility.