name = 'connect'; $this->slug = 'connect'; $this->type = 'extension'; $this->category = 'basic'; $this->public = false; $this->enabled = true; } /** * Initialize module */ public function initialize() { add_action( 'powerkit_plugin_activation', array( $this, 'activation' ) ); add_action( 'powerkit_plugin_deactivation', array( $this, 'deactivation' ) ); add_action( 'event_access_token_refresh', array( $this, 'cron_access_token_refresh' ) ); add_action( 'admin_menu', array( $this, 'register_options_page' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'wp_ajax_powerkit_reset_cache', array( $this, 'ajax_reset_cache' ) ); add_action( 'wp_ajax_nopriv_powerkit_reset_cache', array( $this, 'ajax_reset_cache' ) ); } /** * Plugin activation * * @param bool $networkwide The networkwide. */ public function activation( $networkwide ) { if ( ! wp_next_scheduled( 'event_access_token_refresh' ) ) { wp_schedule_event( time(), 'daily', 'event_access_token_refresh' ); } } /** * Plugin activation * * @param bool $networkwide The networkwide. */ public function deactivation( $networkwide ) { wp_clear_scheduled_hook( 'event_access_token_refresh' ); } /** * Register admin page * * @since 1.0.0 */ public function register_options_page() { add_options_page( esc_html__( 'Connect', 'powerkit' ), esc_html__( 'Connect', 'powerkit' ), 'manage_options', powerkit_get_page_slug( $this->slug ), array( $this, 'build_options_page' ) ); } /** * Build admin page * * @since 1.0.0 */ public function build_options_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'powerkit' ) ); } $this->logout_account(); $this->save_options_page(); $this->user_reset_cache(); ?>

instagram_custom_tab( $tab ); ?>
facebook_custom_tab( $tab ); ?>
twitter_custom_tab( $tab ); ?>


this article for details.', 'powerkit' ), 'https://help.instagram.com/399237934150902' ); } // Twitter. if ( 4301 === $status ) { $message = esc_html__( "Can't Get Request Token.", 'powerkit' ); } if ( 4302 === $status ) { $message = esc_html__( 'Something went wrong, try again or contact the plugin administrator.', 'powerkit' ); } return $message; } /** * Instagram custom tab * * @param string $tab The name of tab. */ public function instagram_custom_tab( $tab ) { $api_data = $this->api_get_data( array( 'instagram-connect', 'facebook-connect' ) ); // Update connect settigs. if ( is_array( $api_data ) && ! empty( $api_data ) ) { $ig_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null; $ig_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null; $ig_user_id = isset( $api_data['user_id'] ) && $api_data['user_id'] ? $api_data['user_id'] : null; $ig_username = isset( $api_data['username'] ) && $api_data['username'] ? $api_data['username'] : null; if ( ( 200 === $ig_status ) && $ig_access_token && $ig_user_id ) { update_option( 'powerkit_connect_instagram_app_type', 'instagram-connect' === $api_data['type'] ? 'personal' : 'business' ); update_option( 'powerkit_connect_instagram_app_access_token', $ig_access_token ); update_option( 'powerkit_connect_instagram_app_user_id', $ig_user_id ); update_option( 'powerkit_connect_instagram_app_username', $ig_username ? $ig_username : $ig_user_id ); update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) ); $this->location_reset_cache( 'instagram' ); } } // Notice. if ( isset( $ig_status ) && ( 200 === $ig_status || 201 === $ig_status ) ) { ?>

api_get_message( $ig_status ) ) ); ?>

api_get_message( $ig_status ) ) ); ?>

location_reset_cache( 'instagram' ); } elseif ( is_array( $ids_business ) && $ids_business ) { $form_business = true; ?>

200, 'type' => $ig_type, 'access_token' => $ig_access_token, 'user_id' => $item['id'], 'username' => $item['username'], ); // Generate marker. $data['marker'] = md5( uniqid( 'connect', true ) ); // Set API data. $api_data = base64_encode( json_encode( $data ) ); ?>

()

manually in the Manual Settings.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?>

powerkit_connect( 'instagram_app_id' ), 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_url' ) ), 'response_type' => 'code', 'scope' => 'user_profile,user_media', 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ), ), 'https://www.instagram.com/oauth/authorize' ); ?>


this link to learn more about linking your Instagram account to a Facebook page.', 'powerkit' ), 'https://help.instagram.com/399237934150902' ); ?>

powerkit_connect( 'instagram_app_fb_client_id' ), 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_fb_url' ) ), 'scope' => 'instagram_basic', 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ), ), 'https://www.facebook.com/dialog/oauth' ); ?>



this page.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?>

api_get_data( 'facebook-connect' ); // Update connect settigs. if ( is_array( $api_data ) && ! empty( $api_data ) && 'facebook-connect' === $api_data['type'] ) { $fb_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null; $fb_content = isset( $api_data['content'] ) && $api_data['content'] ? $api_data['content'] : null; $fb_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null; $fb_accounts = isset( $api_data['ids_accounts'] ) && $api_data['ids_accounts'] ? $api_data['ids_accounts'] : array(); if ( 200 === $fb_status && $fb_access_token ) { update_option( 'powerkit_connect_facebook_app_access_token', $fb_access_token ); update_option( 'powerkit_connect_facebook_app_accounts', $fb_accounts ); update_option( 'powerkit_connect_facebook_app_refresh', true ); $this->location_reset_cache( 'facebook' ); } } // Notice. if ( isset( $fb_status ) && ( 200 === $fb_status ) ) { ?>

api_get_message( $fb_status ) ) ); ?>

api_get_message( $fb_status ) ) ); ?>

%s', $account['username'] ); } else { $usernames[] = sprintf( '%s', $account['id'] ); } } if ( 1 === count( $usernames ) ) { ?>

this page.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?>

powerkit_connect( 'facebook_app_id' ), 'redirect_uri' => rawurlencode( powerkit_connect( 'facebook_app_url' ) ), 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ), ), 'https://www.facebook.com/dialog/oauth' ); ?>

api_get_data( 'twitter-connect' ); // Update connect settigs. if ( is_array( $api_data ) && ! empty( $api_data ) && 'twitter-connect' === $api_data['type'] ) { $tw_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null; $tw_user_id = isset( $api_data['user_id'] ) && $api_data['user_id'] ? $api_data['user_id'] : null; $tw_screen_name = isset( $api_data['screen_name'] ) && $api_data['screen_name'] ? $api_data['screen_name'] : null; $tw_oauth_token = isset( $api_data['oauth_token'] ) && $api_data['oauth_token'] ? $api_data['oauth_token'] : null; $tw_oauth_token_secret = isset( $api_data['oauth_token_secret'] ) && $api_data['oauth_token_secret'] ? $api_data['oauth_token_secret'] : null; $tw_consumer_key = isset( $api_data['consumer_key'] ) && $api_data['consumer_key'] ? $api_data['consumer_key'] : null; $tw_consumer_secret = isset( $api_data['consumer_secret'] ) && $api_data['consumer_secret'] ? $api_data['consumer_secret'] : null; if ( 200 === $tw_status && $tw_oauth_token && $tw_oauth_token_secret ) { update_option( 'powerkit_connect_twitter_app_consumer_key', $tw_consumer_key ); update_option( 'powerkit_connect_twitter_app_consumer_secret', $tw_consumer_secret ); update_option( 'powerkit_connect_twitter_app_user_id', $tw_user_id ); update_option( 'powerkit_connect_twitter_app_screen_name', $tw_screen_name ); update_option( 'powerkit_connect_twitter_app_oauth_token', $tw_oauth_token ); update_option( 'powerkit_connect_twitter_app_oauth_token_secret', $tw_oauth_token_secret ); $this->location_reset_cache( 'twitter' ); } } // Notice. if ( isset( $tw_status ) && ( 200 === $tw_status ) ) { ?>

api_get_message( $tw_status ) ) ); ?>

api_get_message( $tw_status ) ) ); ?>

()

this page.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?>

rawurlencode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ), ), powerkit_connect( 'twitter_app_url' ) ); ?>

%s

', esc_html__( 'The settings are saved.', 'powerkit' ) ); } if ( isset( $_POST['save_instagram_settings'] ) ) { // Input var ok. // Custom fields. if ( isset( $_POST['powerkit_connect_instagram_following'] ) ) { // Input var ok. update_option( 'powerkit_connect_instagram_following', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_following'] ) ) ); // Input var ok. } if ( isset( $_POST['powerkit_connect_instagram_custom_followers'] ) ) { // Input var ok. update_option( 'powerkit_connect_instagram_custom_followers', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_followers'] ) ) ); // Input var ok. } if ( isset( $_POST['powerkit_connect_instagram_custom_name'] ) ) { // Input var ok. update_option( 'powerkit_connect_instagram_custom_name', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_name'] ) ) ); // Input var ok. } if ( isset( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) { // Input var ok. update_option( 'powerkit_connect_instagram_custom_avatar', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) ); // Input var ok. } $this->location_reset_cache( 'instagram' ); printf( '

%s

', esc_html__( 'The settings are saved.', 'powerkit' ) ); } } /** * Logout account * * @since 1.0.0 */ public function logout_account() { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok. return; } if ( isset( $_POST['logout_account'] ) ) { // Input var ok. // Logout Instagram. if ( isset( $_POST['logout_account_type'] ) && 'instagram' === $_POST['logout_account_type'] ) { delete_option( 'powerkit_connect_instagram_app_type' ); delete_option( 'powerkit_connect_instagram_app_access_token' ); delete_option( 'powerkit_connect_instagram_app_user_id' ); delete_option( 'powerkit_connect_instagram_app_username' ); delete_option( 'powerkit_connect_instagram_app_refresh_time' ); } // Logout Facebook. if ( isset( $_POST['logout_account_type'] ) && 'facebook' === $_POST['logout_account_type'] ) { delete_option( 'powerkit_connect_facebook_app_access_token' ); delete_option( 'powerkit_connect_facebook_app_accounts' ); } // Logout Facebook. if ( isset( $_POST['logout_account_type'] ) && 'twitter' === $_POST['logout_account_type'] ) { delete_option( 'powerkit_connect_twitter_app_consumer_key' ); delete_option( 'powerkit_connect_twitter_app_consumer_secret' ); delete_option( 'powerkit_connect_twitter_app_user_id' ); delete_option( 'powerkit_connect_twitter_app_screen_name' ); delete_option( 'powerkit_connect_twitter_app_oauth_token' ); delete_option( 'powerkit_connect_twitter_app_oauth_token_secret' ); } if ( isset( $_POST['logout_account_type'] ) ) { $this->location_reset_cache( $_POST['logout_account_type'] ); } printf( '

%s

', esc_html__( 'Account disabled successfully.', 'powerkit' ) ); } } /** * Tab reset cache * * @param string $slug Slug of tab. */ public function location_reset_cache( $slug ) { // Base. $list = array( 'powerkit_social_follow', 'powerkit_social_links_counter', ); // Instagram. if ( 'instagram' === $slug ) { $list = array_merge( $list, array( 'powerkit_instagram_data', 'powerkit_instagram_recent', ) ); } // Twitter. if ( 'twitter' === $slug ) { $list = array_merge( $list, array( 'powerkit_twitter_data', 'powerkit_twitter_block_cache', 'powerkit_twitter_shortcode_cache', 'powerkit_twitter_widget_cache', ) ); } self::reset_cache( $list ); } /** * User reset cache * * @param bool $forcibly Force clear the cache. */ public function user_reset_cache( $forcibly = false ) { if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok. return; } if ( isset( $_POST['powerkit_reset_cache'] ) ) { // Input var ok. self::reset_cache(); } } /** * Ajax Reset cache */ public function ajax_reset_cache() { powerkit_uuid_hash(); // Check wpnonce. if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok. return; } $list = apply_filters( 'powerkit_ajax_reset_cache', array() ); if ( ! isset( $_REQUEST['page'] ) ) { // Input var ok. return false; } $page = sanitize_key( $_REQUEST['page'] ); // Input var ok; sanitization ok. if ( ! isset( $list[ $page ] ) ) { return false; } self::reset_cache( $list[ $page ] ); die(); } /** * Cron refresh access token */ public function cron_access_token_refresh() { self::access_token_refresh( 'instagram' ); } /** * Refresh access token * * @param string $type The type. * @param bool $should_attempt_refresh The refresh. */ public static function access_token_refresh( $type, $should_attempt_refresh = false ) { if ( 'instagram' === $type && 'personal' === powerkit_connect( 'instagram_app_type' ) ) { if ( (int) get_option( 'powerkit_connect_instagram_app_refresh_time' ) < time() ) { $should_attempt_refresh = true; } if ( $should_attempt_refresh ) { update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) ); $remote_link = add_query_arg( array( 'grant_type' => 'ig_refresh_token', 'access_token' => powerkit_connect( 'instagram_app_access_token' ), ), 'https://graph.instagram.com/refresh_access_token' ); $response = wp_safe_remote_get( $remote_link, array( 'timeout' => 3, ) ); $response = wp_remote_retrieve_body( $response ); $data = json_decode( $response, true ); // Update settings. if ( isset( $data['access_token'] ) ) { update_option( 'powerkit_connect_instagram_app_access_token', $data['access_token'] ); } } } } /** * Reset cache * * @param array $list Reset list. */ public static function reset_cache( $list = false ) { if ( is_array( $list ) ) { $list = $list; } elseif ( is_string( $list ) && $list ) { $list = explode( ' ', $list ); } else { $list = apply_filters( 'powerkit_reset_cache', array() ); $puck = array(); foreach ( $list as $item ) { if ( is_array( $item ) ) { $puck = array_merge( $puck, $item ); } elseif ( is_string( $item ) ) { $puck = array_merge( $puck, explode( ' ', $item ) ); } } $list = $puck; } if ( $list ) { global $wpdb; foreach ( $list as $option_name ) { $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok. } printf( '

%s

', esc_html__( 'Cache purged.', 'powerkit' ) ); } } /** * Register the stylesheets and JavaScript for the admin area. * * @param string $page Current page. */ public function enqueue_scripts( $page ) { if ( is_customize_preview() || 'toplevel_page_powerkit_manager' === $page ) { wp_enqueue_script( 'admin-powerkit-connect', plugin_dir_url( __FILE__ ) . 'js/admin-powerkit-connect.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false ); wp_localize_script( 'admin-powerkit-connect', 'pk_connect', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), ) ); } } } new Powerkit_Connect(); }