tumblr.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Tumblr service definition for Keyring. Clean implementation of OAuth1
  4. */
  5. class Keyring_Service_Tumblr extends Keyring_Service_OAuth1 {
  6. const NAME = 'tumblr';
  7. const LABEL = 'Tumblr';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_tumblr_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_tumblr_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. }
  15. $this->set_endpoint( 'request_token', 'http://www.tumblr.com/oauth/request_token', 'POST' );
  16. $this->set_endpoint( 'authorize', 'http://www.tumblr.com/oauth/authorize', 'GET' );
  17. $this->set_endpoint( 'access_token', 'http://www.tumblr.com/oauth/access_token', 'POST' );
  18. $creds = $this->get_credentials();
  19. $this->app_id = $creds['app_id'];
  20. $this->key = $creds['key'];
  21. $this->secret = $creds['secret'];
  22. $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url );
  23. $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1;
  24. $this->authorization_header = true; // Send OAuth token in the header, not querystring
  25. $this->authorization_realm = 'tumblr.com';
  26. }
  27. function basic_ui_intro() {
  28. echo '<p>' . sprintf( __( 'To get started, <a href="%1$s">register an application with Tumblr</a>. The <strong>Default callback URL</strong> should be set to <code>%2$s</code>, and you can enter whatever you like in the other fields.', 'keyring' ), 'http://www.tumblr.com/oauth/register', Keyring_Util::admin_url( 'tumblr', array( 'action' => 'verify' ) ) ) . '</p>';
  29. echo '<p>' . __( "Once you've created your app, copy the <strong>OAuth Consumer Key</strong> into the <strong>API Key</strong> field below. Click the <strong>Show secret key</strong> link, and then copy the <strong>Secret Key</strong> value into the <strong>API Secret</strong> field below. You don't need an App ID value for Tumblr.", 'keyring' ) . '</p>';
  30. }
  31. function parse_response( $response ) {
  32. return json_decode( $response );
  33. }
  34. function build_token_meta( $token ) {
  35. // Set the token so that we can make requests using it
  36. $this->set_token(
  37. new Keyring_Access_Token(
  38. 'tumblr',
  39. new OAuthToken(
  40. $token['oauth_token'],
  41. $token['oauth_token_secret']
  42. )
  43. )
  44. );
  45. $response = $this->request( 'http://api.tumblr.com/v2/user/info', array( 'method' => 'POST' ) );
  46. if ( Keyring_Util::is_error( $response ) ) {
  47. $meta = array();
  48. } else {
  49. $this->person = $response->response->user;
  50. $meta = array(
  51. 'name' => $this->person->name,
  52. );
  53. }
  54. return apply_filters( 'keyring_access_token_meta', $meta, 'tumblr', $token, $response, $this );
  55. }
  56. function get_display( Keyring_Access_Token $token ) {
  57. return $token->get_meta( 'name' );
  58. }
  59. function test_connection() {
  60. $res = $this->request( 'http://api.tumblr.com/v2/user/info', array( 'method' => 'POST' ) );
  61. if ( !Keyring_Util::is_error( $res ) )
  62. return true;
  63. return $res;
  64. }
  65. }
  66. add_action( 'keyring_load_services', array( 'Keyring_Service_Tumblr', 'init' ) );