forked from woocommerce/storefront-hamburger-menu
/
storefront-hamburger-menu.php
259 lines (228 loc) · 7.22 KB
/
storefront-hamburger-menu.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Plugin Name: Storefront Hamburger Menu
* Plugin URI: https://wordpress.org/plugins/storefront-hamburger-menu/
* Description: Storefront Hamburger Menu turns the default handheld navigation into an off-screen sidebar menu with a "hamburger" toggle.
* Version: 1.0.0
* Author: WooThemes
* Author URI: http://woothemes.com/
* Requires at least: 4.0.0
* Tested up to: 4.4.0
*
* Text Domain: storefront-hamburger-menu
* Domain Path: /languages/
*
* @package Storefront_Hamburger_Menu
* @category Core
* @author Tiago Noronha
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* Returns the main instance of Storefront_Hamburger_Menu to prevent the need to use globals.
*
* @since 1.0.0
* @return object Storefront_Hamburger_Menu
*/
function Storefront_Hamburger_Menu() {
return Storefront_Hamburger_Menu::instance();
} // End Storefront_Hamburger_Menu()
Storefront_Hamburger_Menu();
/**
* Main Storefront_Hamburger_Menu Class
*
* @class Storefront_Hamburger_Menu
* @version 1.0.0
* @since 1.0.0
* @package Storefront_Hamburger_Menu
*/
final class Storefront_Hamburger_Menu {
/**
* Storefront_Hamburger_Menu The single instance of Storefront_Hamburger_Menu.
* @var object
* @access private
* @since 1.0.0
*/
private static $_instance = null;
/**
* The token.
* @var string
* @access public
* @since 1.0.0
*/
public $token;
/**
* The version number.
* @var string
* @access public
* @since 1.0.0
*/
public $version;
/**
* Constructor function.
* @access public
* @since 1.0.0
* @return void
*/
public function __construct() {
$this->token = 'storefront-hamburger-menu';
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->version = '1.0.0';
register_activation_hook( __FILE__, array( $this, 'install' ) );
add_action( 'init', array( $this, 'shm_load_plugin_textdomain' ) );
add_action( 'init', array( $this, 'shm_setup' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'shm_plugin_links' ) );
}
/**
* Main Storefront_Hamburger_Menu Instance
*
* Ensures only one instance of Storefront_Hamburger_Menu is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see Storefront_Hamburger_Menu()
* @return Main Storefront_Hamburger_Menu instance
*/
public static function instance() {
if ( is_null( self::$_instance ) )
self::$_instance = new self();
return self::$_instance;
} // End instance()
/**
* Load the localisation file.
* @access public
* @since 1.0.0
* @return void
*/
public function shm_load_plugin_textdomain() {
load_plugin_textdomain( 'storefront-hamburger-menu', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Cloning is forbidden.
*
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), '1.0.0' );
}
/**
* Unserializing instances of this class is forbidden.
*
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ), '1.0.0' );
}
/**
* Plugin page links
*
* @since 1.0.0
*/
public function shm_plugin_links( $links ) {
$plugin_links = array(
'<a href="http://support.woothemes.com/">' . __( 'Support', 'storefront-hamburger-menu' ) . '</a>',
);
return array_merge( $plugin_links, $links );
}
/**
* Installation.
* Runs on activation. Logs the version number and assigns a notice message to a WordPress option.
* @access public
* @since 1.0.0
* @return void
*/
public function install() {
$this->_log_version_number();
}
/**
* Log the plugin version number.
* @access private
* @since 1.0.0
* @return void
*/
private function _log_version_number() {
// Log the version number.
update_option( $this->token . '-version', $this->version );
}
/**
* Setup all the things.
* Only executes if Storefront or a child theme using Storefront as a parent is active and the extension specific filter returns true.
* Child themes can disable this extension using the storefront_hamburger_menu_supported filter
* @return void
*/
public function shm_setup() {
$theme = wp_get_theme();
if ( 'Storefront' == $theme->name || 'storefront' == $theme->template && apply_filters( 'storefront_hamburger_menu_supported', true ) ) {
add_action( 'wp_enqueue_scripts', array( $this, 'shm_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'shm_add_customizer_css' ), 999 );
add_action( 'customize_preview_init', array( $this, 'shm_customize_preview_js' ) );
add_filter( 'body_class', array( $this, 'shm_body_class' ) );
}
else {
add_action( 'admin_notices', array( $this, 'shm_install_storefront_notice' ) );
}
}
/**
* Storefront install
* If the user activates the plugin while having a different parent theme active, prompt them to install Storefront.
* @since 1.0.0
* @return void
*/
public function shm_install_storefront_notice() {
echo '<div class="notice is-dismissible updated">
<p>' . __( 'Storefront Hamburger Menu requires that you use Storefront as your parent theme.', 'storefront-hamburger-menu' ) . ' <a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=install-theme&theme=storefront' ), 'install-theme_storefront' ) ) .'">' . __( 'Install Storefront now', 'storefront-hamburger-menu' ) . '</a></p>
</div>';
}
/**
* Enqueue CSS and custom styles.
* @since 1.0.0
* @return void
*/
public function shm_styles() {
wp_enqueue_style( 'shm-styles', plugins_url( '/assets/css/style.css', __FILE__ ), '', '1.0.0' );
}
/**
* Add CSS in <head> for styles handled by the Customizer
*
* @since 1.0.0
*/
public function shm_add_customizer_css() {
$header_background_color = storefront_sanitize_hex_color( get_theme_mod( 'storefront_header_background_color', apply_filters( 'storefront_default_header_background_color', '#2c2d33' ) ) );
$header_link_color = storefront_sanitize_hex_color( get_theme_mod( 'storefront_header_link_color', apply_filters( 'storefront_default_header_link_color', '#ffffff' ) ) );
$wc_style = '
@media screen and (max-width: 768px) {
.menu-toggle {
color: ' . $header_link_color . ';
}
.menu-toggle:hover {
color: ' . storefront_adjust_color_brightness( $header_link_color, -100 ) . ';
}
.main-navigation div.menu,
.main-navigation .handheld-navigation {
background-color: ' . $header_background_color . ';
}
.main-navigation ul li a,
ul.menu li a {
color: ' . $header_link_color . ';
}
}
';
wp_add_inline_style( 'storefront-woocommerce-style', $wc_style );
}
/**
* Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
*
* @since 1.0.0
*/
public function shm_customize_preview_js() {
wp_enqueue_script( 'shm-customizer', plugins_url( '/assets/js/customizer.min.js', __FILE__ ), array( 'customize-preview' ), '1.1', true );
}
/**
* Storefront Extension Boilerplate Body Class
* Adds a class based on the extension name and any relevant settings.
*/
public function shm_body_class( $classes ) {
$classes[] = 'storefront-hamburger-menu-active';
return $classes;
}
} // End Class