Ticket #2907: 2907.3.diff
File 2907.3.diff, 8.3 KB (added by , 7 years ago) |
---|
-
classes/tggr-media-source.php
267 267 $class = get_called_class(); 268 268 $setting = str_replace( $class::SETTINGS_PREFIX, '', $field['label_for'] ); 269 269 $textarea_settings = array( 'highlighted_accounts', 'banned_accounts' ); 270 270 $checkbox_settings = array( 'sandbox_mode' ); 271 271 require( dirname( __DIR__ ) . '/views/media-sources-common/page-settings-fields.php' ); 272 272 } 273 273 -
classes/tggr-source-instagram.php
1 1 <?php 2 2 3 // todo Instagram requires oAuth now, so this is broken == https://wordpress.org/support/topic/instagram-issuses/#post-84251184 5 3 if ( $_SERVER['SCRIPT_FILENAME'] == __FILE__ ) 6 4 die( 'Access denied.' ); 7 5 … … 29 27 */ 30 28 protected function __construct() { 31 29 $this->view_folder = dirname( __DIR__ ) . '/views/'. str_replace( '.php', '', basename( __FILE__ ) ); 32 $this->setting_names = array( 'Client ID', ' Highlighted Accounts', 'Banned Accounts', '_newest_media_id' );30 $this->setting_names = array( 'Client ID', 'Client Secret', 'Redirect URL', 'Access Token', 'Sandbox Mode', 'Highlighted Accounts', 'Banned Accounts', '_newest_media_id' ); 33 31 34 32 foreach ( $this->setting_names as $key ) { 35 33 $this->default_settings[ strtolower( str_replace( ' ', '_', $key ) ) ] = ''; 36 34 } 35 36 $this->default_settings[ 'sandbox_mode' ] = 1; 37 37 $this->default_settings[ '_newest_media_id' ] = 0; 38 38 39 39 $this->register_hook_callbacks(); … … 136 136 * @param string $hashtag 137 137 */ 138 138 public function import_new_items( $hashtag ) { 139 if ( empty( TGGRSettings::get_instance()->settings[ __CLASS__ ]['client_id'] ) 140 || empty( TGGRSettings::get_instance()->settings[ __CLASS__ ]['access_token'] ) ){ 141 return; 142 } 139 143 $media = self::get_new_media( 140 144 TGGRSettings::get_instance()->settings[ __CLASS__ ]['client_id'], 145 TGGRSettings::get_instance()->settings[ __CLASS__ ]['access_token'], 141 146 $hashtag, 142 TGGRSettings::get_instance()->settings[ __CLASS__ ]['_newest_media_id'] 147 TGGRSettings::get_instance()->settings[ __CLASS__ ]['_newest_media_id'], 148 TGGRSettings::get_instance()->settings[ __CLASS__ ]['sandbox_mode'] 143 149 ); 144 150 $media = $this->remove_banned_items( $media, 'user', 'username' ); 145 151 … … 152 158 * @mvc Model 153 159 * 154 160 * @param string $client_id 161 * @param string $access_token 155 162 * @param string $hashtag 163 * @param string $sandbox_mode 156 164 * @param string $max_id The ID of the most recent item that is already saved in the database 157 165 * @return mixed string|false 158 166 */ 159 protected static function get_new_media( $client_id, $ hashtag, $max_id) {167 protected static function get_new_media( $client_id, $access_token, $hashtag, $max_id, $sandbox_mode ) { 160 168 $response = $media = false; 161 169 162 if ( $client_id && $hashtag ) { 163 $url = sprintf( 164 '%s/v1/tags/%s/media/recent?client_id=%s', 165 self::API_URL, 166 urlencode( str_replace( '#', '', $hashtag ) ), 167 urlencode( $client_id ) 168 ); 170 if ( $access_token && $hashtag ) { 169 171 172 if ( $sandbox_mode === '0' ){ 173 // url for PUBLIC tags // https://api.instagram.com/v1/tags/XXXX/media/recent/?access_token=XXXX 174 $url = sprintf( 175 '%s/v1/tags/%s/media/recent?access_token=%s&count=9', 176 self::API_URL, 177 urlencode( str_replace( '#', '', $hashtag ) ), 178 urlencode( $access_token ) 179 ); 180 } else { 181 // url for SELF posts https://api.instagram.com/v1/users/self/media/recent/?access_token=XXXX 182 $url = sprintf( 183 '%s/v1/users/self/media/recent?access_token=%s&count=9', 184 self::API_URL, 185 urlencode( $access_token ) 186 ); 187 } 188 170 189 $response = wp_remote_get( $url ); 171 190 $body = json_decode( wp_remote_retrieve_body( $response ) ); 172 191 … … 175 194 } 176 195 } 177 196 178 self::log( __METHOD__, 'Results', compact( 'client_id', 'hashtag', 'max_id', 'response' ) );197 // self::log( __METHOD__, 'Results', compact( 'access_token', 'hashtag', 'max_id', 'response' ) ); 179 198 180 199 return $media; 181 200 } -
views/media-sources-common/page-settings-fields.php
8 8 echo esc_textarea( TGGRSettings::get_instance()->settings[ $class ][ $setting ] ); 9 9 ?></textarea> 10 10 11 <?php elseif ( in_array( $setting, $checkbox_settings, true ) ) : ?> 12 <input type="hidden" name="<?php echo esc_attr( Tagregator::PREFIX ); ?>settings[<?php echo esc_attr( $class ); ?>][<?php echo esc_attr( $setting ); ?>] ); ?>" value="0" /> 13 <input 14 type="checkbox" 15 id="<?php echo esc_attr( $class::SETTINGS_PREFIX . $setting ); ?>" 16 name="<?php echo esc_attr( Tagregator::PREFIX ); ?>settings[<?php echo esc_attr( $class ); ?>][<?php echo esc_attr( $setting ); ?>] ); ?>" 17 class="regular-text" 18 <?php checked( '1', esc_attr( TGGRSettings::get_instance()->settings[ $class ][ $setting ] )); ?> 19 value="1"/> 20 <?php _e( 'Enabled' ); ?> 21 11 22 <?php else: ?> 12 23 13 24 <input -
views/tggr-source-instagram/page-settings-section-header.php
1 <p>You can obtain the Client ID by logging into <a href="https://www.instagram.com/developer/">Instagram's developer portal</a>, and then registering a new client.</p> 1 <p>Instructions:</p> 2 <p>1. You can obtain the Client ID & Client Secret by logging into <a href="https://www.instagram.com/developer/">Instagram's developer portal</a>, and then registering a new client. Insert them to the fields bellow and click <strong>'Save Changes'</strong>.</p> 3 <p></p> 4 <p>2. Copy the Redirect URL from the field below and paste it in your <strong>Valid redirect URIs</strong> field in your Instagram API Client Settings.</p> 5 <p></p> 6 <p>3. <a href="" id="get_access_token">Click here to get your Access Token!</a> - After the Access Token is in the field please click <strong>'Save Changes'</strong>.</p> 7 <p></p> 8 <p><strong>Note:</strong> Sandbox mode will retrieve your account's 9 latest posts ignoring the #hashtag. Non-sandbox will retrieve the latest hashtags posts from all instagram as long as there is permission for 'public_content' in your client.</p> 9 <?php 10 11 $tggroptions = get_option( 'tggr_settings', array() ); 12 13 $cid = $tggroptions['TGGRSourceInstagram']['client_id']; 14 $cse = $tggroptions['TGGRSourceInstagram']['client_secret']; 15 $cre = $tggroptions['TGGRSourceInstagram']['redirect_url']; 16 $fat = $tggroptions['TGGRSourceInstagram']['access_token']; 17 18 if ( ! empty( $_GET['code'] ) ) { 19 $icode = sanitize_text_field( $_GET['code'] ); 20 } else { 21 $icode = ''; 22 } 23 24 if ( $icode !== '' && $fat === '' ) { 25 26 $url = 'https://api.instagram.com/oauth/access_token'; 27 28 $response = wp_remote_post( $url, array( 29 'method' => 'POST', 30 'timeout' => 45, 31 'body' => array( 32 'client_id' => $cid, 33 'client_secret' => $cse, 34 'grant_type' => 'authorization_code', 35 'redirect_uri' => $cre, 36 'code' => $icode, 37 ), 38 ) 39 ); 40 if ( is_wp_error( $response ) ) { 41 $error_message = $response->get_error_message(); 42 echo "Something went wrong: $error_message"; 43 } else { 44 $decode_response = json_decode($response['body'], true); 45 46 $at = $decode_response['access_token']; 47 } 48 49 if ( $at !== '' ) { 50 ?> 51 <script> 52 (function( $ ) { 53 $( document ).ready( function() { 54 $( '#tggr_instagram_access_token' ).val( '<?php echo $at; ?>' ); 55 }); 56 })( jQuery ); 57 </script> 58 <?php 59 } 60 } 61 ?> 62 <script> 63 (function( $ ) { 64 $( document ).ready( function() { 65 $( '#tggr_instagram_redirect_url' ).val(window.location.href); 66 $( '#get_access_token' ).attr('href', 'https://www.instagram.com/oauth/authorize/?client_id=' + $('#tggr_instagram_client_id').val() + '&redirect_uri=' + $( '#tggr_instagram_redirect_url' ).val() + '&response_type=code'); 67 }); 68 })( jQuery ); 69 </script> 70 No newline at end of file