Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. <?php
  2. /**
  3. * This file adds a new meta box to the Edit Post and Edit Page screens that contain
  4. * additional post- and page-specific options for use with the theme
  5. *
  6. * @package Graphene
  7. * @since Graphene 1.1
  8. */
  9.  
  10. /**
  11. * Add the custom meta box
  12. */
  13. function graphene_add_meta_box(){
  14. add_meta_box( 'graphene_custom_meta', __( 'Graphene post-specific options','graphene' ), 'graphene_custom_meta', 'post', 'normal', 'high' );
  15. add_meta_box( 'graphene_custom_meta', __( 'Graphene page-specific options','graphene' ), 'graphene_custom_meta', 'page', 'normal', 'high' );}
  16. add_action( 'add_meta_boxes', 'graphene_add_meta_box' );
  17.  
  18. /**
  19. * Add or update the options
  20. */
  21. function graphene_save_custom_meta( $post_id ){
  22. /* Verify this came from our screen and with proper authorization */
  23. if ( isset( $_POST['graphene_save_custom_meta'] ) ) {
  24. if ( ! wp_verify_nonce( $_POST['graphene_save_custom_meta'], 'graphene_save_custom_meta' ) ) return $post_id;
  25. } else {
  26. return $post_id;}
  27.  
  28. /* Don't do anything if it's an autosave */
  29. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id;
  30.  
  31. /* Check permissions */
  32. if ( 'page' == $_POST['post_type']) {
  33. if ( ! current_user_can( 'edit_page', $post_id ) ) return $post_id;
  34. } else {
  35. if ( ! current_user_can( 'edit_post', $post_id ) ) return $post_id;}
  36.  
  37. /* OK, we're authenticated: process the data */
  38. $graphene_meta = array();
  39.  
  40. if ( in_array( $_POST['graphene_slider_img'], array( 'disabled', 'featured_image', 'post_image', 'custom_url' ) ) )
  41. $graphene_meta['slider_img'] = $_POST['graphene_slider_img'];
  42. if ( in_array( $_POST['graphene_show_addthis'], array( 'show', 'hide' ) ) )
  43. $graphene_meta['show_addthis'] = $_POST['graphene_show_addthis'];
  44. if ( $_POST['graphene_slider_imgurl'] )
  45. $graphene_meta['slider_imgurl'] = esc_url_raw( $_POST['graphene_slider_imgurl'] );
  46. if ( $_POST['graphene_slider_url'] )
  47. $graphene_meta['slider_url'] = esc_url_raw( $_POST['graphene_slider_url'] );
  48.  
  49. /* Post-specific options */
  50. if ( 'post' == $_POST['post_type'] ) {
  51. if ( in_array( $_POST['graphene_post_date_display'], array( 'hidden', 'icon_no_year', 'icon_plus_year', 'text' ) ) )
  52. $graphene_meta['post_date_display'] = $_POST['graphene_post_date_display'];
  53. }
  54.  
  55. /* Page-specific options */
  56. if ( 'page' == $_POST['post_type']) {
  57. if ( $_POST['graphene_nav_description'] )
  58. $graphene_meta['nav_description'] = wp_kses_post( $_POST['graphene_nav_description'] );
  59. }
  60.  
  61. $graphene_meta_defaults = graphene_custom_fields_defaults();
  62. foreach ( $graphene_meta as $key => $value ){
  63. if ( $value == $graphene_meta_defaults[$key] ) unset( $graphene_meta[$key] );
  64. }
  65.  
  66. if ( $graphene_meta ) update_post_meta( $post_id, '_graphene_meta', $graphene_meta );
  67. else delete_post_meta( $post_id, '_graphene_meta' );
  68. }
  69. add_action( 'save_post', 'graphene_save_custom_meta' );
  70.  
  71. /**
  72. * Display the custom meta box content
  73. */
  74. function graphene_custom_meta( $post ){
  75.  
  76. // Use nonce for verification
  77. wp_nonce_field( 'graphene_save_custom_meta', 'graphene_save_custom_meta' );
  78.  
  79. /* Get the current settings */
  80. $graphene_meta = get_post_meta( $post->ID, '_graphene_meta', true );
  81. if ( ! $graphene_meta ) $graphene_meta = array();
  82. $graphene_meta = array_merge( graphene_custom_fields_defaults(), $graphene_meta );
  83. ?>
  84.  
  85. <p><?php _e( "These settings will only be applied to this particular post or page you're editing. They will override the global settings set in the Graphene Options or Graphene Display options page.", 'graphene' ); ?></p>
  86. <h4><?php _e( 'Slider options', 'graphene' ); ?></h4>
  87. <table class="form-table">
  88. <tr>
  89. <th scope="row">
  90. <label for="graphene_slider_img"><?php _e( 'Slider image', 'graphene' ); ?></label>
  91. </th>
  92. <td>
  93. <select id="graphene_slider_img" name="graphene_slider_img">
  94. <option value="" <?php selected( $graphene_meta['slider_img'], '' ); ?>><?php _e( 'Use global setting', 'graphene' ); ?></option>
  95. <option value="disabled" <?php selected( $graphene_meta['slider_img'], 'disabled' ); ?>><?php _e("Don't show image", 'graphene' ); ?></option>
  96. <option value="featured_image" <?php selected( $graphene_meta['slider_img'], 'featured_image' ); ?>><?php _e('Featured Image', 'graphene' ); ?></option>
  97. <option value="post_image" <?php selected( $graphene_meta['slider_img'], 'post_image' ); ?>><?php _e('First image in post', 'graphene' ); ?></option>
  98. <option value="custom_url" <?php selected( $graphene_meta['slider_img'], 'custom_url' ); ?>><?php _e('Custom URL', 'graphene' ); ?></option>
  99. </select>
  100. </td>
  101. </tr>
  102. <tr>
  103. <th scope="row">
  104. <label for="graphene_slider_imgurl"><?php _e( 'Custom slider image URL', 'graphene' ); ?></label>
  105. </th>
  106. <td>
  107. <input type="text" id="graphene_slider_imgurl" name="graphene_slider_imgurl" class="widefat code" value="<?php echo $graphene_meta['slider_imgurl']; ?>" size="60" />
  108. <span class="description"><?php _e( 'Make sure you select Custom URL in the slider image option above to use this custom url.', 'graphene' ); ?></span>
  109. </td>
  110. </tr>
  111. <tr>
  112. <th scope="row">
  113. <label for="graphene_slider_url"><?php _e( 'Custom slider URL', 'graphene' ); ?></label>
  114. </th>
  115. <td>
  116. <input type="text" id="graphene_slider_url" name="graphene_slider_url" class="widefat code" value="<?php echo $graphene_meta['slider_url']; ?>" size="60" />
  117. <span class="description"><?php _e( 'Use this to override the link that is used in the slider.', 'graphene' ); ?></span>
  118. </td>
  119. </tr>
  120. </table>
  121. <h4><?php _e( 'Display options', 'graphene' ); ?></h4>
  122. <table class="form-table">
  123. <tr>
  124. <th scope="row">
  125. <label for="graphene_show_addthis"><?php _e( 'AddThis Social Sharing button', 'graphene' ); ?></label>
  126. </th>
  127. <td>
  128. <select id="graphene_show_addthis" name="graphene_show_addthis">
  129. <option value="" <?php selected( $graphene_meta['show_addthis'], '' ); ?>><?php _e( 'Use global setting', 'graphene' ); ?></option>
  130. <option value="show" <?php selected( $graphene_meta['show_addthis'], 'show' ); ?>><?php _e( 'Show button', 'graphene' ); ?></option>
  131. <option value="hide" <?php selected( $graphene_meta['show_addthis'], 'hide' ); ?>><?php _e( 'Hide button', 'graphene' ); ?></option>
  132. </select>
  133. </td>
  134. </tr>
  135.  
  136. <?php if ( 'post' == $post->post_type) : ?>
  137. <tr>
  138. <th scope="row">
  139. <label for="graphene_post_date_display"><?php _e( 'Post date display', 'graphene' ); ?></label>
  140. </th>
  141. <td>
  142. <select id="graphene_post_date_display" name="graphene_post_date_display">
  143. <option value="" <?php selected( $graphene_meta['post_date_display'], '' ); ?>><?php _e( 'Use global setting', 'graphene' ); ?></option>
  144. <option value="hidden" <?php selected( $graphene_meta['post_date_display'], 'hidden' ); ?>><?php _e( 'Hidden', 'graphene' ); ?></option>
  145. <option value="icon_no_year" <?php selected( $graphene_meta['post_date_display'], 'icon_no_year' ); ?>><?php _e( 'As an icon (without the year)', 'graphene' ); ?></option>
  146. <option value="icon_plus_year" <?php selected( $graphene_meta['post_date_display'], 'icon_plus_year' ); ?>><?php _e( 'As an icon (including the year)', 'graphene' ); ?></option>
  147. <option value="text" <?php selected( $graphene_meta['post_date_display'], 'text' ); ?>><?php _e( 'As inline text', 'graphene' ); ?></option>
  148. </select>
  149. </td>
  150. </tr>
  151. <?php endif; ?>
  152.  
  153. </table>
  154. <?php if ( 'page' == $post->post_type): ?>
  155. <h4><?php _e( 'Navigation options', 'graphene' ); ?></h4>
  156. <table class="form-table">
  157. <tr>
  158. <th scope="row">
  159. <label for="graphene_nav_description"><?php _e( 'Description', 'graphene' ); ?></label>
  160. </th>
  161. <td>
  162. <input type="text" id="graphene_nav_description" name="graphene_nav_description" value="<?php echo $graphene_meta['nav_description']; ?>" size="60" />
  163. <span class="description"><?php _e( 'Only required if you need a description in the navigation menu and you are not using a custom menu.', 'graphene' ); ?></span>
  164. </td>
  165. </tr>
  166. </table>
  167. <?php endif; ?>
  168. <?php
  169. }
  170.  
  171. /**
  172. * Set the default values for the custom fields
  173. *
  174. * @return array Array containing default key-value pairs of the theme's custom fields
  175. *
  176. * @package Graphene
  177. * @since Graphene 1.8
  178. */
  179. function graphene_custom_fields_defaults(){
  180. $defaults = array(
  181. 'slider_img' => '',
  182. 'show_addthis' => '',
  183. 'slider_imgurl' => '',
  184. 'slider_url' => '',
  185. 'post_date_display' => '',
  186. 'nav_description' => '',
  187. );
  188. return $defaults;
  189. }
  190. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement