PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 1.6.3
Block Animations, Motion & Scroll Effects – Ghost Kit v1.6.3
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / blocks / instagram / block.php

block.php in Block Animations, Motion & Scroll Effects – Ghost Kit 1.6.3, at blocks/instagram/block.php

204 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Instagram block.
4 *
5 * @package ghostkit
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class GhostKit_Instagram_Block
14 */
15 class GhostKit_Instagram_Block {
16 /**
17 * GhostKit_Instagram_Block constructor.
18 */
19 public function __construct() {
20 add_action( 'init', array( $this, 'init' ) );
21 }
22
23 /**
24 * Init.
25 */
26 public function init() {
27 if ( function_exists( 'register_block_type' ) ) {
28 register_block_type( 'ghostkit/instagram', array(
29 'render_callback' => array( $this, 'block_render' ),
30 ) );
31 }
32 }
33
34 /**
35 * Get instagram feed from REST
36 *
37 * @param Array $data - data to get feed.
38 * @return array
39 */
40 public function get_feed( $data ) {
41 $request = new WP_REST_Request( 'GET', '/ghostkit/v1/get_instagram_feed' );
42 $request->set_query_params( $data );
43 $response = rest_do_request( $request );
44 $server = rest_get_server();
45 $data = $server->response_to_data( $response, false );
46 $data = isset( $data['response'] ) ? $data['response'] : false;
47
48 return isset( $data['data'] ) ? $data['data'] : false;
49 }
50
51 /**
52 * Get instagram profile from REST
53 *
54 * @param Array $data - data to get profile.
55 * @return array
56 */
57 public function get_profile( $data ) {
58 $request = new WP_REST_Request( 'GET', '/ghostkit/v1/get_instagram_profile' );
59 $request->set_query_params( $data );
60 $response = rest_do_request( $request );
61 $server = rest_get_server();
62 $data = $server->response_to_data( $response, false );
63 $data = isset( $data['response'] ) ? $data['response'] : false;
64
65 return isset( $data['data'] ) ? $data['data'] : false;
66 }
67
68 /**
69 * Register gutenberg block output
70 *
71 * @param array $attributes - block attributes.
72 *
73 * @return string
74 */
75 public function block_render( $attributes ) {
76 ob_start();
77
78 $attributes = array_merge( array(
79 'variant' => 'default',
80 'accessToken' => '',
81 'count' => 8,
82 'columns' => 4,
83 'gap' => 'sm',
84 'showProfile' => true,
85 'showProfileAvatar' => true,
86 'profileAvatarSize' => 70,
87 'showProfileName' => true,
88 'showProfileBio' => true,
89 'showProfileWebsite' => true,
90 'showProfileStats' => true,
91 'className' => '',
92 ), $attributes );
93
94 $attributes['showProfile'] = $attributes['showProfile'] && ( $attributes['showProfileAvatar'] || $attributes['showProfileName'] || $attributes['showProfileBio'] || $attributes['showProfileWebsite'] || $attributes['showProfileStats'] );
95
96 $class = 'ghostkit-instagram';
97
98 // variant classname.
99 if ( 'default' !== $attributes['variant'] ) {
100 $class .= ' ghostkit-instagram-variant-' . $attributes['variant'];
101 }
102
103 if ( $attributes['gap'] ) {
104 $class .= ' ghostkit-instagram-gap-' . $attributes['gap'];
105 }
106
107 if ( $attributes['columns'] ) {
108 $class .= ' ghostkit-instagram-columns-' . $attributes['columns'];
109 }
110
111 if ( $attributes['className'] ) {
112 $class .= ' ' . $attributes['className'];
113 }
114
115 if ( $attributes['accessToken'] ) {
116 $feed = $this->get_feed( array(
117 'access_token' => $attributes['accessToken'],
118 'count' => $attributes['count'],
119 ) );
120 $profile = $this->get_profile( array(
121 'access_token' => $attributes['accessToken'],
122 ) );
123
124 if ( $feed || $profile ) {
125 ?>
126 <div class="<?php echo esc_attr( $class ); ?>">
127 <?php
128 if ( $profile && $attributes['showProfile'] ) {
129 $url = 'https://www.instagram.com/' . esc_attr( $profile['username'] ) . '/';
130
131 ?>
132 <div class="ghostkit-instagram-profile">
133 <?php if ( $attributes['showProfileAvatar'] && isset( $profile['profile_picture'] ) ) : ?>
134 <div class="ghostkit-instagram-profile-avatar">
135 <a href="<?php echo esc_url( $url ); ?>" target="_blank"><img src="<?php echo esc_url( $profile['profile_picture'] ); ?>" alt="<?php echo esc_attr( $profile['full_name'] ); ?>" width="<?php echo esc_attr( $attributes['profileAvatarSize'] ); ?>" height="<?php echo esc_attr( $attributes['profileAvatarSize'] ); ?>" /></a>
136 </div>
137 <?php endif; ?>
138 <div class="ghostkit-instagram-profile-side">
139 <?php if ( $attributes['showProfileName'] && isset( $profile['full_name'] ) ) : ?>
140 <div class="ghostkit-instagram-profile-name">
141 <a href="<?php echo esc_url( $url ); ?>" target="_blank"><?php echo esc_html( $profile['username'] ); ?></a>
142 </div>
143 <?php endif; ?>
144 <?php if ( $attributes['showProfileStats'] && isset( $profile['counts'] ) ) : ?>
145 <div class="ghostkit-instagram-profile-stats">
146 <div>
147 <strong><?php echo esc_html( $profile['counts']['media'] ); ?></strong> <span><?php echo esc_html__( 'Posts', 'ghostkit' ); ?></span>
148 </div>
149 <div>
150 <strong><?php echo esc_html( $profile['counts']['followed_by'] ); ?></strong> <span><?php echo esc_html__( 'Followers', 'ghostkit' ); ?></span>
151 </div>
152 <div>
153 <strong><?php echo esc_html( $profile['counts']['follows'] ); ?></strong> <span><?php echo esc_html__( 'Following', 'ghostkit' ); ?></span>
154 </div>
155 </div>
156 <?php endif; ?>
157 <?php if ( $attributes['showProfileBio'] && isset( $profile['bio'] ) ) : ?>
158 <div class="ghostkit-instagram-profile-bio">
159 <h2><?php echo esc_html( $profile['full_name'] ); ?></h2>
160 <div><?php echo wp_kses_post( $profile['bio'] ); ?></div>
161 </div>
162 <?php endif; ?>
163 <?php if ( $attributes['showProfileWebsite'] && isset( $profile['website'] ) ) : ?>
164 <div class="ghostkit-instagram-profile-website">
165 <a href="<?php echo esc_url( $profile['website'] ); ?>" target="_blank"><?php echo esc_url( $profile['website'] ); ?></a>
166 </div>
167 <?php endif; ?>
168 </div>
169 </div>
170 <?php
171 }
172 if ( $feed ) {
173 ?>
174 <div class="ghostkit-instagram-items">
175 <?php
176 foreach ( $feed as $item ) {
177 ?>
178 <div class="ghostkit-instagram-item">
179 <a href="<?php echo esc_url( $item['link'] ); ?>" target="_blank">
180 <img
181 src="<?php echo esc_url( $item['images']['standard_resolution']['url'] ); ?>"
182 width="<?php echo esc_attr( $item['images']['standard_resolution']['width'] ); ?>"
183 height="<?php echo esc_attr( $item['images']['standard_resolution']['height'] ); ?>"
184 alt="<?php echo esc_attr( $item['caption'] ? : '' ); ?>"
185 >
186 </a>
187 </div>
188 <?php
189 }
190 ?>
191 </div>
192 <?php
193 }
194 ?>
195 </div>
196 <?php
197 }
198 }
199
200 return ob_get_clean();
201 }
202 }
203 new GhostKit_Instagram_Block();
204