index.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /*
  3. Plugin Name: All-in-One Event Calendar Rss
  4. Plugin URI:
  5. Description: Produce un rss con gli eventi di All In One Event Calendar
  6. Version: 0.0.1
  7. Author: gin(e)
  8. Author URI:
  9. License: GPL3
  10. License URI: http://www.gnu.org/licenses/gpl-3.0.html
  11. Text Domain: ai1ec_rss
  12. @author gine
  13. @category social
  14. @package Wordpress All-in-One Event Calendar Rss
  15. @since 0.0.1
  16. Wordpress All In One Event Calendar Rss. A Plugin for WordPress.
  17. Copyright (C) 2019 gine -
  18. This program is free software: you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation, either version 3 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
  28. */
  29. require_once(ABSPATH .'wp-includes/pluggable.php');
  30. if ( ! defined( 'ABSPATH' ) ) :
  31. exit; // Exit if accessed directly
  32. endif;
  33. function set_ai1ecrss_join($join, &$wp_query) {
  34. global $wpdb;
  35. $db_prefix=$wpdb->prefix;
  36. $join .=" JOIN ".$db_prefix."ai1ec_events
  37. ON $wpdb->posts.ID = ".$db_prefix."ai1ec_events.post_id";
  38. return $join;
  39. }
  40. function set_ai1ecrss_where( $where, &$wp_query ){
  41. global $wpdb;
  42. $db_prefix=$wpdb->prefix;
  43. if(! is_admin() && $wp_query->post_type ='ai3ec_event'){
  44. date_default_timezone_set("Europe/Rome");
  45. $start = time()+ (7 * 24 * 60 * 60);
  46. $end = time();
  47. //$where .= ' AND '. $db_prefix.'ai1ec_events.start <= \''.$start.'\'' .' AND '. $db_prefix.'ai1ec_events.end >= \''.$end.'\'';
  48. $where .= ' AND '. $db_prefix.'ai1ec_events.start <= \''.$start.'\'';
  49. return $where;
  50. }
  51. }
  52. function set_ai1ecrss_orderby($orderby) {
  53. global $wpdb;
  54. $db_prefix=$wpdb->prefix;
  55. $orderby = $db_prefix."ai1ec_events.start DESC";
  56. return $orderby;
  57. }
  58. function getEvent($cat){
  59. $args=Array(
  60. 'post_type'=> 'ai1ec_event',
  61. 'no_found_rows' => 1,
  62. 'showposts' => 10
  63. );
  64. if($cat != "all" ) {
  65. $args['tax_query'] = array(
  66. Array(
  67. 'taxonomy' => 'events_categories',
  68. 'field' => 'slug',
  69. 'terms' => Array($cat),
  70. )
  71. );
  72. }
  73. //var_dump($args);
  74. add_filter( 'posts_join', 'set_ai1ecrss_join',10,2 );
  75. add_filter( 'posts_where', 'set_ai1ecrss_where', 10, 2 );
  76. add_filter( 'posts_orderby', 'set_ai1ecrss_orderby' );
  77. //http://codex.wordpress.org/Class_Reference/WP_Query
  78. $cat_query = new WP_Query($args);
  79. remove_filter( 'posts_join', 'set_ai1ecrss_join' );
  80. remove_filter( 'posts_where', 'set_ai1ecrss_where' );
  81. remove_filter( 'posts_orderby', 'set_ai1ecrss_orderby' );
  82. header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
  83. echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
  84. <rss version="2.0"
  85. xmlns:content="http://purl.org/rss/1.0/modules/content/"
  86. xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  87. xmlns:dc="http://purl.org/dc/elements/1.1/"
  88. xmlns:atom="http://www.w3.org/2005/Atom"
  89. xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
  90. xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  91. <channel>
  92. <title><?php bloginfo_rss('name'); ?> - Events Feed</title>
  93. <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
  94. <link><?php bloginfo_rss('url') ?></link>
  95. <description><?php bloginfo_rss('description') ?></description>
  96. <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
  97. <language><?php echo get_option('rss_language'); ?></language>
  98. <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
  99. <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
  100. <?php if($cat_query->have_posts()): ?>
  101. <?php while($cat_query->have_posts()) : $cat_query->the_post(); ?>
  102. <item>
  103. <title><?php the_title_rss(); ?></title>
  104. <link><?php the_permalink_rss(); ?></link>
  105. <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
  106. <guid isPermaLink="false"><?php the_guid(); ?></guid>
  107. <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
  108. <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
  109. <?php rss_enclosure(); ?>
  110. <?php do_action('rss2_item'); ?>
  111. </item>
  112. <?php endwhile; ?>
  113. <?php endif; ?>
  114. </channel>
  115. </rss>
  116. <?php
  117. }
  118. function debug($args){ ?>
  119. <html><head></head><body>ciao</body></html><?php
  120. }
  121. function wds_ai1ec_rss_restapi_endpoint() {
  122. add_rewrite_tag( '%ai1ecrss%', '([^&]+)' );
  123. add_rewrite_rule( '%/wp_ai1ec_rss/([^&]+)/?', 'index.php?ai1ecrss=$matches[1]', 'top' );
  124. }
  125. add_action( 'init', 'wds_ai1ec_rss_restapi_endpoint' );
  126. function wds_ai1ec_rss_restapi_endpoint_call() {
  127. global $wp_query;
  128. $category = $wp_query->get( 'ai1ecrss' );
  129. if ( ! $category)
  130. return;
  131. getEvent($category);
  132. die;
  133. }
  134. add_action( 'template_redirect', 'wds_ai1ec_rss_restapi_endpoint_call' );