untappd-menu.php
| 1 | <?php |
| 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 | |
| 13 | class Jetpack_Untappd { |
| 14 | |
| 15 | function __construct() { |
| 16 | add_action( 'init', array( $this, 'action_init' ) ); |
| 17 | } |
| 18 | |
| 19 | function action_init() { |
| 20 | add_shortcode( 'untappd-menu', array( $this, 'menu_shortcode' ) ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * [untappd-menu] shortcode. |
| 25 | */ |
| 26 | static function menu_shortcode( $atts, $content = '' ) { |
| 27 | // Let's bail if we don't have location or theme. |
| 28 | if ( ! isset( $atts['location'] ) || ! isset( $atts['theme'] ) ) { |
| 29 | if ( current_user_can( 'edit_posts' ) ) { |
| 30 | return __( 'No location or theme ID provided in the untappd-menu shortcode.', 'jetpack' ); |
| 31 | } |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | // Let's apply some defaults. |
| 36 | $atts = shortcode_atts( |
| 37 | array( |
| 38 | 'location' => '', |
| 39 | 'theme' => '', |
| 40 | ), |
| 41 | $atts, |
| 42 | 'untappd-menu' |
| 43 | ); |
| 44 | |
| 45 | // We're going to clean the user input. |
| 46 | $atts = array_map( 'absint', $atts ); |
| 47 | |
| 48 | if ( $atts['location'] < 1 || $atts['theme'] < 1 ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | static $untappd_menu = 1; |
| 53 | |
| 54 | $html = '<div id="menu-container-untappd-' . $untappd_menu . '" class="untappd-menu"></div>'; |
| 55 | $html .= '<script type="text/javascript">' . PHP_EOL; |
| 56 | $html .= '!function(e,n){var t=document.createElement("script"),a=document.getElementsByTagName("script")[0];' . PHP_EOL; |
| 57 | $html .= 't.async=1,a.parentNode.insertBefore(t,a),t.onload=t.onreadystatechange=function(e,a){' . PHP_EOL; |
| 58 | $html .= '(a||!t.readyState||/loaded|complete/.test(t.readyState))&&(t.onload=t.onreadystatechange=null,t=void 0,a||n&&n())},' . PHP_EOL; |
| 59 | $html .= 't.src=e}("https://embed-menu-preloader.untappdapi.com/embed-menu-preloader.min.js",function(){' . PHP_EOL; |
| 60 | $html .= 'PreloadEmbedMenu( "menu-container-untappd-' . $untappd_menu . '",' . $atts['location'] . ',' . $atts['theme'] . ' )});' . PHP_EOL; |
| 61 | $html .= '</script>'; |
| 62 | |
| 63 | $untappd_menu++; |
| 64 | |
| 65 | return $html; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | new Jetpack_Untappd(); |
| 70 |