PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.7
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.7
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / includes / maintenance.php

maintenance.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 2.7.7, at includes/maintenance.php

240 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A simple maintenance mode
4 *
5 * @package BoldBlocks
6 * @author Phi Phan <mrphipv@gmail.com>
7 * @copyright Copyright (c) 2023, Phi Phan
8 */
9
10 namespace BoldBlocks;
11
12 // Exit if accessed directly.
13 defined( 'ABSPATH' ) || exit;
14
15 if ( ! class_exists( Maintenance::class ) ) :
16 /**
17 * A simple maitenance mode for deploying or coming soon message
18 */
19 class Maintenance extends CoreComponent {
20 /**
21 * Run main hooks
22 *
23 * @return void
24 */
25 public function run() {
26 // Register setting fields.
27 add_action( 'init', [ $this, 'register_settings' ] );
28
29 // Starting point.
30 add_action( 'init', [ $this, 'run_maintenance_mode' ] );
31
32 // Add a notice for maintenance mode warning.
33 add_action( 'admin_notices', [ $this, 'add_the_admin_notice_for_maintenance_mode' ] );
34 }
35
36 /**
37 * Register setting fields
38 *
39 * @return void
40 */
41 public function register_settings() {
42 register_setting(
43 'cbb',
44 'cbb_is_maintenance',
45 [
46 'type' => 'boolean',
47 'show_in_rest' => [
48 'name' => 'IsMaintenance',
49 ],
50 'default' => false,
51 ]
52 );
53
54 register_setting(
55 'cbb',
56 'cbb_maintenance_ignore_slug',
57 [
58 'type' => 'string',
59 'show_in_rest' => [
60 'name' => 'MaintenanceSlug',
61 ],
62 'default' => 'wp-login.php',
63 ]
64 );
65
66 register_setting(
67 'cbb',
68 'cbb_maintenance_enable_custom_page',
69 [
70 'type' => 'boolean',
71 'show_in_rest' => [
72 'name' => 'MaintananceEnableCustomPage',
73 ],
74 'default' => false,
75 ]
76 );
77
78 register_setting(
79 'cbb',
80 'cbb_maintenance_page_id',
81 [
82 'type' => 'integer',
83 'show_in_rest' => [
84 'name' => 'MaintanancePageId',
85 ],
86 'default' => 0,
87 ]
88 );
89 }
90
91 /**
92 * Starting point
93 *
94 * @return void
95 */
96 public function run_maintenance_mode() {
97 // Bail if the setting is disabled.
98 if ( ! get_option( 'cbb_is_maintenance' ) ) {
99 return;
100 }
101
102 // Ignore cron job and ajax.
103 if ( wp_doing_cron() || wp_doing_ajax() ) {
104 return;
105 }
106
107 $ignore_slug = get_option( 'cbb_maintenance_ignore_slug' );
108
109 if ( ! $ignore_slug ) {
110 $ignore_slug = [ 'wp-login.php' ];
111 } else {
112 $ignore_slug = preg_split( "/\r\n|\n|\r/", $ignore_slug );
113 $ignore_slug = array_filter(
114 $ignore_slug,
115 function ( $item ) {
116 return trim( $item, ' /' );
117 }
118 );
119 }
120
121 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
122 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? trim( wp_unslash( $_SERVER['REQUEST_URI'] ) ?? '', '/' ) : '';
123
124 // Ignore administrator and the default login page.
125 if ( current_user_can( 'manage_options' ) || in_array( $request_uri, $ignore_slug, true ) ) {
126 return;
127 }
128
129 // Handle custom page.
130 $enable_custom_page = get_option( 'cbb_maintenance_enable_custom_page' );
131 $page_id = get_option( 'cbb_maintenance_page_id' );
132 if ( $enable_custom_page && $page_id ) {
133 $custom_page_url = get_permalink( $page_id );
134 if ( $custom_page_url ) {
135 $parsed_url = wp_parse_url( $custom_page_url );
136 if ( $parsed_url && in_array( trim( $parsed_url['path'] ?? '', '/' ), $ignore_slug, true ) ) {
137 return;
138 }
139 }
140 }
141
142 $page_title = __( 'Maintenance' );
143 $page_content = __( 'The site is currently under maintenance. <br/>Please check back in a few minutes!', 'content-blocks-builder' );
144
145 $site_icon = get_site_icon_url( 64 );
146 if ( $site_icon ) {
147 $site_icon = sprintf( '<img class="site-icon" width="64" src="%1$s" alt="%2$s"/>', $site_icon, get_bloginfo( 'name' ) );
148 } else {
149 $site_icon = sprintf( '<h1 class="site-icon site-title">%s</h1>', get_bloginfo( 'name' ) );
150 }
151
152 if ( ! headers_sent() ) {
153 header( 'Content-Type: text/html; charset=utf-8; Retry-After: 600;' );
154 status_header( 503 );
155 nocache_headers();
156 }
157
158 $text_direction = 'ltr';
159 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
160 $text_direction = 'rtl';
161 }
162
163 $dir_attr = "dir='$text_direction'";
164
165 if ( $enable_custom_page && $page_id ) {
166 $custom_page_url = get_permalink( $page_id );
167 if ( $custom_page_url ) {
168 wp_safe_redirect( $custom_page_url );
169 die();
170 }
171 }
172
173 ob_start();
174 ?>
175 <!DOCTYPE html>
176 <html <?php echo $dir_attr; ?>>
177 <head>
178 <meta charset="utf-8">
179 <?php
180 if ( function_exists( 'wp_robots' ) && function_exists( 'wp_robots_no_robots' ) && function_exists( 'add_filter' ) ) {
181 add_filter( 'wp_robots', 'wp_robots_no_robots' );
182 wp_robots();
183 }
184 ?>
185 <title><?php esc_html_e( $page_title ); ?></title>
186 <style>
187 body { font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif; font-size:1.25rem; color: #333; text-align:center; }
188 .site-icon { margin-bottom: 1rem; }
189 .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 600px; width:clamp(320px,50vw,600px); margin: auto; }
190 .message { padding: 2rem 2rem 2.5rem; border:1px solid #ddd; border-radius: 4px; }
191 .message h1 {
192 margin-top: 0;
193 margin-top: 0;
194 }
195 h1 { font-size: 2.5rem;}
196 </style>
197 </head>
198 <body class="maintenance">
199 <div class="container">
200 <?php echo $site_icon; ?>
201 <div class="message">
202 <h1><?php esc_html_e( $page_title ); ?></h1>
203 <div><?php echo $page_content; ?></div>
204 </div>
205 </div>
206 </body>
207 </html>
208 <?php
209 die( ob_get_clean() );
210 }
211
212 /**
213 * Add the admin notice for the maintenance mode
214 *
215 * @return void
216 */
217 public function add_the_admin_notice_for_maintenance_mode() {
218 // Bail if the setting is disabled.
219 if ( ! get_option( 'cbb_is_maintenance' ) ) {
220 return;
221 }
222
223 // Get the current screen.
224 $screen = get_current_screen();
225
226 if ( ! $screen ) {
227 return;
228 }
229
230 if ( ! ( in_array( $screen->base, [ 'dashboard', 'plugins', 'themes' ], true ) || 'edit.php?post_type=boldblocks_block' === $screen->parent_file ) ) {
231 return;
232 }
233
234 $settings_link = sprintf( '<a href="%1$s">%2$s</a>', admin_url( '/edit.php?post_type=boldblocks_block&page=cbb-settings&tab=developer' ), __( 'Turn it off.', 'content-blocks-buider' ) );
235
236 echo '<div class="notice notice-warning is-dismissible"><p>' . __( 'The site is currently under maintenance. Please remember to turn off this mode when the site is ready. You can do this from the settings page.', 'content-blocks-builder' ) . ' ' . $settings_link . '</p></div>';
237 }
238 }
239 endif;
240