-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctions.php
501 lines (429 loc) · 16.4 KB
/
functions.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/**
* Main entry point for Upfront core
*
* This is where we set up Upfront main class
* which handles core bootstrap and execution context.
*/
require_once(dirname(__FILE__) . '/library/upfront_functions.php');
require_once(dirname(__FILE__) . '/library/upfront_functions_theme.php');
require_once(dirname(__FILE__) . '/library/class_upfront_cache_utils.php');
require_once(dirname(__FILE__) . '/library/class_upfront_permissions.php');
require_once(dirname(__FILE__) . '/library/class_upfront_registry.php');
require_once(dirname(__FILE__) . '/library/class_upfront_logger.php');
require_once(dirname(__FILE__) . '/library/class_upfront_debug.php');
require_once(dirname(__FILE__) . '/library/class_upfront_behavior.php');
require_once(dirname(__FILE__) . '/library/class_upfront_http_response.php');
require_once(dirname(__FILE__) . '/library/class_upfront_server.php');
require_once(dirname(__FILE__) . '/library/class_upfront_model.php');
require_once(dirname(__FILE__) . '/library/class_upfront_module_loader.php');
require_once(dirname(__FILE__) . '/library/class_upfront_theme.php');
require_once(dirname(__FILE__) . '/library/class_upfront_grid.php');
require_once(dirname(__FILE__) . '/library/class_upfront_style_preprocessor.php');
require_once(dirname(__FILE__) . '/library/class_upfront_output.php');
require_once(dirname(__FILE__) . '/library/class_upfront_endpoint.php');
require_once(dirname(__FILE__) . '/library/class_upfront_media.php');
require_once(dirname(__FILE__) . '/library/class_ufront_ufc.php');
require_once(dirname(__FILE__) . '/library/class_upfront_codec.php');
require_once(dirname(__FILE__) . '/library/class_upfront_compat.php');
require_once(dirname(__FILE__) . '/library/class_upfront_postpart.php');
require_once(dirname(__FILE__) . '/library/class_upfront_admin.php');
require_once(dirname(__FILE__) . '/library/class_upfront_compression.php');
Upfront_Behavior::debug()->set_baseline();
/**
* Main class
*/
class Upfront {
/**
* Theme text domain
*
* @var string
*/
const TextDomain = "upfront";
/**
* List of files to exclude in scanning
*
* @TODO refactor var name and location
*
* @var array
*/
public static $Excluded_Files = array(".", "..", ".DS_Store");
/**
* Servers to load automatically
*
* @var array
*/
private $_servers = array(
'ajax',
'javascript_main',
'stylesheet_main',
'stylesheet_editor',
'element_styles',
);
/**
* Debugger instance
*
* @var object
*/
private $_debugger;
/**
* Instantiate class - never to the outsiders
*/
private function __construct () {
$this->_debugger = Upfront_Debug::get_debugger();
$servers = apply_filters('upfront-servers', $this->_servers);
foreach ($servers as $component) $this->_run_server($component);
Upfront_ModuleLoader::serve();
do_action('upfront-core-initialized');
}
/**
* Public spawning interface
*/
public static function serve () {
$me = new self;
$me->_add_hooks();
$me->_add_supports();
}
/**
* Add basic set of hooks
*/
private function _add_hooks () {
if (Upfront_Behavior::compression()->get_option('freeze')) {
Upfront_DependencyCache_Server::serve();
}
add_filter('body_class', array($this, 'inject_grid_scope_class'));
add_action('wp_head', array($this, "inject_global_dependencies"), 0);
add_action('wp_footer', array($this, "inject_upfront_dependencies"), 99);
add_action('upfront-core-wp_dependencies', array($this, "inject_core_wp_dependencies"), 99);
add_action('admin_bar_menu', array($this, 'add_edit_menu'), 85);
if (is_admin()) {
require_once(dirname(__FILE__) . '/library/servers/class_upfront_admin.php');
if (class_exists('Upfront_Server_Admin')) Upfront_Server_Admin::serve();
}
if ( is_rtl() ) {
add_action('wp_head', array($this, "inject_rtl_dependencies"), 99);
}
}
/**
* Set up parent and child theme text domains
*/
public static function load_textdomain () {
$path = untrailingslashit(self::get_root_dir()) . '/languages';
load_theme_textdomain('upfront', $path);
// Now let's try the child theme...
$current = wp_get_theme();
$parent = $current->parent();
if (empty($parent)) return false; // Current theme is not a child theme, carry on...
if ('upfront' !== $parent->get_template()) return false; // Not an Upfront child, carry on...
$child_domain = $current->get('TextDomain');
if (!empty($child_domain) && 'upfront' !== $child_domain) {
load_child_theme_textdomain($child_domain, get_stylesheet_directory() . '/languages');
}
}
/**
* Add theme extra features
*/
private function _add_supports () {
add_theme_support('post-thumbnails');
add_theme_support('title-tag'); // Let WP deal with our theme titles
register_nav_menu('default', __('Default', 'upfront'));
// Do widget text
$do_widget_text = apply_filters(
'upfront-shortcode-enable_in_widgets',
(defined('UPFRONT_DISABLE_WIDGET_TEXT_SHORTCODES') && UPFRONT_DISABLE_WIDGET_TEXT_SHORTCODES ? false : true)
);
if ($do_widget_text) {
add_filter('widget_text', 'do_shortcode');
}
}
/**
* Instantiate auto-bootable server
*
* @param string $comp Server to boot
*/
private function _run_server ($comp) {
$class = Upfront_Server::name_to_class($comp);
if (!$class) return false;
call_user_func(array($class, 'serve'));
}
/**
* Gets parent theme root URL
*
* @return string
*/
public static function get_root_url () {
return get_template_directory_uri();
}
/**
* Gets parent theme root path
*
* @return string
*/
public static function get_root_dir () {
return get_template_directory();
}
/**
* Inject admin toolbar menu entry
*
* @param object $wp_admin_bar Toolbar
*/
public function add_edit_menu ($wp_admin_bar) {
if (!Upfront_Permissions::current(Upfront_Permissions::BOOT)) return false;
$item = array(
'id' => 'upfront-edit_layout',
'title' => '<span class="ab-icon"></span><span class="ab-label">' . __('Upfront', 'upfront') . '</span>',
'href' => (is_admin() ? home_url('/?editmode=true', is_ssl() ? "https" : null) : '#'),
'meta' => array(
'class' => 'upfront-edit_layout upfront-editable_trigger',
),
);
$permalinks_on = get_option('permalink_structure');
if (!$permalinks_on) {
// We're checking WP priv directly because we need an admin for this
if (current_user_can('manage_options')) {
$item['href'] = admin_url('/options-permalink.php');
unset($item['meta']);
} else {
$item = array(); // No such thing for non-admins
}
}
if (!empty($item)) {
$wp_admin_bar->add_menu($item);
}
// Change the existing nodes
$nodes = $wp_admin_bar->get_nodes();
if (!empty($nodes) && is_array($nodes)) {
foreach ($nodes as $node) {
if (!empty($node->href) && preg_match('/customize\.php/', $node->href)) {
$node->href = !empty($node->id) && 'customize-themes' === $node->id // WordPress doubles down on customizer endpoint for themes list too...
? admin_url('themes.php') // ... not gonna happen
: home_url('?editmode=true')
;
}
$wp_admin_bar->add_node($node);
}
}
// Action hook here, so other stuff can add its bar items
// (most notably, the exporter)
do_action('upfront-admin_bar-process', $wp_admin_bar, $item);
}
/**
* Appends grid scope class to the classes array
*
* @param array $cls Classes up to here
*
* @return array
*/
function inject_grid_scope_class ($cls) {
$grid = Upfront_Grid::get_grid();
$cls[] = $grid->get_grid_scope();
return $cls;
}
/**
* Handles core WP front-end dependencies injection
*/
public function inject_core_wp_dependencies () {
$deps = Upfront_CoreDependencies_Registry::get_instance();
if (Upfront_Behavior::compression()->has_experiments()) {
if (defined('DOING_AJAX') && DOING_AJAX) {
$deps->add_wp_script('jquery-ui-core');
$deps->add_wp_script('jquery-ui-widget');
$deps->add_wp_script('jquery-ui-mouse');
$deps->add_wp_script('jquery-effects-core');
$deps->add_wp_script('jquery-effects-slide');
$deps->add_wp_script('jquery-ui-draggable');
$deps->add_wp_script('jquery-ui-droppable');
$deps->add_wp_script('jquery-ui-resizable');
$deps->add_wp_script('jquery-ui-selectable');
$deps->add_wp_script('jquery-ui-sortable');
$deps->add_wp_script('jquery-ui-slider');
$deps->add_wp_script('jquery-ui-datepicker');
} else {
$deps->add_script(admin_url('admin-ajax.php?action=wp_scripts'));
}
} else {
// Non-experiments load
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-effects-core');
wp_enqueue_script('jquery-effects-slide');
wp_enqueue_script('jquery-ui-draggable');
wp_enqueue_script('jquery-ui-droppable');
wp_enqueue_script('jquery-ui-resizable');
wp_enqueue_script('jquery-ui-selectable');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('jquery-ui-slider');
wp_enqueue_script('jquery-ui-datepicker');
}
/**
* Todo Sam: make it cleaner
*/
wp_enqueue_script("wp_shortcode", "/wp-includes/js/shortcode.js", array("underscore"));
}
/**
* Handles global FE dependencies injection
*/
function inject_global_dependencies () {
$deps = Upfront_CoreDependencies_Registry::get_instance();
wp_enqueue_script('jquery');
// Basic styles for upfront to work are always loaded.
$global_style = Upfront_Behavior::compression()->has_experiments()
? '/styles/global.min.css'
: '/styles/global.css'
;
//wp_enqueue_style('upfront-global', self::get_root_url() . $global_style, array(), Upfront_ChildTheme::get_version());
$deps->add_header_style(self::get_root_url() . $global_style);
if (!Upfront_Permissions::current(Upfront_Permissions::BOOT)) {
// Don't queue the front grid if has permission to boot Upfront, queue editor grid instead
//wp_enqueue_style('upfront-front-grid', admin_url('admin-ajax.php?action=upfront_load_grid'), array(), Upfront_ChildTheme::get_version());
$deps->add_header_style(admin_url('admin-ajax.php?action=upfront_load_grid'));
}
if (Upfront_Permissions::current(Upfront_Permissions::BOOT)) {
do_action('upfront-core-wp_dependencies');
//wp_enqueue_style('upfront-editor-interface', self::get_root_url() . ( $this->_debugger->is_dev() ? '/styles/editor-interface.css' : '/styles/editor-interface.min.css' ) , array(), Upfront_ChildTheme::get_version());
$deps->add_header_style(self::get_root_url() . ( $this->_debugger->is_dev() ? '/styles/editor-interface.css' : '/styles/editor-interface.min.css' ));
$link_urls = array(
admin_url('admin-ajax.php?action=upfront_load_editor_grid'),
self::get_root_url() . '/scripts/chosen/chosen.min.css',
self::get_root_url() . '/styles/font-icons.css',
);
foreach ($link_urls as $url) {
$deps->add_style($url);
}
$deps->add_font('Open Sans', array(
'300',
'400',
'600',
'700',
'400italic',
'600italic',
));
add_action('wp_footer', array($this, 'add_responsive_css'));
}
}
/**
* Handles Upfront editor-specific FE dependencies injection
*/
function inject_upfront_dependencies () {
if (!Upfront_Permissions::current(Upfront_Permissions::BOOT)) {
do_action('upfront-core-inject_dependencies'); // Also trigger the dependencies injection hook
return false; // Do not inject for users that can't use this
}
$url = self::get_root_url();
// Boot Edit Mode if the querystring contains the editmode param
if (isset($_GET['editmode'])) echo upfront_boot_editor_trigger();
$storage_key = apply_filters('upfront-data-storage-key', Upfront_Layout::STORAGE_KEY);
$save_storage_key = $storage_key;
$is_ssl = is_ssl() ? '&ssl=1' : '';
if (Upfront_Behavior::debug()->is_dev() && current_user_can('switch_themes') && apply_filters('upfront-enable-dev-saving', true)) {
$save_storage_key .= '_dev';
}
$is_dev = $this->_debugger->is_dev();
$upfront = $theme = wp_get_theme('upfront');
$main_source = $is_dev ? "scripts/setup.js" : "build/main-$upfront->version.js";
$script_urls = array();
// We only need require.js on dev, for build it gets baked into main.js now
if ($is_dev) {
$script_urls[] = "{$url}/scripts/require.js";
}
$script_urls[] = admin_url('admin-ajax.php?action=upfront_load_main&ufver=' . $upfront->version . $is_ssl);
$script_urls[] = "{$url}/{$main_source}";
$deps = Upfront_CoreDependencies_Registry::get_instance();
foreach ($script_urls as $url) {
$deps->add_script($url);
}
$layout_post_data = json_encode(array(
'layout' => Upfront_EntityResolver::get_entity_ids(),
'post_id' => (is_singular() ? apply_filters('upfront-data-post_id', get_the_ID()) : false),
));
echo '<script type="text/javascript">
var _upfront_post_data=' . $layout_post_data . ';
var _upfront_storage_key = "' . esc_js($storage_key) . '";
var _upfront_save_storage_key = "' . esc_js($save_storage_key) . '";
var _upfront_stylesheet = "' . esc_js(get_stylesheet()) . '";
var _upfront_debug_mode = ' . (int)Upfront_Behavior::debug()->is_debug() . ';
var _upfront_please_hold_on = ' . json_encode(__('Please, hold on for just a little bit more', 'upfront')) . ';
</script>';
echo <<<EOAdditivemarkup
<div id="sidebar-ui" class="upfront-ui"></div>
<div id="settings" style="display:none"></div>
<div id="contextmenu" style="display:none"></div>
EOAdditivemarkup;
do_action('upfront-core-inject_dependencies');
}
/**
* Injects responsive CSS
*/
function add_responsive_css () {
include(self::get_root_dir() . '/styles/editor-interface-responsive.html');
}
/**
* Injects dependencies for rtl languages
*/
function inject_rtl_dependencies () {
wp_enqueue_style('upfront-global-rtl', self::get_root_url() . ( $this->_debugger->is_dev() ? "/styles/global-rtl.css" : "/styles/global-rtl.min.css" ), array(), Upfront_ChildTheme::get_version());
}
}
add_action('init', array('Upfront', 'serve'), 0);
add_action('after_setup_theme', array('Upfront', "load_textdomain"));
/**
* Filters wp caption atts to hide the caption in case show_caption is equal to "0"
*/
add_filter("shortcode_atts_caption", 'uf_shortcode_atts_caption', 10, 3);
/**
* Filters wp captions atts to remove the caption in case show_caption is equal to "0"
*
* @param array $out The output array of shortcode attributes.
* @param array $pairs The supported attributes and their defaults.
* @param array $atts The user defined shortcode attributes.
* @return mixed
*/
function uf_shortcode_atts_caption ($out, $pairs, $atts) {
if (isset( $atts['show_caption'] ) && "0" == $atts['show_caption']) {
$out['caption'] = " ";
}
return $out;
}
/**
* Filters image caption shortcode to generate uf caption specific markup
*/
add_filter("img_caption_shortcode", "uf_image_caption_shortcode", 10, 3);
/**
* Uses img_caption_shortcode to add support for UF image variants
*
* @param string $out Out
* @param array $attr Attributes attributed to the shortcode.
* @param string $content Shortcode content.
*
* @return string|void
*/
function uf_image_caption_shortcode ($out, $attr, $content) {
$is_wp_cation = strpos($attr["id"], "uinsert-" ) === false;
if ($is_wp_cation) return; // returning null let's wp do it's own logic and rendering for caption shortcode
$image_reg = preg_match('/src="([^"]+)"/', $content, $image_arr);
$href_reg = preg_match('/href="([^"]+)"/', $content, $anchor_arr);
$data = (object) shortcode_atts( array(
'id' => '',
'caption' => '',
'class' => '',
'uf_variant' => '',
'uf_isLocal' => true,
'uf_show_caption' => true,
'image' => $image_reg ? $image_arr[1] : "",
'linkUrl' => $href_reg ? $anchor_arr[1] : "",
), $attr, 'caption' );
return Upfront_Post_Data_PartView::get_post_image_markup($data);
}
/**
* Loads iconfont in admin to display toolbar icon.
*/
function uf_admin_bar_styles() {
wp_enqueue_style( 'uf-font-icons', get_template_directory_uri() . '/styles/font-icons.css');
}
add_action( 'admin_enqueue_scripts', 'uf_admin_bar_styles' );
/**
* Gets rid of the admin notice and declares support for Woo
*/
function uf_add_woocommerce_support() {
add_theme_support('woocommerce');
}
add_action('after_setup_theme', 'uf_add_woocommerce_support');