Ticket #470: 470_patch_2.diff
File 470_patch_2.diff, 5.2 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/'; … … 26 25 * Constructor 27 26 */ 28 27 public function __construct() { 28 add_action( 'wp_ajax_official_wordpress_events_infinite_scroll' , array( $this, 'infinite_scroll_func' ) ); 29 add_action( 'wp_ajax_nopriv_official_wordpress_events_infinite_scroll' , array( $this, 'infinite_scroll_func' ) ); 30 add_action( 'wp_enqueue_scripts' , array( $this, 'owe_include_essentials' ) ); 29 31 add_shortcode( 'official_wordpress_events', array( $this, 'render_events' ) ); 30 32 } 31 33 … … 53 55 } 54 56 55 57 /** 58 * Infinite Scroll functionality added 59 */ 60 public function infinite_scroll_func() { 61 $offset = $_POST[ 'offset' ]; 62 if( ! is_numeric( $offset ) ) { 63 wp_send_json_error(); 64 return; 65 } 66 $offset = $offset + 1; 67 if ( $tempEvents = $this->get_meetup_events( $offset ) ) { 68 usort( $tempEvents, array( $this, 'sort_events' ) ); 69 $final_ans_array = array(); 70 foreach ( $tempEvents as $event ) : 71 $elem_array = array( 72 'event_url' => esc_attr( esc_url( $event->url ) ), 73 'event_title' => esc_html( $event->title ), 74 'event_start_time' => esc_html( date( 'l, F jS | g:i a', (int) $event->start_timestamp ) ), 75 'event_location' => esc_html( $event->location ), 76 ); 77 array_push( $final_ans_array , $elem_array ); 78 endforeach; 79 } 80 else { 81 wp_send_json_error(); 82 return; 83 } 84 wp_send_json_success( json_encode( $final_ans_array ) ); 85 } 86 87 /** 88 * Footer action class for using wordpress templates 89 */ 90 public function owe_include_essentials() { 91 wp_register_script( 'official_wp_events_infinite_load', plugins_url() . "/official-wordpress-events/template-event.js" , array( 'wp-util' ), null ); 92 wp_enqueue_script( 'official_wp_events_infinite_load' ); 93 } 94 95 /** 56 96 * Sort events based on start timestamp 57 97 * 58 98 * This is a callback for usort() … … 105 145 /** 106 146 * Get WordPress meetups from the Meetup.com API 107 147 * 148 * @param $offset 108 149 * @return array 109 150 */ 110 protected function get_meetup_events( ) {151 protected function get_meetup_events( $offset = 0 ) { 111 152 $events = array(); 112 153 113 154 if ( ! defined( 'MEETUP_API_KEY' ) || ! MEETUP_API_KEY || ! $groups = $this->get_meetup_group_ids() ) { … … 115 156 } 116 157 117 158 $response = $this->remote_get( sprintf( 118 '%s2/events?group_id=%s&time=0, 1m&page=%d&key=%s',159 '%s2/events?group_id=%s&time=0,4m&page=%d&offset=%d&key=%s', 119 160 self::MEETUP_API_BASE_URL, 120 161 implode( ',', $groups ), 121 162 self::POSTS_PER_PAGE, 163 $offset, 122 164 MEETUP_API_KEY 123 165 ) ); 124 166 -
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
Property changes on: template-event.js ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
1 <script> 2 var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>"; 3 </script> 4 1 5 <div id="ofe_events"> 2 6 <ul> 3 7 <?php foreach ( $events as $event ) : ?> … … 13 17 </li> 14 18 15 19 <?php endforeach; ?> 20 <script type="text/html" id="tmpl-single-event"> 21 <li> 22 <a href="{{data.event_url}}"> 23 {{{data.event_title}}} 24 </a><br /> 25 26 {{data.event_start_time}} 27 28 {{data.event_location}} 29 </li> 30 </script> 16 31 </ul> 32 <div id="load_more_events"> 33 <a href="javascript:official_wp_event_js.load(ajaxurl)">Load More Events</a> 34 </div> 35 17 36 </div> <!-- end #ofe_events -->