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