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