Ticket #470: 470_patch_3.diff
File 470_patch_3.diff, 4.9 KB (added by , 10 years ago) |
---|
-
official-wordpress-events.php
5 5 Version: 0.1 6 6 Author: WordPress.org Meta Team 7 7 */ 8 9 8 class Official_WordPress_Events { 10 9 const WORDCAMP_API_BASE_URL = 'http://central.wordcamp.org/wp-json.php'; 11 10 const MEETUP_API_BASE_URL = 'https://api.meetup.com/'; … … 27 26 */ 28 27 public function __construct() { 29 28 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 29 add_action( 'wp_ajax_official_wordpress_events_infinite_scroll' , array( $this, 'infinite_scroll_func' ) ); 30 add_action( 'wp_ajax_nopriv_official_wordpress_events_infinite_scroll' , array( $this, 'infinite_scroll_func' ) ); 30 31 add_shortcode( 'official_wordpress_events', array( $this, 'render_events' ) ); 31 32 } 32 33 … … 42 43 wp_enqueue_style( 'official-wordpress-events' ); 43 44 } 44 45 46 wp_register_script( 'official_wp_events_infinite_load', plugins_url() . "/official-wordpress-events/template-event.js" , array( 'wp-util' ), null ); 47 wp_enqueue_script( 'official_wp_events_infinite_load' ); 45 48 } 46 49 47 50 /** … … 68 71 } 69 72 70 73 /** 74 * Infinite Scroll functionality added 75 */ 76 public function infinite_scroll_func() { 77 $offset = $_POST[ 'offset' ]; 78 79 if( ! is_numeric( $offset ) ) { 80 wp_send_json_error(); 81 return; 82 } 83 84 $offset = $offset + 1; 85 if ( $tempEvents = $this->get_meetup_events( $offset ) ) { 86 usort( $tempEvents, array( $this, 'sort_events' ) ); 87 $final_ans_array = array(); 88 89 foreach ( $tempEvents as $event ) : 90 $elem_array = array( 91 'event_url' => esc_attr( esc_url( $event->url ) ), 92 'event_title' => esc_html( $event->title ), 93 'event_start_time' => esc_html( date( 'l, F jS | g:i a', (int) $event->start_timestamp ) ), 94 'event_location' => esc_html( $event->location ), 95 ); 96 array_push( $final_ans_array , $elem_array ); 97 endforeach; 98 } else { 99 wp_send_json_error(); 100 return; 101 } 102 103 wp_send_json_success( json_encode( $final_ans_array ) ); 104 } 105 106 107 /** 71 108 * Sort events based on start timestamp 72 109 * 73 110 * This is a callback for usort() … … 137 174 /** 138 175 * Get WordPress meetups from the Meetup.com API 139 176 * 177 * @param $offset 140 178 * @return array 141 179 */ 142 protected function get_meetup_events( ) {180 protected function get_meetup_events( $offset = 0 ) { 143 181 $events = array(); 144 182 145 183 if ( ! defined( 'MEETUP_API_KEY' ) || ! MEETUP_API_KEY || ! $groups = $this->get_meetup_group_ids() ) { … … 147 185 } 148 186 149 187 $response = $this->remote_get( sprintf( 150 '%s2/events?group_id=%s&time=0, 1m&page=%d&key=%s',188 '%s2/events?group_id=%s&time=0,4m&page=%d&offset=%d&key=%s', 151 189 self::MEETUP_API_BASE_URL, 152 190 implode( ',', $groups ), 153 191 self::POSTS_PER_PAGE, 192 $offset, 154 193 MEETUP_API_KEY 155 194 ) ); 156 195 -
template-event.js
1 var official_wp_event_js = function() { 2 var flag = 0, 3 offset = 0, 4 singleTemplate = wp.template( "single-event" ); 5 function async_infinite_load( ajaxUrl ) { 6 if( flag == 0 ) { 7 flag = flag + 1; 8 jQuery.post ( 9 ajaxUrl, 10 { 11 'action' : 'official_wordpress_events_infinite_scroll', 12 'offset' : offset, 13 }, 14 function( response ) { 15 if ( response.success ) { 16 var data = jQuery.parseJSON( response.data ); 17 jQuery.each( data , function( key , value ) { 18 jQuery( '#ofe_events ul' ).append( singleTemplate( value ) ); 19 } ); 20 offset = offset + 1; 21 } 22 else { 23 jQuery( '#ofe_events ul' ).append( 'Error Occured' ); 24 } 25 flag = 0; 26 } 27 ); 28 } 29 } 30 return { 31 load : async_infinite_load 32 } 33 }(); 34 -
template-events.php
1 1 <div id="ofe-events"> 2 <script> 3 var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>"; 4 </script> 5 2 6 <?php foreach ( $events as $date => $day_events ) : ?> 3 7 4 8 <h3> … … 22 26 <?php echo date( 'g:i a', $event->start_timestamp ); ?> 23 27 </li> 24 28 <?php endforeach; ?> 29 30 <script type="text/html" id="tmpl-single-event"> 31 <li> 32 <a href="{{data.event_url}}"> 33 {{{data.event_title}}} 34 </a><br /> 35 36 {{data.event_start_time}} 37 38 {{data.event_location}} 39 </li> 40 </script> 25 41 </ul> 26 42 43 <div id="load_more_events"> 44 <a href="javascript:official_wp_event_js.load(ajaxurl)">Load More Events</a> 45 </div> 46 27 47 <?php endforeach; ?> 28 48 </div> <!-- end #ofe-events -->