numberfollowers.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Livecoding.tv Number of Followers Badges.
  4. *
  5. * Get the amount of followers for a user and return an appropriate svg image.
  6. *
  7. * @param string channel (required) LCTV channel name.
  8. * @param string link (optional) true/false to automatically link to channel.
  9. *
  10. * @package LCTVBadges\Badges
  11. * @since 0.0.3
  12. */
  13. /** Bail if no channel name. */
  14. if ( ! isset( $_GET['channel'] ) || empty( $_GET['channel'] ) ) {
  15. exit();
  16. }
  17. /** Set the channel name. */
  18. $channel = strtolower( $_GET['channel'] );
  19. /** Initialize. */
  20. require_once( 'lctv_badges_init.php' );
  21. /** Load the API. */
  22. $lctv_api = new LCTVAPI( array(
  23. 'client_id' => LCTV_CLIENT_ID,
  24. 'client_secret' => LCTV_CLIENT_SECRET,
  25. 'user' => $channel,
  26. ) );
  27. /** Bail if API isn't authorized. */
  28. if ( ! $lctv_api->is_authorized() ) {
  29. header( "Content-type:image/svg+xml" );
  30. echo get_badge_svg( 'lctv followers', 'error', '#e05d44' );
  31. exit();
  32. }
  33. /** Get user followers. */
  34. $api_request = $lctv_api->api_request( 'v1/user/followers/' );
  35. /** Bail on error. */
  36. if ( $api_request === false || isset( $api_request->result->detail ) ) {
  37. header( "Content-type:image/svg+xml" );
  38. echo get_badge_svg( 'lctv followers', 'error', '#e05d44' );
  39. exit();
  40. }
  41. /** Count number of followers. */
  42. $follower_count = count( $api_request->result );
  43. /** Check to auto link. */
  44. if ( isset( $_GET['link'] ) && strtolower( $_GET['link'] ) === 'true' ) {
  45. $link = 'https://www.livecoding.tv/' . urlencode( $channel ) . '/';
  46. } else {
  47. $link = '';
  48. }
  49. /** Output svg image. */
  50. header( "Content-type:image/svg+xml" );
  51. echo get_badge_svg( 'lctv followers', $follower_count, '#4c1', $link );