Нет описания

untappd-menu.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Untappd Shortcodes
  4. *
  5. * @author kraftbj
  6. *
  7. * [untappd-menu location="123" theme="123"]
  8. * @since 4.1.0
  9. * @param location int Location ID for the Untappd venue. Required.
  10. * @param theme int Theme ID for the Untappd menu. Required.
  11. *
  12. * @package automattic/jetpack
  13. */
  14. /**
  15. * Display Untappd data in posts and pages.
  16. */
  17. class Jetpack_Untappd {
  18. /**
  19. * Constructor
  20. */
  21. public function __construct() {
  22. add_action( 'init', array( $this, 'action_init' ) );
  23. }
  24. /**
  25. * Register our shortcodes.
  26. */
  27. public function action_init() {
  28. add_shortcode( 'untappd-menu', array( $this, 'menu_shortcode' ) );
  29. }
  30. /**
  31. * [untappd-menu] shortcode.
  32. *
  33. * @param array $atts Shortocde attributes.
  34. * @param string $content Post content.
  35. */
  36. public static function menu_shortcode( $atts, $content = '' ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  37. // Let's bail if we don't have location or theme.
  38. if ( ! isset( $atts['location'] ) || ! isset( $atts['theme'] ) ) {
  39. if ( current_user_can( 'edit_posts' ) ) {
  40. return __( 'No location or theme ID provided in the untappd-menu shortcode.', 'jetpack' );
  41. }
  42. return;
  43. }
  44. // Let's apply some defaults.
  45. $atts = shortcode_atts(
  46. array(
  47. 'location' => '',
  48. 'theme' => '',
  49. ),
  50. $atts,
  51. 'untappd-menu'
  52. );
  53. // We're going to clean the user input.
  54. $atts = array_map( 'absint', $atts );
  55. if ( $atts['location'] < 1 || $atts['theme'] < 1 ) {
  56. return;
  57. }
  58. static $untappd_menu = 1;
  59. $html = '<div id="menu-container-untappd-' . $untappd_menu . '" class="untappd-menu"></div>';
  60. $html .= '<script type="text/javascript">' . PHP_EOL;
  61. $html .= '!function(e,n){var t=document.createElement("script"),a=document.getElementsByTagName("script")[0];' . PHP_EOL;
  62. $html .= 't.async=1,a.parentNode.insertBefore(t,a),t.onload=t.onreadystatechange=function(e,a){' . PHP_EOL;
  63. $html .= '(a||!t.readyState||/loaded|complete/.test(t.readyState))&&(t.onload=t.onreadystatechange=null,t=void 0,a||n&&n())},' . PHP_EOL;
  64. $html .= 't.src=e}("https://embed-menu-preloader.untappdapi.com/embed-menu-preloader.min.js",function(){' . PHP_EOL;
  65. $html .= 'PreloadEmbedMenu( "menu-container-untappd-' . $untappd_menu . '",' . $atts['location'] . ',' . $atts['theme'] . ' )});' . PHP_EOL;
  66. $html .= '</script>';
  67. $untappd_menu++;
  68. return $html;
  69. }
  70. }
  71. new Jetpack_Untappd();