Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Update classes with latest simply static code changes (v2.2.9) #60

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
93 changes: 55 additions & 38 deletions includes/StaticDeployJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,32 @@
use Simply_Static\Wrapup_Task;
use WP_Error;

/**
* StaticDeployJob - starting point of static deploy
*
* Based on: Simply_Static\Archive_Creation_Job (class-ss-archive-creation-job.php)
*/
class StaticDeployJob extends \WP_Background_Process
{
const CLEAR_FILTER = 'simply_static_deploy_clear_directory';

protected $options;
/**
* The name of the task currently being processed
* @var string
*/
protected $current_task = null;

protected $current_task;
/**
* An instance of the options structure containing all options for this plugin
* @var \Simply_Static\Options
*/
protected $options = null;

protected $task_list;
/**
* Array containing the list of tasks to process
* @var array
*/
protected $task_list = array();

protected $task_class_mapping = [
'store_initial_options' => StoreInitialOptionsTask::class,
Expand All @@ -48,10 +65,10 @@ class StaticDeployJob extends \WP_Background_Process
public function __construct()
{
$this->options = Options::instance();

$this->task_list = $this->compose_task_list();
$this->task_list = apply_filters( 'simplystatic.archive_creation_job.task_list', array(), $this->options->get( 'delivery_method' ) );

if (!static::is_job_done()) {
if (!$this->is_job_done()) {
register_shutdown_function([$this, 'shutdown_handler']);
}

Expand All @@ -64,17 +81,26 @@ public function __construct()
*/
public function start(?int $post_id = null, ?bool $recursive = false)
{
if (static::is_job_done()) {
global $blog_id;

// @TODO: can this be replaced with post_id?
$blog_id = $post_id;
if (!$blog_id) {
$blog_id = get_current_blog_id();
}

do_action( 'ss_archive_creation_job_before_start', $blog_id, $this );

if ($this->is_job_done()) {
Util::debug_log("Starting a job; no job is presently running");
// When we have a post id, we should set that somewhere, since every task does its own request
// with each task request, we compose the task list based on that setting.
update_option(Plugin::SLUG . '_single_deploy_id', $post_id);
update_option(Plugin::SLUG . '_single_deploy_recursive', $recursive);
$this->task_list = $this->compose_task_list();
Util::debug_log(
"Here's our task list: " . implode(', ', $this->task_list)
);
global $blog_id;
Util::debug_log("Here's our task list: " . implode(', ', $this->task_list));

do_action( 'ss_archive_creation_job_before_start_queue', $blog_id, $this );

$this->task_list = $this->compose_task_list();
$first_task = $this->task_list[0];
Expand All @@ -96,12 +122,16 @@ public function start(?int $post_id = null, ?bool $recursive = false)
->save()
->dispatch();

do_action( 'ss_archive_creation_job_after_start_queue', $blog_id, $this );

return true;
} else {
Util::debug_log(
"Not starting; we're already in the middle of a job"
);
// looks like we're in the middle of creating an archive...
do_action( 'ss_archive_creation_job_already_running', $blog_id, $this );

return false;
}
}
Expand All @@ -120,30 +150,24 @@ public function shutdown_handler()
// Note: this function must be public in order to function properly.
$error = error_get_last();
// only trigger on actual errors, not warnings or notices
if (
$error &&
in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_USER_ERROR])
) {
if ($error && in_array($error['type'], [E_ERROR, E_CORE_ERROR, E_USER_ERROR])) {
$this->clear_scheduled_event();
$this->unlock_process();
$this->cancel_process();

$end_time = Util::formatted_datetime();
$this->options->set('archive_end_time', $end_time)->save();
$this->options
->set('archive_end_time', $end_time)
->save();

$error_message = '(' . $error['type'] . ') ' . $error['message'];
$error_message .= ' in <b>' . $error['file'] . '</b>';
$error_message .= ' on line <b>' . $error['line'] . '</b>';

$message = sprintf(
__("Error: %s", 'simply-static'),
$error_message
);
$message = sprintf(__("Error: %s", 'simply-static'), $error_message);
Util::debug_log($message);
do_action(
'simply_static_deploy_error',
new \WP_Error('simply_static_deploy_error', $message)
);
// @TODO: can this one be removed
do_action('simply_static_deploy_error', new \WP_Error('simply_static_deploy_error', $message));
$this->save_status_message($message, 'error');

// restore initial options
Expand All @@ -156,15 +180,13 @@ public function shutdown_handler()
* Is the job done?
* @return boolean True if done, false if not
*/
public static function is_job_done(): bool
public function is_job_done(): bool
{
$options = Options::instance();
$start_time = $options->get('archive_start_time');
$end_time = $options->get('archive_end_time');
$start_time = $this->options->get('archive_start_time');
$end_time = $this->options->get('archive_end_time');
// we're done if the start and end time are null (never run) or if
// the start and end times are both set
return ($start_time == null && $end_time == null) ||
($start_time != null && $end_time != null);
return ($start_time == null && $end_time == null) || ($start_time != null && $end_time != null);
}

public static function last_end_time()
Expand Down Expand Up @@ -223,9 +245,7 @@ protected function task($task_name)
// finished current task, try to find the next one
$next_task = $this->find_next_task();
if ($next_task === null) {
Util::debug_log(
"This task is done and there are no more tasks, time to complete the job"
);
Util::debug_log("This task is done and there are no more tasks, time to complete the job");
// we're done; returning false to remove item from queue
return false;
} else {
Expand All @@ -235,9 +255,7 @@ protected function task($task_name)
}
} else {
// $is_done === false
Util::debug_log(
"We're not done with the " . $task_name . " task yet"
);
Util::debug_log("We're not done with the " . $task_name . " task yet");
// returning current task name to continue processing
return $task_name;
}
Expand Down Expand Up @@ -266,11 +284,10 @@ protected function complete()

$this->options->set('archive_end_time', $end_time);

// @TODO: does this still exist?
do_action('simply_static_deploy_complete', $this->options);

$this->save_status_message(
sprintf(__('Done! Finished in %s', 'simply-static'), $time_string)
);
$this->save_status_message(sprintf(__('Done! Finished in %s', 'simply-static'), $time_string));
parent::complete();
}

Expand Down
17 changes: 16 additions & 1 deletion includes/Tasks/FetchUrlsRecursiveTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FetchUrlsRecursiveTask extends Fetch_Urls_Task
*/
public function perform()
{
$batch_size = apply_filters('simply_static_fetch_urls_batch_size', 10);
$batch_size = apply_filters('simply_static_fetch_urls_batch_size', 50);
// Custom code block
$post_id = get_option(Plugin::SLUG . '_single_deploy_id');
$post_url = get_permalink($post_id);
Expand Down Expand Up @@ -96,6 +96,12 @@ public function perform()
$static_page->set_status_message(__("Do not save or follow", 'simply-static'));
$static_page->save();
continue;
} else {
$success = Url_Fetcher::instance()->fetch( $static_page );
}

if ( ! $success ) {
continue;
}

/**
Expand Down Expand Up @@ -124,6 +130,15 @@ public function perform()
}
// End of custom code block

// Not found? It's maybe a redirection page. Let's try it without our param.
if ( $static_page->http_status_code === 404 ) {
$success = Url_Fetcher::instance()->fetch( $static_page, false );

if ( ! $success ) {
continue;
}
}

// If we get a 30x redirect...
if (in_array($static_page->http_status_code, array(301, 302, 303, 307, 308))) {
$this->handle_30x_redirect($static_page, $save_file, $follow_urls);
Expand Down
9 changes: 6 additions & 3 deletions includes/Tasks/SetupSingleTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Simply_Static\Task;
use Simply_Static\Util;

/**
* SetupSingleTask
*
* Based on: Simply_Static\Setup_Task (class-ss-setup-task.php)
*/
class SetupSingleTask extends Task
{
/**
Expand All @@ -20,9 +25,7 @@ public function perform(): bool
{
$post_id = get_option(Plugin::SLUG . '_single_deploy_id');
$recursive = get_option(Plugin::SLUG . '_single_deploy_recursive');
$this->save_status_message(
"Setting up for single post with id $post_id"
);
$this->save_status_message("Setting up for single post with id $post_id");

// should we create tmp directory of it not exists?
$archive_dir = $this->options->get_archive_dir();
Expand Down