← Back
Editing: Saved_Checks_Section.php
<?php declare( strict_types=1 ); namespace Uncanny_Automator\App\Presentation\Admin; use Uncanny_Automator\App\Feature_State\Application\Get_Allocation_Refresh_Need; use Uncanny_Automator\App\Feature_State\Application\Get_Feature_State; use Uncanny_Automator\App\Feature_State\Domain\Feature_State; use Uncanny_Automator\App\Feature_State\Infrastructure\Mcp_Allocation_Facts_Refresh; use Uncanny_Automator\App\Feature_State\Ports\Allocation_Observation_Port; use Uncanny_Automator\App\Infrastructure\Page_Builder\Page_Builder_Tables_Check; use function Uncanny_Automator\App\Infrastructure\automator_feature_state_query; /** * Settings > Advanced > Cache and saved checks > Saved checks. * * One row for each result Automator saves so pages load faster: this site's * plan and credits, and the storage engine of the tables Page Builder writes * to. A row says what the check decides, what was saved and when, and offers * the button that runs it again ({@see Saved_Checks_Control}). * * Printed outside the page's settings form: each button is a form of its own * and acts at once, with nothing to save. * * @since 8.1.2 */ final class Saved_Checks_Section { /** * Where the Cache and saved checks page prints this section. */ public const HOOK = 'automator_settings_advanced_automator_cache_saved_checks'; /** * @var Saved_Checks_Control */ private $control; /** * @var Get_Allocation_Refresh_Need */ private $need; /** * @var Allocation_Observation_Port */ private $observations; /** * @var Page_Builder_Tables_Check */ private $page_builder_tables; /** * @var callable(): ?array{state: Feature_State, source: string} */ private $feature_state; /** * @param Saved_Checks_Control $control The buttons and their one-time outcome. * @param Get_Allocation_Refresh_Need $need Whether a plan and credits check is due. * @param Allocation_Observation_Port $observations How old the saved plan and credits answer is. * @param Page_Builder_Tables_Check $page_builder_tables Where the Page Builder tables stand. * @param callable|null $feature_state Returns what this page's menus were built from: the * feature state and where it came from (a * Get_Feature_State::SOURCE_* value), or null when unknown. */ public function __construct( Saved_Checks_Control $control, Get_Allocation_Refresh_Need $need, Allocation_Observation_Port $observations, Page_Builder_Tables_Check $page_builder_tables, ?callable $feature_state = null ) { $this->control = $control; $this->need = $need; $this->observations = $observations; $this->page_builder_tables = $page_builder_tables; $this->feature_state = $feature_state ?? static function (): ?array { $query = automator_feature_state_query(); if ( null === $query ) { return null; } // The same memoized answer the menus on this page were built from. return array( 'state' => $query->execute(), 'source' => (string) $query->diagnostics()['source'], ); }; } /** * @return void */ public function register(): void { add_action( self::HOOK, array( $this, 'render' ) ); } /** * Print the section. * * Built from Automator's own components only: an accordion with one item * for each check, rows inside it, chips for every state and value, and a * button at the end of the row that explains it. * * @return void */ public function render(): void { if ( ! current_user_can( automator_get_admin_capability() ) ) { return; } $need = $this->need->execute(); $plan_can_run = Get_Allocation_Refresh_Need::NOT_NEEDED !== $need && $this->control->can_check_plan_and_credits(); $tables = $this->page_builder_tables->current(); $tables_run = Page_Builder_Tables_Check::NOT_RUNNING !== $tables['status']; ?> <div class="uap-settings-panel-content-separator"></div> <section class="automator-saved-checks"> <div class="uap-settings-panel-content-subtitle"><?php echo esc_html_x( 'Saved checks', 'Saved checks', 'uncanny-automator' ); ?></div> <div class="uap-settings-panel-content-paragraph"><?php echo esc_html_x( 'Missing a menu or recently moved your site? Run the check below that fits. The result appears here when the page reloads.', 'Saved checks', 'uncanny-automator' ); ?></div> <?php foreach ( $this->control->take_outcomes() as $outcome ) { ?> <uas-alert type="<?php echo esc_attr( $outcome['type'] ); ?>" class="uap-spacing-top"><?php echo wp_kses( $outcome['message'], array() ); ?></uas-alert> <?php } ?> <uas-accordion class="uap-spacing-top"> <?php $this->render_plan_and_credits( $need ); $this->render_page_builder_tables( $tables ); ?> </uas-accordion> <?php if ( $plan_can_run || $tables_run ) { $this->control->render_button( Saved_Checks_Control::CHECK_EVERYTHING, esc_html_x( 'Run every check again', 'Saved checks', 'uncanny-automator' ) ); } ?> </section> <?php } /** * The plan and credits item. * * @param string $need Answer from Get_Allocation_Refresh_Need. * * @return void */ private function render_plan_and_credits( string $need ): void { $age = $this->saved_age(); $not_needed = Get_Allocation_Refresh_Need::NOT_NEEDED === $need; $can_check = $this->control->can_check_plan_and_credits(); $decided = $this->menus_built_from(); if ( $not_needed ) { $state = array( 'neutral', esc_html_x( 'Not used on this site', 'Saved checks', 'uncanny-automator' ) ); } elseif ( ! $can_check ) { $state = array( 'error', esc_html_x( 'Cannot check', 'Saved checks', 'uncanny-automator' ) ); } elseif ( null === $age ) { $state = array( 'warning', esc_html_x( 'No check time saved', 'Saved checks', 'uncanny-automator' ) ); } elseif ( null !== $decided && Get_Feature_State::SOURCE_RESOLVED !== $decided['source'] ) { // An answer is saved, but the menus on this page do not follow it. $state = array( 'warning', esc_html_x( 'Not decided', 'Saved checks', 'uncanny-automator' ) ); } elseif ( Get_Allocation_Refresh_Need::FRESH === $need ) { $state = array( 'success', esc_html_x( 'Up to date', 'Saved checks', 'uncanny-automator' ) ); } else { $state = array( 'info', esc_html_x( 'Check due', 'Saved checks', 'uncanny-automator' ) ); } ?> <uas-accordion-item open data-check="<?php echo esc_attr( Saved_Checks_Control::CHECK_PLAN_AND_CREDITS ); ?>" data-state="<?php echo esc_attr( $need ); ?>"> <?php $this->render_summary( 'badge-check', esc_html_x( 'Plan and credits', 'Saved checks', 'uncanny-automator' ), $state ); $this->render_row( esc_html_x( 'What it checks', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Whether to show the Uncanny Agent and Page Builder menus on your site.', 'Saved checks', 'uncanny-automator' ) ); $result = $this->plan_result( $decided ); if ( null !== $result ) { $this->render_row( esc_html_x( 'Result', 'Saved checks', 'uncanny-automator' ), $result['sentence'], $result['chip'] ); } if ( $not_needed ) { $this->render_row( esc_html_x( 'Checks', 'Saved checks', 'uncanny-automator' ), esc_html_x( "Your site does not need this check.", 'Saved checks', 'uncanny-automator' ) ); } elseif ( ! $can_check ) { $this->render_row( esc_html_x( 'Checks', 'Saved checks', 'uncanny-automator' ), Saved_Checks_Control::plan_service_address_sentence() ); } else { $this->render_row( esc_html_x( 'Last checked', 'Saved checks', 'uncanny-automator' ), '', null === $age ? array( 'warning', esc_html_x( 'Unknown', 'Saved checks', 'uncanny-automator' ) ) : array( 'neutral', $this->date_and_time( time() - $age ) ) ); $this->render_row( esc_html_x( 'Next automatic check', 'Saved checks', 'uncanny-automator' ), '', array( 'neutral', $this->next_plan_check( $need, $age ) ) ); if ( false !== get_transient( Mcp_Allocation_Facts_Refresh::FAILURE_TRANSIENT ) ) { $this->render_row( esc_html_x( 'Last attempt', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'The last check could not get your plan and credit details.', 'Saved checks', 'uncanny-automator' ), array( 'warning', esc_html_x( 'Wait a minute and try again', 'Saved checks', 'uncanny-automator' ) ) ); } $this->render_action_row( esc_html_x( 'Check now', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Use this if a menu is missing or you recently bought credits. Give changes about a minute to show up.', 'Saved checks', 'uncanny-automator' ), Saved_Checks_Control::CHECK_PLAN_AND_CREDITS, esc_html_x( 'Check plan and credits now', 'Saved checks', 'uncanny-automator' ) ); } ?> </uas-accordion-item> <?php } /** * The Page Builder tables item. * * @param array{status: string, table: string, engine: string} $tables From Page_Builder_Tables_Check. * * @return void */ private function render_page_builder_tables( array $tables ): void { if ( Page_Builder_Tables_Check::READY === $tables['status'] ) { $state = array( 'success', esc_html_x( 'Ready', 'Saved checks', 'uncanny-automator' ) ); } elseif ( Page_Builder_Tables_Check::NOT_READY === $tables['status'] ) { $state = array( 'error', esc_html_x( 'Not ready', 'Saved checks', 'uncanny-automator' ) ); } elseif ( Page_Builder_Tables_Check::FAILED === $tables['status'] ) { $state = array( 'warning', esc_html_x( 'Could not check', 'Saved checks', 'uncanny-automator' ) ); } else { $state = array( 'neutral', esc_html_x( 'Not running', 'Saved checks', 'uncanny-automator' ) ); } ?> <uas-accordion-item open data-check="<?php echo esc_attr( Saved_Checks_Control::CHECK_PAGE_BUILDER_TABLES ); ?>" data-state="<?php echo esc_attr( $tables['status'] ); ?>"> <?php $this->render_summary( 'database', esc_html_x( 'Page Builder storage', 'Saved checks', 'uncanny-automator' ), $state ); $this->render_row( esc_html_x( 'What it checks', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Whether your site stores page data in the format Page Builder needs.', 'Saved checks', 'uncanny-automator' ) ); if ( Page_Builder_Tables_Check::READY === $tables['status'] ) { $this->render_row( esc_html_x( 'Result', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Your site uses the storage format Page Builder needs.', 'Saved checks', 'uncanny-automator' ), array( 'success', 'InnoDB' ) ); $this->render_row( esc_html_x( 'Next automatic check', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'After a Page Builder update or if a page save finds a storage problem.', 'Saved checks', 'uncanny-automator' ) ); } elseif ( Page_Builder_Tables_Check::NOT_READY === $tables['status'] ) { $engine = '' === $tables['engine'] || 'unknown' === $tables['engine'] ? esc_html_x( 'Storage type unknown', 'Saved checks', 'uncanny-automator' ) : $tables['engine']; $this->render_row( esc_html_x( 'Result', 'Saved checks', 'uncanny-automator' ), Saved_Checks_Control::table_not_ready_sentence( $tables['table'], $tables['engine'] ), array( 'error', $engine ) ); $this->render_row( esc_html_x( 'Next automatic check', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'When a page loads, until the storage problem is fixed.', 'Saved checks', 'uncanny-automator' ) ); } elseif ( Page_Builder_Tables_Check::FAILED === $tables['status'] ) { $this->render_row( esc_html_x( 'Result', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Automator could not check storage on this site. Try again in a moment.', 'Saved checks', 'uncanny-automator' ) ); } else { $this->render_row( esc_html_x( 'Checks', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'This check runs when Page Builder is available on your site.', 'Saved checks', 'uncanny-automator' ) ); } if ( Page_Builder_Tables_Check::NOT_RUNNING !== $tables['status'] ) { $this->render_action_row( esc_html_x( 'Check now', 'Saved checks', 'uncanny-automator' ), esc_html_x( 'Use this after moving your site, restoring a backup, or asking your host to fix a storage problem.', 'Saved checks', 'uncanny-automator' ), Saved_Checks_Control::CHECK_PAGE_BUILDER_TABLES, esc_html_x( 'Check tables again', 'Saved checks', 'uncanny-automator' ) ); } ?> </uas-accordion-item> <?php } /** * An item's always-visible header: its icon, its name and its state. * * @param string $icon Icon code. * @param string $name The check's name. Escaped text. * @param array{0: string, 1: string} $state Chip color and label. * * @return void */ private function render_summary( string $icon, string $name, array $state ): void { ?> <div slot="summary"> <uas-icon code="<?php echo esc_attr( $icon ); ?>"></uas-icon> <strong><?php echo esc_html( $name ); ?></strong> <uas-chip size="sm" color="<?php echo esc_attr( $state[0] ); ?>" filled label="<?php echo esc_attr( $state[1] ); ?>"></uas-chip> </div> <?php } /** * One row of an item: what the row is about, a sentence, and a short value * at the row's end. * * @param string $label What the row is about. Escaped text. * @param string $sentence Escaped text, or empty when the value says it all. * @param array{0: string, 1: string}|null $value Chip color and label, or none. * * @return void */ private function render_row( string $label, string $sentence, ?array $value = null ): void { ?> <uas-accordion-item-li> <strong><?php echo wp_kses( $label, array() ); ?></strong> <?php echo wp_kses( $sentence, array() ); if ( null !== $value ) { ?> <uas-chip slot="chip" size="sm" color="<?php echo esc_attr( $value[0] ); ?>" filled label="<?php echo esc_attr( $value[1] ); ?>"></uas-chip> <?php } ?> </uas-accordion-item-li> <?php } /** * The last row of an item: when to use the button, and the button. * * @param string $row_label What the row is about. Escaped text. * @param string $sentence Escaped text. * @param string $check One of Saved_Checks_Control::CHECK_*. * @param string $label The button's words. Escaped text. * * @return void */ private function render_action_row( string $row_label, string $sentence, string $check, string $label ): void { ?> <uas-accordion-item-li> <strong><?php echo wp_kses( $row_label, array() ); ?></strong> <?php echo wp_kses( $sentence, array() ); $this->control->render_button( $check, $label, 'chip' ); ?> </uas-accordion-item-li> <?php } /** * Say when Automator runs the plan and credits check again by itself. * * @param string $need Answer from Get_Allocation_Refresh_Need. * @param int|null $age Seconds since the last check, or null when there was none. * * @return string Escaped text. */ private function next_plan_check( string $need, ?int $age ): string { if ( null === $age || Get_Allocation_Refresh_Need::FRESH !== $need ) { return esc_html_x( 'The next time a page needs this check', 'Saved checks', 'uncanny-automator' ); } return sprintf( /* translators: %s: date and time */ esc_html_x( 'After %s', 'Saved checks', 'uncanny-automator' ), esc_html( $this->date_and_time( time() - $age + (int) $this->need->lifetime_in_seconds() ) ) ); } /** * Get what the menus on this page were built from. * * @return array{state: Feature_State, source: string}|null Null when unknown. */ private function menus_built_from(): ?array { try { $built_from = ( $this->feature_state )(); } catch ( \Throwable $error ) { unset( $error ); return null; } if ( ! is_array( $built_from ) || ! ( $built_from['state'] ?? null ) instanceof Feature_State || ! is_string( $built_from['source'] ?? null ) ) { return null; } return $built_from; } /** * Say what the saved answer means for the menus: one sentence and a short * value. * * When the policy could not decide from the saved answer, the menus come * from an earlier result or are all hidden. The row says that, instead of * presenting a fallback as what the plan allows. * * @param array{state: Feature_State, source: string}|null $built_from From menus_built_from(). * * @return array{sentence: string, chip: array{0: string, 1: string}}|null Escaped text; null when unknown. */ private function plan_result( ?array $built_from ): ?array { if ( null === $built_from ) { return null; } if ( Get_Feature_State::SOURCE_LAST_KNOWN_GOOD === $built_from['source'] ) { return array( 'sentence' => esc_html_x( 'The latest check did not give Automator enough information. The menus still use the last result that worked.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'warning', esc_html_x( 'Using the last result', 'Saved checks', 'uncanny-automator' ) ), ); } if ( Get_Feature_State::SOURCE_RESOLVED !== $built_from['source'] ) { return array( 'sentence' => esc_html_x( 'Automator needs more information before it can show the Uncanny Agent or Page Builder menu. Try checking again.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'warning', esc_html_x( 'Menus hidden', 'Saved checks', 'uncanny-automator' ) ), ); } $agent = $built_from['state']->is_visible( Feature_State::AGENT_LAUNCHER_TAB ); $page_builder = $built_from['state']->is_visible( Feature_State::PAGE_BUILDER_MENU ); if ( $agent && $page_builder ) { return array( 'sentence' => esc_html_x( 'Your plan and credits let Automator show both menus.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'success', esc_html_x( 'Both menus available', 'Saved checks', 'uncanny-automator' ) ), ); } if ( $agent ) { return array( 'sentence' => esc_html_x( 'Your plan and credits let Automator show the Uncanny Agent menu only.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'info', esc_html_x( 'One menu available', 'Saved checks', 'uncanny-automator' ) ), ); } if ( $page_builder ) { return array( 'sentence' => esc_html_x( 'Your plan and credits let Automator show the Page Builder menu only.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'info', esc_html_x( 'One menu available', 'Saved checks', 'uncanny-automator' ) ), ); } return array( 'sentence' => esc_html_x( 'Neither menu is available with your current plan and credits.', 'Saved checks', 'uncanny-automator' ), 'chip' => array( 'neutral', esc_html_x( 'Menus unavailable', 'Saved checks', 'uncanny-automator' ) ), ); } /** * How old the saved plan and credits answer is. * * @return int|null Seconds, or null when nothing usable is saved. */ private function saved_age(): ?int { try { return $this->observations->age_in_seconds(); } catch ( \Throwable $error ) { unset( $error ); return null; } } /** * A moment in the site's own date and time format and time zone. * * @param int $timestamp Unix time. * * @return string */ private function date_and_time( int $timestamp ): string { return (string) wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp ); } }
Save File
Cancel