PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 128 lines | 53 code | 23 blank | 52 comment | 21 complexity | 55d83354151004aac9b292a9b4c10254 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Adds the SEO meta box to the post editing screen for public post types. This feature allows the post author
  4. * to set a custom title, description, and keywords for the post, which will be viewed on the singular post page.
  5. * To use this feature, the theme must support the 'hybrid-core-seo' feature. The functions in this file create
  6. * the SEO meta box and save the settings chosen by the user when the post is saved.
  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 SEO meta box on the 'add_meta_boxes' hook. */
  16. add_action( 'add_meta_boxes', 'hybrid_meta_box_post_add_seo', 10, 2 );
  17. add_action( 'add_meta_boxes', 'hybrid_meta_box_post_remove_seo', 10, 2 );
  18. /* Save the post SEO meta box data on the 'save_post' hook. */
  19. add_action( 'save_post', 'hybrid_meta_box_post_save_seo', 10, 2 );
  20. /**
  21. * Adds the post SEO meta box for all public post types.
  22. *
  23. * @since 1.2.0
  24. * @param string $post_type The post type of the current post being edited.
  25. * @param object $post The current post being edited.
  26. * @return void
  27. */
  28. function hybrid_meta_box_post_add_seo( $post_type, $post ) {
  29. $post_type_object = get_post_type_object( $post_type );
  30. /* Only add meta box if current user can edit, add, or delete meta for the post. */
  31. 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 ) ) )
  32. add_meta_box( 'hybrid-core-post-seo', __( 'SEO', 'hybrid-core' ), 'hybrid_meta_box_post_display_seo', $post_type, 'normal', 'high' );
  33. }
  34. /**
  35. * Remove the meta box from some post types.
  36. *
  37. * @since 1.3.0
  38. * @param string $post_type The post type of the current post being edited.
  39. * @param object $post The current post being edited.
  40. * @return void
  41. */
  42. function hybrid_meta_box_post_remove_seo( $post_type, $post ) {
  43. /* Removes post stylesheets support of the bbPress 'topic' post type. */
  44. if ( function_exists( 'bbp_get_topic_post_type' ) && bbp_get_topic_post_type() == $post_type )
  45. remove_meta_box( 'hybrid-core-post-seo', bbp_get_topic_post_type(), 'normal' );
  46. /* Removes post stylesheets support of the bbPress 'reply' post type. */
  47. elseif ( function_exists( 'bbp_get_reply_post_type' ) && bbp_get_reply_post_type() == $post_type )
  48. remove_meta_box( 'hybrid-core-post-seo', bbp_get_reply_post_type(), 'normal' );
  49. }
  50. /**
  51. * Displays the post SEO meta box.
  52. *
  53. * @since 1.2.0
  54. * @return void
  55. */
  56. function hybrid_meta_box_post_display_seo( $object, $box ) {
  57. wp_nonce_field( basename( __FILE__ ), 'hybrid-core-post-seo' ); ?>
  58. <p>
  59. <label for="hybrid-document-title"><?php _e( 'Document Title:', 'hybrid-core' ); ?></label>
  60. <br />
  61. <input type="text" name="hybrid-document-title" id="hybrid-document-title" value="<?php echo esc_attr( get_post_meta( $object->ID, 'Title', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
  62. </p>
  63. <p>
  64. <label for="hybrid-meta-description"><?php _e( 'Meta Description:', 'hybrid-core' ); ?></label>
  65. <br />
  66. <textarea name="hybrid-meta-description" id="hybrid-meta-description" cols="60" rows="2" tabindex="30" style="width: 99%;"><?php echo esc_textarea( get_post_meta( $object->ID, 'Description', true ) ); ?></textarea>
  67. </p>
  68. <p>
  69. <label for="hybrid-meta-keywords"><?php _e( 'Meta Keywords:', 'hybrid-core' ); ?></label>
  70. <br />
  71. <input type="text" name="hybrid-meta-keywords" id="hybrid-meta-keywords" value="<?php echo esc_attr( get_post_meta( $object->ID, 'Keywords', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" />
  72. </p>
  73. <?php }
  74. /**
  75. * Saves the post SEO meta box settings as post metadata.
  76. *
  77. * @since 1.2.0
  78. * @param int $post_id The ID of the current post being saved.
  79. * @param int $post The post object currently being saved.
  80. */
  81. function hybrid_meta_box_post_save_seo( $post_id, $post ) {
  82. $prefix = hybrid_get_prefix();
  83. /* Verify the nonce before proceeding. */
  84. if ( !isset( $_POST['hybrid-core-post-seo'] ) || !wp_verify_nonce( $_POST['hybrid-core-post-seo'], basename( __FILE__ ) ) )
  85. return $post_id;
  86. $meta = array(
  87. 'Title' => $_POST['hybrid-document-title'],
  88. 'Description' => $_POST['hybrid-meta-description'],
  89. 'Keywords' => $_POST['hybrid-meta-keywords']
  90. );
  91. foreach ( $meta as $meta_key => $new_meta_value ) {
  92. /* Get the meta value of the custom field key. */
  93. $meta_value = get_post_meta( $post_id, $meta_key, true );
  94. /* If there is no new meta value but an old value exists, delete it. */
  95. if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value )
  96. delete_post_meta( $post_id, $meta_key, $meta_value );
  97. /* If a new meta value was added and there was no previous value, add it. */
  98. elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value )
  99. add_post_meta( $post_id, $meta_key, $new_meta_value, true );
  100. /* If the new meta value does not match the old value, update it. */
  101. elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $new_meta_value && $new_meta_value != $meta_value )
  102. update_post_meta( $post_id, $meta_key, $new_meta_value );
  103. }
  104. }
  105. ?>