Making WordPress.org

Ticket #2907: 2907.1.diff

File 2907.1.diff, 8.1 KB (added by dipesh.kakadiya, 7 years ago)

Added snapbox setting

  • classes/tggr-media-source.php

    diff --git a/classes/tggr-media-source.php b/classes/tggr-media-source.php
    index d6b9d0b..738dd15 100755
    a b if ( ! class_exists( 'TGGRMediaSource' ) ) { 
    267267                        $class = get_called_class();
    268268                        $setting = str_replace( $class::SETTINGS_PREFIX, '', $field['label_for'] );
    269269                        $textarea_settings = array( 'highlighted_accounts', 'banned_accounts' );
     270                        $checkbox_settings = array( 'sandbox_mode' );
    270271
    271272                        require( dirname( __DIR__ ) . '/views/media-sources-common/page-settings-fields.php' );
    272273                }
  • classes/tggr-source-instagram.php

    diff --git a/classes/tggr-source-instagram.php b/classes/tggr-source-instagram.php
    index 4d67e84..46f1fe9 100755
    a b if ( ! class_exists( 'TGGRSourceInstagram' ) ) { 
    2929                 */
    3030                protected function __construct() {
    3131                        $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' );
     32                        $this->setting_names = array( 'Client ID', 'Client Secret', 'Redirect URL', 'Access Token', 'Sandbox Mode','Highlighted Accounts', 'Banned Accounts', '_newest_media_id' );
    3333
    3434                        foreach ( $this->setting_names as $key ) {
    3535                                $this->default_settings[ strtolower( str_replace( ' ', '_', $key ) ) ] = '';
    3636                        }
     37                        $this->default_settings[ 'sandbox_mode' ] = 1;
    3738                        $this->default_settings[ '_newest_media_id' ] = 0;
    3839
    3940                        $this->register_hook_callbacks();
    if ( ! class_exists( 'TGGRSourceInstagram' ) ) { 
    142143                        }
    143144                        $media = self::get_new_media(
    144145                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['client_id'],
     146                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['access_token'],
    145147                                $hashtag,
    146                                 TGGRSettings::get_instance()->settings[ __CLASS__ ]['_newest_media_id']
     148                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['_newest_media_id'],
     149                                TGGRSettings::get_instance()->settings[ __CLASS__ ]['sandbox_mode']
    147150                        );
    148151                        $media = $this->remove_banned_items( $media, 'user', 'username' );
    149152
    if ( ! class_exists( 'TGGRSourceInstagram' ) ) { 
    156159                 * @mvc Model
    157160                 *
    158161                 * @param string $client_id
     162                 * @param string $access_token
    159163                 * @param string $hashtag
    160164                 * @param string $max_id The ID of the most recent item that is already saved in the database
     165                 * @param string $sandbox_mode
    161166                 * @return mixed string|false
    162167                 */
    163                 protected static function get_new_media( $client_id, $hashtag, $max_id ) {
     168                protected static function get_new_media( $client_id, $access_token, $hashtag, $max_id, $sandbox_mode ) {
    164169                        $response = $media = false;
    165170
    166                         if ( $client_id && $hashtag ) {
    167                                 $url = sprintf(
    168                                         '%s/v1/tags/%s/media/recent?client_id=%s',
    169                                         self::API_URL,
    170                                         urlencode( str_replace( '#', '', $hashtag ) ),
    171                                         urlencode( $client_id )
    172                                 );
     171                        // url for SELF posts https://api.instagram.com/v1/users/self/media/recent/?access_token=XXXX
     172                        if ( $access_token && $hashtag ) {
     173
     174                                if ( $sandbox_mode === '0' ){
     175                                        // url for PUBLIC tags // https://api.instagram.com/v1/tags/XXXX/media/recent/?access_token=XXXX
     176                                        $url = sprintf(
     177                                                '%s/v1/tags/%s/media/recent?access_token=%s&count=6',
     178                                                self::API_URL,
     179                                                urlencode( str_replace( '#', '', $hashtag ) ),
     180                                                urlencode( $access_token )
     181                                        );
     182                                } else {
     183                                        // url for SELF posts https://api.instagram.com/v1/users/self/media/recent/?access_token=XXXX
     184                                        $url = sprintf(
     185                                                '%s/v1/users/self/media/recent?access_token=%s&count=6',
     186                                                self::API_URL,
     187                                                urlencode( $access_token )
     188                                        );
     189                                }
    173190
    174191                                $response = wp_remote_get( $url );
    175192                                $body     = json_decode( wp_remote_retrieve_body( $response ) );
  • views/media-sources-common/page-settings-fields.php

    diff --git a/views/media-sources-common/page-settings-fields.php b/views/media-sources-common/page-settings-fields.php
    index 1ef4a73..c4e0891 100755
    a b  
    88                echo esc_textarea( TGGRSettings::get_instance()->settings[ $class ][ $setting ] );
    99        ?></textarea>
    1010
     11<?php elseif ( in_array( $setting, $checkbox_settings, true ) ) : ?>
     12
     13    <input type="hidden" name="<?php echo esc_attr( Tagregator::PREFIX ); ?>settings[<?php echo esc_attr( $class ); ?>][<?php echo esc_attr( $setting ); ?>] ); ?>" value="0" />
     14    <input
     15            type="checkbox"
     16            id="<?php echo esc_attr( $class::SETTINGS_PREFIX . $setting ); ?>"
     17            name="<?php echo esc_attr( Tagregator::PREFIX ); ?>settings[<?php echo esc_attr( $class ); ?>][<?php echo esc_attr( $setting ); ?>] ); ?>"
     18            class="regular-text"
     19            <?php checked( '1',  esc_attr( TGGRSettings::get_instance()->settings[ $class ][ $setting ] )); ?>
     20            value="1"
     21    /><?php _e( 'Enabled' ); ?>
     22
    1123<?php else: ?>
    1224
    1325        <input
  • views/tggr-source-instagram/page-settings-section-header.php

    diff --git a/views/tggr-source-instagram/page-settings-section-header.php b/views/tggr-source-instagram/page-settings-section-header.php
    index 5f753b2..7f34b36 100755
    a b  
    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 'Save Changes'.</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 press Save Changes.</p>
     7<p></p>
     8<?php
     9$tggroptions = get_option( 'tggr_settings', array() );
     10$cid = $tggroptions['TGGRSourceInstagram']['client_id'];
     11$cse = $tggroptions['TGGRSourceInstagram']['client_secret'];
     12$cre = $tggroptions['TGGRSourceInstagram']['redirect_url'];
     13$fat = $tggroptions['TGGRSourceInstagram']['access_token'];
     14
     15if ( ! empty( $_GET['code'] ) ) {
     16        $icode = sanitize_text_field( $_GET['code'] );
     17} else {
     18        $icode = '';
     19}
     20
     21if ( $icode !== '' && $fat === '' ) {
     22
     23        $uri = 'https://api.instagram.com/oauth/access_token';
     24        $data = [
     25                'client_id' => $cid,
     26                'client_secret' => $cse,
     27                'grant_type' => 'authorization_code',
     28                'redirect_uri' => $cre,
     29                'code' => $icode,
     30        ];
     31
     32        $ch = curl_init();
     33
     34        curl_setopt( $ch, CURLOPT_URL, $uri ); // uri
     35        curl_setopt( $ch, CURLOPT_POST, true ); // POST
     36        curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); // POST DATA
     37        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // RETURN RESULT true
     38        curl_setopt( $ch, CURLOPT_HEADER, 0 ); // RETURN HEADER false
     39        curl_setopt( $ch, CURLOPT_NOBODY, 0 ); // NO RETURN BODY false / we need the body to return
     40        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); // VERIFY SSL HOST false
     41        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); // VERIFY SSL PEER false
     42
     43        $result = json_decode( curl_exec( $ch ), true ); // execute curl
     44
     45        $at = $result['access_token'];
     46
     47        if ( $at !== '' ) {
     48                ?>
     49        <script>
     50            (function( $ ) {
     51                $( document ).ready( function() {
     52                    $( '#tggr_instagram_access_token' ).val( '<?php echo $at; ?>' );
     53                });
     54            })( jQuery );
     55        </script>
     56                <?php
     57        }
     58}
     59?>
     60<script>
     61    (function( $ ) {
     62        $( document ).ready( function() {
     63            var redirect_url = '<?php echo $cre; ?>';
     64            if ( ! redirect_url ){
     65                redirect_url = window.location.href;
     66            }
     67            $( '#tggr_instagram_redirect_url' ).val(redirect_url);
     68            $( '#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');
     69        });
     70    })( jQuery );
     71</script>
     72 No newline at end of file