PluginProbe
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel / 1.8.0
FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel v1.8.0
1.8.0 1.7.1 1.7.0 1.6.0 1.5.2 trunk 0.1 0.2.0 0.2.1 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 1.0 1.1 1.2 1.3.1 1.4.0 1.4.1 1.5.0 All 27 releases
flywp / includes / StaticSite / Bootstrap.php

Bootstrap.php in FlyWP Helper – Page Cache, Page Optimization, Emails for FlyWP Server Control Panel 1.8.0, at includes/StaticSite/Bootstrap.php

98 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FlyWP\StaticSite;
4
5 use FlyWP\StaticSite\Engine\Cli\ChangesCommand;
6 use FlyWP\StaticSite\Engine\Cli\SiteCommand;
7 use FlyWP\StaticSite\Engine\Incremental\ChangeLog;
8 use WP_CLI;
9
10 /**
11 * Starts the static site parts of the plugin. The FlyWP app builds the static copy. The plugin gives it the site data.
12 *
13 * - register_cli(): the WP-CLI commands `flywp static site` and `flywp static changes clear`. It runs also
14 * when no FLYWP_API_KEY is set.
15 * - The constructor (site with an API key): records content changes when the option flywp_static_enabled is 1.
16 * - The change log table: installed on plugin activation, and on a version change in wp-admin, REST requests
17 * and WP-CLI. Never on a normal front-end request.
18 *
19 * @since 1.8.0
20 */
21 class Bootstrap {
22
23 /**
24 * Option that holds the installed schema version.
25 */
26 const SCHEMA_VERSION_OPTION = 'flywp_static_schema_version';
27
28 /**
29 * Start the change log of a site with an API key.
30 */
31 public function __construct() {
32 ChangeLog::register_hooks();
33
34 add_action( 'admin_init', [ self::class, 'maybe_install' ] );
35 add_action( 'rest_api_init', [ self::class, 'maybe_install' ] );
36 }
37
38 /**
39 * Register the WP-CLI commands. Does nothing outside WP-CLI.
40 *
41 * @return void
42 */
43 public static function register_cli() {
44 if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
45 return;
46 }
47
48 self::quietly( [ self::class, 'maybe_install' ] );
49
50 WP_CLI::add_command( 'flywp static site', new SiteCommand() );
51 WP_CLI::add_command( 'flywp static changes clear', new ChangesCommand() );
52 }
53
54 /**
55 * Run a callback and send its output (for example dbDelta text) to STDERR, so stdout keeps only JSON lines.
56 *
57 * @param callable $callback Callback.
58 * @param resource|null $stream Stream for the output. STDERR when null.
59 *
60 * @return void
61 */
62 public static function quietly( callable $callback, $stream = null ) {
63 ob_start();
64
65 try {
66 call_user_func( $callback );
67 } finally {
68 $noise = (string) ob_get_clean();
69
70 if ( $noise !== '' ) {
71 fwrite( $stream ? $stream : STDERR, $noise ); // phpcs:ignore WordPress.WP.AlternativeFunctions -- a CLI stream, not a file.
72 }
73 }
74 }
75
76 /**
77 * Install or update the change log table when the plugin version changes.
78 *
79 * @return void
80 */
81 public static function maybe_install() {
82 if ( get_option( self::SCHEMA_VERSION_OPTION ) !== FLYWP_VERSION ) {
83 self::install();
84 }
85 }
86
87 /**
88 * Create or update the change log table.
89 *
90 * @return void
91 */
92 public static function install() {
93 ChangeLog::install();
94
95 update_option( self::SCHEMA_VERSION_OPTION, FLYWP_VERSION, true );
96 }
97 }
98