| 1 |
<?php |
| 2 |
|
| 3 |
namespace Microthemer; |
| 4 |
|
| 5 |
// Stop direct call |
| 6 |
if (!defined( 'ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/* |
| 11 |
* AssetLoad |
| 12 |
* |
| 13 |
* Minimal functionality to add CSS, JS (user animation), body classes, Google Fonts, and the viewport meta tag. |
| 14 |
* This file is copied to /wp-content/micro-themes upon activation, to support deactivating Microthemer. |
| 15 |
* It can be manually included as a standalone file using require(ABSPATH . '/wp-content/micro-themes/AssetLoad.php'); |
| 16 |
* Alternatively, it can be installed as a simple plugin: microloader.zip |
| 17 |
*/ |
| 18 |
|
| 19 |
if (!class_exists('\Microthemer\AssetLoad')){ |
| 20 |
|
| 21 |
class AssetLoad { |
| 22 |
|
| 23 |
use FrontAndBackTrait; |
| 24 |
|
| 25 |
var $pluginVersion = '7.5.3.9'; |
| 26 |
protected $isBlockEditorScreen; |
| 27 |
var $logicSettings = array(); |
| 28 |
public $context = 'load'; |
| 29 |
|
| 30 |
public $isFrontend = true; |
| 31 |
public $isAdminArea = false; |
| 32 |
|
| 33 |
var $contentClass; |
| 34 |
var $cssClass; |
| 35 |
var $preferences = array(); |
| 36 |
var $draft = false; |
| 37 |
var $assetLoadingKey = 'asset_loading_published'; |
| 38 |
var $globalStylesheetRequiredKey = "global_stylesheet_required_published"; |
| 39 |
var $globalJSRequiredKey = "load_js_published"; |
| 40 |
var $defaultActionHookOrder = 999999; |
| 41 |
var $asyncAssets = array( |
| 42 |
'css' => array(), |
| 43 |
'js' => array(), |
| 44 |
); |
| 45 |
var $folderLoading = array(); |
| 46 |
var $folderLoadingChecked = false; |
| 47 |
var $mtv; |
| 48 |
var $mts; |
| 49 |
var $cacheParam; |
| 50 |
var $rootUrl; |
| 51 |
var $rootDir; |
| 52 |
var $fileStub = 'active'; |
| 53 |
var $menuSlugs = array(); // for adding first/last classes to menus |
| 54 |
var $menuItemCount = 0; |
| 55 |
var $devMode = false; |
| 56 |
|
| 57 |
public $hooks = array( |
| 58 |
'head' => 'wp_head', |
| 59 |
'footer' => 'wp_footer', |
| 60 |
'enqueue_scripts' => 'wp_enqueue_scripts' |
| 61 |
); |
| 62 |
|
| 63 |
function __construct($standalone = false){ |
| 64 |
|
| 65 |
// bail if this class is being called as a standalone script, but Microthemer is active |
| 66 |
if ($standalone && defined('MT_IS_ACTIVE')){ |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// all good, initiate |
| 71 |
$this->init(); |
| 72 |
} |
| 73 |
|
| 74 |
function init(){ |
| 75 |
|
| 76 |
if (defined('TVR_DEV_MODE') && !empty(constant('TVR_DEV_MODE'))){ |
| 77 |
$this->devMode = true; |
| 78 |
} |
| 79 |
|
| 80 |
$p = $this->getPreferences(); |
| 81 |
|
| 82 |
$this->checkAdminVsFront(); |
| 83 |
|
| 84 |
// Maybe support Amender edits |
| 85 |
if ($this->hasContentCapability()){ |
| 86 |
$this->contentClass = new Content\AssetLoadContent($this, $this->devMode); |
| 87 |
} |
| 88 |
|
| 89 |
// Set the app name |
| 90 |
$this->setAppName(); |
| 91 |
|
| 92 |
// setup version and path vars |
| 93 |
$this->mtv = 'mtv=' . (!empty($p['version']) ? $p['version'] : 7); |
| 94 |
$this->mts = 'mts=' . (!empty($p['num_saves']) ? $p['num_saves'] : 0); |
| 95 |
$this->cacheParam = $this->getCacheParam(); |
| 96 |
$this->getPaths(); |
| 97 |
|
| 98 |
$this->hookCaptureTemplate($p); |
| 99 |
|
| 100 |
// if admin, delay hook until current screen can be checked |
| 101 |
$this->deferHookIfAdmin('current_screen', 'hookCSS'); |
| 102 |
|
| 103 |
if ($this->isFrontend){ |
| 104 |
$this->hookJS($p); |
| 105 |
$this->hookViewportMeta($p); |
| 106 |
$this->hookClasses($p); |
| 107 |
$this->hookLegacy($p); |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
// For admin, we need to use enqueue_block_assets on block editor pages |
| 113 |
// so defer setting CSS hook until the necessary WP functions are available to check that |
| 114 |
function deferHookIfAdmin($hookAction, $hookMethod){ |
| 115 |
if ($this->isAdminArea){ |
| 116 |
add_action($hookAction, array(&$this, $hookMethod)); |
| 117 |
} else { |
| 118 |
$this->$hookMethod(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// get the directory path for /wp-content/micro-themes/, accounting for multisite |
| 123 |
function getPaths(){ |
| 124 |
|
| 125 |
$microDir = '/micro-themes/'; |
| 126 |
$url = content_url(); |
| 127 |
$dir = WP_CONTENT_DIR; |
| 128 |
|
| 129 |
// Not multisite |
| 130 |
if (!is_multisite()) { |
| 131 |
$this->rootUrl = $url . $microDir; |
| 132 |
$this->rootDir = $dir . $microDir; |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// It is multisite, resolve the path |
| 137 |
$blog_id = get_current_blog_id(); |
| 138 |
$primarySite = $blog_id === 1; |
| 139 |
$sitesPath = file_exists( $dir . "/blogs.dir/") |
| 140 |
? '/blogs.dir' |
| 141 |
: '/uploads/sites'; |
| 142 |
$subPath = $primarySite |
| 143 |
? $microDir |
| 144 |
: '/' . $blog_id . $microDir; |
| 145 |
|
| 146 |
$this->rootUrl = $url . $sitesPath . $subPath; |
| 147 |
$this->rootDir = $dir . $sitesPath . $subPath; |
| 148 |
} |
| 149 |
|
| 150 |
function checkAdminVsFront(){ |
| 151 |
|
| 152 |
if (is_admin()){ |
| 153 |
$this->isFrontend = false; |
| 154 |
$this->isAdminArea = true; |
| 155 |
$this->hooks = array( |
| 156 |
'head' => 'admin_enqueue_scripts', |
| 157 |
'footer' => 'admin_footer', |
| 158 |
'enqueue_scripts' => 'admin_enqueue_scripts' |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
function checkBlockEditorScreen(){ |
| 164 |
|
| 165 |
if ($this->isAdminArea){ |
| 166 |
|
| 167 |
global $pagenow; |
| 168 |
|
| 169 |
$screen = function_exists('get_current_screen') |
| 170 |
? get_current_screen() |
| 171 |
: null; |
| 172 |
|
| 173 |
$this->isBlockEditorScreen = !empty($screen->is_block_editor) || $pagenow === 'site-editor.php'; |
| 174 |
|
| 175 |
return $this->isBlockEditorScreen; |
| 176 |
} |
| 177 |
|
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
function supportAdminAssets(){ |
| 182 |
$p = $this->preferences; |
| 183 |
return !empty($p['admin_asset_loading']) || !empty($p['admin_asset_editing']); |
| 184 |
} |
| 185 |
|
| 186 |
function getCacheParam(){ |
| 187 |
return $this->mts; |
| 188 |
} |
| 189 |
|
| 190 |
function getCSSActionHook($p){ |
| 191 |
$logic_test = isset($_GET['test_logic']); |
| 192 |
|
| 193 |
return !empty($p['stylesheet_in_footer']) && !$logic_test |
| 194 |
? $this->hooks['footer'] |
| 195 |
// with test_logic, we want logic checked before output buffer HTML in head screws up JSON |
| 196 |
: (!empty($p['stylesheet_order']) && !$logic_test |
| 197 |
? $this->hooks['head'] |
| 198 |
: ($this->checkBlockEditorScreen() |
| 199 |
? 'enqueue_block_assets' |
| 200 |
: $this->hooks['enqueue_scripts'] |
| 201 |
) |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
function getCSSActionOrder($p){ |
| 206 |
return !empty($p['stylesheet_order']) && !isset($_GET['test_logic']) |
| 207 |
? intval($p['stylesheet_order']) |
| 208 |
: $this->defaultActionHookOrder; |
| 209 |
} |
| 210 |
|
| 211 |
function hookCSS(){ |
| 212 |
|
| 213 |
$p = $this->preferences; |
| 214 |
|
| 215 |
// do not load assets when a builder has replaced the frontend with a UI |
| 216 |
if ($this->isBricksUi()){ |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
// determine which action to hook into |
| 221 |
$action_hook = $this->getCSSActionHook($p); |
| 222 |
|
| 223 |
// determine the action execution order |
| 224 |
$action_order = $this->getCSSActionOrder($p); |
| 225 |
|
| 226 |
// add CSS |
| 227 |
add_action($action_hook, array(&$this, 'addCSS'), $action_order); |
| 228 |
|
| 229 |
// Also add CSS to the login page unless specified otherwise |
| 230 |
if ($this->isFrontend && !empty($p['global_styles_on_login'])){ |
| 231 |
add_action('login_enqueue_scripts', array(&$this, 'addCSS'), $action_order); |
| 232 |
} |
| 233 |
|
| 234 |
// allow style tag atts to be updated if they are async |
| 235 |
add_filter( 'style_loader_tag', array(&$this, 'asyncStyleTag'), 10, 4 ); |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
function addInsteadOfEnqueue(){ |
| 240 |
return !empty($this->preferences['stylesheet_order']) |
| 241 |
|| !empty($this->preferences['stylesheet_in_footer']); |
| 242 |
} |
| 243 |
|
| 244 |
function addGlobalGoogleFonts($p, $add){ |
| 245 |
|
| 246 |
if (!empty($p['g_fonts_used'])){ |
| 247 |
|
| 248 |
$google_url = !empty($p['g_url_with_subsets']) |
| 249 |
? $p['g_url_with_subsets'] |
| 250 |
: (!empty($p['gfont_subset']) |
| 251 |
? $p['g_url'].$p['gfont_subset'] |
| 252 |
: $p['g_url']); |
| 253 |
|
| 254 |
$this->enqueueOrAdd($add, 'microthemer_g_font', $google_url); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
function addGlobalStylesheet(&$p, $add){ |
| 259 |
|
| 260 |
$path = $this->fileStub . '-styles.css'; |
| 261 |
|
| 262 |
if ( |
| 263 |
file_exists($this->rootDir . $path) |
| 264 |
&& !empty($p[$this->globalStylesheetRequiredKey]) || !isset($p[$this->globalStylesheetRequiredKey]) |
| 265 |
){ |
| 266 |
|
| 267 |
$this->enqueueOrAdd( |
| 268 |
$add, |
| 269 |
'microthemer', |
| 270 |
$this->rootUrl . $path . '?' . $this->cacheParam |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
function addCSS(){ |
| 276 |
|
| 277 |
global $wp_styles; |
| 278 |
|
| 279 |
$p = &$this->preferences; |
| 280 |
$asset_loading = !empty($p[$this->assetLoadingKey]) |
| 281 |
? $p[$this->assetLoadingKey] |
| 282 |
: array(); |
| 283 |
$action_hook = $this->getCSSActionHook($p); |
| 284 |
$origConcatSettings = $wp_styles->do_concat; |
| 285 |
|
| 286 |
// ensure that do_item() doesn't echo immediately as this could echo styles before the <html> |
| 287 |
// When using enqueue_block_assets on the Gutenberg Edit Post/Page screen |
| 288 |
// The code below means we shouldn't need the deferredHandles workaround |
| 289 |
if ($action_hook === 'enqueue_block_assets'){ |
| 290 |
$wp_styles->do_concat = true; |
| 291 |
} |
| 292 |
|
| 293 |
// if stylesheet order is set, we add to $wp_styles object rather than enqueuing |
| 294 |
$add = $this->addInsteadOfEnqueue(); |
| 295 |
|
| 296 |
// if Auth, we add a placeholder to update conditional styles on synced other tabs |
| 297 |
$this->addMTPlaceholder(); |
| 298 |
|
| 299 |
// maybe load Tailwind - frontend and back so editor matches up with frontend styling |
| 300 |
$this->contentMethod('maybeLoadTailwind', array(&$p, $add)); |
| 301 |
|
| 302 |
// Global CSS is just for the frontend |
| 303 |
if ($this->isFrontend |
| 304 |
|| ($this->isBlockEditorScreen && !empty($p['admin_asset_loading'])) |
| 305 |
){ |
| 306 |
|
| 307 |
// enqueue any Google Fonts |
| 308 |
$this->addGlobalGoogleFonts($p, $add); |
| 309 |
|
| 310 |
// load the Microthemer stylesheet if new preference hasn't been set, or it has been set to true |
| 311 |
$this->addGlobalStylesheet($p, $add); |
| 312 |
} |
| 313 |
|
| 314 |
// Load conditional assets |
| 315 |
if (isset($asset_loading['logic']) && (count($asset_loading['logic']) || isset($_GET['test_logic']) )){ |
| 316 |
$this->conditionalAssets($asset_loading['logic']); |
| 317 |
} |
| 318 |
|
| 319 |
// Now we have folderLoading config, queue scripts and maybe hook HTML mods |
| 320 |
$this->contentMethod('initContentAmendments'); |
| 321 |
|
| 322 |
// insert MT interface CSS here if AssetAuth child class is running |
| 323 |
$this->addMTCSS(); |
| 324 |
|
| 325 |
// restore the default do_concat setting |
| 326 |
// (which may have been changed if using enqueue_block_assets hook) |
| 327 |
$wp_styles->do_concat = $origConcatSettings; |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
function supportLogicTest(){ |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
function doLogicTest($folders, $logic, $forceAll = false){} |
| 336 |
|
| 337 |
function loadConditionalAssets($folders, $logic){ |
| 338 |
|
| 339 |
// bail if the user has not enabled admin assets |
| 340 |
if ($this->isAdminArea && !$this->supportAdminAssets()){ |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
$subDir = 'mt/conditional/' . $this->fileStub . '/'; |
| 345 |
$dir = $this->rootDir . $subDir; |
| 346 |
$url = $this->rootUrl . $subDir; |
| 347 |
$add = $this->addInsteadOfEnqueue(); |
| 348 |
$foldersDone = array(); |
| 349 |
|
| 350 |
foreach ($folders as $folder){ |
| 351 |
|
| 352 |
if (isset($folder['expr']) && !isset($foldersDone[$folder['slug']])){ |
| 353 |
|
| 354 |
$slug = $folder['slug']; |
| 355 |
$this->folderLoading[$slug] = 0; |
| 356 |
$result = $logic->result(trim($folder['expr'])); |
| 357 |
$cssFileName = $slug . '.css'; |
| 358 |
$cssFile = $dir . $slug . '.css'; |
| 359 |
$fileExists = file_exists($dir . $cssFileName); |
| 360 |
|
| 361 |
$this->folderLoading[$slug] = $result |
| 362 |
? ($fileExists |
| 363 |
? (is_string($result) ? $result : 1) // preserve string result like 'blocksOnly' |
| 364 |
: 'empty') |
| 365 |
: 0; |
| 366 |
|
| 367 |
// load content if true and the file exists |
| 368 |
if ($result && $fileExists){ |
| 369 |
|
| 370 |
$inline = empty($folder['css_external']); |
| 371 |
$async = !empty($folder['css_async']); |
| 372 |
//echo '$cssFile: ' . $url . $cssFileName . '?' . $this->cacheParam; |
| 373 |
|
| 374 |
// Force a non-cached read of the CSS file for inline delivery. |
| 375 |
if ($inline){ |
| 376 |
clearstatcache( true, $cssFile ); |
| 377 |
} |
| 378 |
|
| 379 |
// load the CSS file |
| 380 |
if (!isset($_GET['get_front_data'])){ |
| 381 |
$this->enqueueOrAdd( |
| 382 |
($add || $inline || $async), |
| 383 |
'microthemer-'.$slug, //.'-css', |
| 384 |
$url . $cssFileName . '?' . $this->cacheParam, |
| 385 |
array( |
| 386 |
'inline' => $inline, |
| 387 |
'async' => $async, |
| 388 |
'code' => $inline ? file_get_contents($cssFile) : false, |
| 389 |
'deps' => array('microthemer-css') |
| 390 |
) |
| 391 |
); |
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
// ensure a folder never runs twice |
| 398 |
// which can happen if logic array has repetition (due to an unsolved bug) |
| 399 |
$foldersDone[$folder['slug']] = 1; |
| 400 |
} |
| 401 |
|
| 402 |
/*wp_die('<pre>loading: '.print_r([ |
| 403 |
has_template("wp_template_part", "twentytwentyfive//header", "Template part - Header"), |
| 404 |
$this->folderLoading], true).'</pre>');*/ |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
function asyncStyleTag($html, $handle, $href, $media){ |
| 409 |
|
| 410 |
$noscript = $this->context === 'load' ? '<noscript>' . $html . '</noscript>' : ''; |
| 411 |
|
| 412 |
if (!empty($this->asyncAssets['css'][$handle])){ |
| 413 |
|
| 414 |
// The media print method - works in all browsers and doesn't change resource priority. |
| 415 |
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- Rewriting an already-enqueued style tag for async load. |
| 416 |
$html = '<link rel="stylesheet" href="'.esc_url($href).'" id="'.esc_attr($handle).'-css" |
| 417 |
media="print" onload="this.onload=null; this.media=\''.esc_attr($media).'\';">' |
| 418 |
. $noscript; |
| 419 |
} |
| 420 |
|
| 421 |
return $html; |
| 422 |
} |
| 423 |
|
| 424 |
function conditionalAssets($folders, $forceAll = false, $doingLogicTest = false){ |
| 425 |
|
| 426 |
//echo 'condAssets '; |
| 427 |
|
| 428 |
if (!class_exists('Microthemer\Logic')){ |
| 429 |
require_once dirname(__FILE__) . '/Logic.php'; |
| 430 |
} if (!class_exists('Microthemer\Helper')){ |
| 431 |
require_once dirname(__FILE__) . '/Helper.php'; |
| 432 |
} |
| 433 |
|
| 434 |
$logic = new Logic($this->logicSettings); |
| 435 |
|
| 436 |
if (!$doingLogicTest && ($this->supportLogicTest() || $forceAll)){ |
| 437 |
$this->doLogicTest($folders, $logic, $forceAll); |
| 438 |
} |
| 439 |
|
| 440 |
else { |
| 441 |
$this->loadConditionalAssets($folders, $logic); |
| 442 |
|
| 443 |
// Now we have folderLoading config, queue scripts and maybe hook HTML mods |
| 444 |
//$this->contentMethod('initContentAmendments'); |
| 445 |
} |
| 446 |
|
| 447 |
$this->folderLoadingChecked = true; |
| 448 |
|
| 449 |
} |
| 450 |
|
| 451 |
function hookViewportMeta($p){ |
| 452 |
if (!empty($p['initial_scale'])) { |
| 453 |
add_action($this->hooks['head'], array(&$this, 'addViewportMeta')); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
function hookClasses($p){ |
| 458 |
add_filter('body_class', array(&$this, 'addBodyClasses')); |
| 459 |
} |
| 460 |
|
| 461 |
// legacy functionality that is not enabled by default |
| 462 |
function hookLegacy($p){ |
| 463 |
|
| 464 |
// insert dynamic classes to menus if preferred |
| 465 |
if (!function_exists('add_first_and_last')) { |
| 466 |
if (!empty($p['first_and_last'])) { |
| 467 |
add_filter('nav_menu_css_class', array(&$this, 'addMenuOrdinalClasses'), 10, 3); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
// Add first and last classes to WordPress menus |
| 473 |
function addMenuOrdinalClasses($classes, $item, $args){ |
| 474 |
|
| 475 |
// store menu item count if not done already |
| 476 |
if (empty($this->menuSlugs[$args->menu->slug])){ |
| 477 |
$this->menuSlugs[$args->menu->slug] = $args->menu->count; |
| 478 |
$this->menuItemCount = 0; |
| 479 |
} |
| 480 |
|
| 481 |
// add first or last item |
| 482 |
if ( $this->menuItemCount === 0 ) { |
| 483 |
$classes[] = 'menu-item-first'; |
| 484 |
} else if ($this->menuItemCount === $this->menuSlugs[$args->menu->slug]-1) { |
| 485 |
$classes[] = 'menu-item-last'; |
| 486 |
} |
| 487 |
|
| 488 |
$this->menuItemCount++; |
| 489 |
|
| 490 |
return $classes; |
| 491 |
|
| 492 |
} |
| 493 |
|
| 494 |
function addViewportMeta(){ |
| 495 |
echo '<meta name="viewport" content="width=device-width, initial-scale=1" />'; |
| 496 |
} |
| 497 |
|
| 498 |
// return meta data about the current page |
| 499 |
function pageMeta(){ |
| 500 |
|
| 501 |
global $wp_query, $post, $pagenow; |
| 502 |
|
| 503 |
$logic = null; |
| 504 |
$alt = array(); |
| 505 |
$title = null; |
| 506 |
$type = null; |
| 507 |
$id = null; |
| 508 |
$post_id = 0; |
| 509 |
$post_status = ''; |
| 510 |
$isFSE = $pagenow === 'site-editor.php'; |
| 511 |
$permalink = home_url(); // default to the home page |
| 512 |
$post_title = ''; |
| 513 |
$slug = null; |
| 514 |
$isAdminPostPageEdit = ($this->isAdminArea && !empty($_GET['post']) && isset($post->post_name)); |
| 515 |
$screen = $this->isAdminArea && function_exists('get_current_screen') |
| 516 |
? get_current_screen() |
| 517 |
: null; |
| 518 |
$adminPrefix = esc_html__('Admin - ', 'microthemer'); |
| 519 |
$blockPrefix = ''; |
| 520 |
$isBlockEditorScreen = $this->isBlockEditorScreen; |
| 521 |
//wp_die('<pre>$post: '.print_r($post, 1).' </pre>'); |
| 522 |
//wp_die('<pre>$current_screen: '.print_r($screen, 1).' </pre>'); |
| 523 |
|
| 524 |
// single post/page on frontend or admin side |
| 525 |
if ( $wp_query->is_page || $wp_query->is_single || $isAdminPostPageEdit ) { |
| 526 |
$post_id = $id = $post->ID; |
| 527 |
$slug = $post->post_name ?: $post_id; // with certain previews the slug might not be set so default to the id |
| 528 |
$type = $wp_query->is_page ? 'page' : 'single'; |
| 529 |
$blockPrefix = esc_html__('Blocks - ', 'microthemer'); |
| 530 |
$post_title = $title = ($this->isAdminArea ? $adminPrefix : '') . $post->post_title; |
| 531 |
$post_status = $post->post_status; |
| 532 |
|
| 533 |
// general page logic |
| 534 |
$pageLogic = $isAdminPostPageEdit |
| 535 |
? '\Microthemer\is_public_or_admin("'.$slug.'")' |
| 536 |
: '\Microthemer\is_post_or_page('.$post_id.', "'.$post->post_title.'")'; // 'is_'.$type.'("'.$slug.'")'; |
| 537 |
|
| 538 |
$logic = $pageLogic; |
| 539 |
|
| 540 |
// more alt logic for admin side |
| 541 |
if ($isAdminPostPageEdit && $screen){ |
| 542 |
$alt[] = array( |
| 543 |
'logic' => '\Microthemer\query_admin_screen("base", "'.$screen->base.'")', |
| 544 |
'title' => $adminPrefix . ucwords($screen->base) |
| 545 |
); |
| 546 |
} |
| 547 |
|
| 548 |
// Save the permalink, for turning off Gutenberg |
| 549 |
$permalink = get_permalink($post_id); |
| 550 |
|
| 551 |
} |
| 552 |
|
| 553 |
// todo has_template() when using site editor templates, patterns, template parts, |
| 554 |
|
| 555 |
// any other admin screen that isn't edit post/page |
| 556 |
elseif ($this->isAdminArea && $screen) { |
| 557 |
|
| 558 |
// Add Post/Page - need to convert the folder to page-specific if editor content items exist inside |
| 559 |
// So flag with identifiable logic |
| 560 |
if ($screen->action === 'add' && $screen->base === 'post'){ |
| 561 |
$logic = '\Microthemer\query_admin_screen("action", "add") && \Microthemer\query_admin_screen("base", "post")'; |
| 562 |
$title = $adminPrefix . $screen->action . ' ' . $screen->base; |
| 563 |
} |
| 564 |
|
| 565 |
else { |
| 566 |
$logic = '\Microthemer\query_admin_screen("base", "'.$screen->base.'")'; |
| 567 |
$title = $adminPrefix . $screen->base; |
| 568 |
} |
| 569 |
|
| 570 |
if ($screen->parent_base){ |
| 571 |
$alt[] = array( |
| 572 |
'logic' => '\Microthemer\query_admin_screen("parent_base", "'.$screen->parent_base.'")', |
| 573 |
'title' => $adminPrefix . ucwords($screen->parent_base) |
| 574 |
); |
| 575 |
} |
| 576 |
|
| 577 |
// It's a full site editor page, see if we can get a link for pages |
| 578 |
if ($isFSE){ |
| 579 |
$permalink = Helper::getFSEPermalink($permalink); |
| 580 |
} |
| 581 |
} elseif ( $wp_query->is_home ) { |
| 582 |
$logic = 'is_home()'; |
| 583 |
$title = esc_html__('Blog home', 'microthemer'); |
| 584 |
$type = 'blog-home'; |
| 585 |
} elseif ( $wp_query->is_category ) { |
| 586 |
$id = $wp_query->query_vars['cat']; |
| 587 |
$slug = $wp_query->query['category_name']; |
| 588 |
$logic = 'is_category("'.$slug.'")'; |
| 589 |
$title = esc_html__('Category archive: ', 'microthemer') . $wp_query->queried_object->name; |
| 590 |
$type = 'category'; |
| 591 |
} elseif ( $wp_query->is_tag ) { |
| 592 |
$id = $wp_query->query_vars['tag']; |
| 593 |
$slug = $wp_query->query['tag_name']; |
| 594 |
$logic = 'is_tag()'; |
| 595 |
$title = esc_html__('Tag archive', 'microthemer'); |
| 596 |
$type = 'tag'; |
| 597 |
} elseif ( $wp_query->is_author ) { |
| 598 |
$id = $wp_query->query_vars['author']; |
| 599 |
$slug = $wp_query->query['author_name']; |
| 600 |
$logic = 'is_author()'; |
| 601 |
$title = esc_html__('Author archive', 'microthemer'); |
| 602 |
$type = 'author'; |
| 603 |
} elseif ( $wp_query->is_archive ) { |
| 604 |
$logic = 'is_archive()'; |
| 605 |
$title = esc_html__('Archive', 'microthemer'); |
| 606 |
$type = 'archive'; |
| 607 |
} elseif ( $wp_query->is_search ) { |
| 608 |
$logic = 'is_search()'; |
| 609 |
$title = esc_html__('Search results', 'microthemer'); |
| 610 |
$type = 'search'; |
| 611 |
} elseif ( $wp_query->is_404 ) { |
| 612 |
$logic = 'is_404()'; |
| 613 |
$title = esc_html__('404 page', 'microthemer'); |
| 614 |
$type = '404'; |
| 615 |
} |
| 616 |
|
| 617 |
// Complete the 3 levels of logic when editing the admin area |
| 618 |
// 1 = most specific e.g. single page or current screen |
| 619 |
// 2 = base or parent base |
| 620 |
// 3 = is_admin() |
| 621 |
if ($this->isAdminArea){ |
| 622 |
$alt[] = array( |
| 623 |
'logic' => 'is_admin()', |
| 624 |
'title' => esc_html__('Admin area', 'microthemer') |
| 625 |
); |
| 626 |
} |
| 627 |
|
| 628 |
$min = !$this->devMode ? '-min' : '/page'; |
| 629 |
|
| 630 |
// Tailwind data |
| 631 |
$tailwind = array( |
| 632 |
'classCache' => '', |
| 633 |
'siteWideCache' => '', |
| 634 |
'config' => '' |
| 635 |
); |
| 636 |
|
| 637 |
$this->contentMethod('getFrontendDataTailwindCache', array($type, $id, &$tailwind)); |
| 638 |
|
| 639 |
return array_merge(array( |
| 640 |
'post_id' => $post_id, |
| 641 |
'post_title' => $post_title, |
| 642 |
'id' => $id, |
| 643 |
'post_status' => $post_status, |
| 644 |
'slug' => $slug, |
| 645 |
'title' => $title, |
| 646 |
'logic' => $logic, |
| 647 |
'alt_logic' => $alt, |
| 648 |
'type' => $type, |
| 649 |
'isBlockEditorScreen' => $isBlockEditorScreen, |
| 650 |
'adminPrefix' => $adminPrefix, |
| 651 |
'blockPrefix' => $blockPrefix, |
| 652 |
'permalink' => $permalink, |
| 653 |
'pagenow' => $pagenow, |
| 654 |
'tailwind' => $tailwind |
| 655 |
), $this->authOnlyData($min)); |
| 656 |
|
| 657 |
} |
| 658 |
|
| 659 |
function authOnlyData($min){ |
| 660 |
return array(); |
| 661 |
} |
| 662 |
|
| 663 |
// Microthemer updates a separate database entry with a handful of preferences that are needed on the frontend |
| 664 |
// and these preferences autoload, saving an extra DB request |
| 665 |
function getPreferences(){ |
| 666 |
|
| 667 |
// fallback to the full preferences if frontend preferences haven't been set somehow |
| 668 |
$this->preferences = ( get_option('microthemer_autoload_preferences') |
| 669 |
?: get_option('preferences_themer_loader') ) |
| 670 |
?: array(); |
| 671 |
|
| 672 |
return $this->preferences; |
| 673 |
|
| 674 |
} |
| 675 |
|
| 676 |
function hookCaptureTemplate(){ |
| 677 |
add_filter( 'template_include', array(&$this, 'captureTemplate'), 9999999); |
| 678 |
} |
| 679 |
|
| 680 |
function captureTemplate($template){ |
| 681 |
|
| 682 |
global $_wp_current_template_id; |
| 683 |
|
| 684 |
$themeSlug = get_stylesheet(); |
| 685 |
|
| 686 |
// cache the current template slug for later logic comparison |
| 687 |
$this->logicSettings['currentTemplate'] = !empty($_wp_current_template_id) |
| 688 |
? Helper::removeRedundantThemePrefix( |
| 689 |
Helper::maybeMakeNumber($_wp_current_template_id), $themeSlug |
| 690 |
) |
| 691 |
: basename($template); // probably a classic .php template file |
| 692 |
|
| 693 |
return $template; |
| 694 |
} |
| 695 |
|
| 696 |
function hookJS($p){ |
| 697 |
|
| 698 |
add_action($this->hooks['enqueue_scripts'], array(&$this, 'addJS'), $this->defaultActionHookOrder); |
| 699 |
|
| 700 |
// allow login page to be editable unless disabled |
| 701 |
if ($this->isFrontend && !empty($p['global_styles_on_login'])){ |
| 702 |
add_action('login_enqueue_scripts', array(&$this, 'addJS'), $this->defaultActionHookOrder); |
| 703 |
} |
| 704 |
|
| 705 |
} |
| 706 |
|
| 707 |
function addMTPlaceholder(){} |
| 708 |
function addMTCSS(){} |
| 709 |
function addMTJS(){} |
| 710 |
|
| 711 |
function addJS() { |
| 712 |
|
| 713 |
$p = &$this->preferences; |
| 714 |
|
| 715 |
$deps = !empty($p['active_scripts_deps']) |
| 716 |
? preg_split("/[\s,]+/", $p['active_scripts_deps']) |
| 717 |
: array(); |
| 718 |
|
| 719 |
// enqueue any script libraries |
| 720 |
if (!empty($p['enq_js']) and is_array($p['enq_js'])){ |
| 721 |
foreach ($p['enq_js'] as $k => $arr){ |
| 722 |
if (empty($arr['disabled'])){ |
| 723 |
wp_enqueue_script($arr['display_name']); |
| 724 |
$deps[] = $arr['display_name']; |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
|
| 729 |
// insert MT JavaScript here if AssetAuth child class is running |
| 730 |
// This needs to come before the user's JavaScript so that we can catch/log their JS errors |
| 731 |
$this->addMTJS(); |
| 732 |
|
| 733 |
// enqueue any user JavaScript |
| 734 |
if (!empty($p[$this->globalJSRequiredKey]) || |
| 735 |
(!isset($p[$this->globalJSRequiredKey]) && !empty($p['load_js'])) // legacy config |
| 736 |
) { |
| 737 |
$h = 'mt_user_js'; |
| 738 |
wp_register_script($h, $this->rootUrl . $this->fileStub . '-scripts.js?'.$this->cacheParam); |
| 739 |
wp_enqueue_script($h, false, $deps, null, !empty($p['active_scripts_footer'])); |
| 740 |
} |
| 741 |
|
| 742 |
// enqueue JS animations if used |
| 743 |
if (!empty($p['active_events'])) { |
| 744 |
$h = 'mt_animation_events'; |
| 745 |
wp_register_script($h, $this->rootUrl.'animation-events.js?'.$this->mtv, array('jquery')); |
| 746 |
wp_enqueue_script($h); |
| 747 |
wp_localize_script( $h, 'MT_Events_Data', json_decode($p['active_events'], true) ); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
function enqueueOrAdd($add, $handle, $url, $config = array()){ |
| 752 |
|
| 753 |
global $wp_styles; |
| 754 |
|
| 755 |
// Allow for dependencies to be passed in - this doesn't work if do_item is run, but we need that |
| 756 |
$deps = isset($config['deps']) ? $config['deps'] : array(); |
| 757 |
|
| 758 |
// add to $wp_styles |
| 759 |
if ($add){ |
| 760 |
|
| 761 |
// add content as inline style |
| 762 |
if (!empty($config['inline'])){ |
| 763 |
$wp_styles->add($handle, false, $deps); |
| 764 |
$wp_styles->enqueue(array($handle)); |
| 765 |
$wp_styles->add_inline_style($handle, $config['code']); |
| 766 |
} |
| 767 |
|
| 768 |
// regular external stylesheet |
| 769 |
else { |
| 770 |
|
| 771 |
$wp_styles->add($handle, $url, $deps); |
| 772 |
$wp_styles->enqueue(array($handle)); |
| 773 |
|
| 774 |
if (!empty($config['data_key'])){ |
| 775 |
$wp_styles->add_data($handle, $config['data_key'], $config['data_val']); |
| 776 |
} |
| 777 |
|
| 778 |
if (!empty($config['async'])){ |
| 779 |
$wp_styles->add_data($handle, 'async', '1'); |
| 780 |
$this->asyncAssets['css'][$handle] = 1; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
if (empty($config['doNotDoItem']) && !isset($_GET['get_front_data'])){ |
| 785 |
$wp_styles->do_item($handle); |
| 786 |
$wp_styles->done[] = $handle; |
| 787 |
} |
| 788 |
|
| 789 |
} |
| 790 |
|
| 791 |
// or enqueue normally |
| 792 |
else { |
| 793 |
wp_register_style($handle, $url, false); |
| 794 |
wp_enqueue_style($handle); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
function addBodyClasses($classes){ |
| 799 |
|
| 800 |
global $post; |
| 801 |
|
| 802 |
$p = &$this->preferences; |
| 803 |
|
| 804 |
if (isset($post)) { |
| 805 |
|
| 806 |
$pfx = !empty($p['page_class_prefix']) ? $p['page_class_prefix'] : 'mt'; |
| 807 |
$classes[] = $pfx.'-'.$post->ID; |
| 808 |
$classes[] = $pfx.'-'.$post->post_type.'-'.$post->post_name; |
| 809 |
|
| 810 |
if (!empty($p['insert_custom_field_classes'])){ |
| 811 |
|
| 812 |
$custom_classes = get_post_meta($post->ID, 'my_body_classes', true); |
| 813 |
|
| 814 |
if ($custom_classes){ |
| 815 |
$classes = array_merge($classes, preg_split("/\s+/", trim($custom_classes))); |
| 816 |
} |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
return $classes; |
| 821 |
} |
| 822 |
|
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
} |