Ticket #22422: keys-from-api.patch
| File keys-from-api.patch, 6.2 KB (added by , 13 years ago) |
|---|
-
tumblr-importer.php
49 49 function __construct() { 50 50 add_action( 'tumblr_importer_metadata', array( $this, 'tumblr_importer_metadata' ) ); 51 51 add_filter( 'tumblr_importer_format_post', array( $this, 'filter_format_post' ) ); 52 add_filter( 'tumblr_importer_get_consumer_key', array( $this, 'get_consumer_key' ) );53 52 add_filter( 'wp_insert_post_empty_content', array( $this, 'filter_allow_empty_content' ), 10, 2 ); 54 53 parent::__construct(); 55 54 } … … 60 59 $this->restart(); 61 60 62 61 if ( !isset($this->error) ) $this->error = null; 62 63 $this->set_tumblr_keys(); 63 64 64 $this->consumerkey = defined ('TUMBLR_CONSUMER_KEY') ? TUMBLR_CONSUMER_KEY : ( !empty($_POST['consumerkey']) ? $_POST['consumerkey'] : $this->consumerkey );65 $this->secretkey = defined ('TUMBLR_SECRET_KEY') ? TUMBLR_SECRET_KEY : ( !empty($_POST['secretkey']) ? $_POST['secretkey'] : $this->secretkey );66 67 65 // if we have access tokens, verify that they work 68 66 if ( !empty( $this->access_tokens ) ) { 69 67 // TODO 70 68 } else if ( isset( $_GET['oauth_verifier'] ) ) { 71 69 $this->check_permissions(); 72 } else if ( !empty( $this->consumer key ) && !empty( $this->secretkey ) ) {70 } else if ( !empty( $this->consumer_key ) && !empty( $this->secret_key ) ) { 73 71 $this->check_credentials(); 74 72 } 75 73 if ( isset( $_POST['blogurl'] ) ) { … … 123 121 <form action='?import=tumblr' method='post'> 124 122 <table class="form-table"> 125 123 <tr> 126 <th scope="row"><label for='consumer key'><?php _e('OAuth Consumer Key:','tumblr-importer'); ?></label></label></th>127 <td><input type='text' class="regular-text" name='consumer key' value='<?php if (isset($this->consumerkey)) echo esc_attr($this->consumerkey); ?>' /></td>124 <th scope="row"><label for='consumer_key'><?php _e('OAuth Consumer Key:','tumblr-importer'); ?></label></label></th> 125 <td><input type='text' class="regular-text" name='consumer_key' value='<?php if (isset($this->consumer_key)) echo esc_attr($this->consumer_key); ?>' /></td> 128 126 </tr> 129 127 <tr> 130 <th scope="row"><label for='secret key'><?php _e('Secret Key:','tumblr-importer'); ?></label></label></th>131 <td><input type='text' class="regular-text" name='secret key' value='<?php if (isset($this->secretkey)) echo esc_attr($this->secretkey); ?>' /></td>128 <th scope="row"><label for='secret_key'><?php _e('Secret Key:','tumblr-importer'); ?></label></label></th> 129 <td><input type='text' class="regular-text" name='secret_key' value='<?php if (isset($this->secret_key)) echo esc_attr($this->secret_key); ?>' /></td> 132 130 </tr> 133 131 </table> 134 132 <p class='submit'> … … 172 170 // get the access_tokens 173 171 $url = 'http://www.tumblr.com/oauth/access_token'; 174 172 175 $params = array('oauth_consumer_key' => $this->consumer key,173 $params = array('oauth_consumer_key' => $this->consumer_key, 176 174 "oauth_nonce" => time().rand(), 177 175 "oauth_timestamp" => time(), 178 176 "oauth_token" => $this->request_tokens['oauth_token'], … … 181 179 "oauth_version" => "1.0", 182 180 ); 183 181 184 $params['oauth_signature'] = $this->oauth_signature(array($this->secret key,$this->request_tokens['oauth_token_secret']), 'GET', $url, $params);182 $params['oauth_signature'] = $this->oauth_signature(array($this->secret_key,$this->request_tokens['oauth_token_secret']), 'GET', $url, $params); 185 183 186 184 $url = add_query_arg( array_map('urlencode', $params), $url); 187 185 $response = wp_remote_get( $url ); … … 670 668 * 671 669 */ 672 670 function oauth_get_request_token() { 673 if ( empty($this->consumer key) || empty ($this->secretkey) )671 if ( empty($this->consumer_key) || empty ($this->secret_key) ) 674 672 return false; 675 673 676 674 $url = 'http://www.tumblr.com/oauth/request_token'; 677 675 678 676 $params = array('oauth_callback' => urlencode( self_admin_url('admin.php?import=tumblr') ), 679 'oauth_consumer_key' => $this->consumer key,677 'oauth_consumer_key' => $this->consumer_key, 680 678 "oauth_version" => "1.0", 681 679 "oauth_nonce" => time(), 682 680 "oauth_timestamp" => time(), 683 681 "oauth_signature_method" => "HMAC-SHA1", 684 682 ); 685 683 686 $params['oauth_signature'] = $this->oauth_signature(array($this->secret key,''), 'POST', $url, $params);684 $params['oauth_signature'] = $this->oauth_signature(array($this->secret_key,''), 'POST', $url, $params); 687 685 688 686 $response = wp_remote_post( $url, array('body' => $params)); 689 687 … … 730 728 return $this->blogs; 731 729 } 732 730 733 function get_consumer_key() { 734 return $this->consumerkey; 731 function set_tumblr_keys() { 732 global $wp_version; 733 734 $keys = get_site_transient( 'tumblr_consumer_key' ); 735 if ( ! $keys ) { 736 //$url = 'http://api.wordpress.org/core/importers/1.0/'; 737 $url = 'http://oddbird.org/tumblr/'; 738 $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() ); 739 $keys = maybe_unserialize( wp_remote_retrieve_body( wp_remote_get( $url, $options ) ) ); 740 741 if ( is_array( $keys ) ) 742 set_site_transient( 'tumblr_consumer_key', $keys, 1 * DAY_IN_SECONDS ); 743 else 744 return false; 745 } 746 $this->consumer_key = $keys['consumer_key']; 747 $this->secret_key = $keys['secret_key']; 735 748 } 736 749 737 750 /** … … 754 767 $params = array( 755 768 'offset' => $start, 756 769 'limit' => $count, 757 'api_key' => apply_filters( 'tumblr_importer_get_consumer_key', '' ),770 'api_key' => $this->consumer_key, 758 771 ); 759 772 $url = add_query_arg( $params, $url ); 760 773 } … … 978 991 if ( empty( $this->access_tokens ) ) 979 992 return false; 980 993 981 $params = array('oauth_consumer_key' => $this-> get_consumer_key(),994 $params = array('oauth_consumer_key' => $this->consumer_key, 982 995 "oauth_nonce" => time(), 983 996 "oauth_timestamp" => time(), 984 997 "oauth_token" => $this->access_tokens['oauth_token'], … … 986 999 "oauth_version" => "1.0", 987 1000 ); 988 1001 989 $params['oauth_signature'] = $this->oauth_signature(array($this->secret key,$this->access_tokens['oauth_token_secret']), 'GET', $url, $params);1002 $params['oauth_signature'] = $this->oauth_signature(array($this->secret_key,$this->access_tokens['oauth_token_secret']), 'GET', $url, $params); 990 1003 991 1004 $url = add_query_arg( array_map('urlencode', $params), $url); 992 1005