foursquare.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Foursquare service definition for Keyring.
  4. * https://developer.foursquare.com/docs/oauth.html
  5. * https://foursquare.com/oauth/
  6. */
  7. class Keyring_Service_Foursquare extends Keyring_Service_OAuth2 {
  8. const NAME = 'foursquare';
  9. const LABEL = 'Foursquare';
  10. const API_VERSION = '20120701';
  11. function __construct() {
  12. parent::__construct();
  13. // Enable "basic" UI for entering key/secret
  14. if ( ! KEYRING__HEADLESS_MODE ) {
  15. add_action( 'keyring_foursquare_manage_ui', array( $this, 'basic_ui' ) );
  16. add_filter( 'keyring_foursquare_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  17. }
  18. $this->set_endpoint( 'authorize', 'https://foursquare.com/oauth2/authenticate', 'GET' );
  19. $this->set_endpoint( 'access_token', 'https://foursquare.com/oauth2/access_token', 'GET' );
  20. $this->set_endpoint( 'self', 'https://api.foursquare.com/v2/users/self', 'GET' );
  21. $creds = $this->get_credentials();
  22. $this->app_id = $creds['app_id'];
  23. $this->key = $creds['key'];
  24. $this->secret = $creds['secret'];
  25. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  26. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  27. }
  28. function basic_ui_intro() {
  29. echo '<p>' . sprintf( __( 'If you haven\'t already, you\'ll need to <a href="%s">create a new app at Foursquare</a>. You should only need to worry about these settings:', 'keyring' ), 'https://foursquare.com/developers/register' ) . '</p>';
  30. echo '<ol>';
  31. echo '<li>' . __( "<strong>Your app name</strong>: enter whatever you like, maybe your website's name?", 'keyring' ) . '</li>';
  32. echo '<li>' . sprintf( __( "<strong>Download / welcome page url</strong>: just enter your website's URL, <code>%s</code>", 'keyring' ), get_bloginfo( 'url' ) ) . '</li>';
  33. echo '<li>' . sprintf( __( "<strong>Redirect URI(s)</strong>: Copy-paste this, <code>%s</code>", 'keyring' ), Keyring_Util::admin_url( 'foursquare', array( 'action' => 'verify' ) ) ) . '</li>';
  34. echo '<li>' . __( "<strong>New users can connect via the web</strong>: check the box", 'keyring' ) . '</li>';
  35. echo '</ol>';
  36. echo '<p>' . __( "Once you've saved those changes, copy the <strong>Client id</strong> value into the <strong>API Key</strong> field, and the <strong>Client secret</strong> value into the <strong>API Secret</strong> field and click save (you don't need an App ID value for Foursquare).", 'keyring' ) . '</p>';
  37. }
  38. function build_token_meta( $token ) {
  39. $token = new Keyring_Access_Token( $this->get_name(), $token['access_token'], array() );
  40. $this->set_token( $token );
  41. $res = $this->request( $this->self_url, array( 'method' => $this->self_method ) );
  42. if ( Keyring_Util::is_error( $res ) ) {
  43. $meta = array();
  44. } else {
  45. $meta = array(
  46. 'user_id' => $res->response->user->id,
  47. 'first_name' => $res->response->user->firstName,
  48. 'last_name' => $res->response->user->lastName,
  49. 'picture' => $res->response->user->photo->prefix . '300x300' . $res->response->user->photo->suffix,
  50. );
  51. }
  52. return apply_filters( 'keyring_access_token_meta', $meta, 'foursquare', $token, $res, $this );
  53. }
  54. function get_display( Keyring_Access_Token $token ) {
  55. $meta = $token->get_meta();
  56. return trim( $meta['first_name'] . ' ' . $meta['last_name'] );
  57. }
  58. function request( $url, array $params = array() ) {
  59. $url = add_query_arg( array( 'v' => self::API_VERSION ), $url );
  60. return parent::request( $url, $params );
  61. }
  62. }
  63. add_action( 'keyring_load_services', array( 'Keyring_Service_Foursquare', 'init' ) );