PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/admin-post-navigation/admin-post-navigation.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 290 lines | 124 code | 34 blank | 132 comment | 16 complexity | 300b337952f13e18819b93086a77143c MD5 | raw file
  1. <?php
  2. /**
  3. * @package Admin_Post_Navigation
  4. * @author Scott Reilly
  5. * @version 1.7.2
  6. */
  7. /*
  8. Plugin Name: Admin Post Navigation
  9. Version: 1.7.2
  10. Plugin URI: http://coffee2code.com/wp-plugins/admin-post-navigation/
  11. Author: Scott Reilly
  12. Author URI: http://coffee2code.com/
  13. Text Domain: admin-post-navigation
  14. Domain Path: /lang/
  15. License: GPLv2 or later
  16. License URI: http://www.gnu.org/licenses/gpl-2.0.html
  17. Description: Adds links to navigate to the next and previous posts when editing a post in the WordPress admin.
  18. Compatible with WordPress 3.0 through 3.5+.
  19. =>> Read the accompanying readme.txt file for instructions and documentation.
  20. =>> Also, visit the plugin's homepage for additional information and updates.
  21. =>> Or visit: http://wordpress.org/extend/plugins/admin-post-navigation/
  22. TODO:
  23. * Add screen option allowing user selection of post navigation order
  24. * Put CSS into enqueuable .css file
  25. * Put JS into enqueueable .js file
  26. */
  27. /*
  28. Copyright (c) 2008-2013 by Scott Reilly (aka coffee2code)
  29. This program is free software; you can redistribute it and/or
  30. modify it under the terms of the GNU General Public License
  31. as published by the Free Software Foundation; either version 2
  32. of the License, or (at your option) any later version.
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37. You should have received a copy of the GNU General Public License
  38. along with this program; if not, write to the Free Software
  39. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  40. */
  41. defined( 'ABSPATH' ) or die();
  42. if ( is_admin() && ! class_exists( 'c2c_AdminPostNavigation' ) ) :
  43. class c2c_AdminPostNavigation {
  44. private static $prev_text = '';
  45. private static $next_text = '';
  46. private static $post_statuses = array( 'draft', 'future', 'pending', 'private', 'publish' ); // Filterable later
  47. private static $post_statuses_sql = '';
  48. /**
  49. * Returns version of the plugin.
  50. *
  51. * @since 1.7
  52. */
  53. public static function version() {
  54. return '1.7.2';
  55. }
  56. /**
  57. * Class constructor: initializes class variables and adds actions and filters.
  58. */
  59. public static function init() {
  60. add_action( 'load-post.php', array( __CLASS__, 'register_post_page_hooks' ) );
  61. }
  62. /**
  63. * Filters/actions to hook on the admin post.php page.
  64. *
  65. * @since 1.7
  66. *
  67. */
  68. public static function register_post_page_hooks() {
  69. // Load textdomain
  70. load_plugin_textdomain( 'admin-post-navigation', false, basename( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' );
  71. // Set translatable strings
  72. self::$prev_text = __( '&larr; Previous', 'admin-post-navigation' );
  73. self::$next_text = __( 'Next &rarr;', 'admin-post-navigation' );
  74. // Register hooks
  75. add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_css' ) );
  76. add_action( 'admin_print_footer_scripts', array( __CLASS__, 'add_js' ) );
  77. add_action( 'do_meta_boxes', array( __CLASS__, 'do_meta_box' ), 10, 3 );
  78. }
  79. /**
  80. * Register meta box
  81. *
  82. * By default, the navigation is present for all post types. Filter
  83. * 'c2c_admin_post_navigation_post_types' to limit its use.
  84. *
  85. * @param string $post_type The post type
  86. * @param string $type The mode for the meta box (normal, advanced, or side)
  87. * @param WP_Post $post The post
  88. * @return void
  89. */
  90. public static function do_meta_box( $post_type, $type, $post ) {
  91. $post_types = apply_filters( 'c2c_admin_post_navigation_post_types', get_post_types() );
  92. if ( ! in_array( $post_type, $post_types ) )
  93. return;
  94. $post_statuses = apply_filters( 'c2c_admin_post_navigation_post_statuses', self::$post_statuses, $post_type, $post );
  95. self::$post_statuses_sql = "'" . implode( "', '", array_map( 'esc_sql', $post_statuses ) ) . "'";
  96. $label = self::_get_post_type_label( $post_type );
  97. if ( in_array( $post->post_status, $post_statuses ) )
  98. add_meta_box( 'adminpostnav', sprintf( __( '%s Navigation', 'admin-post-navigation' ), ucfirst( $post_type ) ), array( __CLASS__, 'add_meta_box' ), $post_type, 'side', 'core' );
  99. }
  100. /**
  101. * Adds the content for the post navigation meta_box.
  102. *
  103. * @param object $object
  104. * @param array $box
  105. * @return void (Text is echoed.)
  106. */
  107. public static function add_meta_box( $object, $box ) {
  108. global $post_ID;
  109. $display = '';
  110. $context = self::_get_post_type_label( $object->post_type );
  111. $prev = self::previous_post();
  112. if ( $prev ) {
  113. $post_title = strip_tags( get_the_title( $prev->ID ) ); /* If only the_title_attribute() accepted post ID as arg */
  114. $display .= '<a href="' . get_edit_post_link( $prev->ID ) . '" id="admin-post-nav-prev" title="' .
  115. esc_attr( sprintf( __( 'Previous %1$s: %2$s', 'admin-post-navigation' ), $context, $post_title ) ) .
  116. '" class="admin-post-nav-prev add-new-h2">' . self::$prev_text . '</a>';
  117. }
  118. $next = self::next_post();
  119. if ( $next ) {
  120. if ( ! empty( $display ) )
  121. $display .= ' ';
  122. $post_title = strip_tags( get_the_title( $next->ID ) ); /* If only the_title_attribute() accepted post ID as arg */
  123. $display .= '<a href="' . get_edit_post_link( $next->ID ) .
  124. '" id="admin-post-nav-next" title="' .
  125. esc_attr( sprintf( __( 'Next %1$s: %2$s', 'admin-post-navigation' ), $context, $post_title ) ).
  126. '" class="admin-post-nav-next add-new-h2">' . self::$next_text . '</a>';
  127. }
  128. $display = '<span id="admin-post-nav">' . $display . '</span>';
  129. $display = apply_filters( 'admin_post_nav', $display ); /* Deprecated as of v1.5 */
  130. echo apply_filters( 'c2c_admin_post_navigation_display', $display );
  131. }
  132. /**
  133. * Gets label for post type.
  134. *
  135. * @since 1.7
  136. *
  137. * @param string $post_type The post_type
  138. * @return string The label for the post_type
  139. */
  140. public static function _get_post_type_label( $post_type ) {
  141. $label = $post_type;
  142. $post_type_object = get_post_type_object( $label );
  143. if ( is_object( $post_type_object ) )
  144. $label = $post_type_object->labels->singular_name;
  145. return strtolower( $label );
  146. }
  147. /**
  148. * Outputs CSS within style tags
  149. */
  150. public static function add_css() {
  151. echo <<<HTML
  152. <style type="text/css">
  153. #admin-post-nav {margin-left:20px;}
  154. #adminpostnav #admin-post-nav {margin-left:0;}
  155. h2 #admin-post-nav {font-size:0.6em;}
  156. </style>
  157. HTML;
  158. }
  159. /**
  160. * Outputs the JavaScript used by the plugin.
  161. *
  162. * For those with JS enabled, the navigation links are moved next to the
  163. * "Edit Post" header and the plugin's meta_box is hidden. The fallback
  164. * for non-JS people is that the plugin's meta_box is shown and the
  165. * navigation links can be found there.
  166. */
  167. public static function add_js() {
  168. echo <<<JS
  169. <script type="text/javascript">
  170. jQuery(document).ready(function($) {
  171. $('#admin-post-nav').appendTo($('h2'));
  172. $('#adminpostnav').hide();
  173. });
  174. </script>
  175. JS;
  176. }
  177. /**
  178. * Returns the previous or next post relative to the current post.
  179. *
  180. * Currently, a previous/next post is determined by the next lower/higher
  181. * valid post based on relative sequential post ID and which the user can
  182. * edit. Other post criteria such as post type (draft, pending, etc),
  183. * publish date, post author, category, etc, are not taken into
  184. * consideration when determining the previous or next post.
  185. *
  186. * @param string $type (optional) Either '<' or '>', indicating previous or next post, respectively. Default is '<'.
  187. * @param int $offset (optional) Offset. Default is 0.
  188. * @param int $limit (optional) Limit. Default is 15.
  189. * @return string
  190. */
  191. public static function query( $type = '<', $offset = 0, $limit = 15 ) {
  192. global $post_ID, $wpdb;
  193. if ( $type != '<' )
  194. $type = '>';
  195. $offset = (int) $offset;
  196. $limit = (int) $limit;
  197. $post_type = esc_sql( get_post_type( $post_ID ) );
  198. $sql = "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = '$post_type' AND post_status IN (" . self::$post_statuses_sql . ') ';
  199. // Determine order
  200. if ( function_exists( 'is_post_type_hierarchical' ) && is_post_type_hierarchical( $post_type ) )
  201. $orderby = 'post_title';
  202. else
  203. $orderby = 'ID';
  204. $orderby = esc_sql( apply_filters( 'c2c_admin_post_navigation_orderby', $orderby, $post_type ) );
  205. $post = get_post( $post_ID );
  206. $sql .= "AND $orderby $type '{$post->$orderby}' ";
  207. $sort = $type == '<' ? 'DESC' : 'ASC';
  208. $sql .= "ORDER BY $orderby $sort LIMIT $offset, $limit";
  209. // Find the first one the user can actually edit
  210. $posts = $wpdb->get_results( $sql );
  211. $result = false;
  212. if ( $posts ) {
  213. foreach ( $posts as $post ) {
  214. if ( current_user_can( 'edit_post', $post->ID ) ) {
  215. $result = $post;
  216. break;
  217. }
  218. }
  219. if ( ! $result ) { // The fetch did not yield a post editable by user, so query again.
  220. $offset += $limit;
  221. // Double the limit each time (if haven't found a post yet, chances are we may not, so try to get through posts quicker)
  222. $limit += $limit;
  223. return self::query( $type, $offset, $limit );
  224. }
  225. }
  226. return $result;
  227. }
  228. /**
  229. * Returns the next post relative to the current post.
  230. *
  231. * A convenience function that calls query().
  232. *
  233. * @return object The next post object.
  234. */
  235. public static function next_post() {
  236. return self::query( '>' );
  237. }
  238. /**
  239. * Returns the previous post relative to the current post.
  240. *
  241. * A convenience function that calls query().
  242. *
  243. * @return object The previous post object.
  244. */
  245. public static function previous_post() {
  246. return self::query( '<' );
  247. }
  248. } // end c2c_AdminPostNavigation
  249. c2c_AdminPostNavigation::init();
  250. endif; // end if !class_exists()