← Back
Editing: Saved_Checks_Control.php
<?php declare( strict_types=1 ); namespace Uncanny_Automator\App\Presentation\Admin; use Uncanny_Automator\App\Feature_State\Infrastructure\Mcp_Allocation_Facts_Refresh; use Uncanny_Automator\App\Infrastructure\Page_Builder\Page_Builder_Tables_Check; /** * The "check again" buttons under Settings > Advanced > Cache and saved checks. * * Automator saves the result of two checks so pages load faster: this site's * plan and credits, and the storage engine of the tables Page Builder writes * to. Each button runs its check now, whatever was saved, and reports the * outcome once on the page the person came from. * * Gated on the admin capability and a nonce, like every form that changes * what the site has stored. * * @since 8.1.2 */ final class Saved_Checks_Control { /** * admin-post action. * * @var string */ public const ACTION = 'automator_saved_check_run'; /** * Nonce action. * * @var string */ public const NONCE_ACTION = 'automator_saved_check_run'; /** * The checks a form can name. */ public const CHECK_PLAN_AND_CREDITS = 'plan_and_credits'; public const CHECK_PAGE_BUILDER_TABLES = 'page_builder_tables'; public const CHECK_EVERYTHING = 'everything'; /** * The outcome of a plan and credits check on a site where the address of * the plan and credits service is not valid, so no check can be made. */ public const PLAN_CHECK_UNAVAILABLE = 'unavailable'; /** * @var Mcp_Allocation_Facts_Refresh|null */ private $plan_and_credits; /** * @var Page_Builder_Tables_Check */ private $page_builder_tables; /** * @var callable(string): void */ private $redirect; /** * @param Mcp_Allocation_Facts_Refresh|null $plan_and_credits Asks the plan and credits service; null when * the address of that service is not valid. * @param Page_Builder_Tables_Check $page_builder_tables Asks the database for the table engines. * @param callable|null $redirect Receives the redirect URL. Defaults to * wp_safe_redirect() + exit. */ public function __construct( ?Mcp_Allocation_Facts_Refresh $plan_and_credits, Page_Builder_Tables_Check $page_builder_tables, ?callable $redirect = null ) { $this->plan_and_credits = $plan_and_credits; $this->page_builder_tables = $page_builder_tables; $this->redirect = $redirect ?? static function ( string $url ): void { wp_safe_redirect( $url ); exit; }; } /** * Whether a plan and credits check can be made on this site at all. * * @return bool */ public function can_check_plan_and_credits(): bool { return null !== $this->plan_and_credits; } /** * @return void */ public function register(): void { add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) ); } /** * Run the check the form named, then return to the page it was on. * * @return void */ public function handle(): void { check_admin_referer( self::NONCE_ACTION ); if ( ! current_user_can( automator_get_admin_capability() ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'uncanny-automator' ), 403 ); } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_admin_referer() ran above. $check = isset( $_POST['check'] ) ? sanitize_key( wp_unslash( $_POST['check'] ) ) : ''; if ( ! in_array( $check, array( self::CHECK_PLAN_AND_CREDITS, self::CHECK_PAGE_BUILDER_TABLES, self::CHECK_EVERYTHING ), true ) ) { wp_die( esc_html__( 'Automator does not know that check.', 'uncanny-automator' ), 400 ); } $outcomes = array(); if ( self::CHECK_PAGE_BUILDER_TABLES !== $check ) { $outcomes[] = self::plan_and_credits_outcome( null === $this->plan_and_credits ? self::PLAN_CHECK_UNAVAILABLE : $this->plan_and_credits->check_now() ); } if ( self::CHECK_PLAN_AND_CREDITS !== $check ) { $outcomes[] = self::page_builder_tables_outcome( $this->page_builder_tables->recheck() ); } set_transient( $this->flash_key(), $outcomes, 5 * MINUTE_IN_SECONDS ); $referer = wp_get_referer(); ( $this->redirect )( $referer ? $referer : admin_url() ); } /** * What a plan and credits check that ended this way reports: an alert type * and the escaped sentence. * * @param string $outcome One of Mcp_Allocation_Facts_Refresh::CHECK_*. * * @return array{type: string, message: string} */ public static function plan_and_credits_outcome( string $outcome ): array { switch ( $outcome ) { case Mcp_Allocation_Facts_Refresh::CHECK_STORED: return array( 'type' => 'success', 'message' => esc_html_x( 'Plan and credits checked. See which menus are available below.', 'Saved checks', 'uncanny-automator' ), ); case Mcp_Allocation_Facts_Refresh::CHECK_STORED_UNRESOLVED: return array( 'type' => 'warning', 'message' => esc_html_x( 'Automator saved your latest plan and credit details, but it still needs more information to show the right menus. Check again in a minute.', 'Saved checks', 'uncanny-automator' ), ); case self::PLAN_CHECK_UNAVAILABLE: return array( 'type' => 'error', 'message' => self::plan_service_address_sentence(), ); case Mcp_Allocation_Facts_Refresh::CHECK_BUSY: return array( 'type' => 'info', 'message' => esc_html_x( 'Another plan and credits check is underway. Reload this page in a few seconds.', 'Saved checks', 'uncanny-automator' ), ); case Mcp_Allocation_Facts_Refresh::CHECK_NOT_NEEDED: return array( 'type' => 'info', 'message' => esc_html_x( "Your site does not need this check.", 'Saved checks', 'uncanny-automator' ), ); case Mcp_Allocation_Facts_Refresh::CHECK_NO_LICENSE: return array( 'type' => 'info', 'message' => esc_html_x( 'Add your license key or connect your account before running this check.', 'Saved checks', 'uncanny-automator' ), ); } return array( 'type' => 'error', 'message' => esc_html_x( 'Automator could not get your plan and credit details. Try again in a minute.', 'Saved checks', 'uncanny-automator' ), ); } /** * Say why no plan and credits check can be made. Escaped text. * * @return string */ public static function plan_service_address_sentence(): string { return esc_html_x( 'Automator has the wrong address for checking your plan and credits. If someone set a custom address for this check, ask them to remove it. Then reload this page.', 'Saved checks', 'uncanny-automator' ); } /** * What a Page Builder table check that ended this way reports: an alert * type and the escaped sentence. * * @param array{status: string, table: string, engine: string} $result From Page_Builder_Tables_Check. * * @return array{type: string, message: string} */ public static function page_builder_tables_outcome( array $result ): array { if ( Page_Builder_Tables_Check::READY === $result['status'] ) { return array( 'type' => 'success', 'message' => esc_html_x( 'Storage is ready for Page Builder. The tables it needs use InnoDB.', 'Saved checks', 'uncanny-automator' ), ); } if ( Page_Builder_Tables_Check::NOT_READY === $result['status'] ) { return array( 'type' => 'error', 'message' => self::table_not_ready_sentence( $result['table'], $result['engine'] ), ); } if ( Page_Builder_Tables_Check::FAILED === $result['status'] ) { return array( 'type' => 'error', 'message' => esc_html_x( 'Automator could not check Page Builder storage. Try again in a moment.', 'Saved checks', 'uncanny-automator' ), ); } return array( 'type' => 'info', 'message' => esc_html_x( 'This check runs when Page Builder is available on your site.', 'Saved checks', 'uncanny-automator' ), ); } /** * Say which table is not on InnoDB and what to do about it. Escaped text. * * @param string $table The table. * @param string $engine Its storage engine, or "unknown". * * @return string */ public static function table_not_ready_sentence( string $table, string $engine ): string { if ( '' === $engine || 'unknown' === $engine ) { return sprintf( /* translators: %s: database table name */ esc_html_x( 'WordPress did not say how it stores data in %s. Ask your web host to make sure that table uses InnoDB. Then check again.', 'Saved checks', 'uncanny-automator' ), esc_html( $table ) ); } return sprintf( /* translators: 1: database table name, 2: storage engine name such as MyISAM */ esc_html_x( 'Ask your web host to switch %1$s from %2$s to InnoDB so Page Builder can use it. Then check again.', 'Saved checks', 'uncanny-automator' ), esc_html( $table ), esc_html( $engine ) ); } /** * The outcomes of this user's last click, once. Each message is escaped text. * * @return array<int, array{type: string, message: string}> */ public function take_outcomes(): array { $stored = get_transient( $this->flash_key() ); if ( ! is_array( $stored ) ) { return array(); } delete_transient( $this->flash_key() ); $outcomes = array(); foreach ( $stored as $outcome ) { if ( is_array( $outcome ) && isset( $outcome['type'], $outcome['message'] ) ) { $outcomes[] = array( 'type' => (string) $outcome['type'], 'message' => (string) $outcome['message'], ); } } return $outcomes; } /** * One button: a form of its own that posts the named check. * * @param string $check One of the CHECK_* names. * @param string $label The button's words. * @param string $slot The slot of the surrounding component the form goes in, when it has one. * * @return void */ public function render_button( string $check, string $label, string $slot = '' ): void { ?> <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="automator-saved-check-form<?php echo '' === $slot ? ' uap-spacing-top' : ''; ?>"<?php echo '' === $slot ? '' : ' slot="' . esc_attr( $slot ) . '"'; ?>> <input type="hidden" name="action" value="<?php echo esc_attr( self::ACTION ); ?>" /> <input type="hidden" name="check" value="<?php echo esc_attr( $check ); ?>" /> <?php wp_nonce_field( self::NONCE_ACTION ); ?> <uas-button type="submit" size="sm" color="secondary"><uas-icon code="rotate"></uas-icon> <?php echo esc_html( $label ); ?></uas-button> </form> <?php } /** * @return string */ private function flash_key(): string { return 'automator_saved_checks_outcome_' . get_current_user_id(); } }
Save File
Cancel