PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/admin/meta-box-post-template.php

https://bitbucket.org/lgorence/quickpress
PHP | 133 lines | 51 code | 24 blank | 58 comment | 26 complexity | 7b080528c02c5fbcc7a91cb4377b62ae MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Adds the template meta box to the post editing screen for public post types. This feature allows users and
  4. * devs to create custom templates for any post type, not just pages as default in WordPress core. The
  5. * functions in this file create the template meta box and save the template chosen by the user when the
  6. * post is saved. This file is only used if the theme supports the 'hybrid-core-template-hierarchy' feature.
  7. *
  8. * @package HybridCore
  9. * @subpackage Admin
  10. * @author Justin Tadlock <justin@justintadlock.com>
  11. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  12. * @link http://themehybrid.com/hybrid-core
  13. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  14. */
  15. /* Add the post template meta box on the 'add_meta_boxes' hook. */
  16. add_action( 'add_meta_boxes', 'hybrid_meta_box_post_add_template', 10, 2 );
  17. add_action( 'add_meta_boxes', 'hybrid_meta_box_post_remove_template', 10, 2 );
  18. /* Save the post template meta box data on the 'save_post' hook. */
  19. add_action( 'save_post', 'hybrid_meta_box_post_save_template', 10, 2 );
  20. /**
  21. * Adds the post template meta box for all public post types, excluding the 'page' post type since WordPress
  22. * core already handles page templates.
  23. *
  24. * @since 1.2.0
  25. * @return void
  26. */
  27. function hybrid_meta_box_post_add_template( $post_type, $post ) {
  28. $post_type_object = get_post_type_object( $post_type );
  29. /* Only add meta box if current user can edit, add, or delete meta for the post. */
  30. if ( ( true === $post_type_object->public ) && ( current_user_can( 'edit_post_meta', $post->ID ) || current_user_can( 'add_post_meta', $post->ID ) || current_user_can( 'delete_post_meta', $post->ID ) ) )
  31. add_meta_box( 'hybrid-core-post-template', __( 'Template', 'hybrid-core' ), 'hybrid_meta_box_post_display_template', $post_type, 'side', 'default' );
  32. }
  33. /**
  34. * Remove the meta box from some post types.
  35. *
  36. * @since 1.3.0
  37. * @param string $post_type The post type of the current post being edited.
  38. * @param object $post The current post being edited.
  39. * @return void
  40. */
  41. function hybrid_meta_box_post_remove_template( $post_type, $post ) {
  42. /* Removes meta box from pages since this is a built-in WordPress feature. */
  43. if ( 'page' == $post_type )
  44. remove_meta_box( 'hybrid-core-post-template', 'page', 'side' );
  45. /* Removes meta box from the bbPress 'topic' post type. */
  46. elseif ( function_exists( 'bbp_get_topic_post_type' ) && bbp_get_topic_post_type() == $post_type )
  47. remove_meta_box( 'hybrid-core-post-template', bbp_get_topic_post_type(), 'side' );
  48. /* Removes meta box from the bbPress 'reply' post type. */
  49. elseif ( function_exists( 'bbp_get_reply_post_type' ) && bbp_get_reply_post_type() == $post_type )
  50. remove_meta_box( 'hybrid-core-post-template', bbp_get_reply_post_type(), 'side' );
  51. }
  52. /**
  53. * Displays the post template meta box.
  54. *
  55. * @since 1.2.0
  56. * @return void
  57. */
  58. function hybrid_meta_box_post_display_template( $object, $box ) {
  59. /* Get the post type object. */
  60. $post_type_object = get_post_type_object( $object->post_type );
  61. /* Get a list of available custom templates for the post type. */
  62. $templates = hybrid_get_post_templates( $object->post_type );
  63. wp_nonce_field( basename( __FILE__ ), 'hybrid-core-post-meta-box-template' ); ?>
  64. <p>
  65. <?php if ( 0 != count( $templates ) ) { ?>
  66. <select name="hybrid-post-template" id="hybrid-post-template" class="widefat">
  67. <option value=""></option>
  68. <?php foreach ( $templates as $label => $template ) { ?>
  69. <option value="<?php echo esc_attr( $template ); ?>" <?php selected( esc_attr( get_post_meta( $object->ID, "_wp_{$post_type_object->name}_template", true ) ), esc_attr( $template ) ); ?>><?php echo esc_html( $label ); ?></option>
  70. <?php } ?>
  71. </select>
  72. <?php } else { ?>
  73. <?php _e( 'No templates exist for this post type.', 'hybrid-core' ); ?>
  74. <?php } ?>
  75. </p>
  76. <?php
  77. }
  78. /**
  79. * Saves the post template meta box settings as post metadata.
  80. *
  81. * @since 1.2.0
  82. * @param int $post_id The ID of the current post being saved.
  83. * @param int $post The post object currently being saved.
  84. * @return void|int
  85. */
  86. function hybrid_meta_box_post_save_template( $post_id, $post ) {
  87. /* Verify the nonce before proceeding. */
  88. if ( !isset( $_POST['hybrid-core-post-meta-box-template'] ) || !wp_verify_nonce( $_POST['hybrid-core-post-meta-box-template'], basename( __FILE__ ) ) )
  89. return $post_id;
  90. /* Return here if the template is not set. There's a chance it won't be if the post type doesn't have any templates. */
  91. if ( !isset( $_POST['hybrid-post-template'] ) )
  92. return $post_id;
  93. /* Get the posted meta value. */
  94. $new_meta_value = $_POST['hybrid-post-template'];
  95. /* Set the $meta_key variable based off the post type name. */
  96. $meta_key = "_wp_{$post->post_type}_template";
  97. /* Get the meta value of the meta key. */
  98. $meta_value = get_post_meta( $post_id, $meta_key, true );
  99. /* If there is no new meta value but an old value exists, delete it. */
  100. if ( current_user_can( 'delete_post_meta', $post_id ) && '' == $new_meta_value && $meta_value )
  101. delete_post_meta( $post_id, $meta_key, $meta_value );
  102. /* If a new meta value was added and there was no previous value, add it. */
  103. elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
  104. add_post_meta( $post_id, $meta_key, $new_meta_value, true );
  105. /* If the new meta value does not match the old value, update it. */
  106. elseif ( current_user_can( 'edit_post_meta', $post_id ) && $new_meta_value && $new_meta_value != $meta_value )
  107. update_post_meta( $post_id, $meta_key, $new_meta_value );
  108. }
  109. ?>