WordPress.org

Plugin Directory

Changeset 551888


Ignore:
Timestamp:
06/01/12 16:03:41 (23 months ago)
Author:
ryelle
Message:

use admin ajax to process oauth request, switch code structure to extend VsMeet class

Location:
meetup-widgets/trunk
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • meetup-widgets/trunk/vs_meetup.php

    r513075 r551888  
    33 * Plugin Name: Meetup Widgets 
    44 * Description: Add widgets displaying information from Meetup.com 
    5  * Version: 1.0 
     5 * Version: 2.0 
    66 * Author: Kelly Dwan 
    77 * Author URI: http://redradar.net 
     8 * Plugin URI: http://me.redradar.net/category/plugins/meetup-widgets/ 
    89 * License: GPL2 
    9  * Date: 2.8.12 
    10  */ 
    11   
    12 /** ======= 
    13  *  TODO 
    14  *       
     10 * Date: 5.29.2012 
    1511 */ 
    1612 
    17 require_once('vs_meetup_widgets.php'); 
     13/**  
     14 * If the class exists we've declared it in another Meetup integration plugin, 
     15 * and should not do so twice. 
     16 */ 
     17if (!class_exists('VsMeet')): 
    1818 
    1919class VsMeet { 
    2020    /* global variables */ 
    21     protected $key = ""; 
    22     protected $secret = ""; 
    23      
     21    private $req_url = 'http://api.meetup.com/oauth/request/'; 
     22    private $authurl = 'http://www.meetup.com/authorize/'; 
     23    private $acc_url = 'http://api.meetup.com/oauth/access/'; 
     24    private $api_url = 'http://api.meetup.com/'; 
     25         
     26    private $key = ''; 
     27    private $secret = '';    
    2428    protected $api_key = ""; 
    2529     
    2630    public function __construct($var = '') { 
     31        $options = get_option('vs_meet_options'); 
     32        $this->key = $options['vs_meetup_key']; 
     33        $this->secret = $options['vs_meetup_secret']; 
     34        $this->api_key = $options['vs_meetup_api_key']; 
     35         
    2736        register_activation_hook( __FILE__, array ($this, 'install' ) ); 
    2837         
     
    3241        // add admin page & options 
    3342        add_filter( 'admin_init' , array( $this , 'register_fields' ) ); 
    34          
    35         // register widgets 
    36         add_action('widgets_init', create_function('', 'return register_widget("VsMeetSingleWidget");')); 
    37         add_action('widgets_init', create_function('', 'return register_widget("VsMeetListWidget");')); 
    3843    } 
    3944     
    4045    public function install(){ 
    41         //nothing here yet, as there's really nothing to 'install' 
     46        //nothing here yet, as there's really nothing to 'install' that isn't covered by __construct 
    4247    } 
    4348     
     
    4550        register_setting( 'general', 'vs_meet_options', array($this,'validate')); 
    4651         
    47         add_settings_section('vs_meet', __('Meetup API Settings','vsmeet_domain'), array($this, 'setting_section_vs_meetup'), 'general'); 
     52        add_settings_section('vs_meet', 'Meetup API Settings', array($this, 'setting_section_vs_meetup'), 'general'); 
    4853         
    4954        add_settings_field('vs_meetup_key', '<label for="vs_meetup_key">'.__('OAuth Meetup Key:' , 'vsmeet_domain' ).'</label>' , array(&$this, 'setting_vs_meetup_key') , 'general', 'vs_meet' ); 
     
    5661 
    5762    function setting_section_vs_meetup(){ 
    58         echo "<p>". spritf( 
    59             __('To use the RSVP functionality of this plugin, you need to create an OAuth consumer. You can do that (or reset your information) by going here: %1$s. You can get (&amp; reset, if needed) your API key at %2$s','vsmeet_domain'), 
    60             "<a target='_blank' href='http://www.meetup.com/meetup_api/oauth_consumers/'>". _x('Your OAuth Consumers','meetup.com page title','vsmeet_domain') ."</a>", 
    61             "<a target='_blank' href='http://www.meetup.com/meetup_api/key/'>". _x('Getting an API key','meetup.com page title','vsmeet_domain') ."</a>" 
    62         ) ."</p>"; 
     63        echo "<p>".__("To use this plugin, you need to create an OAuth consumer. You can do that (or reset your information) by going here: <a target='_blank' href='http://www.meetup.com/meetup_api/oauth_consumers/'>Your OAuth Consumers</a>",'vsmeet_domain')."</p>"; 
    6364    } 
    6465 
     
    9293     
    9394} 
    94 $vsm = new VsMeet(); 
    95 //$vsml = new VsMeetLogin(); 
     95endif; 
     96 
     97require_once('vs_meetup_widgets.php'); 
     98 
     99/** 
     100 * Initialize Meetup Widgets 
     101 */ 
     102function meetup_widgets_start() { 
     103    if (!class_exists('OAuth')){ 
     104        add_action('pre_current_active_plugins','vsmw_need_oauth'); 
     105    }  
     106    $vsmw = new VsMeetWidget(); 
     107 
     108} add_action( 'init', 'meetup_widgets_start' ); 
     109 
     110// Register widgets 
     111add_action('widgets_init', create_function('', 'return register_widget("VsMeetSingleWidget");')); 
     112add_action('widgets_init', create_function('', 'return register_widget("VsMeetListWidget");')); 
     113 
     114function vsmw_need_oauth() { 
     115    echo '<div id="message" class="updated"><p>Some functionality of <strong>Meetup Login</strong> requires <strong><a href="http://php.net/manual/en/book.oauth.php">OAuth</a></strong>, which is not detected on this server. RSVPing from this site is disabled - RSVP links will instead bring you to the event on Meetup.com.</p></div>'; 
     116} 
  • meetup-widgets/trunk/vs_meetup_widgets.php

    r513075 r551888  
    55 */ 
    66 
    7 class VsMeetWidget { 
     7class VsMeetWidget extends VsMeet{ 
     8    private $req_url = 'http://api.meetup.com/oauth/request/'; 
     9    private $authurl = 'http://www.meetup.com/authorize/'; 
     10    private $acc_url = 'http://api.meetup.com/oauth/access/'; 
     11    private $api_url = 'http://api.meetup.com/'; 
     12    private $callback_url = ''; 
     13         
     14    private $key = ''; 
     15    private $secret = '';    
     16    protected $api_key = ""; 
     17     
     18    public function __construct() { 
     19        $options = get_option('vs_meet_options'); 
     20        $this->key = $options['vs_meetup_key']; 
     21        $this->secret = $options['vs_meetup_secret']; 
     22        $this->api_key = $options['vs_meetup_api_key']; 
     23        $this->callback_url = admin_url( 'admin-ajax.php' ) .'?action=meetup_event'; 
     24         
     25        parent::__construct(); 
     26         
     27        // add login function to ajax requests 
     28        add_action( 'wp_ajax_nopriv_meetup_event', array($this, 'meetup_event_popup') ); 
     29        add_action( 'wp_ajax_meetup_event', array($this, 'meetup_event_popup') ); 
     30    } 
     31     
    832    /** 
    933     * Get a single event, with a link to RSVP (OAuth, new tiny window). 
     
    2953                $out .= '<p>'. wp_trim_words(strip_tags($event->description),20) .'</p>'; 
    3054                $out .= '<p><span class="rsvp-count">'.$event->yes_rsvp_count.' '._n('attendee', 'attendees', $event->yes_rsvp_count).'</span>'; 
    31                 if (!empty($options['vs_meetup_key']) && !empty($options['vs_meetup_secret'])) { 
    32                     $out .= "<span class='rsvp-add'><a href='#' onclick='javascript:window.open(\"".plugins_url('vs-oauth-event.php', __FILE__)."?event=$id\",\"authenticate\",\"width=400,height=600\");'>RSVP?</a></span></p>"; 
     55                if ( !empty($options['vs_meetup_key']) && !empty($options['vs_meetup_secret']) && class_exists('OAuth')) { 
     56                    $out .= "<span class='rsvp-add'><a href='#' onclick='javascript:window.open(\"{$this->callback_url}&event=$id\",\"authenticate\",\"width=400,height=600\");'>RSVP?</a></span></p>"; 
    3357                } else { 
    3458                    $out .= '<span class="rsvp-add"><a href="'.$event->event_url.'">RSVP?</a></span></p>'; 
     
    83107        } 
    84108        return $out; 
     109    } 
     110     
     111    /** 
     112     * Create the event RSVP popup 
     113     */ 
     114    function meetup_event_popup() { 
     115        session_start(); 
     116        $header = '<html dir="ltr" lang="en-US"> 
     117            <head> 
     118                <meta charset="UTF-8" /> 
     119                <meta name="viewport" content="width=device-width" /> 
     120                <title>RSVP to a Meetup</title> 
     121                <link rel="stylesheet" type="text/css" media="all" href="'.get_bloginfo( 'stylesheet_url' ).'" /> 
     122                <style> 
     123                    .button { 
     124                        padding:3%; 
     125                        color:white; 
     126                        background-color:#B03C2D; 
     127                        border-radius:3px; 
     128                        display:block; 
     129                        font-weight:bold; 
     130                        width:40%; 
     131                        float:left; 
     132                        text-align:center; 
     133                    } 
     134                    .button.no { 
     135                        margin-left:8%; 
     136                    } 
     137                </style> 
     138            </head> 
     139            <body> 
     140                <div id="page" class="hfeed meetup event" style="padding:15px;">'; 
     141        if (array_key_exists('event',$_GET)) $_SESSION['event'] = $_GET['event']; 
     142        if (!array_key_exists('state',$_SESSION)) $_SESSION['state'] = 0; 
     143        // In state=1 the next request should include an oauth_token. 
     144        // If it doesn't go back to 0 
     145        if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0; 
     146        try { 
     147            $oauth = new OAuth($this->key, $this->secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION ); 
     148            $oauth->enableDebug(); 
     149            if(!isset($_GET['oauth_token']) && !$_SESSION['state']) { 
     150                $request_token_info = $oauth->getRequestToken($this->req_url); 
     151                $_SESSION['secret'] = $request_token_info['oauth_token_secret']; 
     152                $_SESSION['state'] = 1; 
     153                header('Location: '.$this->authurl.'?oauth_token='.$request_token_info['oauth_token'].'&oauth_callback='.$this->callback_url); 
     154                exit; 
     155            } else if($_SESSION['state']==1) { 
     156                $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']); 
     157                $verifier = (array_key_exists('verifier',$_GET)) ? $_GET['verifier'] : null;  
     158                $access_token_info = $oauth->getAccessToken($this->acc_url,null,$verifier); 
     159                $_SESSION['state'] = 2; 
     160                $_SESSION['token'] = $access_token_info['oauth_token']; 
     161                $_SESSION['secret'] = $access_token_info['oauth_token_secret']; 
     162            }  
     163            $oauth->setToken($_SESSION['token'],$_SESSION['secret']); 
     164            if (array_key_exists('rsvp',$_GET)) { // button has been pressed. 
     165                //send the RSVP. 
     166                if ('yes' == $_GET['rsvp']) 
     167                    $oauth->fetch("{$this->api_url}/rsvp", array('event_id'=>$_SESSION['event'], 'rsvp'=>'yes'), OAUTH_HTTP_METHOD_POST);    
     168                else 
     169                    $response = $oauth->fetch("{$this->api_url}/rsvp", array('event_id'=>$_SESSION['event'], 'rsvp'=>'no'), OAUTH_HTTP_METHOD_POST);     
     170                $rsvp = json_decode($oauth->getLastResponse()); 
     171                 
     172                echo $header; 
     173                echo '<h1 style="padding:20px 0 0;"><a>'.$rsvp->description.'</a></h1>'; 
     174                echo '<p>'.$rsvp->details.'.</p>'; 
     175                exit; 
     176            } else { 
     177                // Get event info to display here. 
     178                $oauth->fetch("{$this->api_url}/2/events?event_id=".$_SESSION['event']); 
     179                $event = json_decode($oauth->getLastResponse()); 
     180                $event = $event->results[0]; 
     181                $out  = '<h1 id="site-title" style="padding:20px 0 0;"><a target="_blank" href="'.$event->event_url.'">'.$event->name.'</a></h1>'; 
     182                $out .= '<p style="text-align:justify;">'.$event->description.'</p>'; 
     183                $out .= '<p><span class="rsvp-count">'.$event->yes_rsvp_count.' '._n('attendee', 'attendees', $event->yes_rsvp_count).'</span></p>'; 
     184                if (null !== $event->venue) { 
     185                    $venue = $event->venue->name.' '.$event->venue->address_1 . ', ' . $event->venue->city . ', ' . $event->venue->state; 
     186                    $out .= "<h3 class='event_location'>Location: <a href='http://maps.google.com/maps?q=$venue+%28".$event->venue->name."%29&z=17' target='_blank'>$venue</a></h3>"; 
     187                } else { 
     188                    $out .= "<p class='event_location'>Location: TBA</p>"; 
     189                } 
     190                $out .= '<h2>'.date('F d, Y @ g:i a',intval($event->time/1000 + $event->utc_offset/1000)).'</h2>'; 
     191                 
     192                echo $header . $out; 
     193                $oauth->fetch("{$this->api_url}/rsvps?event_id=".$_SESSION['event']); 
     194                $rsvps = json_decode($oauth->getLastResponse()); 
     195                $oauth->fetch("{$this->api_url}/members?relation=self"); 
     196                $me = json_decode($oauth->getLastResponse()); 
     197                $my_id = $me->results[0]->id; 
     198                foreach ($rsvps->results as $user){ 
     199                    if ($my_id == $user->member_id){ 
     200                        echo "<h3 style='padding:20px 0 0; font-weight:normal; font-size:16px'>Your RSVP: <strong>{$user->response}</strong></h3>"; 
     201                        echo "<p>You can change your RSVP below.</p>"; 
     202                    } 
     203                } 
     204                 
     205                echo "<h1 style='padding:20px 0 0; font-weight:bold; font-size:22px'>RSVP: </h1>"; 
     206                echo "<p style='font-size:.9em'>Please RSVP at meetup.com if you're bringing someone.</p>"; 
     207                echo "<a class='button yes' href='{$this->callback_url}&rsvp=yes'>Yes</a>"; 
     208                echo "<a class='button no' href='{$this->callback_url}&rsvp=no'>No</a>"; 
     209                echo "<p style='clear:both'></p>"; 
     210                //echo "<pre>".print_r($event,true)."</pre>"; 
     211                exit;   
     212            } 
     213        } catch(OAuthException $E) { 
     214            echo $header; 
     215            echo "<h1 class='entry-title'>There was an error processing your request. Please try again.</h1>"; 
     216            if (WP_DEBUG) echo "<pre>".print_r($E,true)."</pre>"; 
     217        } 
     218        unset($_SESSION['state']); 
     219        unset($_SESSION['event']); 
     220        echo "</div> </body> </html>"; 
    85221    } 
    86222} 
Note: See TracChangeset for help on using the changeset viewer.