twitter.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Twitter service definition for Keyring. Clean implementation of OAuth1
  4. */
  5. class Keyring_Service_Twitter extends Keyring_Service_OAuth1 {
  6. const NAME = 'twitter';
  7. const LABEL = 'Twitter';
  8. function __construct() {
  9. parent::__construct();
  10. // Enable "basic" UI for entering key/secret
  11. if ( ! KEYRING__HEADLESS_MODE ) {
  12. add_action( 'keyring_twitter_manage_ui', array( $this, 'basic_ui' ) );
  13. add_filter( 'keyring_twitter_basic_ui_intro', array( $this, 'basic_ui_intro' ) );
  14. }
  15. $this->authorization_header = true;
  16. $this->authorization_realm = "twitter.com";
  17. $this->set_endpoint( 'request_token', 'https://twitter.com/oauth/request_token', 'POST' );
  18. $this->set_endpoint( 'authorize', 'https://twitter.com/oauth/authorize', 'GET' );
  19. $this->set_endpoint( 'access_token', 'https://twitter.com/oauth/access_token', 'POST' );
  20. $this->set_endpoint( 'verify', 'https://api.twitter.com/1.1/account/verify_credentials.json', '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>' . sprintf( __( "If you haven't already, you'll need to <a href='https://dev.twitter.com/apps/new'>create an app on Twitter</a> (log in using your normal Twitter account). Make sure you enter something for the <strong>Callback URL</strong>, even though Keyring will override it with the correct value. Just enter your homepage, e.g. <code>%s</code>.", 'keyring' ), get_bloginfo( 'url' ) ) . '</p>';
  31. echo '<p>' . __( "Once you've created an app, copy and paste your <strong>Consumer key</strong> and <strong>Consumer secret</strong> (from under the <strong>OAuth settings</strong> section of your app's details) into the boxes below. You don't need an App ID for Twitter.", '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' => $token['user_id'],
  53. 'username' => $token['screen_name'],
  54. 'name' => $response->name,
  55. 'picture' => str_replace( '_normal.', '.', $response->profile_image_url ),
  56. '_classname' => get_called_class(),
  57. );
  58. }
  59. return apply_filters( 'keyring_access_token_meta', $meta, 'twitter', $token, $response, $this );
  60. }
  61. function get_display( Keyring_Access_Token $token ) {
  62. return '@' . $token->get_meta( 'username' );
  63. }
  64. function test_connection() {
  65. $res = $this->request( 'https://api.twitter.com/1.1/account/verify_credentials.json' );
  66. if ( !Keyring_Util::is_error( $res ) )
  67. return true;
  68. return $res;
  69. }
  70. }
  71. add_action( 'keyring_load_services', array( 'Keyring_Service_Twitter', 'init' ) );