| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Enqueues admin scripts, styles, and support-request bootstrap assets. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_AdminAssetEnqueuer { |
| 11 |
|
| 12 |
/** |
| 13 |
* Include things necessary for ajax. |
| 14 |
* |
| 15 |
* @param string $hook |
| 16 |
* @param callable(string, Throwable): void $errorReporter |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function addScripts($hook, callable $errorReporter): void { |
| 20 |
// only load this stuff for this plugin. |
| 21 |
// thanks to https://pippinsplugins.com/loading-scripts-correctly-in-the-wordpress-admin/ |
| 22 |
if (!array_key_exists('abj404_settingsPageName', $GLOBALS) || |
| 23 |
$hook != $GLOBALS['abj404_settingsPageName']) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
try { |
| 28 |
$includesUrl = ABJ404_URL . 'includes/'; |
| 29 |
$subpage = self::requestStringParam('subpage'); |
| 30 |
// Default plugin landing is redirects when subpage is not specified. |
| 31 |
if ($subpage === '') { |
| 32 |
$subpage = 'abj404_redirects'; |
| 33 |
} |
| 34 |
$isEditPage = self::isEditPageRequest($subpage); |
| 35 |
|
| 36 |
$isOptionsPage = ($subpage === 'abj404_options'); |
| 37 |
$isStatsPage = ($subpage === 'abj404_stats'); |
| 38 |
$isToolsPage = ($subpage === 'abj404_tools'); |
| 39 |
$isCardAccordionPage = in_array($subpage, array('abj404_options', 'abj404_tools', 'abj404_stats'), true); |
| 40 |
$isLogsPage = ($subpage === 'abj404_logs'); |
| 41 |
$isListPage = !$isEditPage && in_array($subpage, array('abj404_redirects', 'abj404_captured', 'abj404_logs'), true); |
| 42 |
$needsDestinationAutocomplete = in_array($subpage, array('abj404_redirects', 'abj404_captured', 'abj404_options', 'abj404_edit'), true); |
| 43 |
|
| 44 |
// remove the "thank you for creating with wordpress" message |
| 45 |
add_filter('admin_footer_text', |
| 46 |
'ABJ_404_Solution_WordPress_Connector::remove_admin_footer_text'); |
| 47 |
// remove the version number message |
| 48 |
add_filter('update_footer', |
| 49 |
'ABJ_404_Solution_WordPress_Connector::remove_admin_footer_text', 11); |
| 50 |
|
| 51 |
// jquery is used for the searchable dropdown list of pages for adding a redirect and other things. |
| 52 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('jquery'); |
| 53 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('jquery-ui-autocomplete'); |
| 54 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('jquery-effects-core'); |
| 55 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('jquery-effects-highlight'); |
| 56 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('jquery-color'); |
| 57 |
|
| 58 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-admin-ajax', |
| 59 |
ABJ404_URL . 'includes/js/abj404-admin-ajax.js', array('jquery')); |
| 60 |
|
| 61 |
wp_register_script('abj404-redirect_to_ajax', $includesUrl . 'ajax/redirect_to_ajax.js', |
| 62 |
array('jquery', 'jquery-ui-autocomplete')); |
| 63 |
wp_register_script('abj404-exclude_pages_ajax', $includesUrl . 'ajax/exclude_pages_ajax.js', |
| 64 |
array('jquery', 'jquery-ui-autocomplete', 'abj404-redirect_to_ajax')); |
| 65 |
$translation_array = array( |
| 66 |
'type_a_page_name' => __('(Type a page name or an external URL)', '404-solution'), |
| 67 |
'a_page_has_been_selected' => __('(A page has been selected.)', '404-solution'), |
| 68 |
'an_external_url_will_be_used' => __('(An external URL will be used.)', '404-solution') |
| 69 |
); |
| 70 |
wp_localize_script('abj404-redirect_to_ajax', 'abj404localization', $translation_array ); |
| 71 |
if ($needsDestinationAutocomplete) { |
| 72 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-redirect_to_ajax'); |
| 73 |
wp_localize_script('abj404-exclude_pages_ajax', 'abj404localization', $translation_array ); |
| 74 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-exclude_pages_ajax'); |
| 75 |
} |
| 76 |
|
| 77 |
wp_register_script('abj404-enable_disable_apply_button_js', |
| 78 |
ABJ404_URL . 'includes/js/enableDisableApplyButton.js'); |
| 79 |
$translation_array = array('{altText}' => __('Choose at least one URL', '404-solution')); |
| 80 |
wp_localize_script('abj404-enable_disable_apply_button_js', 'abj404localization', $translation_array); |
| 81 |
if ($isListPage) { |
| 82 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-enable_disable_apply_button_js'); |
| 83 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-trash_link_ajax', $includesUrl . 'ajax/trash_link_ajax.js', |
| 84 |
array('jquery')); |
| 85 |
} |
| 86 |
if ($isListPage || $isEditPage) { |
| 87 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-table-interactions', $includesUrl . 'js/tableInteractions.js', |
| 88 |
array('jquery')); |
| 89 |
} |
| 90 |
|
| 91 |
if ($isListPage || $isStatsPage) { |
| 92 |
self::enqueueViewUpdaterModules($includesUrl . 'ajax/'); |
| 93 |
} |
| 94 |
|
| 95 |
if ($isStatsPage) { |
| 96 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-stats-confidence-chart', |
| 97 |
ABJ404_URL . 'includes/js/statsConfidenceChart.js', array()); |
| 98 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-stats-trends', |
| 99 |
ABJ404_URL . 'includes/js/statsTrends.js', array()); |
| 100 |
} |
| 101 |
|
| 102 |
if ($isToolsPage) { |
| 103 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-tools-migrate-plugin', |
| 104 |
ABJ404_URL . 'includes/js/toolsMigratePlugin.js', array()); |
| 105 |
} |
| 106 |
|
| 107 |
if ($isLogsPage) { |
| 108 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-search_logs_ajax', $includesUrl . 'ajax/search_logs_ajax.js', |
| 109 |
array('jquery', 'jquery-ui-autocomplete')); |
| 110 |
} |
| 111 |
|
| 112 |
if ($isOptionsPage) { |
| 113 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-general-js', $includesUrl . 'js/general.js', |
| 114 |
array('jquery')); |
| 115 |
wp_localize_script('abj404-general-js', 'abj404General', array( |
| 116 |
'savingSettings' => __('Saving settings...', '404-solution'), |
| 117 |
)); |
| 118 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-theme-preview', $includesUrl . 'js/themePreview.js', |
| 119 |
array('jquery')); |
| 120 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-settings-mode-toggle', $includesUrl . 'ajax/SettingsModeToggle.js', |
| 121 |
array('jquery')); |
| 122 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-restore-defaults', $includesUrl . 'ajax/RestoreDefaults.js', |
| 123 |
array('jquery')); |
| 124 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-behavior-tiles', ABJ404_URL . 'includes/js/behaviorTiles.js', |
| 125 |
array()); |
| 126 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-settings-deferred', ABJ404_URL . 'includes/js/settingsDeferred.js', |
| 127 |
array('jquery')); |
| 128 |
} |
| 129 |
|
| 130 |
if ($isCardAccordionPage) { |
| 131 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-options-accordion', $includesUrl . 'js/optionsAccordion.js', |
| 132 |
array('jquery')); |
| 133 |
wp_localize_script('abj404-options-accordion', 'abj404Accordion', array( |
| 134 |
'expandAll' => __('Expand All', '404-solution'), |
| 135 |
'collapseAll' => __('Collapse All', '404-solution'), |
| 136 |
)); |
| 137 |
} |
| 138 |
|
| 139 |
if ($isOptionsPage) { |
| 140 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-engine-profiles', |
| 141 |
$includesUrl . 'ajax/ajax-engine-profiles.js', |
| 142 |
array('jquery')); |
| 143 |
wp_localize_script('abj404-engine-profiles', 'abj404EngineProfiles', array( |
| 144 |
'nonce' => wp_create_nonce('abj404_engine_profiles_nonce'), |
| 145 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 146 |
'i18n' => array( |
| 147 |
'edit' => __('Edit', '404-solution'), |
| 148 |
'delete' => __('Delete', '404-solution'), |
| 149 |
'addProfile' => __('Add Engine Profile', '404-solution'), |
| 150 |
'editProfile' => __('Edit Engine Profile', '404-solution'), |
| 151 |
'nameRequired' => __('Profile name is required.', '404-solution'), |
| 152 |
'patternRequired' => __('URL pattern is required.', '404-solution'), |
| 153 |
'saved' => __('Profile saved.', '404-solution'), |
| 154 |
'saveFailed' => __('Failed to save profile.', '404-solution'), |
| 155 |
'confirmDelete' => __('Delete this engine profile?', '404-solution'), |
| 156 |
), |
| 157 |
)); |
| 158 |
} |
| 159 |
|
| 160 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt( |
| 161 |
'abj404-review-feedback', |
| 162 |
$includesUrl . 'js/reviewFeedback.js', |
| 163 |
array() |
| 164 |
); |
| 165 |
|
| 166 |
self::registerSupportRequestAssets(); |
| 167 |
|
| 168 |
ABJ_404_Solution_WPUtils::my_wp_enq_style('abj404solution-styles', ABJ404_URL . 'includes/html/404solutionStyles.css', |
| 169 |
array()); |
| 170 |
ABJ_404_Solution_WPUtils::my_wp_enq_style('abj404solution-themes', ABJ404_URL . 'includes/html/adminThemes.css', |
| 171 |
array()); |
| 172 |
|
| 173 |
if (is_rtl()) { |
| 174 |
ABJ_404_Solution_WPUtils::my_wp_enq_style('abj404solution-rtl', ABJ404_URL . 'includes/html/404solutionStyles-rtl.css', |
| 175 |
array('abj404solution-styles')); |
| 176 |
} |
| 177 |
} catch (Throwable $e) { |
| 178 |
call_user_func($errorReporter, 'admin_enqueue_scripts', $e); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Read a scalar GET parameter as sanitized text. |
| 184 |
* |
| 185 |
* @param string $name |
| 186 |
* @return string |
| 187 |
*/ |
| 188 |
private static function requestStringParam(string $name): string { |
| 189 |
return array_key_exists($name, $_GET) |
| 190 |
? sanitize_text_field(ABJ_404_Solution_RequestInputNormalizer::normalizeScalar($_GET[$name])) |
| 191 |
: ''; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Identify request shapes that need edit-form assets instead of list-table assets. |
| 196 |
* |
| 197 |
* @param string $subpage |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
private static function isEditPageRequest(string $subpage): bool { |
| 201 |
if ($subpage === 'abj404_edit') { |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
if (self::requestStringParam('action') !== 'edit') { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
if (!in_array($subpage, array('abj404_redirects', 'abj404_captured'), true)) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
return array_key_exists('id', $_GET) || array_key_exists('idnum', $_GET); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Enqueue the support-request button assets specifically for wp-admin/plugins.php. |
| 218 |
* |
| 219 |
* @param string $hook |
| 220 |
* @param callable(string, Throwable): void $errorReporter |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public static function enqueueSupportRequestAssetsOnPluginsPage($hook, callable $errorReporter): void { |
| 224 |
if ($hook !== 'plugins.php') { |
| 225 |
return; |
| 226 |
} |
| 227 |
try { |
| 228 |
self::registerSupportRequestAssets(); |
| 229 |
} catch (Throwable $e) { |
| 230 |
call_user_func($errorReporter, 'admin_enqueue_scripts:plugins.php', $e); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @param string $vuBase URL prefix for the ajax/ assets directory. |
| 236 |
* @return void |
| 237 |
*/ |
| 238 |
private static function enqueueViewUpdaterModules(string $vuBase): void { |
| 239 |
$enq = array('ABJ_404_Solution_WPUtils', 'my_wp_enq_scrpt'); |
| 240 |
$enq('abj404-view-updater-nonce-refresh', |
| 241 |
$vuBase . 'view_updater_nonce_refresh.js', array('jquery')); |
| 242 |
$enq('abj404-view-updater-stage-diagnostics', |
| 243 |
$vuBase . 'view_updater_stage_diagnostics.js', array('jquery')); |
| 244 |
$enq('abj404-view-updater-compare', |
| 245 |
$vuBase . 'view_updater_compare.js', array('jquery')); |
| 246 |
$enq('abj404-view-updater-refresh-pill', |
| 247 |
$vuBase . 'view_updater_refresh_pill.js', array('jquery')); |
| 248 |
$enq('abj404-view-updater-stats', $vuBase . 'view_updater_stats.js', |
| 249 |
array('jquery', 'abj404-view-updater-refresh-pill', 'abj404-view-updater-nonce-refresh')); |
| 250 |
$enq('abj404-view-updater-table-init', $vuBase . 'view_updater_table_init.js', |
| 251 |
array('jquery', 'abj404-view-updater-refresh-pill', 'abj404-view-updater-stats', |
| 252 |
'abj404-view-updater-nonce-refresh')); |
| 253 |
$enq('abj404-view-updater-pagination-request', |
| 254 |
$vuBase . 'view_updater_pagination_request.js', |
| 255 |
array('jquery', 'abj404-view-updater-compare')); |
| 256 |
$enq('abj404-view-updater-lazy-backfill', |
| 257 |
$vuBase . 'view_updater_lazy_backfill.js', |
| 258 |
array('jquery', 'abj404-view-updater-nonce-refresh')); |
| 259 |
$enq('abj404-view-updater-pagination-response-apply', |
| 260 |
$vuBase . 'view_updater_pagination_response_apply.js', |
| 261 |
array('jquery', 'abj404-view-updater-table-init', 'abj404-view-updater-lazy-backfill')); |
| 262 |
$enq('abj404-view-updater-pagination-error-notice', |
| 263 |
$vuBase . 'view_updater_pagination_error_notice.js', |
| 264 |
array('jquery', 'abj404-view-updater-stage-diagnostics', |
| 265 |
'abj404-view-updater-table-init', 'abj404-view-updater-nonce-refresh')); |
| 266 |
$enq('abj404-view-updater-pagination', $vuBase . 'view_updater_pagination.js', |
| 267 |
array('jquery', 'abj404-view-updater-compare', 'abj404-view-updater-stage-diagnostics', |
| 268 |
'abj404-view-updater-table-init', |
| 269 |
'abj404-view-updater-refresh-pill', |
| 270 |
'abj404-view-updater-nonce-refresh', |
| 271 |
'abj404-view-updater-pagination-request', |
| 272 |
'abj404-view-updater-pagination-response-apply', |
| 273 |
'abj404-view-updater-pagination-error-notice')); |
| 274 |
$enq('abj404-view-updater', $vuBase . 'view_updater.js', |
| 275 |
array('jquery', 'jquery-ui-autocomplete', |
| 276 |
'abj404-view-updater-stage-diagnostics', |
| 277 |
'abj404-view-updater-compare', |
| 278 |
'abj404-view-updater-refresh-pill', 'abj404-view-updater-stats', |
| 279 |
'abj404-view-updater-table-init', |
| 280 |
'abj404-view-updater-pagination', |
| 281 |
'abj404-view-updater-lazy-backfill', |
| 282 |
'abj404-view-updater-nonce-refresh')); |
| 283 |
} |
| 284 |
|
| 285 |
/** @return void */ |
| 286 |
private static function registerSupportRequestAssets(): void { |
| 287 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-support-request-client', |
| 288 |
ABJ404_URL . 'includes/ajax/SupportRequest.js', array()); |
| 289 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-support-request-transport', |
| 290 |
ABJ404_URL . 'includes/js/support-request-transport.js', |
| 291 |
array('abj404-support-request-client')); |
| 292 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-support-request-modal-view', |
| 293 |
ABJ404_URL . 'includes/js/support-request-modal-view.js', array()); |
| 294 |
ABJ_404_Solution_WPUtils::my_wp_enq_scrpt('abj404-support-request-button', |
| 295 |
ABJ404_URL . 'includes/js/support-request-button.js', |
| 296 |
array('abj404-support-request-client', 'abj404-support-request-transport', 'abj404-support-request-modal-view')); |
| 297 |
if (!function_exists('wp_add_inline_script')) { |
| 298 |
return; |
| 299 |
} |
| 300 |
$supportNonce = wp_create_nonce(ABJ_404_Solution_Ajax_SupportRequest::NONCE_ACTION); |
| 301 |
$previewNonce = wp_create_nonce(ABJ_404_Solution_Ajax_SupportRequestPreview::NONCE_ACTION); |
| 302 |
$ajaxUrl = function_exists('admin_url') ? admin_url('admin-ajax.php') : '/wp-admin/admin-ajax.php'; |
| 303 |
$payload = wp_json_encode(array( |
| 304 |
'ajaxurl' => $ajaxUrl, |
| 305 |
'nonces' => array( |
| 306 |
'support_request' => $supportNonce, |
| 307 |
'support_request_preview' => $previewNonce, |
| 308 |
), |
| 309 |
)); |
| 310 |
$bootstrap = 'window.ABJ404=window.ABJ404||{};Object.assign(window.ABJ404,' |
| 311 |
. (is_string($payload) ? $payload : '{}') . ');'; |
| 312 |
wp_add_inline_script('abj404-support-request-client', $bootstrap, 'before'); |
| 313 |
} |
| 314 |
} |
| 315 |
|