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