diff --git a/js/admin/batch-size-controller.js b/js/admin/batch-size-controller.js new file mode 100644 index 0000000..f8be869 --- /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..ad45103 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 (!$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,16 @@ 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..b407c2e 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) { + $fileLimit = self::BATCH_SIZE_DEFAULT; + } $pathsInBatch = []; // TODO: Q: will iterator_to_array() allow rm'ing unused var? 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 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 @@ +
+