untappd-menu.php 2.0 KB

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