Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created June 5, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/81dd463c78aae9d70b441a310a4d427f to your computer and use it in GitHub Desktop.
Save billerickson/81dd463c78aae9d70b441a310a4d427f to your computer and use it in GitHub Desktop.
<?php
/**
* AMP functionality
*
* @package EAStarter
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
/**
* Is AMP?
* Conditional tag
*/
function ea_is_amp() {
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}
/**
* AMP Class
*
*/
function ea_amp_class( $default, $active, $variable ) {
$output = '';
if( ea_is_amp() ) {
$output .= ' [class]="' . $variable . ' ? \'' . $default . ' ' . $active . '\' : \'' . $default . '\'"';
}
$output .= ' class="' . $default . '"';
return $output;
}
/**
* AMP Toggle
*
*/
function ea_amp_toggle( $variable = '', $disable = array() ) {
if( ! ea_is_amp() )
return;
$settings = $variable . ': !' . $variable;
if( !empty( $disable ) ) {
foreach( $disable as $disable_var ) {
$settings .= ', ' . $disable_var . ': false';
}
}
return ' on="tap:AMP.setState({' . $settings . '})"';
}
/**
* AMP Nav Dropdown
*
*/
function ea_amp_nav_dropdown( $theme_location = false, $depth = 0 ) {
$key = 'nav';
if( !empty( $theme_location ) )
$key .= ucwords( $theme_location );
global $submenu_index;
$submenu_index++;
$key .= 'SubmenuExpanded' . $submenu_index;
if( 1 < $depth )
$key .= 'Depth' . $depth;
return ea_amp_toggle( $key ) . ea_amp_class( 'submenu-expand', 'expanded', $key );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment