| 1 |
<?php |
| 2 |
/** |
| 3 |
* Twitter block. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Twitter_Block |
| 14 |
*/ |
| 15 |
class GhostKit_Twitter_Block { |
| 16 |
/** |
| 17 |
* GhostKit_Twitter_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/twitter', array( |
| 29 |
'render_callback' => array( $this, 'block_render' ), |
| 30 |
) ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get twitter 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_twitter_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 |
|
| 47 |
return isset( $data['response'] ) && isset( $data['success'] ) && $data['success'] ? $data['response'] : false; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get twitter profile from REST |
| 52 |
* |
| 53 |
* @param Array $data - data to get profile. |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function get_profile( $data ) { |
| 57 |
$request = new WP_REST_Request( 'GET', '/ghostkit/v1/get_twitter_profile' ); |
| 58 |
$request->set_query_params( $data ); |
| 59 |
$response = rest_do_request( $request ); |
| 60 |
$server = rest_get_server(); |
| 61 |
$data = $server->response_to_data( $response, false ); |
| 62 |
|
| 63 |
return isset( $data['response'] ) && isset( $data['success'] ) && $data['success'] ? $data['response'] : false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register gutenberg block output |
| 68 |
* |
| 69 |
* @param array $attributes - block attributes. |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function block_render( $attributes ) { |
| 74 |
ob_start(); |
| 75 |
|
| 76 |
$attributes = array_merge( array( |
| 77 |
'variant' => 'default', |
| 78 |
'consumerKey' => '', |
| 79 |
'consumerSecret' => '', |
| 80 |
'accessToken' => '', |
| 81 |
'accessTokenSecret' => '', |
| 82 |
'userName' => 'nkdevv', |
| 83 |
|
| 84 |
'count' => 3, |
| 85 |
'showReplies' => false, |
| 86 |
'showRetweets' => true, |
| 87 |
'showFeedAvatar' => true, |
| 88 |
'feedAvatarSize' => 48, |
| 89 |
'showFeedName' => true, |
| 90 |
'showFeedDate' => true, |
| 91 |
'feedTextConvertLinks' => 'links_media', |
| 92 |
'showFeedActions' => true, |
| 93 |
|
| 94 |
'showProfile' => true, |
| 95 |
'showProfileAvatar' => true, |
| 96 |
'profileAvatarSize' => 70, |
| 97 |
'showProfileName' => true, |
| 98 |
'showProfileStats' => true, |
| 99 |
'showProfileDescription' => true, |
| 100 |
'showProfileWebsite' => true, |
| 101 |
'showProfileLocation' => true, |
| 102 |
|
| 103 |
'className' => '', |
| 104 |
), $attributes ); |
| 105 |
|
| 106 |
$api_data_ready = $attributes['consumerKey'] && $attributes['consumerSecret'] && $attributes['accessToken'] && $attributes['accessTokenSecret']; |
| 107 |
|
| 108 |
$attributes['showProfile'] = $attributes['showProfile'] && ( $attributes['showProfileAvatar'] || $attributes['showProfileName'] || $attributes['showProfileDescription'] || $attributes['showProfileWebsite'] || $attributes['showProfileStats'] || $attributes['showProfileLocation'] ); |
| 109 |
|
| 110 |
$class = 'ghostkit-twitter'; |
| 111 |
|
| 112 |
// variant classname. |
| 113 |
if ( 'default' !== $attributes['variant'] ) { |
| 114 |
$class .= ' ghostkit-twitter-variant-' . $attributes['variant']; |
| 115 |
} |
| 116 |
|
| 117 |
if ( $attributes['className'] ) { |
| 118 |
$class .= ' ' . $attributes['className']; |
| 119 |
} |
| 120 |
|
| 121 |
if ( $api_data_ready && $attributes['userName'] ) { |
| 122 |
$profile = $this->get_profile( array( |
| 123 |
'consumer_key' => $attributes['consumerKey'], |
| 124 |
'consumer_secret' => $attributes['consumerSecret'], |
| 125 |
'access_token' => $attributes['accessToken'], |
| 126 |
'access_token_secret' => $attributes['accessTokenSecret'], |
| 127 |
'screen_name' => $attributes['userName'], |
| 128 |
) ); |
| 129 |
$feed = $this->get_feed( array( |
| 130 |
'consumer_key' => $attributes['consumerKey'], |
| 131 |
'consumer_secret' => $attributes['consumerSecret'], |
| 132 |
'access_token' => $attributes['accessToken'], |
| 133 |
'access_token_secret' => $attributes['accessTokenSecret'], |
| 134 |
'count' => $attributes['count'], |
| 135 |
'exclude_replies' => $attributes['showReplies'] ? 'false' : 'true', |
| 136 |
'include_rts' => $attributes['showRetweets'] ? 'true' : 'false', |
| 137 |
'screen_name' => $attributes['userName'], |
| 138 |
) ); |
| 139 |
|
| 140 |
if ( $feed || $profile ) { |
| 141 |
?> |
| 142 |
<div class="<?php echo esc_attr( $class ); ?>"> |
| 143 |
<?php |
| 144 |
if ( $profile && $attributes['showProfile'] ) { |
| 145 |
?> |
| 146 |
<div class="ghostkit-twitter-profile"> |
| 147 |
<?php |
| 148 |
$url = 'https://twitter.com/' . $profile['screen_name'] . '/'; |
| 149 |
?> |
| 150 |
<?php if ( $attributes['showProfileAvatar'] && isset( $profile['profile_images_https'] ) ) : ?> |
| 151 |
<div class="ghostkit-twitter-profile-avatar"> |
| 152 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"><img src="<?php echo esc_url( $profile['profile_images_https']['original'] ); ?>" alt="<?php echo esc_attr( $profile['name'] ); ?>" width="<?php echo esc_attr( $attributes['profileAvatarSize'] ); ?>" height="<?php echo esc_attr( $attributes['profileAvatarSize'] ); ?>" /></a> |
| 153 |
</div> |
| 154 |
<?php endif; ?> |
| 155 |
<div class="ghostkit-twitter-profile-side"> |
| 156 |
<?php if ( $attributes['showProfileName'] && isset( $profile['name'] ) ) : ?> |
| 157 |
<div class="ghostkit-twitter-profile-name"> |
| 158 |
<h2 class="ghostkit-twitter-profile-fullname"> |
| 159 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"><?php echo esc_html( $profile['name'] ); ?></a> |
| 160 |
<?php if ( $profile['verified'] ) : ?> |
| 161 |
<span class="ghostkit-twitter-profile-verified"><?php echo esc_html__( 'Verified account', 'ghostkit' ); ?></span> |
| 162 |
<?php endif; ?> |
| 163 |
</h2> |
| 164 |
<h3 class="ghostkit-twitter-profile-username"> |
| 165 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank">@<?php echo esc_html( $profile['screen_name'] ); ?></a> |
| 166 |
</h3> |
| 167 |
</div> |
| 168 |
<?php endif; ?> |
| 169 |
<?php if ( $attributes['showProfileStats'] ) : ?> |
| 170 |
<div class="ghostkit-twitter-profile-stats"> |
| 171 |
<div> |
| 172 |
<strong><?php echo esc_html( $profile['statuses_count_short'] ); ?></strong> <span><?php echo esc_html__( 'Tweets', 'ghostkit' ); ?></span> |
| 173 |
</div> |
| 174 |
<div> |
| 175 |
<strong><?php echo esc_html( $profile['friends_count_short'] ); ?></strong> <span><?php echo esc_html__( 'Following', 'ghostkit' ); ?></span> |
| 176 |
</div> |
| 177 |
<div> |
| 178 |
<strong><?php echo esc_html( $profile['followers_count_short'] ); ?></strong> <span><?php echo esc_html__( 'Followers', 'ghostkit' ); ?></span> |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
<?php endif; ?> |
| 182 |
<?php if ( $attributes['showProfileDescription'] && isset( $profile['description_entitled'] ) && $profile['description_entitled'] ) : ?> |
| 183 |
<div class="ghostkit-twitter-profile-description"> |
| 184 |
<?php echo wp_kses_post( $profile['description_entitled'] ); ?> |
| 185 |
</div> |
| 186 |
<?php endif; ?> |
| 187 |
<?php if ( $attributes['showProfileWebsite'] && isset( $profile['url_entitled'] ) && $profile['url_entitled'] ) : ?> |
| 188 |
<div class="ghostkit-twitter-profile-website"> |
| 189 |
<span class="fas fa-link"></span> <?php echo wp_kses_post( $profile['url_entitled'] ); ?> |
| 190 |
</div> |
| 191 |
<?php endif; ?> |
| 192 |
<?php if ( $attributes['showProfileLocation'] && isset( $profile['location'] ) && $profile['location'] ) : ?> |
| 193 |
<div class="ghostkit-twitter-profile-location"> |
| 194 |
<span class="fas fa-map-marker-alt"></span> <?php echo esc_html( $profile['location'] ); ?> |
| 195 |
</div> |
| 196 |
<?php endif; ?> |
| 197 |
</div> |
| 198 |
</div> |
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
if ( $feed ) { |
| 203 |
?> |
| 204 |
<div class="ghostkit-twitter-items"> |
| 205 |
<?php |
| 206 |
foreach ( $feed as $item ) { |
| 207 |
$old_item = $item; |
| 208 |
$is_retweet = false; |
| 209 |
|
| 210 |
if ( isset( $item['retweeted_status'] ) ) { |
| 211 |
$item = $item['retweeted_status']; |
| 212 |
$is_retweet = true; |
| 213 |
} |
| 214 |
|
| 215 |
$url = 'https://twitter.com/' . $item['user']['screen_name'] . '/status/' . $item['id_str']; |
| 216 |
?> |
| 217 |
<div class="ghostkit-twitter-item"> |
| 218 |
<?php if ( $attributes['showFeedAvatar'] ) : ?> |
| 219 |
<div class="ghostkit-twitter-item-avatar"> |
| 220 |
<?php if ( $is_retweet ) : ?> |
| 221 |
<br> |
| 222 |
<?php endif; ?> |
| 223 |
<a href="https://twitter.com/<?php echo esc_attr( $item['user']['screen_name'] ); ?>/" target="_blank"> |
| 224 |
<img src="<?php echo esc_url( $item['user']['profile_images_https']['bigger'] ); ?>" alt="<?php echo esc_attr( $item['user']['screen_name'] ); ?>" width="<?php echo esc_attr( $attributes['feedAvatarSize'] ); ?>" height="<?php echo esc_attr( $attributes['feedAvatarSize'] ); ?>"> |
| 225 |
</a> |
| 226 |
</div> |
| 227 |
<?php endif; ?> |
| 228 |
<div class="ghostkit-twitter-item-content"> |
| 229 |
<?php if ( $is_retweet ) : ?> |
| 230 |
<div class="ghostkit-twitter-item-retweeted"> |
| 231 |
<span class="ghostkit-twitter-item-retweeted-icon"><span class="fas fa-retweet"></span></span> |
| 232 |
<a href="https://twitter.com/<?php echo esc_attr( $old_item['user']['screen_name'] ); ?>/" target="_blank"> |
| 233 |
<strong><?php echo esc_html( $old_item['user']['name'] ); ?></strong> |
| 234 |
</a> |
| 235 |
<?php echo esc_html__( 'Retweeted', 'ghostkit' ); ?> |
| 236 |
</div> |
| 237 |
<?php endif; ?> |
| 238 |
<?php if ( $attributes['showFeedName'] || $attributes['showFeedDate'] ) : ?> |
| 239 |
<div class="ghostkit-twitter-item-meta"> |
| 240 |
<?php if ( $attributes['showFeedName'] ) : ?> |
| 241 |
<div class="ghostkit-twitter-item-meta-name"> |
| 242 |
<a href="https://twitter.com/<?php echo esc_attr( $item['user']['screen_name'] ); ?>/" target="_blank"> |
| 243 |
<strong><?php echo esc_html( $item['user']['name'] ); ?></strong> |
| 244 |
<?php if ( $item['user']['verified'] ) : ?> |
| 245 |
<span class="ghostkit-twitter-item-meta-name-verified"><?php echo esc_html__( 'Verified account', 'ghostkit' ); ?></span> |
| 246 |
<?php endif; ?> |
| 247 |
<span>@<?php echo esc_html( $item['user']['screen_name'] ); ?></span> |
| 248 |
</a> |
| 249 |
</div> |
| 250 |
<?php endif; ?> |
| 251 |
<?php if ( $attributes['showFeedDate'] ) : ?> |
| 252 |
<div class="ghostkit-twitter-item-meta-date"> |
| 253 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"><?php echo esc_html( $item['date_formatted'] ); ?></a> |
| 254 |
</div> |
| 255 |
<?php endif; ?> |
| 256 |
</div> |
| 257 |
<?php endif; ?> |
| 258 |
<?php if ( 'links_media' === $attributes['feedTextConvertLinks'] ) : ?> |
| 259 |
<div class="ghostkit-twitter-item-text"><?php echo wp_kses_post( $item['text_entitled'] ); ?></div> |
| 260 |
<?php elseif ( 'links' === $attributes['feedTextConvertLinks'] ) : ?> |
| 261 |
<div class="ghostkit-twitter-item-text"><?php echo wp_kses_post( $item['text_entitled_no_media'] ); ?></div> |
| 262 |
<?php else : ?> |
| 263 |
<div class="ghostkit-twitter-item-text"><?php echo wp_kses_post( $item['text'] ); ?></div> |
| 264 |
<?php endif; ?> |
| 265 |
<?php if ( $attributes['showFeedActions'] ) : ?> |
| 266 |
<div class="ghostkit-twitter-item-actions"> |
| 267 |
<div class="ghostkit-twitter-item-actions-retweet"> |
| 268 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"> |
| 269 |
<span class="fas fa-retweet"></span> |
| 270 |
<?php if ( $item['retweet_count_short'] ) : ?> |
| 271 |
<span><?php echo esc_html( $item['retweet_count_short'] ); ?></span> |
| 272 |
<?php endif; ?> |
| 273 |
</a> |
| 274 |
</div> |
| 275 |
<div class="ghostkit-twitter-item-actions-like"> |
| 276 |
<a href="<?php echo esc_url( $url ); ?>" target="_blank"> |
| 277 |
<span class="far fa-heart"></span> |
| 278 |
<?php if ( $item['favorite_count_short'] ) : ?> |
| 279 |
<span><?php echo esc_html( $item['favorite_count_short'] ); ?></span> |
| 280 |
<?php endif; ?> |
| 281 |
</a> |
| 282 |
</div> |
| 283 |
</div> |
| 284 |
<?php endif; ?> |
| 285 |
</div> |
| 286 |
</div> |
| 287 |
<?php |
| 288 |
} |
| 289 |
?> |
| 290 |
</div> |
| 291 |
<?php |
| 292 |
} |
| 293 |
?> |
| 294 |
</div> |
| 295 |
<?php |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
return ob_get_clean(); |
| 300 |
} |
| 301 |
} |
| 302 |
new GhostKit_Twitter_Block(); |
| 303 |
|