Screen.php
5.57 KB
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
<?php
/**
* WooCommerce Navigation Screen
*
* @package Woocommerce Navigation
*/
namespace Automattic\WooCommerce\Admin\Features\Navigation;
use Automattic\WooCommerce\Admin\Features\Navigation\Menu;
/**
* Contains logic for the WooCommerce Navigation menu.
*/
class Screen {
/**
* Class instance.
*
* @var Screen instance
*/
protected static $instance = null;
/**
* Screen IDs of registered pages.
*
* @var array
*/
protected static $screen_ids = array();
/**
* Registered post types.
*
* @var array
*/
protected static $post_types = array();
/**
* Registered taxonomies.
*
* @var array
*/
protected static $taxonomies = array();
/**
* Get class instance.
*/
final public static function instance() {
if ( ! static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Init.
*/
public function init() {
add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
}
/**
* Returns an array of filtered screen ids.
*/
public static function get_screen_ids() {
return apply_filters( 'woocommerce_navigation_screen_ids', self::$screen_ids );
}
/**
* Returns an array of registered post types.
*/
public static function get_post_types() {
return apply_filters( 'woocommerce_navigation_post_types', self::$post_types );
}
/**
* Returns an array of registered post types.
*/
public static function get_taxonomies() {
return apply_filters( 'woocommerce_navigation_taxonomies', self::$taxonomies );
}
/**
* Check if we're on a WooCommerce page
*
* @return bool
*/
public static function is_woocommerce_page() {
global $pagenow;
// Get taxonomy if on a taxonomy screen.
$taxonomy = '';
if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) {
if ( isset( $_GET['taxonomy'] ) ) { // phpcs:ignore CSRF ok.
$taxonomy = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); // phpcs:ignore CSRF ok.
}
}
$taxonomies = self::get_taxonomies();
// Get post type if on a post screen.
$post_type = '';
if ( in_array( $pagenow, array( 'edit.php', 'post.php', 'post-new.php' ), true ) ) {
if ( isset( $_GET['post'] ) ) { // phpcs:ignore CSRF ok.
$post_type = get_post_type( (int) $_GET['post'] ); // phpcs:ignore CSRF ok.
} elseif ( isset( $_GET['post_type'] ) ) { // phpcs:ignore CSRF ok.
$post_type = sanitize_text_field( wp_unslash( $_GET['post_type'] ) ); // phpcs:ignore CSRF ok.
}
}
$post_types = self::get_post_types();
// Get current screen ID.
$current_screen = get_current_screen();
$screen_ids = self::get_screen_ids();
$current_screen_id = $current_screen ? $current_screen->id : null;
if (
in_array( $post_type, $post_types, true ) ||
in_array( $taxonomy, $taxonomies, true ) ||
self::is_woocommerce_core_taxonomy( $taxonomy ) ||
in_array( $current_screen_id, $screen_ids, true )
) {
return true;
}
return false;
}
/**
* Check if a given taxonomy is a WooCommerce core related taxonomy.
*
* @param string $taxonomy Taxonomy.
* @return bool
*/
public static function is_woocommerce_core_taxonomy( $taxonomy ) {
if ( in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) {
return true;
}
if ( 'pa_' === substr( $taxonomy, 0, 3 ) ) {
return true;
}
return false;
}
/**
* Add navigation classes to body.
*
* @param string $classes Classes.
* @return string
*/
public function add_body_class( $classes ) {
if ( self::is_woocommerce_page() ) {
$classes .= ' has-woocommerce-navigation';
/**
* Adds the ability to skip disabling of the WP toolbar.
*
* @param boolean $bool WP Toolbar disabled.
*/
if ( apply_filters( 'woocommerce_navigation_wp_toolbar_disabled', true ) ) {
$classes .= ' is-wp-toolbar-disabled';
}
}
return $classes;
}
/**
* Adds a screen ID to the list of screens that use the navigtion.
* Finds the parent if none is given to grab the correct screen ID.
*
* @param string $callback Callback or URL for page.
* @param string|null $parent Parent screen ID.
*/
public static function add_screen( $callback, $parent = null ) {
global $submenu;
$plugin_page = self::get_plugin_page( $callback );
if ( ! $parent ) {
$parent = Menu::get_parent_key( $callback );
}
$screen_id = get_plugin_page_hookname( $plugin_page, $parent );
// This screen has already been added.
if ( in_array( $screen_id, self::$screen_ids, true ) ) {
return;
}
self::$screen_ids[] = $screen_id;
}
/**
* Get the plugin page slug.
*
* @param string $callback Callback.
* @return string
*/
public static function get_plugin_page( $callback ) {
$url = Menu::get_callback_url( $callback );
$parts = wp_parse_url( $url );
if ( ! isset( $parts['query'] ) ) {
return $callback;
}
parse_str( $parts['query'], $query );
if ( ! isset( $query['page'] ) ) {
return $callback;
}
$plugin_page = wp_unslash( $query['page'] );
$plugin_page = plugin_basename( $plugin_page );
return $plugin_page;
}
/**
* Register post type for use in WooCommerce Navigation screens.
*
* @param string $post_type Post type to add.
*/
public static function register_post_type( $post_type ) {
if ( ! in_array( $post_type, self::$post_types, true ) ) {
self::$post_types[] = $post_type;
}
}
/**
* Register taxonomy for use in WooCommerce Navigation screens.
*
* @param string $taxonomy Taxonomy to add.
*/
public static function register_taxonomy( $taxonomy ) {
if ( ! in_array( $taxonomy, self::$taxonomies, true ) ) {
self::$taxonomies[] = $taxonomy;
}
}
}