Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
642 lines (559 loc) · 22.6 KB
/
Copy pathfunctions.php
File metadata and controls
642 lines (559 loc) · 22.6 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?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_theme_style.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_action('wp_enqueue_scripts', array($this, 'enqueue_jquery'), 0);
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);
add_action('wp_ajax_upfront_log_client_error', array($this, 'log_client_error'));
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);
}
}
/**
* Ensure direct head scripts from Upfront and ClassicPress can use jQuery.
*/
public function enqueue_jquery () {
wp_enqueue_script('jquery');
wp_scripts()->add_data('jquery', 'group', 0);
}
/**
* Set up parent and child theme text domains
*/
public static function load_textdomain () {
$path = untrailingslashit(self::get_root_dir()) . '/languages';
$locale = apply_filters('theme_locale', determine_locale(), self::TextDomain);
$mofile = $path . '/' . self::TextDomain . '-' . $locale . '.mo';
if (is_readable($mofile)) {
load_textdomain(self::TextDomain, $mofile, $locale);
} else {
load_theme_textdomain(self::TextDomain, $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', __('Standard', '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;
$edit_url = home_url('/?editmode=true', is_ssl() ? "https" : null);
$item = array(
'id' => 'upfront-edit_layout',
'title' => '<span class="ab-icon"></span><span class="ab-label">' . __('Upfront', 'upfront') . '</span>',
'href' => $edit_url,
'meta' => array(
'class' => 'upfront-edit_layout upfront-editable_trigger',
'onclick' => 'if (!this.classList.contains("active")) { event.stopImmediatePropagation(); window.location.assign(this.href); return false; }',
'html' => '<style>#wp-admin-bar-upfront-edit_layout > .ab-item { opacity: 1 !important; cursor: pointer !important; }</style>',
),
);
$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);
}
public function log_client_error () {
if (!Upfront_Permissions::current(Upfront_Permissions::BOOT)) wp_die(0);
$message = isset($_POST['message']) ? sanitize_text_field(wp_unslash($_POST['message'])) : '';
if ($message) error_log('Upfront client error: ' . $message);
wp_die(1);
}
/**
* 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) {
} else {
$deps->add_script(admin_url('admin-ajax.php?action=wp_scripts'));
}
}
wp_enqueue_style('upfront-nouislider', self::get_root_url() . '/scripts/vendor/nouislider/nouislider.min.css', array(), '15.8.1');
wp_enqueue_style('upfront-flatpickr', self::get_root_url() . '/scripts/vendor/flatpickr/flatpickr.min.css', array(), '4.6.13');
/**
* 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');
if (Upfront_Permissions::current(Upfront_Permissions::BOOT) && wp_script_is('backbone', 'enqueued')) {
wp_add_inline_script('backbone', 'if (window.define && window.define.amd) { window._upfrontDefineAmd = window.define.amd; window.define.amd = false; }', 'before');
wp_add_inline_script('backbone', 'if (window._upfrontDefineAmd) { window.define.amd = window._upfrontDefineAmd; delete window._upfrontDefineAmd; }', 'after');
}
// 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
}
if (empty($_GET['editmode']) && !apply_filters('upfront-load-editor-dependencies', false)) {
do_action('upfront-core-inject_dependencies');
return false;
}
$url = self::get_root_url();
// Boot Edit Mode if requested by querystring or a virtual editor page.
if (apply_filters('upfront-auto-boot-editor', true)) {
echo upfront_boot_editor_trigger(apply_filters('upfront-editor-boot-mode', ''));
}
$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';
}
$upfront = $theme = wp_get_theme('upfront');
$main_source = 'scripts/setup.js';
$script_urls = array();
$require_source = 'scripts/require.js';
$require_source_path = dirname(__FILE__) . '/' . $require_source;
$script_urls[] = add_query_arg('mtime', filemtime($require_source_path), "{$url}/{$require_source}");
$locale = function_exists('determine_locale') ? determine_locale() : get_locale();
$l10n_path = dirname(__FILE__) . '/languages/upfront-' . $locale . '.mo';
$main_args = array(
'action' => 'upfront_load_main',
'ufver' => $upfront->version,
'locale' => $locale,
'l10n' => is_file($l10n_path) ? filemtime($l10n_path) : 0,
);
if (is_ssl()) $main_args['ssl'] = 1;
$script_urls[] = add_query_arg($main_args, admin_url('admin-ajax.php'));
$main_source_path = dirname(__FILE__) . '/' . $main_source;
$script_urls[] = add_query_arg('mtime', filemtime($main_source_path), "{$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', Upfront_Output::get_post_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(__('Bitte halte noch ein kleines bisschen länger durch.', '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');
echo '<script type="text/javascript">
(function () {
function reportUpfrontError(message) {
message = String(message || "Unknown client error");
var loadingText = document.querySelector(".upfront-loading-text");
if (loadingText) loadingText.textContent = "Upfront konnte nicht laden: " + message;
if (window.console && console.error) console.error("Upfront konnte nicht laden:", message);
if (window.jQuery) jQuery.post(' . json_encode(admin_url('admin-ajax.php')) . ', {
action: "upfront_log_client_error",
message: message
});
}
window.addEventListener("error", function (event) {
reportUpfrontError(event.message + (event.filename ? " in " + event.filename + ":" + event.lineno : ""));
});
window.addEventListener("unhandledrejection", function (event) {
reportUpfrontError(event.reason && event.reason.message ? event.reason.message : event.reason);
});
if (window.requirejs) {
requirejs.onError = function (error) {
var modules = error.requireModules && error.requireModules.length ? " [" + error.requireModules.join(", ") + "]" : "";
reportUpfrontError((error.message || error.requireType || error) + modules);
};
}
if (window.jQuery) {
jQuery(document).ajaxError(function (event, xhr, settings, error) {
if (!settings || String(settings.data || "").indexOf("upfront_") === -1) return;
reportUpfrontError("AJAX " + xhr.status + " " + (error || xhr.statusText));
});
}
})();
</script>';
}
/**
* 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' );
/**
* Registers Upfront avatar source option under Settings > General.
*/
function uf_register_general_avatar_settings() {
register_setting(
'general',
'upfront_external_avatars_enabled',
array(
'type' => 'integer',
'default' => 0,
'sanitize_callback' => 'uf_sanitize_external_avatars_enabled',
)
);
add_settings_field(
'upfront_external_avatars_enabled',
esc_html__('Upfront: Externe Avatare', 'upfront'),
'uf_render_external_avatars_setting_field',
'general'
);
}
add_action('admin_init', 'uf_register_general_avatar_settings');
/**
* Sanitizes external avatar setting value.
*
* @param mixed $value Raw value from the settings form.
*
* @return int 1 if enabled, 0 otherwise.
*/
function uf_sanitize_external_avatars_enabled($value) {
return !empty($value) ? 1 : 0;
}
/**
* Renders external avatars checkbox in Settings > General.
*/
function uf_render_external_avatars_setting_field() {
$value = (int) get_option('upfront_external_avatars_enabled', 0);
?>
<label for="upfront_external_avatars_enabled">
<input
type="checkbox"
name="upfront_external_avatars_enabled"
id="upfront_external_avatars_enabled"
value="1"
<?php checked(1, $value); ?>
/>
<?php esc_html_e('Externe Avatar-Dienste (z.B. Gravatar) fuer Upfront laden', 'upfront'); ?>
</label>
<p class="description">
<?php esc_html_e('Standard ist AUS. Wenn deaktiviert, nutzt Upfront nur lokale Platzhalterbilder.', 'upfront'); ?>
</p>
<?php
}
/**
* 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');
/**
* Temporary targeted debug trace for failing Upfront AJAX model requests.
* Writes to wp-content/upfront-ajax-debug.log regardless of PHP's global error_log target.
*/
function uf_trace_upfront_ajax_model_requests() {
if (!defined('DOING_AJAX') || !DOING_AJAX) return;
if (empty($_POST['action']) || $_POST['action'] !== 'upfront-wp-model') return;
$trace_id = substr(md5(uniqid('uf-ajax-', true)), 0, 12);
$start = microtime(true);
$log_file = WP_CONTENT_DIR . '/upfront-ajax-debug.log';
$model_action = isset($_POST['model_action']) ? (string)$_POST['model_action'] : '';
$keys = implode(',', array_keys((array)$_POST));
error_log(sprintf("[%s] start uri=%s action=%s model_action=%s keys=%s\n", $trace_id, isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '', isset($_POST['action']) ? $_POST['action'] : '', $model_action, $keys), 3, $log_file);
register_shutdown_function(function () use ($trace_id, $start, $log_file, $model_action) {
$elapsed = round((microtime(true) - $start) * 1000, 2);
$last = error_get_last();
if ($last && in_array($last['type'], array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR), true)) {
error_log(sprintf("[%s] fatal model_action=%s duration_ms=%s type=%s msg=%s file=%s line=%s\n", $trace_id, $model_action, $elapsed, $last['type'], $last['message'], $last['file'], $last['line']), 3, $log_file);
}
else {
error_log(sprintf("[%s] finish model_action=%s duration_ms=%s\n", $trace_id, $model_action, $elapsed), 3, $log_file);
}
});
}
add_action('init', 'uf_trace_upfront_ajax_model_requests', 1);