SimpleCalendarAdminPost_Types

Admin post types.

Defined (1)

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

/includes/admin/post-types.php  
  1. class Post_Types { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. * @since 3.0.0 
  6. */ 
  7. public function __construct() { 
  8.  
  9. // Add meta boxes to custom content. 
  10. new Meta_Boxes(); 
  11.  
  12. // Add column headers in calendar feeds admin archives. 
  13. add_filter( 'manage_calendar_posts_columns', array( $this, 'add_calendar_feed_column_headers' ) ); 
  14. // Process column contents for calendar feeds. 
  15. add_action( 'manage_calendar_posts_custom_column', array( $this, 'calendar_feed_column_content' ), 10, 2 ); 
  16.  
  17. // Add actions in calendar feed rows. 
  18. add_filter( 'post_row_actions', array( $this, 'row_actions' ), 10, 2 ); 
  19. // Add bulk actions. 
  20. add_action( 'admin_init', array( $this, 'bulk_actions' ) ); 
  21. // Add content to edit calendars page. 
  22. add_action( 'load-edit.php', array( $this, 'edit_table_hooks' ) ); 
  23.  
  24. // Default calendar post type content (default event template). 
  25. add_filter( 'default_content', array( $this, 'default_event_template' ), 10, 2 ); 
  26.  
  27. // Add a clear cache link in submit post box. 
  28. add_action( 'post_submitbox_misc_actions', array( $this, 'clear_cache_button' ) ); 
  29.  
  30. // Add media button to post editor for adding a shortcode. 
  31. add_action( 'media_buttons', array( $this, 'add_shortcode_button' ), 100 ); 
  32. add_action( 'edit_form_after_editor', array( $this, 'add_shortcode_panel' ), 100 ); 
  33.  
  34. /** 
  35. * Add column headers to Calendar feeds custom post type page. 
  36. * @since 3.0.0 
  37. * @param array $columns Default columns. 
  38. * @return array Filtered output. 
  39. */ 
  40. public function add_calendar_feed_column_headers( $columns ) { 
  41.  
  42. // New columns. 
  43. $feed_info = array( 'feed' => __( 'Events Source', 'google-calendar-events' ) ); 
  44. $calendar_info = array( 'calendar' => __( 'Calendar Type', 'google-calendar-events' ) ); 
  45. $shortcode = array( 'shortcode' => __( 'Shortcode', 'google-calendar-events' ) ); 
  46.  
  47. // Merge with existing columns and rearrange. 
  48. $columns = array_slice( $columns, 0, 2, true ) + $feed_info + $calendar_info + $shortcode + array_slice( $columns, 2, null, true ); 
  49.  
  50. return $columns; 
  51.  
  52. /** 
  53. * Fill out the Calendar feed post type columns. 
  54. * @since 3.0.0 
  55. * @param string $column_name Column identifier. 
  56. * @param int $post_id The calendar feed post id. 
  57. * @return void 
  58. */ 
  59. public function calendar_feed_column_content( $column_name, $post_id ) { 
  60.  
  61. switch ( $column_name ) { 
  62.  
  63. case 'feed': 
  64.  
  65. $feed = simcal_get_feed( $post_id ); 
  66. echo isset( $feed->name ) ? $feed->name : '—'; 
  67. break; 
  68.  
  69. case 'calendar': 
  70.  
  71. $info = '—'; 
  72.  
  73. if ( $terms = wp_get_object_terms( $post_id, 'calendar_type' ) ) { 
  74.  
  75. $calendar_type = sanitize_title( current( $terms )->name ); 
  76. $calendar = simcal_get_calendar( $calendar_type ); 
  77.  
  78. if ( $calendar instanceof Calendar ) { 
  79. $info = $calendar->name; 
  80. $views = get_post_meta( $post_id, '_calendar_view', true );; 
  81. $view = isset( $views[ $calendar->type ] ) ? $views[ $calendar->type ] : ''; 
  82.  
  83. if ( isset( $calendar->views[ $view ] ) ) { 
  84. $info .= ' → ' . $calendar->views[ $view ]; 
  85.  
  86. echo $info; 
  87. break; 
  88.  
  89. case 'shortcode' : 
  90.  
  91. simcal_print_shortcode_tip( $post_id ); 
  92. break; 
  93.  
  94. /** 
  95. * Add actions to Calendar feed post type row view. 
  96. * @since 3.0.0 
  97. * @param array $actions Default actions 
  98. * @param \WP_Post $post Post object. 
  99. * @return array Filtered output. 
  100. */ 
  101. public function row_actions( $actions, $post ) { 
  102.  
  103. // Add a clear feed cache action link. 
  104. if ( $post->post_type == 'calendar' ) { 
  105. $actions['duplicate_feed'] = '<a href="' . esc_url( add_query_arg( array( 'duplicate_feed' => $post->ID ) ) ) . '">' . __( 'Clone', 'google-calendar-events' ) . '</a>'; 
  106. $actions['clear_cache'] = '<a href="' . esc_url( add_query_arg( array( 'clear_cache' => $post->ID ) ) ) . '">' . __( 'Clear Cache', 'google-calendar-events' ) . '</a>'; 
  107.  
  108. return $actions; 
  109.  
  110. /** 
  111. * Bulk actions. 
  112. * @since 3.0.0 
  113. */ 
  114. public function bulk_actions() { 
  115.  
  116. // Clear an individual feed cache. 
  117. // @todo Convert the clear cache request to ajax. 
  118. if ( isset( $_REQUEST['clear_cache'] ) ) { 
  119.  
  120. $id = intval( $_REQUEST['clear_cache'] ); 
  121.  
  122. if ( $id > 0 ) { 
  123. simcal_delete_feed_transients( $id ); 
  124.  
  125. wp_redirect( remove_query_arg( 'clear_cache' ) ); 
  126.  
  127. // Duplicate a feed post type. 
  128. if ( isset( $_REQUEST['duplicate_feed'] ) ) { 
  129.  
  130. $id = intval( $_REQUEST['duplicate_feed'] ); 
  131.  
  132. if ( $id > 0 ) { 
  133. $this->duplicate_feed( $id ); 
  134.  
  135. wp_redirect( remove_query_arg( 'duplicate_feed' ) ); 
  136.  
  137. $bulk_actions = new Bulk_Actions( 'calendar' ); 
  138.  
  139. $bulk_actions->register_bulk_action( array( 
  140. 'menu_text' => __( 'Clear cache', 'google-calendar-events' ),  
  141. 'action_name' => 'clear_calendars_cache',  
  142. 'callback' => function( $post_ids ) { 
  143. simcal_delete_feed_transients( $post_ids ); 
  144. },  
  145. 'admin_notice' => __( 'Cache cleared.', 'google-calendar-events' ),  
  146. ); 
  147.  
  148. $bulk_actions->init(); 
  149.  
  150. /** 
  151. * Edit calendars table hooks. 
  152. * @since 3.0.0 
  153. * @internal 
  154. */ 
  155. public function edit_table_hooks() { 
  156.  
  157. $screen = simcal_is_admin_screen(); 
  158.  
  159. if ( 'edit-calendar' == $screen ) { 
  160. add_action( 'in_admin_footer', function() { 
  161.  
  162. } ); 
  163.  
  164. /** 
  165. * Clone a feed post type. 
  166. * @since 3.0.0 
  167. * @param int $post_id 
  168. */ 
  169. private function duplicate_feed( $post_id ) { 
  170.  
  171. if ( $duplicate = get_post( intval( $post_id ), 'ARRAY_A' ) ) { 
  172.  
  173. if ( 'calendar' == $duplicate['post_type'] ) { 
  174.  
  175. $duplicate['post_title'] = $duplicate['post_title'] . ' (' . __( 'Copy', 'google-calendar-events' ) . ')'; 
  176.  
  177. unset( $duplicate['ID'] ); 
  178. unset( $duplicate['guid'] ); 
  179. unset( $duplicate['comment_count'] ); 
  180.  
  181. $duplicate_id = wp_insert_post( $duplicate ); 
  182.  
  183. $taxonomies = get_object_taxonomies( $duplicate['post_type'] ); 
  184. foreach ( $taxonomies as $taxonomy ) { 
  185. $terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) ); 
  186. wp_set_object_terms( $duplicate_id, $terms, $taxonomy ); 
  187.  
  188. $custom_fields = get_post_custom( $post_id ); 
  189. foreach ( $custom_fields as $key => $value ) { 
  190. add_post_meta( $duplicate_id, $key, maybe_unserialize( $value[0] ) ); 
  191.  
  192.  
  193. /** 
  194. * Default event template builder content. 
  195. * @since 3.0.0 
  196. * @param string $content 
  197. * @param \WP_Post $post 
  198. * @return string 
  199. */ 
  200. public function default_event_template( $content, $post ) { 
  201. return 'calendar' == $post->post_type ? simcal_default_event_template() : $content; 
  202.  
  203. /** 
  204. * Clear cache button. 
  205. * @since 3.0.0 
  206. */ 
  207. public function clear_cache_button() { 
  208.  
  209. global $post, $post_type; 
  210.  
  211. if ( $post_type == 'calendar' && isset( $post->ID ) ) { 
  212. echo '<a id="simcal-clear-cache" class="button" data-id="' . $post->ID . ' ">' . 
  213. '<i class="simcal-icon-spinner simcal-icon-spin" style="display: none;"></i> ' . 
  214. __( 'Clear cache', 'google-calendar-events' ) . 
  215. '</a>'; 
  216.  
  217. /** 
  218. * Add a shortcode button. 
  219. * Adds a button to add a calendar shortcode in WordPress content editor. 
  220. * Uses Thickbox. http://codex.wordpress.org/ThickBox 
  221. * @since 3.0.0 
  222. */ 
  223. public function add_shortcode_button() { 
  224.  
  225. $post_types = array(); 
  226.  
  227. $settings = get_option( 'simple-calendar_settings_calendars' ); 
  228. if ( isset( $settings['general']['attach_calendars_posts'] ) ) { 
  229. $post_types = $settings['general']['attach_calendars_posts']; 
  230.  
  231. global $post_type; 
  232.  
  233. if ( in_array( $post_type, $post_types ) ) { 
  234.  
  235. // Thickbox will ignore height and width, will adjust these in js. 
  236. // @see https://core.trac.wordpress.org/ticket/17249 
  237. ?> 
  238. <a href="#TB_inline?height=250&width=500&inlineId=simcal-insert-shortcode-panel" id="simcal-insert-shortcode-button" class="thickbox button insert-calendar add_calendar"> 
  239. <span class="wp-media-buttons-icon dashicons-before dashicons-calendar-alt"></span> <?php _e( 'Add Calendar', 'google-calendar-events' ); ?> 
  240. </a> 
  241. <?php 
  242.  
  243.  
  244.  
  245. /** 
  246. * Panel for the add shortcode media button. 
  247. * Prints the panel for choosing a calendar to insert as a shortcode in a page or post. 
  248. * @since 3.0.0 
  249. */ 
  250. public function add_shortcode_panel() { 
  251.  
  252. $calendars = simcal_get_calendars(); 
  253.  
  254. ?> 
  255. <div id="simcal-insert-shortcode-panel" style="display:none;"> 
  256. <div class="simcal-insert-shortcode-panel"> 
  257. <h1><?php _e( 'Add Calendar', 'google-calendar-events' ); ?></h1> 
  258. <?php _e( 'Add a calendar to your post.', 'google-calendar-events' ); ?> 
  259. <?php if ( ! empty( $calendars ) && is_array( $calendars ) ) : ?> 
  260. <p> 
  261. <label for="simcal-choose-calendar"> 
  262. <?php $multiselect = count( $calendars ) > 15 ? ' simcal-field-select-enhanced' : ''; ?> 
  263. <select id="simcal-choose-calendar" 
  264. class="simcal-field simcal-field-select<?php echo $multiselect; ?>" 
  265. name=""> 
  266. <?php foreach ( $calendars as $id => $title ) : ?> 
  267. <option value="<?php echo $id ?>"><?php echo $title ?></option> 
  268. <?php endforeach; ?> 
  269. </select> 
  270. </label> 
  271. </p> 
  272. <p><input type="button" value="<?php _e( 'Insert Calendar', 'google-calendar-events' ); ?>" id="simcal-insert-shortcode" class="button button-primary button-large" name="" /></p> 
  273. <?php else : ?> 
  274. <p><em><?php _e( 'Could not find any calendars to add to this post.', 'google-calendar-events' ); ?></em></p> 
  275. <strong><a href="post-new.php?post_type=calendar"><?php _e( 'Please add and configure new calendar first.', 'google-calendar-events' ); ?></a></strong> 
  276. <?php endif; ?> 
  277. </div> 
  278. </div> 
  279. <?php 
  280.  
  281.