Making WordPress.org

Ticket #2907: 2907.3.diff

File 2907.3.diff, 8.3 KB (added by xkon, 7 years ago)

cURL changed to wp_remote_post

  • classes/tggr-media-source.php

     
    267267                        $class = get_called_class();
    268268                        $setting = str_replace( $class::SETTINGS_PREFIX, '', $field['label_for'] );
    269269                        $textarea_settings = array( 'highlighted_accounts', 'banned_accounts' );
    270 
     270                        $checkbox_settings = array( 'sandbox_mode' );
    271271                        require( dirname( __DIR__ ) . '/views/media-sources-common/page-settings-fields.php' );
    272272                }
    273273
  • classes/tggr-source-instagram.php

     
    11<?php
    22
    3 // todo Instagram requires oAuth now, so this is broken == https://wordpress.org/support/topic/instagram-issuses/#post-8425118
    4 
    53if ( $_SERVER['SCRIPT_FILENAME'] == __FILE__ )
    64        die( 'Access denied.' );
    75
     
    2927                 */
    3028                protected function __construct() {
    3129                        $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' );
    3331
    3432                        foreach ( $this->setting_names as $key ) {
    3533                                $this->default_settings[ strtolower( str_replace( ' ', '_', $key ) ) ] = '';
    3634                        }
     35
     36                        $this->default_settings[ 'sandbox_mode' ] = 1;
    3737                        $this->default_settings[ '_newest_media_id' ] = 0;
    3838
    3939                        $this->register_hook_callbacks();
     
    136136                 * @param string $hashtag
    137137                 */
    138138                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                        }
    139143                        $media = self::get_new_media(
    140144                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['client_id'],
     145                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['access_token'],
    141146                                $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']
    143149                        );
    144150                        $media = $this->remove_banned_items( $media, 'user', 'username' );
    145151
     
    152158                 * @mvc Model
    153159                 *
    154160                 * @param string $client_id
     161                 * @param string $access_token
    155162                 * @param string $hashtag
     163                 * @param string $sandbox_mode
    156164                 * @param string $max_id The ID of the most recent item that is already saved in the database
    157165                 * @return mixed string|false
    158166                 */
    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 ) {
    160168                        $response = $media = false;
    161169
    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 ) {
    169171
     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
    170189                                $response = wp_remote_get( $url );
    171190                                $body     = json_decode( wp_remote_retrieve_body( $response ) );
    172191
     
    175194                                }
    176195                        }
    177196
    178                         self::log( __METHOD__, 'Results', compact( 'client_id', 'hashtag', 'max_id', 'response' ) );
     197//                      self::log( __METHOD__, 'Results', compact( 'access_token', 'hashtag', 'max_id', 'response' ) );
    179198
    180199                        return $media;
    181200                }
  • views/media-sources-common/page-settings-fields.php

     
    88                echo esc_textarea( TGGRSettings::get_instance()->settings[ $class ][ $setting ] );
    99        ?></textarea>
    1010
     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
    1122<?php else: ?>
    1223
    1324        <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 &amp; 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
     18if ( ! empty( $_GET['code'] ) ) {
     19        $icode = sanitize_text_field( $_GET['code'] );
     20} else {
     21        $icode = '';
     22}
     23
     24if ( $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