CatchResponsiveMetaBox

Class to Renders and save metabox options.

Defined (1)

The class is defined in the following location(s).

/inc/catchresponsive-metabox.php  
  1. class CatchResponsiveMetaBox { 
  2. private $meta_box; 
  3.  
  4. private $fields; 
  5.  
  6. /** 
  7. * Constructor 
  8. * @since Catch Responsive 1.4 
  9. * @access public 
  10. */ 
  11. public function __construct( $meta_box_id, $meta_box_title, $post_type ) { 
  12.  
  13. $this->meta_box = array ( 
  14. 'id' => $meta_box_id,  
  15. 'title' => $meta_box_title,  
  16. 'post_type' => $post_type,  
  17. ); 
  18.  
  19. $this->fields = array( 
  20. 'catchresponsive-layout-option',  
  21. 'catchresponsive-header-image',  
  22. 'catchresponsive-featured-image' 
  23. ); 
  24.  
  25.  
  26. // Add metaboxes 
  27. add_action( 'add_meta_boxes', array( $this, 'add' ) ); 
  28.  
  29. add_action( 'save_post', array( $this, 'save' ) ); 
  30.  
  31. /** 
  32. * Add Meta Box for multiple post types. 
  33. * @since Catch Responsive 1.4 
  34. * @access public 
  35. */ 
  36. public function add($postType) { 
  37. if ( in_array( $postType, $this->meta_box['post_type'] ) ) { 
  38. add_meta_box( $this->meta_box['id'], $this->meta_box['title'], array( $this, 'show' ), $postType ); 
  39.  
  40. /** 
  41. * Renders metabox 
  42. * @since Catch Responsive 1.4 
  43. * @access public 
  44. */ 
  45. public function show() { 
  46. global $post; 
  47.  
  48. $layout_options = catchresponsive_metabox_layouts(); 
  49. $featured_image_options = catchresponsive_metabox_featured_image_options(); 
  50. $header_image_options = catchresponsive_metabox_header_featured_image_options(); 
  51.  
  52.  
  53. // Use nonce for verification 
  54. wp_nonce_field( basename( __FILE__ ), 'catchresponsive_custom_meta_box_nonce' ); 
  55.  
  56. // Begin the field table and loop ?> 
  57. <div id="catchresponsive-ui-tabs" class="ui-tabs"> 
  58. <ul class="catchresponsive-ui-tabs-nav" id="catchresponsive-ui-tabs-nav"> 
  59. <li><a href="#frag1"><?php _e( 'Layout Options', 'catch-responsive' ); ?></a></li> 
  60. <li><a href="#frag3"><?php _e( 'Header Featured Image Options', 'catch-responsive' ); ?></a></li> 
  61. <li><a href="#frag4"><?php _e( 'Single Page/Post Image Layout ', 'catch-responsive' ); ?></a></li> 
  62. </ul> 
  63. <div id="frag1" class="catch_ad_tabhead"> 
  64. <table id="layout-options" class="form-table" width="100%"> 
  65. <tbody> 
  66. <tr> 
  67. <select name="catchresponsive-layout-option" id="custom_element_grid_class"> 
  68. <?php 
  69. foreach ( $layout_options as $field ) { 
  70. $metalayout = get_post_meta( $post->ID, 'catchresponsive-layout-option', true ); 
  71. if ( empty( $metalayout ) ) { 
  72. $metalayout='default'; 
  73. ?> 
  74. <option value="<?php echo $field['value']; ?>" <?php selected( $metalayout, $field['value'] ); ?>><?php echo $field['label']; ?></option> 
  75. <?php 
  76. } // end foreach 
  77. ?> 
  78. </select> 
  79. </tr> 
  80. </tbody> 
  81. </table> 
  82. </div> 
  83.  
  84. <div id="frag3" class="catch_ad_tabhead"> 
  85. <table id="header-image-metabox" class="form-table" width="100%"> 
  86. <tbody> 
  87. <tr> 
  88. <?php 
  89. foreach ( $header_image_options as $field ) { 
  90.  
  91. $metaheader = get_post_meta( $post->ID, $field['id'], true ); 
  92.  
  93. if ( empty( $metaheader ) ) { 
  94. $metaheader='default'; 
  95. ?> 
  96.  
  97. <td style="width: 100px;"> 
  98. <label class="description"> 
  99. <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $metaheader ); ?>/>  <?php echo $field['label']; ?> 
  100. </label> 
  101. </td> 
  102.  
  103. <?php 
  104. } // end foreach 
  105. ?> 
  106. </tr> 
  107. </tbody> 
  108. </table> 
  109. </div> 
  110.  
  111. <div id="frag4" class="catch_ad_tabhead"> 
  112. <table id="featured-image-metabox" class="form-table" width="100%"> 
  113. <tbody> 
  114. <tr> 
  115. <?php 
  116. foreach ($featured_image_options as $field) { 
  117.  
  118. $metaimage = get_post_meta( $post->ID, $field['id'], true ); 
  119.  
  120. if (empty( $metaimage ) ) { 
  121. $metaimage='default'; 
  122. ?> 
  123. <td style="width: 100px;"> 
  124. <label class="description"> 
  125. <input type="radio" name="<?php echo $field['id']; ?>" value="<?php echo $field['value']; ?>" <?php checked( $field['value'], $metaimage ); ?>/>  <?php echo $field['label']; ?> 
  126. </label> 
  127. </td> 
  128.  
  129. <?php 
  130. } // end foreach 
  131. ?> 
  132. </tr> 
  133. </tbody> 
  134. </table> 
  135. </div> 
  136. </div> 
  137. <?php 
  138.  
  139. /** 
  140. * Save custom metabox data 
  141. * @action save_post 
  142. * @since Catch Responsive 1.4 
  143. * @access public 
  144. */ 
  145. public function save( $post_id ) { 
  146. global $post_type; 
  147.  
  148. $post_type_object = get_post_type_object( $post_type ); 
  149.  
  150. if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // Check Autosave 
  151. || ( ! isset( $_POST['post_ID'] ) || $post_id != $_POST['post_ID'] ) // Check Revision 
  152. || ( ! in_array( $post_type, $this->meta_box['post_type'] ) ) // Check if current post type is supported. 
  153. || ( ! check_admin_referer( basename( __FILE__ ), 'catchresponsive_custom_meta_box_nonce') ) // Check nonce - Security 
  154. || ( ! current_user_can( $post_type_object->cap->edit_post, $post_id ) ) ) // Check permission 
  155. return $post_id; 
  156.  
  157. foreach ( $this->fields as $field ) { 
  158. $old = get_post_meta( $post_id, $field, true); 
  159.  
  160. $new = $_POST[ $field ]; 
  161.  
  162. delete_post_meta( $post_id, $field ); 
  163.  
  164. if ( '' == $new || array() == $new ) { 
  165. return; 
  166. else { 
  167. if ( ! update_post_meta ($post_id, $field, sanitize_key ( $new ) ) ) { 
  168. add_post_meta($post_id, $field, sanitize_key ( $new ), true ); 
  169. } // end foreach