/includes/post-types.php

  1. <?php 
  2. /** 
  3. * Custom Post Types and Taxonomies 
  4. * 
  5. * @package SimpleCalendar 
  6. */ 
  7. namespace SimpleCalendar; 
  8.  
  9. if ( ! defined( 'ABSPATH' ) ) { 
  10. exit; 
  11.  
  12. /** 
  13. * Custom Post Types and Taxonomies. 
  14. * 
  15. * Register and initialize custom post types and custom taxonomies. 
  16. * 
  17. * @since 3.0.0 
  18. */ 
  19. class Post_Types { 
  20.  
  21. /** 
  22. * Hook in WordPress init to register custom content. 
  23. * 
  24. * @since 3.0.0 
  25. */ 
  26. public function __construct() { 
  27. // Register custom taxonomies. 
  28. add_action( 'init', array( __CLASS__, 'register_taxonomies' ), 5 ); 
  29. // Register custom post types. 
  30. add_action( 'init', array( __CLASS__, 'register_post_types' ), 5 ); 
  31. // Filter the calendar feed post content to display a calendar view. 
  32. add_filter( 'the_content', array( $this, 'filter_post_content' ), 100 ); 
  33. // Delete calendar transients and notices upon post deletion. 
  34. add_action( 'before_delete_post', array( $this, 'upon_deletion' ), 10, 1 ); 
  35.  
  36. /** 
  37. * Register custom taxonomies. 
  38. * 
  39. * @since 3.0.0 
  40. */ 
  41. public static function register_taxonomies() { 
  42.  
  43. do_action( 'simcal_register_taxonomies' ); 
  44.  
  45. if ( ! taxonomy_exists( 'calendar_feed' ) ) { 
  46.  
  47. // Feed Type. 
  48. $labels = array( 
  49. 'name' => __( 'Events Source Types', 'google-calendar-events' ),  
  50. 'singular_name' => __( 'Events Source Type', 'google-calendar-events' ),  
  51. 'menu_name' => __( 'Events Source Type', 'google-calendar-events' ),  
  52. 'all_items' => __( 'All Events Source Types', 'google-calendar-events' ),  
  53. 'parent_item' => __( 'Parent Events Source Type', 'google-calendar-events' ),  
  54. 'parent_item_colon' => __( 'Parent Events Source Type:', 'google-calendar-events' ),  
  55. 'new_item_name' => __( 'New Events Source Type', 'google-calendar-events' ),  
  56. 'add_new_item' => __( 'Add New Events Source Type', 'google-calendar-events' ),  
  57. 'edit_item' => __( 'Edit Events Source Type', 'google-calendar-events' ),  
  58. 'update_item' => __( 'Update Events Source Type', 'google-calendar-events' ),  
  59. 'view_item' => __( 'View Events Source Type', 'google-calendar-events' ),  
  60. 'separate_items_with_commas' => __( 'Separate events source types with commas', 'google-calendar-events' ),  
  61. 'add_or_remove_items' => __( 'Add or remove events source types', 'google-calendar-events' ),  
  62. 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ),  
  63. 'popular_items' => __( 'Popular events source types', 'google-calendar-events' ),  
  64. 'search_items' => __( 'Search Events Source Types', 'google-calendar-events' ),  
  65. 'not_found' => __( 'Not Found', 'google-calendar-events' ),  
  66. ); 
  67.  
  68. $args = array( 
  69. 'hierarchical' => true,  
  70. 'labels' => $labels,  
  71. 'public' => false,  
  72. 'show_admin_column' => false,  
  73. 'show_in_nav_menus' => false,  
  74. 'show_tagcloud' => false,  
  75. 'show_ui' => false,  
  76. ); 
  77. register_taxonomy( 'calendar_feed', array( 'calendar' ), $args ); 
  78.  
  79.  
  80. if ( ! taxonomy_exists( 'calendar_type' ) ) { 
  81.  
  82. // Calendar Type. 
  83. $labels = array( 
  84. 'name' => __( 'Calendar Types', 'google-calendar-events' ),  
  85. 'singular_name' => __( 'Calendar Type', 'google-calendar-events' ),  
  86. 'menu_name' => __( 'Calendar Type', 'google-calendar-events' ),  
  87. 'all_items' => __( 'All Calendar Types', 'google-calendar-events' ),  
  88. 'parent_item' => __( 'Parent Calendar Type', 'google-calendar-events' ),  
  89. 'parent_item_colon' => __( 'Parent Calendar Type:', 'google-calendar-events' ),  
  90. 'new_item_name' => __( 'New Calendar Type', 'google-calendar-events' ),  
  91. 'add_new_item' => __( 'Add New Calendar Type', 'google-calendar-events' ),  
  92. 'edit_item' => __( 'Edit Calendar Type', 'google-calendar-events' ),  
  93. 'update_item' => __( 'Update Calendar Type', 'google-calendar-events' ),  
  94. 'view_item' => __( 'View Calendar Type', 'google-calendar-events' ),  
  95. 'separate_items_with_commas' => __( 'Separate calendar types with commas', 'google-calendar-events' ),  
  96. 'add_or_remove_items' => __( 'Add or remove calendar types', 'google-calendar-events' ),  
  97. 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ),  
  98. 'popular_items' => __( 'Popular calendar types', 'google-calendar-events' ),  
  99. 'search_items' => __( 'Search Calendar Types', 'google-calendar-events' ),  
  100. 'not_found' => __( 'Not Found', 'google-calendar-events' ),  
  101. ); 
  102.  
  103. $args = array( 
  104. 'hierarchical' => true,  
  105. 'labels' => $labels,  
  106. 'public' => false,  
  107. 'show_admin_column' => false,  
  108. 'show_in_nav_menus' => false,  
  109. 'show_tagcloud' => false,  
  110. 'show_ui' => false,  
  111. ); 
  112. register_taxonomy( 'calendar_type', array( 'calendar' ), $args ); 
  113.  
  114.  
  115. if ( ! taxonomy_exists( 'calendar_category' ) ) { 
  116.  
  117. // Feed Category. 
  118. $labels = array( 
  119. 'name' => __( 'Categories', 'google-calendar-events' ),  
  120. 'singular_name' => __( 'Category', 'google-calendar-events' ),  
  121. 'menu_name' => __( 'Categories', 'google-calendar-events' ),  
  122. 'all_items' => __( 'All Categories', 'google-calendar-events' ),  
  123. 'parent_item' => __( 'Parent Category', 'google-calendar-events' ),  
  124. 'parent_item_colon' => __( 'Parent Category:', 'google-calendar-events' ),  
  125. 'new_item_name' => __( 'New Category', 'google-calendar-events' ),  
  126. 'add_new_item' => __( 'Add New Category', 'google-calendar-events' ),  
  127. 'edit_item' => __( 'Edit Category', 'google-calendar-events' ),  
  128. 'update_item' => __( 'Update Category', 'google-calendar-events' ),  
  129. 'view_item' => __( 'View Category', 'google-calendar-events' ),  
  130. 'separate_items_with_commas' => __( 'Separate categories with commas', 'google-calendar-events' ),  
  131. 'add_or_remove_items' => __( 'Add or remove categories', 'google-calendar-events' ),  
  132. 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ),  
  133. 'popular_items' => __( 'Popular Categories', 'google-calendar-events' ),  
  134. 'search_items' => __( 'Search Categories', 'google-calendar-events' ),  
  135. 'not_found' => __( 'Not Found', 'google-calendar-events' ),  
  136. ); 
  137.  
  138. $args = array( 
  139. 'hierarchical' => true,  
  140. 'labels' => $labels,  
  141. 'public' => true,  
  142. 'show_admin_column' => true,  
  143. 'show_in_nav_menus' => true,  
  144. 'show_tagcloud' => false,  
  145. 'show_ui' => true,  
  146. ); 
  147.  
  148. register_taxonomy( 'calendar_category', array( 'calendar' ), $args ); 
  149.  
  150.  
  151. /** 
  152. * Register custom post types. 
  153. * 
  154. * @since 3.0.0 
  155. */ 
  156. public static function register_post_types() { 
  157.  
  158. do_action( 'simcal_register_post_types' ); 
  159.  
  160. if ( ! post_type_exists( 'calendar' ) ) { 
  161.  
  162. // Calendar feed post type. 
  163. $labels = array( 
  164. 'name' => _x( 'Calendars', 'Post Type General Name', 'google-calendar-events' ),  
  165. 'singular_name' => _x( 'Calendar', 'Post Type Singular Name', 'google-calendar-events' ),  
  166. 'menu_name' => __( 'Calendars', 'google-calendar-events' ),  
  167. 'name_admin_bar' => __( 'Calendar', 'google-calendar-events' ),  
  168. 'parent_item_colon' => __( 'Parent Calendar:', 'google-calendar-events' ),  
  169. 'all_items' => __( 'All Calendars', 'google-calendar-events' ),  
  170. 'add_new_item' => __( 'Add New Calendar', 'google-calendar-events' ),  
  171. 'add_new' => __( 'Add New', 'google-calendar-events' ),  
  172. 'new_item' => __( 'New Calendar', 'google-calendar-events' ),  
  173. 'edit_item' => __( 'Edit Calendar', 'google-calendar-events' ),  
  174. 'update_item' => __( 'Update Calendar', 'google-calendar-events' ),  
  175. 'view_item' => __( 'View Calendar', 'google-calendar-events' ),  
  176. 'search_items' => __( 'Search Calendar', 'google-calendar-events' ),  
  177. 'not_found' => __( 'Calendars not found', 'google-calendar-events' ),  
  178. 'not_found_in_trash' => __( 'Calendars not found in Trash', 'google-calendar-events' ),  
  179. ); 
  180.  
  181. $rewrite_rules = array( 
  182. 'feeds' => false,  
  183. 'pages' => false,  
  184. 'with_front' => false,  
  185. 'slug' => 'calendar',  
  186. ); 
  187.  
  188. $svg_icon = 'data:image/svg+xml;base64, PD94<BASE64_ENCODED>'; 
  189.  
  190. $args = array( 
  191. 'capability_type' => 'post',  
  192. 'exclude_from_search' => false,  
  193. 'has_archive' => false,  
  194. 'hierarchical' => false,  
  195. 'label' => __( 'Calendar', 'google-calendar-events' ),  
  196. 'labels' => $labels,  
  197. 'query_var' => true,  
  198. 'public' => true,  
  199. 'publicly_queryable' => true,  
  200. 'menu_icon' => $svg_icon,  
  201. 'menu_position' => 26.8,  
  202. 'rewrite' => $rewrite_rules,  
  203. 'show_in_admin_bar' => true,  
  204. 'show_in_menu' => true,  
  205. 'show_in_nav_menus' => true,  
  206. 'show_ui' => true,  
  207. 'supports' => array( 'title', 'editor' ),  
  208. 'taxonomies' => array( 
  209. 'calendar_category',  
  210. 'calendar_feed',  
  211. 'calendar_type',  
  212. ),  
  213. ); 
  214.  
  215. register_post_type( 'calendar', $args ); 
  216.  
  217.  
  218. /** 
  219. * Filter post content to output a calendar. 
  220. * 
  221. * @since 3.0.0 
  222. * 
  223. * @param string $the_content 
  224. * 
  225. * @return string 
  226. */ 
  227. public function filter_post_content( $the_content ) { 
  228.  
  229. if ( is_singular() ) { 
  230.  
  231. global $post; 
  232.  
  233. if ( 'calendar' == $post->post_type ) { 
  234.  
  235. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { 
  236. return ''; 
  237. } else { 
  238. ob_start(); 
  239. simcal_print_calendar( $post ); 
  240. return ob_get_clean(); 
  241.  
  242. } else { 
  243.  
  244. $post_types = array(); 
  245. $settings = get_option( 'simple-calendar_settings_calendars' ); 
  246. if ( isset( $settings['general']['attach_calendars_posts'] ) ) { 
  247. $post_types = $settings['general']['attach_calendars_posts']; 
  248.  
  249. if ( empty( $post_types ) ) { 
  250. return $the_content; 
  251.  
  252. if ( in_array( $post->post_type, (array) $post_types ) ) { 
  253.  
  254. $id = absint( get_post_meta( $post->ID, '_simcal_attach_calendar_id', true ) ); 
  255.  
  256. if ( $id > 0 ) { 
  257.  
  258. $pos = esc_attr( get_post_meta( $post->ID, '_simcal_attach_calendar_position', true ) ); 
  259.  
  260. ob_start(); 
  261.  
  262. if ( 'after' == $pos ) { 
  263. echo $the_content; 
  264. simcal_print_calendar( $id ); 
  265. } elseif ( 'before' == $pos ) { 
  266. simcal_print_calendar( $id ); 
  267. echo $the_content; 
  268. } else { 
  269. echo $the_content; 
  270.  
  271. return ob_get_clean(); 
  272.  
  273.  
  274.  
  275. return $the_content; 
  276.  
  277. /** 
  278. * Upon posts deletion. 
  279. * 
  280. * Delete transients and notices when a calendar is deleted. 
  281. * 
  282. * @since 3.0.0 
  283. * 
  284. * @param $post_id 
  285. */ 
  286. public function upon_deletion( $post_id ) { 
  287.  
  288. $post_type = get_post_type( $post_id ); 
  289.  
  290. if ( 'calendar' == $post_type ) { 
  291.  
  292. $notices = get_option( 'simple-calendar_admin_notices', array() ); 
  293.  
  294. if ( ! empty( $notices ) && isset( $notices[ 'calendar_' . strval( $post_id ) ] ) ) { 
  295. unset( $notices[ 'calendar_' . strval( $post_id ) ] ); 
  296. update_option( 'simple-calendar_admin_notices', $notices ); 
  297.  
  298. simcal_delete_feed_transients( $post_id ); 
  299.  
.