/includes/admin/meta-boxes.php

  1. <?php 
  2. /** 
  3. * Meta Boxes 
  4. * 
  5. * @package SimpleCalendar/Admin 
  6. */ 
  7. namespace SimpleCalendar\Admin; 
  8.  
  9. use SimpleCalendar\Admin\Metaboxes as Metabox; 
  10.  
  11. if ( ! defined( 'ABSPATH' ) ) { 
  12. exit; 
  13.  
  14. /** 
  15. * Meta boxes class. 
  16. * 
  17. * Handles write panels in post types and post meta. 
  18. * 
  19. * @since 3.0.0 
  20. */ 
  21. class Meta_Boxes { 
  22.  
  23. /** 
  24. * Saved meta boxes status. 
  25. * 
  26. * @access private 
  27. * @var bool 
  28. */ 
  29. private static $saved_meta_boxes = false; 
  30.  
  31. /** 
  32. * Post types to attach calendars. 
  33. * 
  34. * @access private 
  35. * @var array 
  36. */ 
  37. private $post_types = array(); 
  38.  
  39. /** 
  40. * Hook in tabs. 
  41. * 
  42. * @since 3.0.0 
  43. */ 
  44. public function __construct() { 
  45.  
  46. $settings = get_option( 'simple-calendar_settings_calendars' ); 
  47. if ( isset( $settings['general']['attach_calendars_posts'] ) ) { 
  48. $this->post_types = $settings['general']['attach_calendars_posts']; 
  49.  
  50. // Load meta boxes to save settings. 
  51. new Metabox\Settings(); 
  52. new Metabox\Attach_Calendar(); 
  53. new Metabox\Newsletter(); 
  54. do_action( 'simcal_load_meta_boxes' ); 
  55.  
  56. // Add meta boxes. 
  57. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 ); 
  58.  
  59. // Process meta boxes. 
  60. add_action( 'simcal_save_settings_meta', '\SimpleCalendar\Admin\Metaboxes\Settings::save', 10, 2 ); 
  61. add_action( 'simcal_save_attach_calendar_meta', '\SimpleCalendar\Admin\Metaboxes\Attach_Calendar::save', 10, 2 ); 
  62.  
  63. // Save meta boxes data. 
  64. add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 ); 
  65.  
  66. // Uncomment this for debugging $_POST while saving a meta box. 
  67. // add_action( 'save_post', function() { echo '<pre>'; print_r( $_POST ); echo '</pre>'; die(); } ); 
  68.  
  69. /** 
  70. * Add meta boxes. 
  71. * 
  72. * @since 3.0.0 
  73. */ 
  74. public function add_meta_boxes() { 
  75.  
  76. add_meta_box( 
  77. 'simcal-calendar-settings',  
  78. __( 'Calendar Settings', 'google-calendar-events' ),  
  79. '\SimpleCalendar\Admin\Metaboxes\Settings::html',  
  80. 'calendar',  
  81. 'normal',  
  82. 'core' 
  83. ); 
  84.  
  85. $addons = apply_filters( 'simcal_installed_addons', array() ); 
  86. if ( empty( $addons ) ) { 
  87.  
  88. add_meta_box( 
  89. 'simcal-newsletter',  
  90. __( 'Get 20% off all Pro Add-ons', 'google-calendar-events' ),  
  91. '\SimpleCalendar\Admin\Metaboxes\Newsletter::html',  
  92. 'calendar',  
  93. 'side',  
  94. 'default' 
  95. ); 
  96.  
  97.  
  98. add_meta_box( 
  99. 'simcal-get-shortcode',  
  100. __( 'Calendar Shortcode', 'google-calendar-events' ),  
  101. '\SimpleCalendar\Admin\Metaboxes\Get_Shortcode::html',  
  102. 'calendar',  
  103. 'side',  
  104. 'default' 
  105. ); 
  106.  
  107. // Add meta box if there are calendars. 
  108. if ( ( true == simcal_get_calendars() ) && ! empty( $this->post_types ) ) { 
  109. foreach ( $this->post_types as $post_type ) { 
  110. add_meta_box( 
  111. 'simcal-attach-calendar',  
  112. __( 'Attach Calendar', 'google-calendar-events' ),  
  113. '\SimpleCalendar\Admin\Metaboxes\Attach_Calendar::html',  
  114. $post_type,  
  115. 'side',  
  116. 'low' 
  117. ); 
  118.  
  119. do_action( 'simcal_add_meta_boxes' ); 
  120.  
  121. /** 
  122. * Check if we're saving, then trigger action. 
  123. * 
  124. * @since 3.0.0 
  125. * 
  126. * @param int $post_id 
  127. * @param object $post 
  128. * 
  129. * @return void 
  130. */ 
  131. public function save_meta_boxes( $post_id, $post ) { 
  132.  
  133. // $post_id and $post are required. 
  134. if ( empty( $post_id ) || empty( $post ) || self::$saved_meta_boxes ) { 
  135. return; 
  136.  
  137. // Don't save meta boxes for revisions or autosaves. 
  138. if ( defined( 'DOING_AUTOSAVE' ) || is_int( wp_is_post_revision( $post ) ) || is_int( wp_is_post_autosave( $post ) ) ) { 
  139. return; 
  140.  
  141. // Check the nonce. 
  142. if ( empty( $_POST['simcal_meta_nonce'] ) || ! wp_verify_nonce( $_POST['simcal_meta_nonce'], 'simcal_save_data' ) ) { 
  143. return; 
  144.  
  145. // Check the post being saved == the $post_id to prevent triggering this call for other save_post events. 
  146. if ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id ) { 
  147. return; 
  148.  
  149. // Check user has permission to edit 
  150. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  151. return; 
  152.  
  153. // We need this save event to run once to avoid potential endless loops. 
  154. // This would have been perfect: 
  155. // `remove_action( current_filter(), __METHOD__ );` 
  156. // But cannot be used due to a WordPress bug: 
  157. // @link https://core.trac.wordpress.org/ticket/17817 
  158. // @see also https://github.com/woothemes/woocommerce/issues/6485 
  159. self::$saved_meta_boxes = true; 
  160.  
  161. // Check the post type. 
  162. if ( 'calendar' == $post->post_type ) { 
  163. do_action( 'simcal_save_settings_meta', $post_id, $post ); 
  164. } elseif ( in_array( $post->post_type, $this->post_types ) ) { 
  165. do_action( 'simcal_save_attach_calendar_meta', $post_id, $post ); 
  166.  
  167. do_action( 'simcal_save_meta_boxes', $post_id, $post ); 
  168.  
.