eventbrite.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Eventbrite service definition for Keyring.
  4. * https://developer.eventbrite.com/docs/
  5. */
  6. class Keyring_Service_Eventbrite extends Keyring_Service_OAuth2 {
  7. const NAME = 'eventbrite';
  8. const LABEL = 'Eventbrite';
  9. const API_BASE = 'https://www.eventbriteapi.com/v3/';
  10. const OAUTH_BASE = 'https://www.eventbrite.com/oauth/';
  11. function __construct() {
  12. parent::__construct();
  13. // Enable "basic" UI for entering key/secret
  14. if ( !KEYRING__HEADLESS_MODE ) {
  15. add_action( 'keyring_eventbrite_manage_ui', array( $this, 'basic_ui' ) );
  16. add_filter( 'keyring_eventbrite_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  17. }
  18. $creds = $this->get_credentials();
  19. $this->key = $creds['key'];
  20. $this->secret = $creds['secret'];
  21. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  22. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  23. $this->authorization_header = 'Bearer'; // Oh, you
  24. $this->authorization_parameter = false;
  25. $this->set_endpoint( 'authorize', self::OAUTH_BASE . 'authorize', 'GET' );
  26. $this->set_endpoint( 'access_token', self::OAUTH_BASE . 'token', 'POST' );
  27. $this->set_endpoint( 'self', self::API_BASE . 'users/me/', 'GET' );
  28. }
  29. function basic_ui_intro() {
  30. echo '<p>' . sprintf( __( 'To get started, <a href="%1$s">register an OAuth client on Evenbrite</a>. The most important setting is the <strong>OAuth Redirect URI</strong>, which should be set to <code>%2$s</code>. You can set the other values to whatever you like.', 'keyring' ), 'https://www.eventbrite.com/myaccount/apps/', Keyring_Util::admin_url( 'eventbrite', array( 'action' => 'verify' ) ) ) . '</p>';
  31. echo '<p>' . __( "Once you've saved those changes, copy the <strong>CLIENT/APPLICATION KEY</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.", 'keyring' ) . '</p>';
  32. }
  33. function build_token_meta( $token ) {
  34. $meta = array();
  35. if ( empty( $token['access_token'] ) ) {
  36. return $meta;
  37. }
  38. $this->set_token(
  39. new Keyring_Access_Token(
  40. $this->get_name(),
  41. $token['access_token'],
  42. array()
  43. )
  44. );
  45. $response = $this->request( $this->self_url, array( 'method' => $this->self_method ) );
  46. if ( !Keyring_Util::is_error( $response ) ) {
  47. $meta = array(
  48. 'user_id' => $response->id,
  49. 'name' => $response->name,
  50. );
  51. }
  52. return apply_filters( 'keyring_access_token_meta', $meta, 'eventbrite', $token, $response, $this );
  53. }
  54. function get_display( Keyring_Access_Token $token ) {
  55. return $token->get_meta( 'name' );
  56. }
  57. function parse_response( $response ) {
  58. return json_decode( $response );
  59. }
  60. function test_connection() {
  61. $res = $this->request( $this->self_url, array( 'method' => $this->self_method ) );
  62. if ( !Keyring_Util::is_error( $res ) ) {
  63. return true;
  64. }
  65. return $res;
  66. }
  67. }
  68. add_action( 'keyring_load_services', array( 'Keyring_Service_Eventbrite', 'init' ) );