tripit.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * TripIt service definition for Keyring. Clean implementation of OAuth1
  4. */
  5. class Keyring_Service_TripIt extends Keyring_Service_OAuth1 {
  6. const NAME = 'tripit';
  7. const LABEL = 'TripIt';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_tripit_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_tripit_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. }
  15. $this->authorization_header = true;
  16. $this->authorization_realm = false;
  17. $this->set_endpoint( 'request_token', 'https://api.tripit.com/oauth/request_token', 'POST' );
  18. $this->set_endpoint( 'authorize', 'https://www.tripit.com/oauth/authorize', 'GET' );
  19. $this->set_endpoint( 'access_token', 'https://api.tripit.com/oauth/access_token', 'POST' );
  20. $this->set_endpoint( 'verify', 'https://api.tripit.com/v1/get/profile/id/me', '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. $this->requires_token( true );
  28. }
  29. function basic_ui_intro() {
  30. echo '<p>' . __( "If you haven't created an app on TripIt yet, <a href='https://www.tripit.com/developer/create'>create one now</a>. Make sure you set it to being a 'Web application or widget', and other than that the settings are all up to you.", 'keyring' ) . '</p>';
  31. echo '<p>' . __( "Once you've created your app, you will see a yellow box at the top of the page, where you can get your <strong>API Key</strong> and <strong>API Secret</strong>, to enter below (you don't need an App ID value for TripIt).", 'keyring' ) . '</p>';
  32. }
  33. function parse_response( $response ) {
  34. return json_decode( $response );
  35. }
  36. function build_token_meta( $token ) {
  37. // Set the token so that we can make requests using it
  38. $this->set_token(
  39. new Keyring_Access_Token(
  40. $this->get_name(),
  41. new OAuthToken(
  42. $token['oauth_token'],
  43. $token['oauth_token_secret']
  44. )
  45. )
  46. );
  47. $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) );
  48. if ( Keyring_Util::is_error( $response ) ) {
  49. $meta = array();
  50. } else {
  51. $meta = array(
  52. 'user_id' => $response->Profile->{'@attributes'}->ref,
  53. 'username' => $response->Profile->screen_name,
  54. 'name' => $response->Profile->public_display_name,
  55. 'picture' => $response->Profile->photo_url,
  56. '_classname' => get_called_class(),
  57. );
  58. }
  59. return apply_filters( 'keyring_access_token_meta', $meta, 'tripit', $token, $response, $this );
  60. }
  61. function get_display( Keyring_Access_Token $token ) {
  62. return $token->get_meta( 'name' );
  63. }
  64. function request( $url, array $params = array() ) {
  65. $url = add_query_arg( array( 'format' => 'json' ), $url );
  66. return parent::request( $url, $params );
  67. }
  68. function test_connection() {
  69. $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) );
  70. if ( !Keyring_Util::is_error( $response ) )
  71. return true;
  72. return $response;
  73. }
  74. }
  75. add_action( 'keyring_load_services', array( 'Keyring_Service_TripIt', 'init' ) );