| @@ -1,202 +1,146 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Getwid\Blocks; | |
| 4 | - | |
| 5 | -use Getwid\Settings; | |
| 6 | - | |
| 7 | -class Instagram extends \Getwid\Blocks\AbstractBlock { | |
| 8 | - | |
| 9 | - protected static $blockName = 'getwid/instagram'; | |
| 10 | - | |
| 11 | - public function __construct() { | |
| 12 | - | |
| 13 | - parent::__construct( self::$blockName ); | |
| 14 | - | |
| 15 | - add_action( 'wp_ajax_get_instagram_token', [ $this, 'get_instagram_token'] ); | |
| 16 | - | |
| 17 | - register_block_type( | |
| 18 | - 'getwid/instagram', | |
| 19 | - array( | |
| 20 | - 'attributes' => array( | |
| 21 | - 'photoCount' => array( | |
| 22 | - 'type' => 'number', | |
| 23 | - 'default' => 6 | |
| 24 | - ), | |
| 25 | - 'gridColumns' => array( | |
| 26 | - 'type' => 'number', | |
| 27 | - 'default' => 3 | |
| 28 | - ), | |
| 29 | - 'spacing' => array( | |
| 30 | - 'type' => 'string', | |
| 31 | - 'default' => 'default' | |
| 32 | - ), | |
| 33 | - 'align' => array( | |
| 34 | - 'type' => 'string' | |
| 35 | - ), | |
| 36 | - 'className' => array( | |
| 37 | - 'type' => 'string' | |
| 38 | - ), | |
| 39 | - ), | |
| 40 | - 'render_callback' => [ $this, 'render_callback' ] | |
| 41 | - ) | |
| 42 | - ); | |
| 43 | - | |
| 44 | - getwid_maybe_add_option( 'getwid_instagram_token', '', true ); | |
| 45 | - | |
| 46 | - if ( $this->isEnabled() ) { | |
| 47 | - add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] ); | |
| 48 | - } | |
| 49 | - } | |
| 50 | - | |
| 51 | - public function getLabel() { | |
| 52 | - return __('Instagram', 'getwid'); | |
| 53 | - } | |
| 54 | - | |
| 55 | - public function get_instagram_token() { | |
| 56 | - | |
| 57 | - check_ajax_referer( 'getwid_nonce_get_instagram_token', 'nonce' ); | |
| 58 | - | |
| 59 | - $action = sanitize_text_field( wp_unslash( $_POST[ 'option' ] ) ); | |
| 60 | - | |
| 61 | - $response = false; | |
| 62 | - if ( $action == 'get' ) { | |
| 63 | - $response = get_option( 'getwid_instagram_token', '' ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - wp_send_json_success( $response ); | |
| 67 | - } | |
| 68 | - | |
| 69 | - public function block_frontend_styles($styles) { | |
| 70 | - | |
| 71 | - return $styles; | |
| 72 | - } | |
| 73 | - | |
| 74 | - public function block_frontend_assets() { | |
| 75 | - | |
| 76 | - if ( is_admin() ) { | |
| 77 | - return; | |
| 78 | - } | |
| 79 | - | |
| 80 | - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { | |
| 81 | - return; | |
| 82 | - } | |
| 83 | - | |
| 84 | - $rtl = is_rtl() ? '.rtl' : ''; | |
| 85 | - | |
| 86 | - wp_enqueue_style( | |
| 87 | - self::$blockName, | |
| 88 | - getwid_get_plugin_url( 'assets/blocks/instagram/style' . $rtl . '.css' ), | |
| 89 | - [], | |
| 90 | - getwid()->settings()->getVersion() | |
| 91 | - ); | |
| 92 | - | |
| 93 | - } | |
| 94 | - | |
| 95 | - public function render_callback( $attributes ) { | |
| 96 | - $error = false; | |
| 97 | - $empty = false; | |
| 98 | - | |
| 99 | - //Get Access Token | |
| 100 | - $access_token = get_option( 'getwid_instagram_token' ); | |
| 101 | - | |
| 102 | - //If Empty Token | |
| 103 | - if ( empty($access_token) ) { | |
| 104 | - if ( current_user_can('manage_options') ) { | |
| 105 | - return '<p>' . sprintf( | |
| 106 | - //translators: %s is a link | |
| 107 | - __( 'Instagram Access Token is not set. <a href="%s">Connect Instagram Account</a>.', 'getwid' ), | |
| 108 | - esc_url( getwid()->settingsPage()->getTabUrl('general') ) ) . '</p>'; | |
| 109 | - } else { | |
| 110 | - return ''; | |
| 111 | - } | |
| 112 | - } | |
| 113 | - | |
| 114 | - $instagram_media = get_transient( 'getwid_instagram_response_data' ); | |
| 115 | - | |
| 116 | - if ( false === $instagram_media ) { | |
| 117 | - | |
| 118 | - $api_uri = 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,permalink,caption,thumbnail_url&access_token=' . $access_token . '&limit=100'; | |
| 119 | - | |
| 120 | - //Get data from Instagram | |
| 121 | - $response = wp_remote_get( | |
| 122 | - $api_uri, | |
| 123 | - array( 'timeout' => 15 ) | |
| 124 | - ); | |
| 125 | - | |
| 126 | - if ( is_wp_error( $response ) ) { | |
| 127 | - if ( current_user_can('manage_options') ){ | |
| 128 | - return '<p>' . $response->get_error_message() . '</p>'; | |
| 129 | - } else { | |
| 130 | - return ''; | |
| 131 | - } | |
| 132 | - } else { | |
| 133 | - $instagram_media = json_decode( wp_remote_retrieve_body( $response ), false ); | |
| 134 | - | |
| 135 | - //JSON valid | |
| 136 | - if ( json_last_error() === JSON_ERROR_NONE ) { | |
| 137 | - if ( isset($instagram_media->data) ) { | |
| 138 | - | |
| 139 | - //Cache response | |
| 140 | - $expiration = intval( get_option( 'getwid_instagram_cache_timeout', 30 ) ); | |
| 141 | - | |
| 142 | - set_transient( 'getwid_instagram_response_data', $instagram_media, $expiration * MINUTE_IN_SECONDS ); | |
| 143 | - | |
| 144 | - } else { | |
| 145 | - if ( current_user_can( 'manage_options' ) ) { | |
| 146 | - return '<p>' . $instagram_media->error->message . '</p>'; | |
| 147 | - } else { | |
| 148 | - return ''; | |
| 149 | - } | |
| 150 | - } | |
| 151 | - } else { | |
| 152 | - return __( 'Error in json_decode.', 'getwid' ); | |
| 153 | - } | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | - $class = $block_name = 'wp-block-getwid-instagram'; | |
| 158 | - | |
| 159 | - if ( isset( $attributes[ 'align' ] ) ) { | |
| 160 | - $class .= ' align' . $attributes[ 'align' ]; | |
| 161 | - } | |
| 162 | - | |
| 163 | - $wrapper_class = 'wp-block-getwid-instagram__wrapper'; | |
| 164 | - $wrapper_class .= " has-" . $attributes[ 'gridColumns' ] . "-columns"; | |
| 165 | - | |
| 166 | - if ( isset( $attributes[ 'spacing' ] ) && $attributes[ 'spacing' ] != 'default' ) { | |
| 167 | - $class .= ' has-spacing-' . $attributes[ 'spacing' ]; | |
| 168 | - } | |
| 169 | - | |
| 170 | - if ( isset( $attributes[ 'className' ] ) ) { | |
| 171 | - $class .= ' ' . $attributes[ 'className' ]; | |
| 172 | - } | |
| 173 | - | |
| 174 | - ob_start(); | |
| 175 | - ?><div class="<?php echo esc_attr( $class ); ?>"> | |
| 176 | - <div class="<?php echo esc_attr( $wrapper_class );?>"> | |
| 177 | - <?php | |
| 178 | - $counter = 1; | |
| 179 | - foreach ( $instagram_media->data as $key => $value ) { | |
| 180 | - if ( $counter <= $attributes[ 'photoCount' ] ) { | |
| 181 | - $extra_attr = array( | |
| 182 | - 'block_name' => $block_name, | |
| 183 | - 'post' => $value | |
| 184 | - ); | |
| 185 | - getwid_get_template_part( 'instagram/post', $attributes, false, $extra_attr ); | |
| 186 | - } | |
| 187 | - $counter ++; | |
| 188 | - } // end foreach | |
| 189 | - ?></div> | |
| 190 | - </div><?php | |
| 191 | - | |
| 192 | - $result = ob_get_clean(); | |
| 193 | - | |
| 194 | - $this->block_frontend_assets(); | |
| 195 | - | |
| 196 | - return $result; | |
| 197 | - } | |
| 198 | -} | |
| 199 | - | |
| 200 | -getwid()->blocksManager()->addBlock( | |
| 201 | - new \Getwid\Blocks\Instagram() | |
| 202 | -); | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Getwid\Blocks; | |
| 4 | + | |
| 5 | +use Getwid\StringEncryption; | |
| 6 | + | |
| 7 | +class Instagram extends AbstractBlock { | |
| 8 | + | |
| 9 | + public function __construct() { | |
| 10 | + | |
| 11 | + parent::__construct( 'getwid/instagram' ); | |
| 12 | + | |
| 13 | + add_action( 'wp_ajax_check_instagram_token', array( $this, 'check_instagram_token' ) ); | |
| 14 | + | |
| 15 | + register_block_type( | |
| 16 | + getwid_get_plugin_path( 'assets/blocks/instagram' ), | |
| 17 | + array( | |
| 18 | + 'render_callback' => array( $this, 'render_callback' ), | |
| 19 | + ) | |
| 20 | + ); | |
| 21 | + | |
| 22 | + getwid_maybe_add_option( 'getwid_instagram_token', '', true ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function get_label() { | |
| 26 | + return __( 'Instagram', 'getwid' ); | |
| 27 | + } | |
| 28 | + | |
| 29 | + public function check_instagram_token() { | |
| 30 | + | |
| 31 | + check_ajax_referer( 'getwid_nonce_check_instagram_token', 'nonce' ); | |
| 32 | + | |
| 33 | + $response = (bool) get_option( 'getwid_instagram_token', '' ); | |
| 34 | + | |
| 35 | + wp_send_json_success( $response ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function render_callback( $attributes, $content ) { | |
| 39 | + | |
| 40 | + $attributes = wp_parse_args( | |
| 41 | + $attributes, | |
| 42 | + array( | |
| 43 | + 'photoCount' => 6, | |
| 44 | + 'gridColumns' => 3, | |
| 45 | + 'spacing' => 'default', | |
| 46 | + ) | |
| 47 | + ); | |
| 48 | + $encryption = new StringEncryption(); | |
| 49 | + | |
| 50 | + $access_token = $encryption->decrypt( get_option( 'getwid_instagram_token', '' ) ); | |
| 51 | + | |
| 52 | + if ( empty( $access_token ) ) { | |
| 53 | + if ( current_user_can( 'manage_options' ) ) { | |
| 54 | + return '<p>' . sprintf( | |
| 55 | + // translators: %s is a link. | |
| 56 | + __( 'Instagram Access Token is not set. <a href="%s">Connect Instagram Account</a>.', 'getwid' ), | |
| 57 | + esc_url( getwid()->settingsPage()->getTabUrl( 'general' ) ) | |
| 58 | + ) . '</p>'; | |
| 59 | + } | |
| 60 | + | |
| 61 | + return ''; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $instagram_media = get_transient( 'getwid_instagram_response_data' ); | |
| 65 | + | |
| 66 | + if ( false === $instagram_media ) { | |
| 67 | + $api_uri = 'https://graph.instagram.com/me/media?fields=id,media_type,media_url,permalink,caption,thumbnail_url&access_token=' . $access_token . '&limit=100'; | |
| 68 | + | |
| 69 | + $response = wp_remote_get( | |
| 70 | + $api_uri, | |
| 71 | + array( 'timeout' => 15 ) | |
| 72 | + ); | |
| 73 | + | |
| 74 | + if ( is_wp_error( $response ) ) { | |
| 75 | + if ( current_user_can( 'manage_options' ) ) { | |
| 76 | + return '<p>' . esc_html( $response->get_error_message() ) . '</p>'; | |
| 77 | + } | |
| 78 | + | |
| 79 | + return ''; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $instagram_media = json_decode( wp_remote_retrieve_body( $response ), false ); | |
| 83 | + | |
| 84 | + if ( JSON_ERROR_NONE === json_last_error() ) { | |
| 85 | + if ( isset( $instagram_media->data ) ) { | |
| 86 | + $expiration = intval( get_option( 'getwid_instagram_cache_timeout', 30 ) ); | |
| 87 | + | |
| 88 | + set_transient( 'getwid_instagram_response_data', $instagram_media, $expiration * MINUTE_IN_SECONDS ); | |
| 89 | + } elseif ( current_user_can( 'manage_options' ) ) { | |
| 90 | + return '<p>' . esc_html( $instagram_media->error->message ) . '</p>'; | |
| 91 | + } else { | |
| 92 | + return ''; | |
| 93 | + } | |
| 94 | + } else { | |
| 95 | + return __( 'Error in json_decode.', 'getwid' ); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + $class = 'wp-block-getwid-instagram'; | |
| 100 | + $block_name = 'wp-block-getwid-instagram'; | |
| 101 | + | |
| 102 | + if ( isset( $attributes['align'] ) ) { | |
| 103 | + $class .= ' align' . $attributes['align']; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $wrapper_class = 'wp-block-getwid-instagram__wrapper'; | |
| 107 | + $wrapper_class .= ' has-' . $attributes['gridColumns'] . '-columns'; | |
| 108 | + | |
| 109 | + if ( isset( $attributes['spacing'] ) && 'default' !== $attributes['spacing'] ) { | |
| 110 | + $class .= ' has-spacing-' . $attributes['spacing']; | |
| 111 | + } | |
| 112 | + | |
| 113 | + if ( isset( $attributes['className'] ) ) { | |
| 114 | + $class .= ' ' . $attributes['className']; | |
| 115 | + } | |
| 116 | + | |
| 117 | + ob_start(); | |
| 118 | + ?> | |
| 119 | + <div class="<?php echo esc_attr( $class ); ?>"> | |
| 120 | + <div class="<?php echo esc_attr( $wrapper_class ); ?>"> | |
| 121 | + <?php | |
| 122 | + $counter = 1; | |
| 123 | + foreach ( $instagram_media->data as $value ) { | |
| 124 | + if ( $counter <= $attributes['photoCount'] ) { | |
| 125 | + $extra_attr = array( | |
| 126 | + 'block_name' => $block_name, | |
| 127 | + 'post' => $value, | |
| 128 | + ); | |
| 129 | + getwid_get_template_part( 'instagram/post', $attributes, false, $extra_attr ); | |
| 130 | + } | |
| 131 | + ++$counter; | |
| 132 | + } | |
| 133 | + ?> | |
| 134 | + </div> | |
| 135 | + </div> | |
| 136 | + <?php | |
| 137 | + | |
| 138 | + $result = ob_get_clean(); | |
| 139 | + | |
| 140 | + return $result; | |
| 141 | + } | |
| 142 | +} | |
| 143 | + | |
| 144 | +getwid()->blocksManager()->addBlock( | |
| 145 | + new Instagram() | |
| 146 | +); | |