| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers Social Links |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Modules/Helper |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Author social fields |
| 11 |
*/ |
| 12 |
function powerkit_get_author_fields() { |
| 13 |
return array( |
| 14 |
'facebook' => esc_html__( 'Facebook Profile URL', 'powerkit' ), |
| 15 |
'twitter' => esc_html__( 'Twitter Profile URL', 'powerkit' ), |
| 16 |
'instagram' => esc_html__( 'Instagram Profile URL', 'powerkit' ), |
| 17 |
'pinterest' => esc_html__( 'Pinterest Profile URL', 'powerkit' ), |
| 18 |
'youtube' => esc_html__( 'YouTube Profile URL', 'powerkit' ), |
| 19 |
'telegram' => esc_html__( 'Telegram Profile URL', 'powerkit' ), |
| 20 |
'vimeo' => esc_html__( 'Vimeo Profile URL', 'powerkit' ), |
| 21 |
'soundcloud' => esc_html__( 'SoundCloud Profile URL', 'powerkit' ), |
| 22 |
'spotify' => esc_html__( 'Spotify Profile URL', 'powerkit' ), |
| 23 |
'dribbble' => esc_html__( 'Dribbble Profile URL', 'powerkit' ), |
| 24 |
'behance' => esc_html__( 'Behance Profile URL', 'powerkit' ), |
| 25 |
'github' => esc_html__( 'GitHub Profile URL', 'powerkit' ), |
| 26 |
'vk' => esc_html__( 'VK Profile URL', 'powerkit' ), |
| 27 |
'linkedin' => esc_html__( 'LinkedIn Profile URL', 'powerkit' ), |
| 28 |
'twitch' => esc_html__( 'Twitch Profile URL', 'powerkit' ), |
| 29 |
'flickr' => esc_html__( 'Flickr Profile URL', 'powerkit' ), |
| 30 |
'snapchat' => esc_html__( 'Snapchat Profile URL', 'powerkit' ), |
| 31 |
'medium' => esc_html__( 'Medium Profile URL', 'powerkit' ), |
| 32 |
'tumblr' => esc_html__( 'Tumblr Profile URL', 'powerkit' ), |
| 33 |
'bloglovin' => esc_html__( 'Bloglovin Profile URL', 'powerkit' ), |
| 34 |
'rss' => esc_html__( 'RSS Profile URL', 'powerkit' ), |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Author Social Links |
| 40 |
* |
| 41 |
* @param mixed $author The author. |
| 42 |
* @param string $class Class name of the wrapping <div>. |
| 43 |
* @param bool $title Title of the link. |
| 44 |
* @param bool $only_data Output only data. |
| 45 |
*/ |
| 46 |
function powerkit_author_social_links( $author, $class = 'default', $title = false, $only_data = false ) { |
| 47 |
|
| 48 |
$links = powerkit_get_author_fields(); |
| 49 |
|
| 50 |
$list = array(); |
| 51 |
|
| 52 |
// Website. |
| 53 |
$url = get_the_author_meta( 'url', $author ); |
| 54 |
|
| 55 |
if ( $url ) { |
| 56 |
$list[] = array( |
| 57 |
'label' => esc_html__( 'Website', 'powerkit' ), |
| 58 |
'key' => 'website', |
| 59 |
'url' => $url, |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
// Loop through the array of social links. |
| 64 |
foreach ( $links as $key => $label ) { |
| 65 |
|
| 66 |
if ( isset( $author->$key ) ) { |
| 67 |
$author_url = $author->$key; |
| 68 |
} else { |
| 69 |
$author_url = get_the_author_meta( $key, $author ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( empty( $author_url ) ) { |
| 73 |
$author_url = get_post_meta( $author, sprintf( 'cap-%s', $key ), true ); |
| 74 |
} |
| 75 |
|
| 76 |
// Check if slug of a social icon is not empty. |
| 77 |
if ( $author_url ) { |
| 78 |
$list[] = array( |
| 79 |
'url' => $author_url, |
| 80 |
'key' => $key, |
| 81 |
'label' => $label, |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( $only_data ) { |
| 87 |
return $list; |
| 88 |
} |
| 89 |
|
| 90 |
// Check if the list is not empty. |
| 91 |
if ( $list ) { |
| 92 |
?> |
| 93 |
<div class="pk-author-social-links pk-social-links-wrap pk-social-links-template-<?php echo esc_html( $class ); ?>"> |
| 94 |
<div class="pk-social-links-items"> |
| 95 |
<?php |
| 96 |
// Loop through the array of social links. |
| 97 |
foreach ( $list as $data ) { |
| 98 |
?> |
| 99 |
<div class="pk-social-links-item pk-social-links-<?php echo esc_html( $data['key'] ); ?>"> |
| 100 |
<a href="<?php echo esc_url( $data['url'] ); ?>" class="pk-social-links-link" target="_blank"> |
| 101 |
<i class="pk-icon pk-icon-<?php echo esc_html( $data['key'] ); ?>"></i> |
| 102 |
<?php if ( $title ) { ?> |
| 103 |
<span class="pk-social-links-title pk-font-primary"><?php echo esc_html( $data['label'] ); ?></span> |
| 104 |
<?php } ?> |
| 105 |
</a> |
| 106 |
</div> |
| 107 |
<?php |
| 108 |
} |
| 109 |
?> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
<?php |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get social params with filter |
| 118 |
* |
| 119 |
* @param string $id ID group. |
| 120 |
* @param string $key Filter key. |
| 121 |
* @return mix Specific social params. |
| 122 |
*/ |
| 123 |
function powerkit_social_links_specific_param( $id, $key = false ) { |
| 124 |
$params = apply_filters( 'powerkit_social_links_list', array() ); |
| 125 |
|
| 126 |
foreach ( $params as $param ) { |
| 127 |
if ( $key ) { |
| 128 |
if ( $param['id'] === $id && isset( $param[ $key ] ) ) { |
| 129 |
return $param[ $key ]; |
| 130 |
} |
| 131 |
} else { |
| 132 |
if ( $param['id'] === $id ) { |
| 133 |
return $param; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Get templates options |
| 141 |
* |
| 142 |
* @return array Items. |
| 143 |
*/ |
| 144 |
function powerkit_social_links_get_templates() { |
| 145 |
$options = array(); |
| 146 |
|
| 147 |
$templates = apply_filters( 'powerkit_social_links_templates', array() ); |
| 148 |
|
| 149 |
if ( $templates ) { |
| 150 |
foreach ( $templates as $key => $item ) { |
| 151 |
if ( isset( $item['public'] ) && false === $item['public'] ) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
if ( isset( $item['name'] ) ) { |
| 155 |
$options[ $key ] = $item['name']; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $options; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get color schemes options |
| 165 |
* |
| 166 |
* @return array Items. |
| 167 |
*/ |
| 168 |
function powerkit_social_links_get_color_schemes() { |
| 169 |
$options = array(); |
| 170 |
|
| 171 |
$color_schemes = apply_filters( 'powerkit_social_links_color_schemes', array() ); |
| 172 |
|
| 173 |
if ( $color_schemes ) { |
| 174 |
foreach ( $color_schemes as $key => $item ) { |
| 175 |
if ( isset( $item['public'] ) && false === $item['public'] ) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
if ( isset( $item['name'] ) ) { |
| 179 |
$options[ $key ] = $item['name']; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return $options; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Adapt link with option |
| 191 |
* |
| 192 |
* @param array $link Url link social. |
| 193 |
* @param string $slug The slug default. |
| 194 |
*/ |
| 195 |
function powerkit_social_links_parse_link( $link, $slug = null ) { |
| 196 |
|
| 197 |
$parse = array(); |
| 198 |
|
| 199 |
if ( is_array( $link ) ) { |
| 200 |
foreach ( $link as $extra_name => $links ) { |
| 201 |
$link_type = get_option( $extra_name ); |
| 202 |
foreach ( $links as $type => $url ) { |
| 203 |
if ( $link_type === $type ) { |
| 204 |
$link = $url; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if ( ! is_array( $link ) ) { |
| 211 |
preg_match( '/%(powerkit_social_links_.*?)%/', $link, $parse ); |
| 212 |
|
| 213 |
if ( ! $slug ) { |
| 214 |
$slug = get_option( $parse[1] ); |
| 215 |
} |
| 216 |
|
| 217 |
// Return replaced value. |
| 218 |
if ( isset( $parse[0] ) && isset( $parse[1] ) ) { |
| 219 |
return str_replace( $parse[0], $slug, $link ); |
| 220 |
} |
| 221 |
|
| 222 |
return $link; |
| 223 |
} |
| 224 |
|
| 225 |
// Return empty value. |
| 226 |
return ''; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
/** |
| 231 |
* Template appearance |
| 232 |
* |
| 233 |
* @param array $params Parameters. |
| 234 |
*/ |
| 235 |
function powerkit_social_links_appearance( $params ) { |
| 236 |
|
| 237 |
$wrap_class = null; |
| 238 |
|
| 239 |
$params = array_merge( |
| 240 |
array( |
| 241 |
'cache' => true, |
| 242 |
'maximum' => -1, |
| 243 |
'template' => 'inline', |
| 244 |
'scheme' => 'light', |
| 245 |
'labels' => true, |
| 246 |
'titles' => true, |
| 247 |
'counts' => true, |
| 248 |
'mode' => 'mixed', |
| 249 |
), |
| 250 |
(array) $params |
| 251 |
); |
| 252 |
|
| 253 |
// Socials list. |
| 254 |
$links = array(); |
| 255 |
|
| 256 |
$social_links_order = get_option( 'powerkit_social_links_order_multiple_list', array() ); |
| 257 |
$social_links_check = get_option( 'powerkit_social_links_multiple_list', array() ); |
| 258 |
|
| 259 |
// Sort. |
| 260 |
if ( $social_links_order && $social_links_check ) { |
| 261 |
$social_links_order = array_flip( $social_links_order ); |
| 262 |
|
| 263 |
foreach ( $social_links_order as $key => $val ) { |
| 264 |
if ( in_array( $key, $social_links_check, true ) ) { |
| 265 |
$links[] = $key; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// Maximum. |
| 271 |
if ( isset( $params['maximum'] ) && intval( $params['maximum'] ) > 0 ) { |
| 272 |
$links = array_slice( $links, 0, $params['maximum'], true ); |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* ---------- Build Template. |
| 277 |
*/ |
| 278 |
|
| 279 |
// Template columns. |
| 280 |
if ( in_array( $params['template'], array( 'col-2', 'col-3', 'col-4', 'col-5', 'col-6' ), true ) ) { |
| 281 |
$wrap_class .= ' pk-social-links-template-columns'; |
| 282 |
} |
| 283 |
|
| 284 |
// Wrap Class. |
| 285 |
$wrap_class .= sprintf( ' pk-social-links-template-%s', $params['template'] ); |
| 286 |
|
| 287 |
/* |
| 288 |
* ---------- Build Scheme. |
| 289 |
*/ |
| 290 |
|
| 291 |
// Light Background. |
| 292 |
if ( 'light-bg' === $params['scheme'] ) { |
| 293 |
$wrap_class .= ' pk-social-links-scheme-light'; |
| 294 |
} |
| 295 |
|
| 296 |
// Light Rounded. |
| 297 |
if ( 'light-rounded' === $params['scheme'] ) { |
| 298 |
$wrap_class .= ' pk-social-links-scheme-bold'; |
| 299 |
} |
| 300 |
|
| 301 |
// Wrap Color scheme. |
| 302 |
$wrap_class .= sprintf( ' pk-social-links-scheme-%s', $params['scheme'] ); |
| 303 |
|
| 304 |
// Wrap Class | Counts, titles, labels. |
| 305 |
$wrap_class .= ( $params['titles'] ) ? ' pk-social-links-titles-enabled' : ' pk-social-links-titles-disabled'; |
| 306 |
$wrap_class .= ( $params['counts'] ) ? ' pk-social-links-counts-enabled' : ' pk-social-links-counts-disabled'; |
| 307 |
$wrap_class .= ( $params['labels'] ) ? ' pk-social-links-labels-enabled' : ' pk-social-links-labels-disabled'; |
| 308 |
|
| 309 |
if ( $params['counts'] ) { |
| 310 |
switch ( $params['mode'] ) { |
| 311 |
case 'php': |
| 312 |
$wrap_class .= ' pk-social-links-mode-php'; |
| 313 |
break; |
| 314 |
|
| 315 |
case 'rest': |
| 316 |
$wrap_class .= ' pk-social-links-mode-rest'; |
| 317 |
|
| 318 |
// Smart Load restapi scripts. |
| 319 |
add_action( 'wp_footer', 'powerkit_social_links_rest_api_scripts', 99 ); |
| 320 |
break; |
| 321 |
case 'mixed': |
| 322 |
$wrap_class .= ' pk-social-links-mode-php'; |
| 323 |
$wrap_class .= ' pk-social-links-mode-rest'; |
| 324 |
|
| 325 |
// Smart Load restapi scripts. |
| 326 |
add_action( 'wp_footer', 'powerkit_social_links_rest_api_scripts', 99 ); |
| 327 |
break; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
// Link Attributes. |
| 332 |
$target = get_option( 'powerkit_social_links_link_target', 'new' ) === 'new' ? '_blank' : '_self'; |
| 333 |
$rel = get_option( 'powerkit_social_links_nofollow' ) ? 'nofollow' : ''; |
| 334 |
|
| 335 |
// Icon prefix. |
| 336 |
$powerkit_social_links_icon_prefix = apply_filters( 'powerkit_social_links_icon_prefix', 'pk-icon' ); |
| 337 |
?> |
| 338 |
<div class="pk-social-links-wrap <?php echo esc_attr( $wrap_class ); ?>"> |
| 339 |
<div class="pk-social-links-items"> |
| 340 |
<?php |
| 341 |
if ( $links ) { |
| 342 |
foreach ( $links as $item ) { |
| 343 |
$id = is_array( $item ) ? $item['id'] : $item; |
| 344 |
|
| 345 |
$title = get_option( sprintf( 'powerkit_social_links_title_%s', $id ) ); |
| 346 |
$label = get_option( sprintf( 'powerkit_social_links_label_%s', $id ) ); |
| 347 |
|
| 348 |
// Link User. |
| 349 |
$link = powerkit_social_links_parse_link( powerkit_social_links_specific_param( $id, 'link' ) ); |
| 350 |
|
| 351 |
// Account Count. |
| 352 |
$result = array(); |
| 353 |
|
| 354 |
if ( $params['counts'] ) { |
| 355 |
if ( 'rest' === $params['mode'] ) { |
| 356 |
$result = powerkit_social_links_get_count( $id, $params['labels'], 'forcibly', true ); // Count User. |
| 357 |
} else { |
| 358 |
$result = powerkit_social_links_get_count( $id, $params['labels'], $params['cache'], true ); // Count User. |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
$class = ( ! isset( $result['count'] ) || ! $result['count'] ) ? ' pk-social-links-no-count' : ''; |
| 363 |
?> |
| 364 |
<div class="pk-social-links-item pk-social-links-<?php echo esc_attr( $id ); ?> <?php echo esc_attr( $class ); ?>" data-id="<?php echo esc_attr( $id ); ?>"> |
| 365 |
<a href="<?php echo esc_url( $link ); ?>" class="pk-social-links-link" target="<?php echo esc_attr( $target ); ?>" rel="<?php echo esc_attr( $rel ); ?>"> |
| 366 |
<i class="pk-social-links-icon <?php echo sprintf( '%2$s %2$s-%1$s', esc_attr( $id ), esc_attr( $powerkit_social_links_icon_prefix ) ); ?>"></i> |
| 367 |
<?php if ( $params['titles'] && $title ) { ?> |
| 368 |
<span class="pk-social-links-title pk-font-heading"><?php echo esc_html( $title ); ?></span> |
| 369 |
<?php } ?> |
| 370 |
|
| 371 |
<?php if ( $params['counts'] ) { ?> |
| 372 |
<span class="pk-social-links-count pk-font-secondary"><?php echo esc_html( $result['count'] ); ?></span> |
| 373 |
<?php } ?> |
| 374 |
|
| 375 |
<?php if ( isset( $result['error'] ) && ( current_user_can( 'editor' ) || current_user_can( 'administrator' ) ) ) { ?> |
| 376 |
<span class="pk-social-links-count pk-font-secondary pk-social-links-note pk-tippy"> |
| 377 |
<span class="pk-social-links-note-icon"></span> |
| 378 |
|
| 379 |
<?php powerkit_alert_warning( powerkit_social_links_specific_param( $id, 'name' ) . ' : ' . $result['error'] ); ?> |
| 380 |
</span> |
| 381 |
<?php } ?> |
| 382 |
|
| 383 |
<?php if ( $params['labels'] && $label ) { ?> |
| 384 |
<span class="pk-social-links-label pk-font-secondary"><?php echo esc_html( $label ); ?></span> |
| 385 |
<?php } ?> |
| 386 |
</a> |
| 387 |
</div> |
| 388 |
<?php |
| 389 |
} |
| 390 |
} else { |
| 391 |
/* translators: Social Settings Link. */ |
| 392 |
powerkit_alert_warning( sprintf( __( 'Please select social links in <object><a href="%s" target="_blank">Social Links Settings</a></object>.', 'powerkit' ), esc_url( powerkit_get_page_url( 'social_links' ) ) ) ); |
| 393 |
} |
| 394 |
?> |
| 395 |
</div> |
| 396 |
</div> |
| 397 |
<?php |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
/** |
| 402 |
* Function get social links |
| 403 |
* |
| 404 |
* @param bool $labels Display labels. |
| 405 |
* @param bool $titles Display titles. |
| 406 |
* @param bool $counts Display counts. |
| 407 |
* @param string $template Template links. |
| 408 |
* @param string $scheme Color scheme. |
| 409 |
* @param string $mode Counter mode. |
| 410 |
* @param mixed $maximum Maximum Number of Social Links. |
| 411 |
*/ |
| 412 |
function powerkit_social_links( $labels = true, $titles = true, $counts = true, $template = 'inline', $scheme = 'light', $mode = 'mixed', $maximum = -1 ) { |
| 413 |
$params = array( |
| 414 |
'labels' => $labels, |
| 415 |
'titles' => $titles, |
| 416 |
'counts' => $counts, |
| 417 |
'template' => $template, |
| 418 |
'scheme' => $scheme, |
| 419 |
'mode' => $mode, |
| 420 |
'maximum' => $maximum, |
| 421 |
'cache' => true, |
| 422 |
); |
| 423 |
|
| 424 |
powerkit_social_links_appearance( $params ); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/** |
| 429 |
* Add Social Links REST API Scripts |
| 430 |
*/ |
| 431 |
function powerkit_social_links_rest_api_scripts() { |
| 432 |
?> |
| 433 |
<script type="text/javascript"> |
| 434 |
"use strict"; |
| 435 |
|
| 436 |
(function($) { |
| 437 |
|
| 438 |
$( window ).on( 'load', function() { |
| 439 |
|
| 440 |
// Get all links. |
| 441 |
var powerkitSLinksIds = []; |
| 442 |
|
| 443 |
var powerkitSLinksRestBox = $( '.pk-social-links-mode-rest' ); |
| 444 |
|
| 445 |
// Generate links Ids. |
| 446 |
$( powerkitSLinksRestBox ).each( function( index, wrap ) { |
| 447 |
|
| 448 |
if ( ! $( wrap ).hasClass( 'pk-social-links-counts-disabled' ) ) { |
| 449 |
|
| 450 |
$( wrap ).find( '.pk-social-links-item' ).each( function() { |
| 451 |
if ( $( this ).attr( 'data-id' ).length > 0 ) { |
| 452 |
powerkitSLinksIds.push( $( this ).attr( 'data-id' ) ); |
| 453 |
} |
| 454 |
}); |
| 455 |
} |
| 456 |
}); |
| 457 |
|
| 458 |
// Generate links data. |
| 459 |
var powerkitSLinksData = {}; |
| 460 |
|
| 461 |
if( powerkitSLinksIds.length > 0 ) { |
| 462 |
powerkitSLinksData = { 'ids' : powerkitSLinksIds.join() }; |
| 463 |
} |
| 464 |
|
| 465 |
// Check data. |
| 466 |
if ( ! Object.entries( powerkitSLinksData ).length ) { |
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
// Get results by REST API. |
| 471 |
$.ajax({ |
| 472 |
type: 'GET', |
| 473 |
url: '<?php echo esc_url( home_url() ); ?>/wp-json/social-counts/v1/get-counts', |
| 474 |
data: powerkitSLinksData, |
| 475 |
beforeSend: function(){ |
| 476 |
|
| 477 |
// Add Loading Class. |
| 478 |
powerkitSLinksRestBox.addClass( 'pk-social-links-loading' ); |
| 479 |
}, |
| 480 |
success: function( response ) { |
| 481 |
|
| 482 |
if ( ! $.isEmptyObject( response ) && ! response.hasOwnProperty( 'code' ) ) { |
| 483 |
|
| 484 |
// SLinks loop. |
| 485 |
$.each( response, function( index, data ) { |
| 486 |
|
| 487 |
// Find Bsa Item. |
| 488 |
var powerkitSLinksItem = powerkitSLinksRestBox.find( '.pk-social-links-item[data-id="' + index + '"]'); |
| 489 |
|
| 490 |
// Set Class. |
| 491 |
if ( data.hasOwnProperty( 'class' ) ) { |
| 492 |
powerkitSLinksItem.addClass( data.class ); |
| 493 |
} |
| 494 |
|
| 495 |
// Set Count. |
| 496 |
if ( data.hasOwnProperty( 'result' ) && data.result !== null && data.result.hasOwnProperty( 'count' ) ) { |
| 497 |
|
| 498 |
if ( data.result.count ) { |
| 499 |
// Class Item. |
| 500 |
powerkitSLinksItem.removeClass( 'pk-social-links-no-count' ).addClass( 'pk-social-links-item-count' ); |
| 501 |
|
| 502 |
// Count item. |
| 503 |
powerkitSLinksItem.find( '.pk-social-links-count' ).not( '.pk-tippy' ).html( data.result.count ); |
| 504 |
} |
| 505 |
} else { |
| 506 |
powerkitSLinksItem.addClass( 'pk-social-links-no-count' ); |
| 507 |
} |
| 508 |
|
| 509 |
}); |
| 510 |
} |
| 511 |
|
| 512 |
// Remove Loading Class. |
| 513 |
powerkitSLinksRestBox.removeClass( 'pk-social-links-loading' ); |
| 514 |
}, |
| 515 |
error: function() { |
| 516 |
|
| 517 |
// Remove Loading Class. |
| 518 |
powerkitSLinksRestBox.removeClass( 'pk-social-links-loading' ); |
| 519 |
} |
| 520 |
}); |
| 521 |
}); |
| 522 |
|
| 523 |
})(jQuery); |
| 524 |
</script> |
| 525 |
<?php |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
/** |
| 530 |
* This is our callback function that embeds our resource in a WP_REST_Response |
| 531 |
* |
| 532 |
* @param array $request REST API Request. |
| 533 |
*/ |
| 534 |
function powerkit_social_links_restapi( $request ) { |
| 535 |
|
| 536 |
// Get Social Links. |
| 537 |
$social_social_links = apply_filters( 'powerkit_social_links_list', array() ); |
| 538 |
$social_social_links = array_keys( $social_social_links ); |
| 539 |
|
| 540 |
// Error, when Social Links are empty. |
| 541 |
if ( empty( $social_social_links ) ) { |
| 542 |
return rest_ensure_response( |
| 543 |
array( |
| 544 |
'code' => 'social_links_not_found', |
| 545 |
'message' => esc_html__( 'Social Links not found.', 'powerkit' ), |
| 546 |
) |
| 547 |
); |
| 548 |
} |
| 549 |
|
| 550 |
// Labels. |
| 551 |
if ( isset( $request['labels'] ) ) { |
| 552 |
$labels = (bool) $request['labels']; |
| 553 |
} else { |
| 554 |
$labels = false; |
| 555 |
} |
| 556 |
|
| 557 |
// Cache Results. |
| 558 |
if ( isset( $request['cache'] ) ) { |
| 559 |
$cache = (bool) $request['cache']; |
| 560 |
} else { |
| 561 |
$cache = true; |
| 562 |
} |
| 563 |
|
| 564 |
// Get Counts. |
| 565 |
$link_counts = array(); |
| 566 |
|
| 567 |
if ( isset( $request['ids'] ) ) { |
| 568 |
$ids = explode( ',', $request['ids'] ); |
| 569 |
$ids = array_map( 'trim', $ids ); |
| 570 |
} else { |
| 571 |
$ids = $social_social_links; |
| 572 |
} |
| 573 |
|
| 574 |
foreach ( $ids as $link_id ) { |
| 575 |
if ( in_array( $link_id, $social_social_links, true ) ) { |
| 576 |
|
| 577 |
$result = powerkit_social_links_get_count( $link_id, $labels, $cache, true ); // Count User. |
| 578 |
|
| 579 |
$class = ( ! $result['count'] ) ? ' pk-social-links-no-count' : ''; |
| 580 |
$class .= ( isset( $result['error'] ) ) ? ' pk-social-links-error' : ''; |
| 581 |
|
| 582 |
$link_counts[ $link_id ] = array( |
| 583 |
'result' => $result, |
| 584 |
'class' => $class, |
| 585 |
); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// Return Succes Result. |
| 590 |
return rest_ensure_response( $link_counts ); |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* This function is where we register our routes for our example endpoint. |
| 595 |
*/ |
| 596 |
function powerkit_social_links_register_api_routes() { |
| 597 |
|
| 598 |
register_rest_route( |
| 599 |
'social-counts/v1', |
| 600 |
'/get-counts', |
| 601 |
array( |
| 602 |
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
| 603 |
'methods' => WP_REST_Server::READABLE, |
| 604 |
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
| 605 |
'callback' => 'powerkit_social_links_restapi', |
| 606 |
'permission_callback' => function() { |
| 607 |
return true; |
| 608 |
}, |
| 609 |
) |
| 610 |
); |
| 611 |
} |
| 612 |
add_action( 'rest_api_init', 'powerkit_social_links_register_api_routes' ); |
| 613 |
|