145 lines
3.8 KiB
PHP
145 lines
3.8 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* WP-CLI commands for publishing.
|
||
|
|
*
|
||
|
|
* The one place the fork is genuinely better driven from a terminal is the
|
||
|
|
* first full export of an existing site: thousands of pages, a long run, and
|
||
|
|
* a browser tab that must not be closed. `wp sirius export --all` does it
|
||
|
|
* with no tab involved.
|
||
|
|
*
|
||
|
|
* @package SiriusPress
|
||
|
|
*/
|
||
|
|
|
||
|
|
defined( 'ABSPATH' ) || exit;
|
||
|
|
|
||
|
|
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Publish this site to its BCNR name.
|
||
|
|
*/
|
||
|
|
final class SPE_CLI {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Queue pages and push them to the name's storage.
|
||
|
|
*
|
||
|
|
* ## OPTIONS
|
||
|
|
*
|
||
|
|
* [--all]
|
||
|
|
* : Queue every published page, archive and attachment page first.
|
||
|
|
*
|
||
|
|
* [--limit=<n>]
|
||
|
|
* : Stop after this many uploads. Default: everything queued.
|
||
|
|
*
|
||
|
|
* [--dry-run]
|
||
|
|
* : Build each file and report what would be uploaded, without uploading.
|
||
|
|
*
|
||
|
|
* ## EXAMPLES
|
||
|
|
*
|
||
|
|
* wp sirius export --all
|
||
|
|
* wp sirius export --limit=50
|
||
|
|
* wp sirius export --all --dry-run
|
||
|
|
*
|
||
|
|
* @when after_wp_load
|
||
|
|
*/
|
||
|
|
public function export( $args, $assoc ) {
|
||
|
|
if ( ! SP_Settings::is_configured() ) {
|
||
|
|
WP_CLI::error( 'No BCNR name is configured. Set one in Sirius Press settings first.' );
|
||
|
|
}
|
||
|
|
|
||
|
|
if ( ! empty( $assoc['all'] ) ) {
|
||
|
|
$queued = SPE_Runner::queue_full_site();
|
||
|
|
WP_CLI::log( sprintf( 'Queued %d paths.', $queued ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
$dry = ! empty( $assoc['dry-run'] );
|
||
|
|
$limit = isset( $assoc['limit'] ) ? (int) $assoc['limit'] : 0;
|
||
|
|
|
||
|
|
if ( ! $dry && SP_Settings::MODE_SERVER !== SP_Settings::mode() ) {
|
||
|
|
WP_CLI::error(
|
||
|
|
'This site is set to manual signing, so no key is available here. '
|
||
|
|
. 'Switch to automatic publishing in settings, or sign from the Publishing screen in wp-admin.'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$done = 0;
|
||
|
|
$fail = 0;
|
||
|
|
$skip = 0;
|
||
|
|
|
||
|
|
while ( true ) {
|
||
|
|
$rows = SPE_Queue::claim( 10 );
|
||
|
|
if ( ! $rows ) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
foreach ( $rows as $row ) {
|
||
|
|
if ( $limit > 0 && $done + $fail + $skip >= $limit ) {
|
||
|
|
break 2;
|
||
|
|
}
|
||
|
|
if ( $dry ) {
|
||
|
|
$body = SPE_Runner::build( $row );
|
||
|
|
if ( is_wp_error( $body ) ) {
|
||
|
|
WP_CLI::warning( sprintf( '%s — %s', $row->path, $body->get_error_message() ) );
|
||
|
|
$fail++;
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
WP_CLI::log( sprintf( '%s (%s)', $row->path, size_format( strlen( $body ) ) ) );
|
||
|
|
$skip++;
|
||
|
|
// A dry run must not consume the queue, so the row is left
|
||
|
|
// pending — which also means this loop would spin forever
|
||
|
|
// on the same rows. Stop after one pass instead.
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
$outcome = SPE_Runner::process( $row );
|
||
|
|
if ( 'done' === $outcome ) {
|
||
|
|
$done++;
|
||
|
|
WP_CLI::log( sprintf( '✓ %s', $row->path ) );
|
||
|
|
} elseif ( 'skipped' === $outcome ) {
|
||
|
|
$skip++;
|
||
|
|
} else {
|
||
|
|
$fail++;
|
||
|
|
WP_CLI::warning( sprintf( '✗ %s', $row->path ) );
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if ( $dry ) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
WP_CLI::success( sprintf( '%d uploaded, %d unchanged, %d failed.', $done, $skip, $fail ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Show the publishing status of this site.
|
||
|
|
*
|
||
|
|
* @when after_wp_load
|
||
|
|
*/
|
||
|
|
public function status() {
|
||
|
|
$counts = SPE_Queue::counts();
|
||
|
|
$owner = SP_Gateway::owner_of_name();
|
||
|
|
$mine = SP_Settings::publishing_address();
|
||
|
|
|
||
|
|
WP_CLI::log( 'Name: ' . ( SP_Settings::name() ?: '(not set)' ) );
|
||
|
|
WP_CLI::log( 'Network: ' . SP_Settings::network() );
|
||
|
|
WP_CLI::log( 'Gateway: ' . SP_Settings::gateway() );
|
||
|
|
WP_CLI::log( 'Mode: ' . SP_Settings::mode() );
|
||
|
|
WP_CLI::log( 'Signs as: ' . ( $mine ?: '(no key stored)' ) );
|
||
|
|
WP_CLI::log( 'Owner: ' . ( $owner ?: '(gateway did not answer)' ) );
|
||
|
|
|
||
|
|
if ( $owner && $mine && ! hash_equals( $owner, $mine ) ) {
|
||
|
|
WP_CLI::warning( 'The stored key does not own this name. Uploads will be refused.' );
|
||
|
|
}
|
||
|
|
|
||
|
|
WP_CLI::log(
|
||
|
|
sprintf(
|
||
|
|
'Queue: %d pending, %d done, %d failed',
|
||
|
|
$counts[ SPE_Queue::STATUS_PENDING ],
|
||
|
|
$counts[ SPE_Queue::STATUS_DONE ],
|
||
|
|
$counts[ SPE_Queue::STATUS_FAILED ]
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
WP_CLI::add_command( 'sirius', 'SPE_CLI' );
|