PageRenderTime 81ms CodeModel.GetById 21ms RepoModel.GetById 4ms app.codeStats 0ms

/wp-content/themes/news/library/extensions/custom-field-series.php

https://bitbucket.org/lgorence/quickpress
PHP | 270 lines | 88 code | 43 blank | 139 comment | 16 complexity | 2789be46b47ef00324c7dedd606fcfb8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Custom Field Series - A script for creating a series of posts by custom field.
  4. *
  5. * Custom Field Series was created to allow users to add individual posts to a larger series of posts. It was
  6. * created before WordPress made it easy for developers to create new taxonomies. Ideally, one would use a
  7. * taxonomy to handle this functionality. However, this method is lighter, provides an extremely simple
  8. * method for adding posts to a series, and offers backwards compatibility for people that have used this
  9. * method before.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License as published by the Free Software Foundation; either version 2 of the License,
  13. * or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  16. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * @package CustomFieldSeries
  19. * @version 0.4.0
  20. * @author Justin Tadlock <justin@justintadlock.com>
  21. * @copyright Copyright (c) 2007 - 2012, Justin Tadlock
  22. * @link http://justintadlock.com/archives/2007/11/01/wordpress-custom-fields-listing-a-series-of-posts
  23. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  24. */
  25. /* Add support for the 'custom-field-series' extension to posts. */
  26. add_action( 'init', 'custom_field_series_post_type_support' );
  27. /* Register metadata for the custom field series. */
  28. add_action( 'init', 'custom_field_series_register_meta' );
  29. /* Create the meta box on the 'admin_menu' hook. */
  30. add_action( 'admin_menu', 'custom_field_series_admin_setup' );
  31. /**
  32. * Checks for a series of posts by the current post's metadata. The function grabs the meta value for the
  33. * 'Series' meta key and checks if any posts have been given the same value. If posts are found with this
  34. * meta key/value pair, the function adds them to an unordered list.
  35. *
  36. * @since 0.1.0
  37. * @access public
  38. * @param array $args Array of arguments.
  39. */
  40. function custom_field_series( $args = array() ) {
  41. /* Set $series to an empty string. */
  42. $series = '';
  43. /* Get the current post ID. */
  44. $post_id = get_the_ID();
  45. /* Get the series meta value for the post. */
  46. $meta_value = get_post_meta( $post_id, custom_field_series_meta_key(), true );
  47. /* If a meta value was found, create a list of posts in the series. */
  48. if ( !empty( $meta_value ) ) {
  49. /* Set up the default post query arguments. */
  50. $defaults = array(
  51. 'order' => 'DESC',
  52. 'orderby' => 'ID',
  53. 'include' => '',
  54. 'exclude' => '',
  55. 'post_type' => 'any',
  56. 'numberposts' => -1,
  57. 'meta_key' => custom_field_series_meta_key(),
  58. 'meta_value' => $meta_value,
  59. 'echo' => true
  60. );
  61. /* Allow developers to override the arguments used. */
  62. $args = apply_filters( 'custom_field_series_args', $args );
  63. $args = wp_parse_args( $args, $defaults );
  64. /* Get all posts in the current series. */
  65. $series_posts = get_posts( $args );
  66. /* If posts were found, display them. */
  67. if ( !empty( $series_posts ) ) {
  68. /* Format the series class with the name of the series. */
  69. $class = sanitize_html_class( sanitize_title_with_dashes( $meta_value ) );
  70. /* Create the opening wrapper div, title, and list element. */
  71. $series = '<div class="series series-' . esc_attr( $class ) . '">';
  72. $series .= '<h4 class="series-title">' . apply_filters( 'custom_field_series_title', __( 'Articles in this series', 'custom-field-series' ) ) . '</h4>';
  73. $series .= '<ul>';
  74. /* Loop through the posts. */
  75. foreach ( $series_posts as $serial ) {
  76. /* If the current post in the loop matches the post we're viewing, don't link to it. */
  77. if ( $serial->ID == $post_id )
  78. $series .= '<li class="current-post">' . $serial->post_title . '</li>';
  79. /* Display a link to the post. */
  80. else
  81. $series .= '<li><a href="' . get_permalink( $serial->ID ) . '" title="' . esc_attr( $serial->post_title ) . '">' . $serial->post_title . '</a></li>';
  82. }
  83. /* Close the unordered list and wrapper div. */
  84. $series .= '</ul></div>';
  85. }
  86. }
  87. /* Allow developers to overwrite the HTML of the series. */
  88. $series = apply_filters( 'custom_field_series', $series );
  89. /* If $echo is set to true, display the series. */
  90. if ( !empty( $args['echo'] ) )
  91. echo $series;
  92. /* If $echo is not set to true, return the series. */
  93. else
  94. return $series;
  95. }
  96. /**
  97. * Adds post type support of 'custom-field-series' to the 'post' post type. Developers should register support
  98. * for additional post types.
  99. *
  100. * @since 0.4.0
  101. * @access private
  102. * @return void
  103. */
  104. function custom_field_series_post_type_support() {
  105. add_post_type_support( 'post', 'custom-field-series' );
  106. }
  107. /**
  108. * Registers the custom field series meta key 'Series' for for specific object types and provides a
  109. * function to sanitize the metadata on update.
  110. *
  111. * @since 0.4.0
  112. * @access private
  113. * @return void
  114. */
  115. function custom_field_series_register_meta() {
  116. register_meta( 'post', custom_field_series_meta_key(), 'custom_field_series_sanitize_meta' );
  117. }
  118. /**
  119. * Callback function for sanitizing meta when add_metadata() or update_metadata() is called by WordPress.
  120. * If a developer wants to set up a custom method for sanitizing the data, they should use the
  121. * "sanitize_{$meta_type}_meta_{$meta_key}" filter hook to do so.
  122. *
  123. * @since 0.4.0
  124. * @param mixed $meta_value The value of the data to sanitize.
  125. * @param string $meta_key The meta key name.
  126. * @param string $meta_type The type of metadata (post, comment, user, etc.)
  127. * @return mixed $meta_value
  128. */
  129. function custom_field_series_sanitize_meta( $meta_value, $meta_key, $meta_type ) {
  130. return strip_tags( $meta_value );
  131. }
  132. /**
  133. * Returns the meta key used for the 'Series' custom field so that developers can overwrite the key if they
  134. * need to for their project.
  135. *
  136. * @since 0.4.0
  137. * @access public
  138. * @return string The meta key used for the series metadata.
  139. */
  140. function custom_field_series_meta_key() {
  141. return apply_filters( 'custom_field_series_meta_key', 'Series' );
  142. }
  143. /**
  144. * Admin setup for the custom field series script.
  145. *
  146. * @since 0.4.0
  147. * @access private
  148. * @return void
  149. */
  150. function custom_field_series_admin_setup() {
  151. /* Load the post meta boxes on the new post and edit post screens. */
  152. add_action( 'load-post.php', 'custom_field_series_load_meta_boxes' );
  153. add_action( 'load-post-new.php', 'custom_field_series_load_meta_boxes' );
  154. }
  155. /**
  156. * Hooks into the 'add_meta_boxes' hook to add the custom field series meta box and the 'save_post' hook
  157. * to save the metadata.
  158. *
  159. * @since 0.4.0
  160. * @access private
  161. * @return void
  162. */
  163. function custom_field_series_load_meta_boxes() {
  164. /* Add the custom field series meta box on the 'add_meta_boxes' hook. */
  165. add_action( 'add_meta_boxes', 'custom_field_series_create_meta_box', 10, 2 );
  166. /* Saves the post meta box data. */
  167. add_action( 'save_post', 'custom_field_series_meta_box_save', 10, 2 );
  168. }
  169. /**
  170. * Creates the meta box on the post editing screen for the 'post' post type.
  171. *
  172. * @since 0.3.0
  173. * @access private
  174. * @param string $post_type The post type of the current post being edited.
  175. * @param object $post The current post object.
  176. * @return void
  177. */
  178. function custom_field_series_create_meta_box( $post_type, $post ) {
  179. if ( post_type_supports( $post_type, 'custom-field-series' ) )
  180. add_meta_box( 'custom-field-series', __( 'Series', 'custom-field-series' ), 'custom_field_series_meta_box', $post_type, 'side', 'default' );
  181. }
  182. /**
  183. * Displays the input field with the meta box.
  184. *
  185. * @since 0.3.0
  186. * @access private
  187. * @param object $object The post object currently being edited.
  188. * @param array $box Specific information about the meta box being loaded.
  189. * @return void
  190. */
  191. function custom_field_series_meta_box( $object, $box ) { ?>
  192. <p>
  193. <?php wp_nonce_field( basename( __FILE__ ), 'custom-field-series-nonce' ); ?>
  194. <input type="text" name="custom-field-series" id="custom-field-series" value="<?php echo esc_attr( get_post_meta( $object->ID, custom_field_series_meta_key(), true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
  195. </p>
  196. <?php
  197. }
  198. /**
  199. * Saves the single value for the 'Series' meta key, which was set using the custom field series meta box.
  200. *
  201. * @since 0.3.0
  202. * @access private
  203. * @param int $post_id The ID of the current post being saved.
  204. * @param object $post The post object currently being saved.
  205. * @return void
  206. */
  207. function custom_field_series_meta_box_save( $post_id, $post ) {
  208. /* Verify the nonce before proceeding. */
  209. if ( !isset( $_POST['custom-field-series-nonce'] ) || !wp_verify_nonce( $_POST['custom-field-series-nonce'], basename( __FILE__ ) ) )
  210. return $post_id;
  211. /* Get the posted series title and strip all tags from it. */
  212. $new_meta_value = $_POST['custom-field-series'];
  213. /* Get the meta key. */
  214. $meta_key = custom_field_series_meta_key();
  215. /* Get the meta value of the custom field key. */
  216. $meta_value = get_post_meta( $post_id, $meta_key, true );
  217. /* If there is no new meta value but an old value exists, delete it. */
  218. if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
  219. delete_post_meta( $post_id, $meta_key, $meta_value );
  220. /* If a new meta value was added and there was no previous value, add it. */
  221. elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
  222. add_post_meta( $post_id, $meta_key, $new_meta_value, true );
  223. /* If the old layout doesn't match the new layout, update the post layout meta. */
  224. elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $meta_value !== $new_meta_value )
  225. update_post_meta( $post_id, $meta_key, $new_meta_value );
  226. }
  227. ?>