Skip to content

Instantly share code, notes, and snippets.

@westonruter westonruter/amp.php Secret forked from billerickson/amp.php
Last active Jun 6, 2019

Embed
What would you like to do?
<?php
/**
* AMP functionality
*
* @package EAStarter
* @author Bill Erickson
* @since 1.0.0
* @license GPL-2.0+
**/
/**
* Is AMP?
*
* Conditional tag
*
* @return bool Whether AMP.
*/
function ea_is_amp() {
return function_exists( 'is_amp_endpoint' ) && is_amp_endpoint();
}
/**
* Generate a class attribute and an AMP class attribute binding.
*
* @param string $default Default class value.
* @param string $active Value when the state enabled.
* @param string $state State variable to toggle based on.
* @return string HTML attributes.
*/
function ea_amp_class( $default, $active, $state ) {
$output = '';
if ( ea_is_amp() ) {
$output .= sprintf(
' [class]="%s"',
esc_attr(
sprintf(
'%s ? %s : %s',
wp_json_encode( $state ),
wp_json_encode( $default . ' ' . $active ),
wp_json_encode( $default )
)
)
);
}
$output .= sprintf( ' class="%s"', esc_attr( $default ) );
return $output;
}
/**
* Add the AMP toggle 'on' attribute.
*
* @param string $state State to toggle.
* @return string The 'on' attribute.
*/
function ea_amp_toggle( $state ) {
if ( ! ea_is_amp() ) {
return '';
}
return sprintf(
' on="%s',
esc_attr(
sprintf(
'tap:AMP.setState({ %s: ! %s })',
esc_js( $state ),
esc_js( $state )
)
)
);
}
/**
* AMP Nav Dropdown toggle and class attributes.
*
* @param string $theme_location Theme location.
* @param int $depth Depth.
* @return string The class and on attributes.
*/
function ea_amp_nav_dropdown( $theme_location = '', $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
You can’t perform that action at this time.