PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/events-manager/classes/em-event-post.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 289 lines | 256 code | 9 blank | 24 comment | 94 complexity | d93de1b5bee883803fa6b75adceb5c45 MD5 | raw file
  1. <?php
  2. /**
  3. * Controls how events are queried and displayed via the WordPress Custom Post APIs
  4. * @author marcus
  5. *
  6. */
  7. class EM_Event_Post {
  8. function init(){
  9. global $wp_query;
  10. //Front Side Modifiers
  11. if( !is_admin() ){
  12. //override single page with formats?
  13. add_filter('the_content', array('EM_Event_Post','the_content'));
  14. add_filter('the_excerpt_rss', array('EM_Event_Post','the_excerpt_rss'));
  15. //display as page template?
  16. if( get_option('dbem_cp_events_template_page') ){
  17. add_filter('single_template',array('EM_Event_Post','single_template'));
  18. }
  19. //Override post template tags
  20. add_filter('the_date',array('EM_Event_Post','the_date'));
  21. add_filter('get_the_date',array('EM_Event_Post','the_date'),10,2);
  22. add_filter('the_category',array('EM_Event_Post','the_category'),10,3);
  23. }
  24. add_action('parse_query', array('EM_Event_Post','parse_query'));
  25. add_action('publish_future_post',array('EM_Event_Post','publish_future_post'),10,1);
  26. }
  27. function publish_future_post($post_id){
  28. global $wpdb, $EM_Event, $EM_Location, $EM_Notices;
  29. $post_type = get_post_type($post_id);
  30. $is_post_type = $post_type == EM_POST_TYPE_EVENT || $post_type == 'event-recurring';
  31. $saving_status = !in_array(get_post_status($post_id), array('trash','auto-draft')) && !defined('DOING_AUTOSAVE');
  32. if(!defined('UNTRASHING_'.$post_id) && $is_post_type && $saving_status ){
  33. $EM_Event = em_get_event($post_id, 'post_id');
  34. $EM_Event->set_status(1);
  35. }
  36. }
  37. /**
  38. * Overrides the default post format of an event and can display an event as a page, which uses the page.php template.
  39. * @param string $template
  40. * @return string
  41. */
  42. function single_template($template){
  43. global $post;
  44. if( !locate_template('single-'.EM_POST_TYPE_EVENT.'.php') && $post->post_type == EM_POST_TYPE_EVENT ){
  45. $template = locate_template(array('page.php','index.php'),false);
  46. }
  47. return $template;
  48. }
  49. function the_excerpt_rss( $content ){
  50. global $post, $EM_Event;
  51. if( $post->post_type == EM_POST_TYPE_EVENT ){
  52. if( get_option('dbem_cp_events_formats') ){
  53. $EM_Event = em_get_event($post);
  54. $content = $EM_Event->output( get_option ( 'dbem_rss_description_format' ), "rss");
  55. $content = ent2ncr(convert_chars($content)); //Some RSS filtering
  56. }
  57. }
  58. return $content;
  59. }
  60. function the_content( $content ){
  61. global $post, $EM_Event;
  62. if( $post->post_type == EM_POST_TYPE_EVENT ){
  63. if( is_archive() || is_search() ){
  64. if(get_option('dbem_cp_events_archive_formats')){
  65. $EM_Event = em_get_event($post);
  66. $content = $EM_Event->output(get_option('dbem_event_list_item_format'));
  67. }
  68. }else{
  69. if( get_option('dbem_cp_events_formats') && !post_password_required() ){
  70. $EM_Event = em_get_event($post);
  71. ob_start();
  72. em_locate_template('templates/event-single.php',true);
  73. $content = ob_get_clean();
  74. }else{
  75. $EM_Event = em_get_event($post);
  76. if( $EM_Event->event_rsvp ){
  77. $content .= $EM_Event->output('<h2>Bookings</h2>#_BOOKINGFORM');
  78. }
  79. }
  80. }
  81. }
  82. return $content;
  83. }
  84. function the_date( $the_date, $d = '' ){
  85. global $post;
  86. if( $post->post_type == EM_POST_TYPE_EVENT ){
  87. $EM_Event = em_get_event($post);
  88. if ( '' == $d ){
  89. $the_date = date(get_option('date_format'), $EM_Event->start);
  90. }else{
  91. $the_date = date($d, $EM_Event->start);
  92. }
  93. }
  94. return $the_date;
  95. }
  96. function the_category( $thelist, $separator = '', $parents='' ){
  97. global $post, $wp_rewrite;
  98. if( $post->post_type == EM_POST_TYPE_EVENT ){
  99. $EM_Event = em_get_event($post);
  100. $categories = $EM_Event->get_categories();
  101. if( empty($categories) ) return '';
  102. /* Copied from get_the_category_list function, with a few minor edits to make urls work, and removing parent stuff (for now) */
  103. $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
  104. $thelist = '';
  105. if ( '' == $separator ) {
  106. $thelist .= '<ul class="post-categories">';
  107. foreach ( $categories as $category ) {
  108. $thelist .= "\n\t<li>";
  109. switch ( strtolower( $parents ) ) {
  110. case 'multiple':
  111. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
  112. break;
  113. case 'single':
  114. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
  115. $thelist .= $category->name.'</a></li>';
  116. break;
  117. case '':
  118. default:
  119. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
  120. }
  121. }
  122. $thelist .= '</ul>';
  123. } else {
  124. $i = 0;
  125. foreach ( $categories as $category ) {
  126. if ( 0 < $i )
  127. $thelist .= $separator;
  128. switch ( strtolower( $parents ) ) {
  129. case 'multiple':
  130. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
  131. break;
  132. case 'single':
  133. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
  134. $thelist .= "$category->name</a>";
  135. break;
  136. case '':
  137. default:
  138. $thelist .= '<a href="' . $category->get_url() . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
  139. }
  140. ++$i;
  141. }
  142. }
  143. /* End copying */
  144. }
  145. return $thelist;
  146. }
  147. function parse_query(){
  148. global $wp_query;
  149. //Search Query Filtering
  150. if( !is_admin() ){
  151. if( !empty($wp_query->query_vars['s']) && !get_option('dbem_cp_events_search_results') ){
  152. $wp_query->query_vars['post_type'] = array_diff( get_post_types(array('exclude_from_search' => false)), array(EM_POST_TYPE_EVENT));
  153. }
  154. }else{
  155. if( !empty($wp_query->query_vars[EM_TAXONOMY_CATEGORY]) && is_numeric($wp_query->query_vars[EM_TAXONOMY_CATEGORY]) ){
  156. //sorts out filtering admin-side as it searches by id
  157. $term = get_term_by('id', $wp_query->query_vars[EM_TAXONOMY_CATEGORY], EM_TAXONOMY_CATEGORY);
  158. $wp_query->query_vars[EM_TAXONOMY_CATEGORY] = ( $term !== false && !is_wp_error($term) )? $term->name:0;
  159. }
  160. }
  161. //Scoping
  162. if( !empty($wp_query->query_vars['post_type']) && ($wp_query->query_vars['post_type'] == EM_POST_TYPE_EVENT || $wp_query->query_vars['post_type'] == 'event-recurring') && (empty($wp_query->query_vars['post_status']) || !in_array($wp_query->query_vars['post_status'],array('trash','pending','draft'))) ) {
  163. //Let's deal with the scope - default is future
  164. if( is_admin() ){
  165. $scope = $wp_query->query_vars['scope'] = (!empty($_REQUEST['scope'])) ? $_REQUEST['scope']:'future';
  166. //TODO limit what a user can see admin side for events/locations/recurring events
  167. }else{
  168. if( !empty($wp_query->query_vars['calendar_day']) ) $wp_query->query_vars['scope'] = $wp_query->query_vars['calendar_day'];
  169. if( empty($wp_query->query_vars['scope']) ){
  170. if( is_archive() ){
  171. $scope = $wp_query->query_vars['scope'] = get_option('dbem_events_page_scope');
  172. }else{
  173. $scope = $wp_query->query_vars['scope'] = 'all'; //otherwise we'll get 404s for past events
  174. }
  175. }else{
  176. $scope = $wp_query->query_vars['scope'];
  177. }
  178. }
  179. $query = array();
  180. $time = current_time('timestamp');
  181. if ( preg_match ( "/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/", $scope ) ) {
  182. $today = strtotime($scope);
  183. $tomorrow = $today + 60*60*24-1;
  184. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  185. $query[] = array( 'key' => '_start_ts', 'value' => array($today,$tomorrow), 'compare' => 'BETWEEN' );
  186. }else{
  187. $query[] = array( 'key' => '_start_ts', 'value' => $tomorrow, 'compare' => '<=' );
  188. $query[] = array( 'key' => '_end_ts', 'value' => $today, 'compare' => '>=' );
  189. }
  190. }elseif ($scope == "future"){
  191. $today = strtotime(date('Y-m-d', $time));
  192. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  193. $query[] = array( 'key' => '_start_ts', 'value' => $today, 'compare' => '>=' );
  194. }else{
  195. $query[] = array( 'key' => '_end_ts', 'value' => $today, 'compare' => '>=' );
  196. }
  197. }elseif ($scope == "past"){
  198. $today = strtotime(date('Y-m-d', $time));
  199. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  200. $query[] = array( 'key' => '_start_ts', 'value' => $today, 'compare' => '<' );
  201. }else{
  202. $query[] = array( 'key' => '_end_ts', 'value' => $today, 'compare' => '<' );
  203. }
  204. }elseif ($scope == "today"){
  205. $today = strtotime(date('Y-m-d', $time));
  206. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  207. //date must be only today
  208. $query[] = array( 'key' => '_start_ts', 'value' => $today, 'compare' => '=');
  209. }else{
  210. $query[] = array( 'key' => '_start_ts', 'value' => $today, 'compare' => '<=' );
  211. $query[] = array( 'key' => '_end_ts', 'value' => $today, 'compare' => '>=' );
  212. }
  213. }elseif ($scope == "tomorrow"){
  214. $tomorrow = strtotime(date('Y-m-d',$time+60*60*24));
  215. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  216. //date must be only tomorrow
  217. $query[] = array( 'key' => '_start_ts', 'value' => $tomorrow, 'compare' => '=');
  218. }else{
  219. $query[] = array( 'key' => '_start_ts', 'value' => $tomorrow, 'compare' => '<=' );
  220. $query[] = array( 'key' => '_end_ts', 'value' => $tomorrow, 'compare' => '>=' );
  221. }
  222. }elseif ($scope == "month"){
  223. $start_month = strtotime(date('Y-m-d',$time));
  224. $end_month = strtotime(date('Y-m-t',$time));
  225. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  226. $query[] = array( 'key' => '_start_ts', 'value' => array($start_month,$end_month), 'type' => 'numeric', 'compare' => 'BETWEEN');
  227. }else{
  228. $query[] = array( 'key' => '_start_ts', 'value' => $end_month, 'compare' => '<=' );
  229. $query[] = array( 'key' => '_end_ts', 'value' => $start_month, 'compare' => '>=' );
  230. }
  231. }elseif ($scope == "next-month"){
  232. $start_month_timestamp = strtotime('+1 month', $time); //get the end of this month + 1 day
  233. $start_month = strtotime(date('Y-m-1',$start_month_timestamp));
  234. $end_month = strtotime(date('Y-m-t',$start_month_timestamp));
  235. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  236. $query[] = array( 'key' => '_start_ts', 'value' => array($start_month,$end_month), 'type' => 'numeric', 'compare' => 'BETWEEN');
  237. }else{
  238. $query[] = array( 'key' => '_start_ts', 'value' => $end_month, 'compare' => '<=' );
  239. $query[] = array( 'key' => '_end_ts', 'value' => $start_month, 'compare' => '>=' );
  240. }
  241. }elseif( preg_match('/(\d\d?)\-months/',$scope,$matches) ){ // next x months means this month (what's left of it), plus the following x months until the end of that month.
  242. $months_to_add = $matches[1];
  243. $start_month = strtotime(date('Y-m-d',$time));
  244. $end_month = strtotime(date('Y-m-t',strtotime("+$months_to_add month", $time)));
  245. if( get_option('dbem_events_current_are_past') && $wp_query->query_vars['post_type'] != 'event-recurring' ){
  246. $query[] = array( 'key' => '_start_ts', 'value' => array($start_month,$end_month), 'type' => 'numeric', 'compare' => 'BETWEEN');
  247. }else{
  248. $query[] = array( 'key' => '_start_ts', 'value' => $end_month, 'compare' => '<=' );
  249. $query[] = array( 'key' => '_end_ts', 'value' => $start_month, 'compare' => '>=' );
  250. }
  251. }
  252. if( !empty($query) && is_array($query) ){
  253. $wp_query->query_vars['meta_query'] = $query;
  254. }
  255. if( is_admin() ){
  256. //admin areas don't need special ordering, so make it simple
  257. $wp_query->query_vars['orderby'] = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby']:'meta_value_num';
  258. $wp_query->query_vars['meta_key'] = '_start_ts';
  259. $wp_query->query_vars['order'] = (!empty($_REQUEST['order'])) ? $_REQUEST['order']:'ASC';
  260. }else{
  261. if( get_option('dbem_events_default_archive_orderby') == 'title'){
  262. $wp_query->query_vars['orderby'] = 'title';
  263. $wp_query->query_vars['order'] = get_option('dbem_events_default_archive_order','ASC');
  264. }else{
  265. $wp_query->query_vars['orderby'] = 'meta_value_num';
  266. $wp_query->query_vars['meta_key'] = '_start_ts';
  267. }
  268. $wp_query->query_vars['order'] = get_option('dbem_events_default_archive_order','ASC');
  269. }
  270. }elseif( !empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == EM_POST_TYPE_EVENT ){
  271. $wp_query->query_vars['scope'] = 'all';
  272. if( $wp_query->query_vars['post_status'] == 'pending' ){
  273. $wp_query->query_vars['orderby'] = 'meta_value_num';
  274. $wp_query->query_vars['order'] = 'ASC';
  275. $wp_query->query_vars['meta_key'] = '_start_ts';
  276. }
  277. }
  278. }
  279. }
  280. EM_Event_Post::init();