forked from billerickson/EA-Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamp.php
99 lines (87 loc) · 1.99 KB
/
amp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?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();
}
/**
* 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\'',
$state,
$default . ' ' . $active,
$default
)
)
);
}
$output .= sprintf( ' class="%s"', esc_attr( $default ) );
return $output;
}
/**
* Add the AMP toggle 'on' attribute.
*
* @param string $state State to toggle.
* @param array $disable, list of states to disable
* @return string The 'on' attribute.
*/
function ea_amp_toggle( $state = '', $disable = array() ) {
if( ! ea_is_amp() )
return;
$settings = sprintf(
'%1$s: ! %1$s',
esc_js( $state )
);
if( !empty( $disable ) ) {
foreach( $disable as $disableState ) {
$settings .= sprintf(
', %s: false',
esc_js( $disableState )
);
}
}
return sprintf(
' on="tap:AMP.setState({%s})"',
$settings
);
}
/**
* 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 = 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 );
}