-
Notifications
You must be signed in to change notification settings - Fork 7
/
bu-navigation.php
329 lines (272 loc) · 9.51 KB
/
bu-navigation.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Plugin Name: BU Navigation
* Plugin URI: http://developer.bu.edu/bu-navigation/
* Author: Boston University (IS&T)
* Author URI: http://sites.bu.edu/web/
* Description: Provides alternative navigation elements designed for blogs with large page counts
* Version: 1.3.4
* Text Domain: bu-navigation
* Domain Path: /languages
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package BU_Navigation
*/
/**
* Copyright 2014 by Boston University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
/*
@author Niall Kavanagh <[email protected]>
@author Gregory Cornelius <[email protected]>
@author Mike Burns <[email protected]>
@author Tyler Wiest <[email protected]>
@author Andrew Bauer <[email protected]>
@author Jonathan Williams <[email protected]>
*/
// Absolute server path to this plugin dir and file for use by included files.
define( 'BU_NAV_PLUGIN', __FILE__ );
define( 'BU_NAV_PLUGIN_DIR', dirname( __FILE__ ) );
// Primary navigation max items to display per level.
define( 'BU_NAVIGATION_PRIMARY_MAX', 6 );
// Primary navigation maxium depth.
define( 'BU_NAVIGATION_PRIMARY_DEPTH', 1 );
require_once BU_NAV_PLUGIN_DIR . '/includes/settings.php';
require_once BU_NAV_PLUGIN_DIR . '/includes/library.php';
require_once BU_NAV_PLUGIN_DIR . '/includes/class-tree-view.php';
require_once BU_NAV_PLUGIN_DIR . '/includes/class-reorder.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-model.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-format.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-nav-labels.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-widget.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-get-urls.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/data-active-section.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/filters.php';
require_once BU_NAV_PLUGIN_DIR . '/composer-includes/bu-navigation-core-widget/src/class-navigation-widget.php';
// Include Block root file.
require_once BU_NAV_PLUGIN_DIR . '/src/block.php';
/**
* Convenience class to wrap loading and init functions.
*/
class BU_Navigation_Plugin {
/**
* Singleton object that handles loading and initialization for the Admin interface.
*
* @var object $admin Instance of the BU_Navigation_Admin class.
*/
public $admin;
/**
* Singleton object that handles settings that affect the entire plugin.
*
* @var object $settings Instance of the BU_Navigation_Settings class.
*/
public $settings;
/**
* Plugin version number.
*
* Useful for cache busting on enqueued assets.
*
* @var string
*/
const VERSION = '1.3.4';
/**
* Plugin class constructor.
*
* Instantiates the settings object and registers hooks.
*
* @return void
*/
public function __construct() {
$this->settings = new BU_Navigation_Settings( $this );
$this->register_hooks();
}
/**
* Attach WordPress hook callbacks
*
* @return void
*/
public function register_hooks() {
add_action( 'plugins_loaded', array( $this, 'add_cache_groups' ) );
add_action( 'init', array( $this, 'init' ), 1 );
add_action( 'widgets_init', array( $this, 'load_widget' ) );
}
/**
* Calls wp_cache_add_non_persistent_groups()
*
* Calling wp_cache_add_non_persistent_groups() doesn't appear to do anything
* in modern WordPress?
*
* @return void
*/
public function add_cache_groups() {
if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {
wp_cache_add_non_persistent_groups( array( 'bu-navigation' ) );
}
}
/**
* Initialization function for navigation plugin
*
* @hook init
* @return void
*/
public function init() {
load_plugin_textdomain( 'bu-navigation', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
if ( defined( 'BU_TS_IS_LOADED' ) ) {
require_once BU_NAV_PLUGIN_DIR . '/tests/lettuce/sandbox-setup.php';
}
$this->load_extras();
if ( is_admin() ) {
$this->load_admin();
}
do_action( 'bu_navigation_init' );
}
/**
* Loads admin.php and instantiates the admin object.
*
* @return void
*/
public function load_admin() {
require_once dirname( __FILE__ ) . '/admin/admin.php';
$this->admin = new BU_Navigation_Admin( $this );
}
/**
* Initializes navigation widgets
*
* @return void
*/
public function load_widget() {
if ( ! is_blog_installed() || ! $this->supports( 'widget' ) ) {
return;
}
require_once dirname( __FILE__ ) . '/class-bu-widget-pages.php';
register_widget( 'BU_Widget_Pages' );
}
/**
* Loads plugins for this... plugin
* Any .php file placed in the extras directory will be automatically loaded.
*
* @return void
*/
public function load_extras() {
$pattern = sprintf( '%s/extras/*.php', BU_NAV_PLUGIN_DIR );
$files = glob( $pattern );
if ( ( is_array( $files ) ) && ( count( $files ) > 0 ) ) {
foreach ( $files as $filename ) {
@include_once $filename;
}
}
}
/**
* Navigation plugin features
*
* @todo discuss these open source defaults with BU
*
* The navigation plugin is configurable in a few different regards. This function returns an associative array
* of features, with the key representing the feature name and the value holding the default.
*
* bu-navigation-manager (on by default)
* - turn on or off the navigation management interfaces ("Edit Order" pages, "Navigation Attributes" metabox)
*
* bu-navigation-widget (on by default)
* - turn on or off the "Content Navigation" widget (on by default)
*
* bu-navigation-primary (off by default -- theme authors, use add_theme_support( 'bu-navigation-primary' ))
* - turn on or off the "Primary Navigation" appearance menu item
*
* bu-navigation-links
* - turn on or off the external link feature, include with 'page' post type nav menus (on by default)
*
* @return array Array of supported feature defaults.
*/
public function features() {
return array(
'manager' => true,
'widget' => true,
'links' => true,
'primary' => false,
);
}
/**
* Does the current install or theme support a navigation feature?
*
* There are two different ways to configure navigation features -- with PHP constants, or through the Theme Features API.
*
* These work as follows:
* 1. Define `BU_NAVIGATION_SUPPORTS_*` constant as true or false in wp-config.php or your theme's functions.php (highest priority)
* 2. Call add_theme_support( 'bu-navigation-*' ) within your theme's functions.php file (recommended for theme authors)
*
* @param string $feature Name of the feature.
* @return boolean Whether or not the feature is supported.
*/
public function supports( $feature ) {
$feature = strtolower( $feature );
$defaults = $this->features();
if ( ! in_array( $feature, array_keys( $defaults ) ) ) {
$this->log( '%s - Unknown feature: %s', __METHOD__, $feature );
return false;
}
$supported_const = 'BU_NAVIGATION_SUPPORTS_' . strtoupper( $feature );
$disabled = ( defined( $supported_const ) && constant( $supported_const ) == false );
$supported = ( defined( $supported_const ) && constant( $supported_const ) == true ) || $defaults[ $feature ];
$theme_supported = current_theme_supports( 'bu-navigation-' . $feature );
return ( ! $disabled && ( $supported || $theme_supported ) );
}
/**
* Gets the supported post_types by the bu-navigation plugin.
*
* @todo needs-unit-test
*
* @param boolean $include_link true|false link post_type is something special, so we don't always need it.
* @param string $output type of output (names|objects).
* @return array of post_type strings or objects depending on $output param
*/
public function supported_post_types( $include_link = false, $output = 'names' ) {
$post_types = get_post_types(
array(
'show_ui' => true,
'hierarchical' => true,
), $output
);
$post_types = apply_filters( 'bu_navigation_post_types', $post_types );
if ( $this->supports( 'links' ) && $include_link ) {
if ( 'names' === $output ) {
$post_types[ BU_NAVIGATION_LINK_POST_TYPE ] = BU_NAVIGATION_LINK_POST_TYPE;
} else {
$post_types[ BU_NAVIGATION_LINK_POST_TYPE ] = get_post_type_object( BU_NAVIGATION_LINK_POST_TYPE );
}
}
return $post_types;
}
/**
* Log messages through error_log. Except when running through cli.
* Prepends [bu-navigation]
*
* @return null
*/
public function log() {
if ( php_sapi_name() === 'cli' ) {
return;
}
$args = func_get_args();
$args[0] = sprintf( '[bu-navigation] %s', $args[0] );
error_log( call_user_func_array( 'sprintf', $args ) );
}
}
// Instantiate plugin (only once).
if ( ! isset( $GLOBALS['bu_navigation_plugin'] ) ) {
$GLOBALS['bu_navigation_plugin'] = new BU_Navigation_Plugin();
}