PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.8
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.8
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.8, at includes/maintenance.php

249 lines 6.7 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', 'content-blocks-builder' );
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 // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
148 $site_icon = sprintf( '<img class="site-icon" width="64" src="%1$s" alt="%2$s"/>', $site_icon, get_bloginfo( 'name' ) );
149 } else {
150 $site_icon = sprintf( '<h1 class="site-icon site-title">%s</h1>', get_bloginfo( 'name' ) );
151 }
152
153 if ( ! headers_sent() ) {
154 header( 'Content-Type: text/html; charset=utf-8; Retry-After: 600;' );
155 status_header( 503 );
156 nocache_headers();
157 }
158
159 $text_direction = 'ltr';
160 if ( function_exists( 'is_rtl' ) && is_rtl() ) {
161 $text_direction = 'rtl';
162 }
163
164 if ( $enable_custom_page && $page_id ) {
165 $custom_page_url = get_permalink( $page_id );
166 if ( $custom_page_url ) {
167 wp_safe_redirect( $custom_page_url );
168 die();
169 }
170 }
171
172 ob_start();
173 ?>
174 <!DOCTYPE html>
175 <html dir="<?php echo esc_attr( $text_direction ); ?>">
176 <head>
177 <meta charset="utf-8">
178 <?php
179 if ( function_exists( 'wp_robots' ) && function_exists( 'wp_robots_no_robots' ) && function_exists( 'add_filter' ) ) {
180 add_filter( 'wp_robots', 'wp_robots_no_robots' );
181 wp_robots();
182 }
183 ?>
184 <title><?php echo esc_html( $page_title ); ?></title>
185 <style>
186 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; }
187 .site-icon { margin-bottom: 1rem; }
188 .container { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); max-width: 600px; width:clamp(320px,50vw,600px); margin: auto; }
189 .message { padding: 2rem 2rem 2.5rem; border:1px solid #ddd; border-radius: 4px; }
190 .message h1 {
191 margin-top: 0;
192 margin-top: 0;
193 }
194 h1 { font-size: 2.5rem;}
195 </style>
196 </head>
197 <body class="maintenance">
198 <div class="container">
199 <?php
200 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
201 echo $site_icon;
202 ?>
203 <div class="message">
204 <h1><?php echo esc_html( $page_title ); ?></h1>
205 <div>
206 <?php
207 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
208 echo $page_content;
209 ?>
210 </div>
211 </div>
212 </div>
213 </body>
214 </html>
215 <?php
216 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
217 die( ob_get_clean() );
218 }
219
220 /**
221 * Add the admin notice for the maintenance mode
222 *
223 * @return void
224 */
225 public function add_the_admin_notice_for_maintenance_mode() {
226 // Bail if the setting is disabled.
227 if ( ! get_option( 'cbb_is_maintenance' ) ) {
228 return;
229 }
230
231 // Get the current screen.
232 $screen = get_current_screen();
233
234 if ( ! $screen ) {
235 return;
236 }
237
238 if ( ! ( in_array( $screen->base, [ 'dashboard', 'plugins', 'themes' ], true ) || 'edit.php?post_type=boldblocks_block' === $screen->parent_file ) ) {
239 return;
240 }
241
242 $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-builder' ) );
243
244 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
245 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>';
246 }
247 }
248 endif;
249