SimpleCalendarAdminMeta_Boxes

Meta boxes class.

Defined (1)

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

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