From 2731e1ef663027b8cea023820d6361a15fd3c8b5 Mon Sep 17 00:00:00 2001 From: Chris A Date: Tue, 17 Jan 2023 22:11:32 +1000 Subject: [PATCH 1/4] Added admin option to customise batch sizes --- js/admin/batch-size-controller.js | 27 +++++++++++++++++++++ src/Controller.php | 39 +++++++++++++++++++++++++++++++ src/Deployer.php | 9 +++---- views/admin-page.latte | 18 ++++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 js/admin/batch-size-controller.js diff --git a/js/admin/batch-size-controller.js b/js/admin/batch-size-controller.js new file mode 100644 index 0000000..30fff06 --- /dev/null +++ b/js/admin/batch-size-controller.js @@ -0,0 +1,27 @@ +document.addEventListener("DOMContentLoaded", function() { + const bulkUploadField = document.querySelector("#useBulkUpload"); + const batchSizeWrapper = document.querySelector("#batchSize__wrapper"); + const batchSizeField = document.querySelector("#batchSize"); + + // Handle toggling display of our batch size field + if( bulkUploadField && batchSizeWrapper ) { + bulkUploadField.addEventListener("input", function() { + batchSizeWrapper.classList.toggle("hidden"); + }); + } + + // Some simple validation + if( batchSizeField ) { + batchSizeField.addEventListener("input", function(event) { + let batchSize = parseInt(batchSizeField.value); + let minBatchSize = parseInt(batchSizeField.getAttribute("min")); + let maxBatchSize = parseInt(batchSizeField.getAttribute("max")); + if( batchSize < minBatchSize ) { + batchSize = minBatchSize; + } else if( batchSize > maxBatchSize ) { + batchSize = maxBatchSize; + } + batchSizeField.value = batchSize; + }) + } +}); diff --git a/src/Controller.php b/src/Controller.php index c2df21b..59fa308 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -49,6 +49,12 @@ public function __construct() 1 ); + add_action( + 'admin_enqueue_scripts', + [ $this, 'wp2staticAdminScripts' ], + 0 + ); + do_action( 'wp2static_register_addon', 'wp2static-addon-cloudflare-workers', @@ -129,6 +135,17 @@ public static function seedOptions(): void 'ie 13e736c51a7a73dabc0b83f75d3bedce' ) ); + + $wpdb->query( + $wpdb->prepare( + "INSERT IGNORE INTO {$wpdb->prefix}wp2static_addon_cloudflare_workers_options " . + '(name, value, label, description) VALUES (%s, %s, %s, %s);', + 'batchSize', + Deployer::BATCH_SIZE_DEFAULT, + 'Batch Size', + 'Number of files to include in each batch' + ) + ); } /** @@ -334,6 +351,17 @@ public static function saveOptionsFromUI(): void [ 'name' => 'accountID' ] ); + $batchSize = sanitize_text_field($_POST['batchSize']); + if( !isset($batchSize) || empty($batchSize) ) { + $batchSize = (string) Deployer::BATCH_SIZE_DEFAULT; + } + $batchSize = preg_replace('/\D/', "", $batchSize); + $wpdb->update( + $tableName, + [ 'value' => $batchSize ], + [ 'name' => 'batchSize' ] + ); + wp_safe_redirect(admin_url('admin.php?page=wp2static-addon-cloudflare-workers')); exit; } @@ -372,4 +400,15 @@ public function addOptionsPage(): void [ $this, 'renderCloudflareWorkersPage' ] ); } + + public static function wp2staticAdminScripts() : void { + wp_register_script( + 'wp2static_addon_cloudflare_admin_scripts', + plugins_url( '../js/admin/batch-size-controller.js', __FILE__ ), + [], + Config::get('version'), + false + ); + wp_enqueue_script( 'wp2static_addon_cloudflare_admin_scripts' ); + } } diff --git a/src/Deployer.php b/src/Deployer.php index 59af459..50eabc1 100644 --- a/src/Deployer.php +++ b/src/Deployer.php @@ -23,7 +23,7 @@ */ class Deployer { - + public const BATCH_SIZE_DEFAULT = 10000; public function uploadFiles( string $processedSitePath ): void { if (Controller::getValue('useBulkUpload') === '1') { @@ -211,9 +211,10 @@ public function bulkUploadFiles( string $processedSitePath ): void $batches = []; $filesInBatch = 0; - // TODO: add select menu for user-overriding batch size or be clever and auto-retry - // with smaller batch sizes on errors - $fileLimit = 10000; + $fileLimit = Controller::getValue("batchSize"); + if( !$fileLimit || empty($fileLimit) ) { + $fileLimit = self::BATCH_SIZE_DEFAULT; + } $pathsInBatch = []; // TODO: Q: will iterator_to_array() allow rm'ing unused var? diff --git a/views/admin-page.latte b/views/admin-page.latte index b30dbb9..935e2aa 100644 --- a/views/admin-page.latte +++ b/views/admin-page.latte @@ -89,6 +89,24 @@ + + + + + + + + + From 1ecb1addb1c35e1a60b5028235cad852082627c3 Mon Sep 17 00:00:00 2001 From: Chris A Date: Tue, 17 Jan 2023 22:11:41 +1000 Subject: [PATCH 2/4] include js dir in build script --- tools/build_release.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/build_release.sh b/tools/build_release.sh index 50f32ff..0f74b7f 100755 --- a/tools/build_release.sh +++ b/tools/build_release.sh @@ -30,6 +30,7 @@ cp -r $EXEC_DIR/wp2static-addon-cloudflare-workers.php $TMP_DIR/wp2static-addon- cp -r $EXEC_DIR/src $TMP_DIR/wp2static-addon-cloudflare-workers/ cp -r $EXEC_DIR/vendor $TMP_DIR/wp2static-addon-cloudflare-workers/ cp -r $EXEC_DIR/views $TMP_DIR/wp2static-addon-cloudflare-workers/ +cp -r $EXEC_DIR/js $TMP_DIR/wp2static-addon-cloudflare-workers/ cd $TMP_DIR From bcf14388be1cd79933c62fae05c739f89b066a70 Mon Sep 17 00:00:00 2001 From: Chris A Date: Tue, 17 Jan 2023 22:11:45 +1000 Subject: [PATCH 3/4] minor code tidy up to improve linting --- js/admin/batch-size-controller.js | 14 +++++++------- src/Controller.php | 15 ++++++++------- src/Deployer.php | 4 ++-- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/js/admin/batch-size-controller.js b/js/admin/batch-size-controller.js index 30fff06..8c117e3 100644 --- a/js/admin/batch-size-controller.js +++ b/js/admin/batch-size-controller.js @@ -1,24 +1,24 @@ -document.addEventListener("DOMContentLoaded", function() { +document.addEventListener("DOMContentLoaded", function () { const bulkUploadField = document.querySelector("#useBulkUpload"); const batchSizeWrapper = document.querySelector("#batchSize__wrapper"); const batchSizeField = document.querySelector("#batchSize"); // Handle toggling display of our batch size field - if( bulkUploadField && batchSizeWrapper ) { - bulkUploadField.addEventListener("input", function() { + if (bulkUploadField && batchSizeWrapper) { + bulkUploadField.addEventListener("input", function () { batchSizeWrapper.classList.toggle("hidden"); }); } // Some simple validation - if( batchSizeField ) { - batchSizeField.addEventListener("input", function(event) { + if (batchSizeField) { + batchSizeField.addEventListener("input", function (event) { let batchSize = parseInt(batchSizeField.value); let minBatchSize = parseInt(batchSizeField.getAttribute("min")); let maxBatchSize = parseInt(batchSizeField.getAttribute("max")); - if( batchSize < minBatchSize ) { + if (batchSize < minBatchSize) { batchSize = minBatchSize; - } else if( batchSize > maxBatchSize ) { + } else if (batchSize > maxBatchSize) { batchSize = maxBatchSize; } batchSizeField.value = batchSize; diff --git a/src/Controller.php b/src/Controller.php index 59fa308..ad45103 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -135,7 +135,7 @@ public static function seedOptions(): void 'ie 13e736c51a7a73dabc0b83f75d3bedce' ) ); - + $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->prefix}wp2static_addon_cloudflare_workers_options " . @@ -352,10 +352,10 @@ public static function saveOptionsFromUI(): void ); $batchSize = sanitize_text_field($_POST['batchSize']); - if( !isset($batchSize) || empty($batchSize) ) { - $batchSize = (string) Deployer::BATCH_SIZE_DEFAULT; + if (!$batchSize) { + $batchSize = (string)Deployer::BATCH_SIZE_DEFAULT; } - $batchSize = preg_replace('/\D/', "", $batchSize); + $batchSize = preg_replace('/\D/', '', $batchSize); $wpdb->update( $tableName, [ 'value' => $batchSize ], @@ -401,14 +401,15 @@ public function addOptionsPage(): void ); } - public static function wp2staticAdminScripts() : void { + public static function wp2staticAdminScripts(): void + { wp_register_script( 'wp2static_addon_cloudflare_admin_scripts', - plugins_url( '../js/admin/batch-size-controller.js', __FILE__ ), + plugins_url('../js/admin/batch-size-controller.js', __FILE__), [], Config::get('version'), false ); - wp_enqueue_script( 'wp2static_addon_cloudflare_admin_scripts' ); + wp_enqueue_script('wp2static_addon_cloudflare_admin_scripts'); } } diff --git a/src/Deployer.php b/src/Deployer.php index 50eabc1..b407c2e 100644 --- a/src/Deployer.php +++ b/src/Deployer.php @@ -211,8 +211,8 @@ public function bulkUploadFiles( string $processedSitePath ): void $batches = []; $filesInBatch = 0; - $fileLimit = Controller::getValue("batchSize"); - if( !$fileLimit || empty($fileLimit) ) { + $fileLimit = Controller::getValue('batchSize'); + if (!$fileLimit) { $fileLimit = self::BATCH_SIZE_DEFAULT; } $pathsInBatch = []; From 6422e91c4b3fa981209ad29f16d22b6a64714ea1 Mon Sep 17 00:00:00 2001 From: Chris A Date: Tue, 17 Jan 2023 22:17:54 +1000 Subject: [PATCH 4/4] added missing semicolon --- js/admin/batch-size-controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/admin/batch-size-controller.js b/js/admin/batch-size-controller.js index 8c117e3..f8be869 100644 --- a/js/admin/batch-size-controller.js +++ b/js/admin/batch-size-controller.js @@ -22,6 +22,6 @@ document.addEventListener("DOMContentLoaded", function () { batchSize = maxBatchSize; } batchSizeField.value = batchSize; - }) + }); } });