From e21100fb5c51b7e771dd9f836d6674145caa18e5 Mon Sep 17 00:00:00 2001 From: Pedro Figueroa <59029273+mardelnet@users.noreply.github.com> Date: Thu, 6 Feb 2025 09:38:48 +0100 Subject: [PATCH] PLANET-7736: Move JS scripts away from PHP files (#2509) This commit removes JS scripts from the PHP files and enqueue them correctly using WordPress methods and Webpack. --- assets/src/js/bulk_export.js | 12 ++ assets/src/js/dismiss_dashboard_notice.js | 7 + assets/src/js/filter_block_names.js | 20 +++ assets/src/js/media_import_button.js | 6 + assets/src/js/metabox_search.js | 12 ++ .../Block/BlockUsageTable.php | 21 +-- src/EnqueueController.php | 152 ++++++++++++++++++ src/Exporter.php | 27 +--- src/MasterSite.php | 34 ++-- webpack.config.js | 5 + 10 files changed, 233 insertions(+), 63 deletions(-) create mode 100644 assets/src/js/bulk_export.js create mode 100644 assets/src/js/dismiss_dashboard_notice.js create mode 100644 assets/src/js/filter_block_names.js create mode 100644 assets/src/js/media_import_button.js create mode 100644 assets/src/js/metabox_search.js diff --git a/assets/src/js/bulk_export.js b/assets/src/js/bulk_export.js new file mode 100644 index 0000000000..7c9dc5f6cd --- /dev/null +++ b/assets/src/js/bulk_export.js @@ -0,0 +1,12 @@ +/* global bulkExportText */ + +document.addEventListener('DOMContentLoaded', () => { + jQuery(() => { + jQuery('select[name=\'action\'], select[name=\'action2\']').each(function () { + jQuery(''; foreach ($this->blocks_ns as $ns) { echo sprintf( @@ -400,23 +402,6 @@ private function blocktype_dropdown(): void ); } echo ''; - - echo ""; } /** diff --git a/src/EnqueueController.php b/src/EnqueueController.php index 69aa6dfc1b..f739fb20b0 100644 --- a/src/EnqueueController.php +++ b/src/EnqueueController.php @@ -18,6 +18,11 @@ public function __construct() add_action('enqueue_hubspot_cookie_script', [$this, 'enqueue_hubspot_cookie']); add_action('enqueue_share_buttons_script', [$this, 'enqueue_share_buttons']); add_action('enqueue_google_tag_manager_script', [$this, 'enqueue_google_tag_manager']); + add_action('enqueue_bulk_export_script', [$this, 'enqueue_bulk_export']); + add_action('enqueue_media_import_button_script', [$this, 'enqueue_media_import_button']); + add_action('enqueue_filter_block_names_script', [$this, 'enqueue_filter_block_names']); + add_action('enqueue_metabox_search_script', [$this, 'enqueue_metabox_search']); + add_action('enqueue_dismiss_dashboard_notice_script', [$this, 'enqueue_dismiss_dashboard_notice']); } /** @@ -38,6 +43,21 @@ public function enqueue_toggle_comment_submit(): void ); } + /** + * Enqueues the filter block names script. + * + */ + public function enqueue_filter_block_names(): void + { + $this->enqueue_script( + 'filter-block-names-script', + '/assets/build/filterBlockNames.js', + [], + $this->get_file_version('/assets/build/filterBlockNames.js'), + true + ); + } + /** * Enqueues the HubSpot cookie script. * @@ -56,7 +76,137 @@ public function enqueue_hubspot_cookie(): void ); } + /** + * Enqueues the bulk export script. + * + * @param string $text The text to be passed to the script. + */ + public function enqueue_bulk_export(string $text): void + { + $script = [ + 'id' => 'bulkExportText', + 'name' => 'bulk-export-script', + 'path' => '/assets/build/bulkExport.js', + ]; + + $this->enqueue_script( + $script['name'], + $script['path'], + [], + $this->get_file_version($script['path']), + true + ); + + if (!wp_script_is($script['name'], 'enqueued')) { + return; + } + + $inline_script = 'var ' . $script['id'] . ' = "' . $text . '";'; + + wp_add_inline_script($script['name'], $inline_script); + } + + /** + * Enqueues the dismiss dashboard notice script. + * + * This method registers and enqueues the JavaScript file used to add + * the dismiss button to the dashboard notices. + * + */ + public function enqueue_dismiss_dashboard_notice(): void + { + $script = [ + 'id' => 'dismissDashboardNotice', + 'name' => 'dismiss-dashboard-notice-script', + 'path' => '/assets/build/dismissDashboardNotice.js', + ]; + + $this->enqueue_script( + $script['name'], + $script['path'], + [], + $this->get_file_version($script['path']), + true + ); + + if (!wp_script_is($script['name'], 'enqueued')) { + return; + } + + wp_localize_script($script['name'], $script['id'], array( + 'ajaxurl' => admin_url('admin-ajax.php'), + )); + } + + /** + * Enqueues the media import button script. + * + * This method registers and enqueues the JavaScript file used to add + * the media import button to the media library. + * + * @param string $label The label for the media import button. + * + */ + public function enqueue_media_import_button(string $label): void + { + $script = [ + 'id' => 'mediaImportLabel', + 'name' => 'media-import-button-script', + 'path' => '/assets/build/mediaImportButton.js', + ]; + + $this->enqueue_script( + $script['name'], + $script['path'], + [], + $this->get_file_version($script['path']), + true + ); + + if (!wp_script_is($script['name'], 'enqueued')) { + return; + } + + $btn_label = 'var ' . $script['id'] . ' = "' . $label . '";'; + + wp_add_inline_script($script['name'], $btn_label); + } + + /** + * Enqueues the metabox search script. + * + * This method registers and enqueues the JavaScript file used to manage + * the functionality of setting the metabox weight based on the parent page. + * + * @param array $data The data to be passed to the script. + */ + public function enqueue_metabox_search(array $data): void + { + $script = [ + 'id' => 'metaboxSearchData', + 'name' => 'metabox-search-script', + 'path' => '/assets/build/metaboxSearch.js', + ]; + + $this->enqueue_script( + $script['name'], + $script['path'], + [], + $this->get_file_version($script['path']), + true + ); + + if (!wp_script_is($script['name'], 'enqueued')) { + return; + } + + wp_localize_script($script['name'], $script['id'], $data); + } + /** + * Enqueues the Google Tag Manager script and passes the context data to it. + * + * @param array $context The context data to be passed to the script. * Enqueues the Google Tag Manager script and passes the context data to it. * */ @@ -115,6 +265,8 @@ public function enqueue_google_tag_manager(array $context): void * This method registers and enqueues the JavaScript file for handling * share buttons on the website. * + * @param array $social_data The data to be passed to the script. + * */ public function enqueue_share_buttons(array $social_data): void { diff --git a/src/Exporter.php b/src/Exporter.php index e7a7e3b645..9c9d51bd5e 100644 --- a/src/Exporter.php +++ b/src/Exporter.php @@ -17,7 +17,7 @@ public function __construct() add_action('admin_action_export_data', [ $this, 'single_post_export_data' ]); add_filter('post_row_actions', [ $this, 'single_post_export' ], 10, 2); add_filter('page_row_actions', [ $this, 'single_post_export' ], 10, 2); - add_action('admin_footer-edit.php', [ $this, 'single_post_export_bulk' ]); + add_action('admin_footer', [ $this, 'single_post_export_bulk' ]); add_action('load-edit.php', [ $this, 'single_post_export_bulk_action' ]); add_action('admin_head', [ $this, 'add_import_button' ]); } @@ -44,15 +44,8 @@ public function single_post_export_bulk(): void if (!current_user_can('edit_posts')) { return; } - ?> - - - - '; -?> - $options['act_page'] ?? null, + 'action_weight' => Search\Search::DEFAULT_ACTION_WEIGHT, + 'page_weight' => Search\Search::DEFAULT_PAGE_WEIGHT, + ]; + + do_action('enqueue_metabox_search_script', $script_data); } // phpcs:enable Generic.WhiteSpace.ScopeIndent @@ -1668,17 +1664,9 @@ public function show_dashboard_notice(): void return; } - echo '
' - . wp_kses_post($message) - . '
' - . " - "; + do_action('enqueue_dismiss_dashboard_notice_script'); + + echo '
' . wp_kses_post($message) . '
'; } /** diff --git a/webpack.config.js b/webpack.config.js index b4e63ad46c..7d8774a8a0 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -39,6 +39,11 @@ module.exports = (env, argv) => { hubspotCookie: './assets/src/js/hubspot_cookie.js', shareButtons: './assets/src/js/share_buttons.js', googleTagManager: './assets/src/js/google_tag_manager.js', + bulkExport: './assets/src/js/bulk_export.js', + mediaImportButton: './assets/src/js/media_import_button.js', + filterBlockNames: './assets/src/js/filter_block_names.js', + metaboxSearch: './assets/src/js/metabox_search.js', + dismissDashboardNotice: './assets/src/js/dismiss_dashboard_notice.js', frontendIndex: './assets/src/frontendIndex.js', editorIndex: './assets/src/editorIndex.js', ArticlesScript: './assets/src/blocks/Articles/ArticlesScript.js',