Admin
1 week ago
OptimiseAssets
1 week ago
ThirdParty
1 year ago
AdminBar.php
1 week ago
AssetsManager.php
1 week ago
BulkChanges.php
1 week ago
CleanUp.php
2 months ago
Debug.php
1 week ago
DebugException.php
1 week ago
FileSystem.php
1 year ago
HardcodedAssets.php
1 week ago
LoadExceptions.php
1 week ago
Main.php
1 week ago
MainFront.php
1 week ago
Maintenance.php
1 week ago
Menu.php
1 week ago
MetaBoxes.php
1 week ago
Misc.php
1 week ago
MiscArray.php
1 week ago
ObjectCache.php
2 months ago
OwnAssets.php
1 week ago
PluginTracking.php
1 week ago
Preloads.php
1 week ago
Regex.php
1 week ago
Settings.php
1 week ago
Tips.php
1 year ago
Update.php
1 week ago
LoadExceptions.php
133 lines
| 1 | <?php |
| 2 | namespace WpAssetCleanUp; |
| 3 | |
| 4 | /** |
| 5 | * Class LoadExceptions |
| 6 | * @package WpAssetCleanUp |
| 7 | */ |
| 8 | class LoadExceptions |
| 9 | { |
| 10 | /** |
| 11 | * Case 1: If $postType is not mentioned, it will get all post types |
| 12 | * Case 2: If $postType is set and $assetType & $handle are not set, it will get all rules for $postType |
| 13 | * Case 3: If all parameters are set, it will get any terms set for the CSS/JS handle loaded within $postType pages |
| 14 | * |
| 15 | * @param string $postType |
| 16 | * @param string $assetType |
| 17 | * @param string $handle |
| 18 | * |
| 19 | * @return array|\array[][]|mixed |
| 20 | */ |
| 21 | public static function getTaxonomyValuesAssocToPostTypeLoadExceptions($postType = '', $assetType = '', $handle = '') |
| 22 | { |
| 23 | $exceptionsListDefault = array(); |
| 24 | |
| 25 | if ($postType) { |
| 26 | if ($assetType === '' && $handle === '') { |
| 27 | // Default for all results for this $postType |
| 28 | $exceptionsListDefault = array($postType => array('styles' => array(), 'scripts' => array())); |
| 29 | } else { |
| 30 | // Default for the terms list for the specific $handle of $assetType ("styles" or "scripts") |
| 31 | $exceptionsListDefault = array(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | $exceptionsListJson = get_option(WPACU_PLUGIN_ID . '_post_type_via_tax_load_exceptions'); |
| 36 | $exceptionsList = @json_decode($exceptionsListJson, true); |
| 37 | |
| 38 | // Issues with decoding the JSON file? Return an empty list |
| 39 | if (wpacuJsonLastError() !== JSON_ERROR_NONE) { |
| 40 | return $exceptionsListDefault; |
| 41 | } |
| 42 | |
| 43 | // Return any handles added as load exceptions for the requested $postType |
| 44 | if ($postType !== '' && isset($exceptionsList[$postType])) { |
| 45 | /* |
| 46 | * Fetch load exceptions for a certain handle (either a CSS or a JS) |
| 47 | */ |
| 48 | if ( ! empty($exceptionsList[$postType][$assetType][$handle]['values'])) { |
| 49 | return $exceptionsList[$postType] [$assetType] [$handle] ['values']; |
| 50 | } |
| 51 | |
| 52 | if ($assetType === '' && $handle === '') { |
| 53 | /* |
| 54 | * Fetch all load exceptions (CSS & JS) |
| 55 | */ |
| 56 | return $exceptionsList[$postType]; |
| 57 | } |
| 58 | } elseif (is_array($exceptionsList) && ! empty($exceptionsList)) { |
| 59 | return $exceptionsList; |
| 60 | } |
| 61 | |
| 62 | return $exceptionsListDefault; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * e.g. When an asset is unloaded, site-wide |
| 67 | * exceptions to the rule can be added, for the asset to load on all pages of [taxonomy] type |
| 68 | * You might need a CSS/JS to be unloaded site-wide, but on /category/food/, /category/other/ |
| 69 | * you can make an exception, and have the CSS/JS loaded |
| 70 | * |
| 71 | * @param $taxonomy (optional, if it's not there, all load exceptions will load for all taxonomies) |
| 72 | * |
| 73 | * @return array|array[]|mixed |
| 74 | */ |
| 75 | public static function getLoadExceptionsViaTaxType($taxonomy = '') |
| 76 | { |
| 77 | if ($taxonomy) { |
| 78 | // Default for all results for this $taxonomy |
| 79 | $exceptionsListDefault = array($taxonomy => array('styles' => array(), 'scripts' => array())); |
| 80 | } else { |
| 81 | // Default for the asset list for the specific $taxonomy ("styles" / "scripts") |
| 82 | $exceptionsListDefault = array(); |
| 83 | } |
| 84 | |
| 85 | $exceptionsListJson = get_option(WPACU_PLUGIN_ID . '_tax_type_load_exceptions'); |
| 86 | |
| 87 | $exceptionsList = @json_decode($exceptionsListJson, true); |
| 88 | |
| 89 | // Issues with decoding the JSON file? Return an empty list |
| 90 | if (wpacuJsonLastError() !== JSON_ERROR_NONE) { |
| 91 | return $exceptionsListDefault; |
| 92 | } |
| 93 | |
| 94 | // Return any handles added as load exceptions for the requested $taxonomy |
| 95 | if ($taxonomy !== '' && isset($exceptionsList[$taxonomy])) { |
| 96 | return $exceptionsList[$taxonomy]; |
| 97 | } |
| 98 | |
| 99 | if (is_array($exceptionsList) && ! empty($exceptionsList)) { |
| 100 | return $exceptionsList; |
| 101 | } |
| 102 | |
| 103 | return $exceptionsListDefault; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * e.g. When an asset is unloaded, site-wide |
| 108 | * * exceptions to the rule can be added, for the asset to load on all archive pages of any author |
| 109 | * * You might need a CSS/JS to be unloaded site-wide, but on /author/[any_author_title_slug_here]/ |
| 110 | * * you can make an exception, and have the CSS/JS loaded |
| 111 | * |
| 112 | * @return array|array[] |
| 113 | */ |
| 114 | public static function getLoadExceptionsViaAuthorType() |
| 115 | { |
| 116 | $exceptionsListDefault = array('styles' => array(), 'scripts' => array()); |
| 117 | |
| 118 | $exceptionsListJson = get_option(WPACU_PLUGIN_ID . '_author_type_load_exceptions'); |
| 119 | |
| 120 | $exceptionsList = @json_decode($exceptionsListJson, true); |
| 121 | |
| 122 | // Issues with decoding the JSON file? Return an empty list |
| 123 | if (wpacuJsonLastError() !== JSON_ERROR_NONE) { |
| 124 | return $exceptionsListDefault; |
| 125 | } |
| 126 | |
| 127 | if (is_array($exceptionsList) && ! empty($exceptionsList)) { |
| 128 | return $exceptionsList; |
| 129 | } |
| 130 | |
| 131 | return $exceptionsListDefault; |
| 132 | } |
| 133 | } |