Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bulk upload batch size control #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions js/admin/batch-size-controller.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
});
40 changes: 40 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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'
)
);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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');
}
}
9 changes: 5 additions & 4 deletions src/Deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class Deployer
{

public const BATCH_SIZE_DEFAULT = 10000;
public function uploadFiles( string $processedSitePath ): void
{
if (Controller::getValue('useBulkUpload') === '1') {
Expand Down Expand Up @@ -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?
Expand Down
1 change: 1 addition & 0 deletions tools/build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions views/admin-page.latte
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@
</td>
</tr>

<tr id="batchSize__wrapper" class="{=(int) $options[useBulkUpload]->value === 0 ? hidden : ''}">
<td style="width:50%;">
<label for="{$options[batchSize]->name}">
{$options[batchSize]->label}
</label>
</td>
<td>
<input
id="{$options[batchSize]->name}"
name="{$options[batchSize]->name}"
type="number"
min="1"
max="10000"
value="{$options[batchSize]->value}"
/>
</td>
</tr>

</tbody>
</table>

Expand Down