moves.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Moves service definition for Keyring.
  4. * https://dev.moves-app.com/
  5. */
  6. class Keyring_Service_Moves extends Keyring_Service_OAuth2 {
  7. const NAME = 'moves';
  8. const LABEL = 'Moves';
  9. const SCOPE = 'activity location';
  10. function __construct() {
  11. parent::__construct();
  12. $this->place_types = array(
  13. 'unknown' => __( 'Unknown', 'keyring' ),
  14. 'home' => __( 'Home', 'keyring' ),
  15. 'work' => __( 'Work', 'keyring' ),
  16. 'school' => __( 'School', 'keyring' ),
  17. 'user' => __( 'Manually Named', 'keyring' ),
  18. 'foursquare' => __( 'Selected from foursquare', 'keyring' ),
  19. );
  20. $this->activity_types = array(
  21. 'wlk' => __( 'Walking', 'keyring' ),
  22. 'cyc' => __( 'Cycling', 'keyring' ),
  23. 'run' => __( 'Running', 'keyring' ),
  24. 'trp' => __( 'Transport', 'keyring' ),
  25. );
  26. // Enable "basic" UI for entering key/secret
  27. if ( ! KEYRING__HEADLESS_MODE ) {
  28. add_action( 'keyring_moves_manage_ui', array( $this, 'basic_ui' ) );
  29. add_filter( 'keyring_moves_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  30. }
  31. $this->set_endpoint( 'authorize', 'https://api.moves-app.com/oauth/v1/authorize', 'GET' );
  32. $this->set_endpoint( 'access_token', 'https://api.moves-app.com/oauth/v1/access_token', 'POST' );
  33. $this->set_endpoint( 'verify_token', 'https://api.moves-app.com/oauth/v1/tokeninfo', 'GET' );
  34. $this->set_endpoint( 'profile', 'https://api.moves-app.com/api/v1/user/profile', 'GET' );
  35. $creds = $this->get_credentials();
  36. $this->app_id = $creds['app_id'];
  37. $this->key = $creds['key'];
  38. $this->secret = $creds['secret'];
  39. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  40. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  41. $this->authorization_header = 'Bearer';
  42. $this->authorization_parameter = false;
  43. add_filter( 'keyring_moves_request_token_params', array( $this, 'request_token_params' ) );
  44. }
  45. function basic_ui_intro() {
  46. echo '<p>' . __( "Head over and <a href='https://dev.moves-app.com/apps/new'>create a new application</a> on Moves-app which you'll use to connect.", 'keyring' ) . '</p>';
  47. echo '<p>' . sprintf( __( "Once it's created, click the <strong>Development</strong> tab. Your <strong>App ID</strong> and <strong>API Key</strong> are both shown on that page as <strong>Client ID</strong>. Enter your <strong>Client secret</strong> in the <strong>API Secret</strong> box. On that tab there is also a <strong>Redirect URI</strong> box, which you should set to <code>%s</code>.", 'keyring' ), Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify' ) ) ) . '</p>';
  48. }
  49. function request_token_params( $params ) {
  50. $params['scope'] = apply_filters( 'keyring_moves_scope', self::SCOPE );
  51. return $params;
  52. }
  53. function build_token_meta( $token ) {
  54. $meta = array(
  55. 'user_id' => $token['user_id'],
  56. 'refresh_token' => $token['refresh_token'],
  57. 'expires' => time() + $token['expires_in'],
  58. '_classname' => get_called_class(),
  59. );
  60. $this->set_token(
  61. new Keyring_Access_Token(
  62. $this->get_name(),
  63. $token['access_token'],
  64. array()
  65. )
  66. );
  67. $response = $this->request( $this->profile_url );
  68. if ( !Keyring_Util::is_error( $response ) ) {
  69. $meta['first_date'] = $response->profile->firstDate;
  70. }
  71. return apply_filters( 'keyring_access_token_meta', $meta, self::NAME, $token, array(), $this );
  72. }
  73. function get_display( Keyring_Access_Token $token ) {
  74. return $token->get_meta( 'user_id' );
  75. }
  76. }
  77. add_action( 'keyring_load_services', array( 'Keyring_Service_Moves', 'init' ) );