PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/elementor/includes/maintenance-mode.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 383 lines | 197 code | 47 blank | 139 comment | 16 complexity | 3dea42d50cff701126478b13b8a9c3fd MD5 | raw file
  1. <?php
  2. namespace Elementor;
  3. use Elementor\TemplateLibrary\Source_Local;
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit; // Exit if accessed directly.
  6. }
  7. /**
  8. * Elementor maintenance mode.
  9. *
  10. * Elementor maintenance mode handler class is responsible for the Elementor
  11. * "Maintenance Mode" and the "Coming Soon" features.
  12. *
  13. * @since 1.4.0
  14. */
  15. class Maintenance_Mode {
  16. /**
  17. * The options prefix.
  18. */
  19. const OPTION_PREFIX = 'elementor_maintenance_mode_';
  20. /**
  21. * The maintenance mode.
  22. */
  23. const MODE_MAINTENANCE = 'maintenance';
  24. /**
  25. * The coming soon mode.
  26. */
  27. const MODE_COMING_SOON = 'coming_soon';
  28. /**
  29. * Get elementor option.
  30. *
  31. * Retrieve elementor option from the database.
  32. *
  33. * @since 1.4.0
  34. * @access public
  35. * @static
  36. *
  37. * @param string $option Option name. Expected to not be SQL-escaped.
  38. * @param mixed $default Optional. Default value to return if the option
  39. * does not exist. Default is false.
  40. *
  41. * @return bool False if value was not updated and true if value was updated.
  42. */
  43. public static function get( $option, $default = false ) {
  44. return get_option( self::OPTION_PREFIX . $option, $default );
  45. }
  46. /**
  47. * Set elementor option.
  48. *
  49. * Update elementor option in the database.
  50. *
  51. * @since 1.4.0
  52. * @access public
  53. * @static
  54. *
  55. * @param string $option Option name. Expected to not be SQL-escaped.
  56. * @param mixed $value Option value. Must be serializable if non-scalar.
  57. * Expected to not be SQL-escaped.
  58. *
  59. * @return bool False if value was not updated and true if value was updated.
  60. */
  61. public static function set( $option, $value ) {
  62. return update_option( self::OPTION_PREFIX . $option, $value );
  63. }
  64. /**
  65. * Body class.
  66. *
  67. * Add "Maintenance Mode" CSS classes to the body tag.
  68. *
  69. * Fired by `body_class` filter.
  70. *
  71. * @since 1.4.0
  72. * @access public
  73. *
  74. * @param array $classes An array of body classes.
  75. *
  76. * @return array An array of body classes.
  77. */
  78. public function body_class( $classes ) {
  79. $classes[] = 'elementor-maintenance-mode';
  80. return $classes;
  81. }
  82. /**
  83. * Template redirect.
  84. *
  85. * Redirect to the "Maintenance Mode" template.
  86. *
  87. * Fired by `template_redirect` action.
  88. *
  89. * @since 1.4.0
  90. * @access public
  91. */
  92. public function template_redirect() {
  93. if ( Plugin::$instance->preview->is_preview_mode() ) {
  94. return;
  95. }
  96. $user = wp_get_current_user();
  97. $exclude_mode = self::get( 'exclude_mode', [] );
  98. $is_login_page = false;
  99. /**
  100. * Is login page
  101. *
  102. * Filters whether the maintenance mode displaying the login page or a regular page.
  103. *
  104. * @since 1.0.4
  105. *
  106. * @param bool $is_login_page Whether its a login page.
  107. */
  108. $is_login_page = apply_filters( 'elementor/maintenance_mode/is_login_page', $is_login_page );
  109. if ( $is_login_page ) {
  110. return;
  111. }
  112. if ( 'logged_in' === $exclude_mode && is_user_logged_in() ) {
  113. return;
  114. }
  115. if ( 'custom' === $exclude_mode ) {
  116. $exclude_roles = self::get( 'exclude_roles', [] );
  117. $user_roles = $user->roles;
  118. if ( is_multisite() && is_super_admin() ) {
  119. $user_roles[] = 'super_admin';
  120. }
  121. $compare_roles = array_intersect( $user_roles, $exclude_roles );
  122. if ( ! empty( $compare_roles ) ) {
  123. return;
  124. }
  125. }
  126. add_filter( 'body_class', [ $this, 'body_class' ] );
  127. if ( 'maintenance' === self::get( 'mode' ) ) {
  128. $protocol = wp_get_server_protocol();
  129. header( "$protocol 503 Service Unavailable", true, 503 );
  130. header( 'Content-Type: text/html; charset=utf-8' );
  131. header( 'Retry-After: 600' );
  132. }
  133. // Setup global post for Elementor\frontend so `_has_elementor_in_page = true`.
  134. $GLOBALS['post'] = get_post( self::get( 'template_id' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  135. // Set the template as `$wp_query->current_object` for `wp_title` and etc.
  136. query_posts( [
  137. 'p' => self::get( 'template_id' ),
  138. 'post_type' => Source_Local::CPT,
  139. ] );
  140. }
  141. /**
  142. * Register settings fields.
  143. *
  144. * Adds new "Maintenance Mode" settings fields to Elementor admin page.
  145. *
  146. * The method need to receive the an instance of the Tools settings page
  147. * to add the new maintenance mode functionality.
  148. *
  149. * Fired by `elementor/admin/after_create_settings/{$page_id}` action.
  150. *
  151. * @since 1.4.0
  152. * @access public
  153. *
  154. * @param Tools $tools An instance of the Tools settings page.
  155. */
  156. public function register_settings_fields( Tools $tools ) {
  157. $templates = Plugin::$instance->templates_manager->get_source( 'local' )->get_items( [
  158. 'type' => 'page',
  159. ] );
  160. $templates_options = [];
  161. foreach ( $templates as $template ) {
  162. $templates_options[ $template['template_id'] ] = esc_html( $template['title'] );
  163. }
  164. ob_start();
  165. $this->print_template_description();
  166. $template_description = ob_get_clean();
  167. $tools->add_tab(
  168. 'maintenance_mode', [
  169. 'label' => esc_html__( 'Maintenance Mode', 'elementor' ),
  170. 'sections' => [
  171. 'maintenance_mode' => [
  172. 'callback' => function() {
  173. echo '<h2>' . esc_html__( 'Maintenance Mode', 'elementor' ) . '</h2>';
  174. echo '<div>' . esc_html__( 'Set your entire website as MAINTENANCE MODE, meaning the site is offline temporarily for maintenance, or set it as COMING SOON mode, meaning the site is offline until it is ready to be launched.', 'elementor' ) . '</div>';
  175. },
  176. 'fields' => [
  177. 'maintenance_mode_mode' => [
  178. 'label' => esc_html__( 'Choose Mode', 'elementor' ),
  179. 'field_args' => [
  180. 'type' => 'select',
  181. 'std' => '',
  182. 'options' => [
  183. '' => esc_html__( 'Disabled', 'elementor' ),
  184. self::MODE_COMING_SOON => esc_html__( 'Coming Soon', 'elementor' ),
  185. self::MODE_MAINTENANCE => esc_html__( 'Maintenance', 'elementor' ),
  186. ],
  187. 'desc' => '<div class="elementor-maintenance-mode-description" data-value="" style="display: none">' .
  188. esc_html__( 'Choose between Coming Soon mode (returning HTTP 200 code) or Maintenance Mode (returning HTTP 503 code).', 'elementor' ) .
  189. '</div>' .
  190. '<div class="elementor-maintenance-mode-description" data-value="maintenance" style="display: none">' .
  191. esc_html__( 'Maintenance Mode returns HTTP 503 code, so search engines know to come back a short time later. It is not recommended to use this mode for more than a couple of days.', 'elementor' ) .
  192. '</div>' .
  193. '<div class="elementor-maintenance-mode-description" data-value="coming_soon" style="display: none">' .
  194. esc_html__( 'Coming Soon returns HTTP 200 code, meaning the site is ready to be indexed.', 'elementor' ) .
  195. '</div>',
  196. ],
  197. ],
  198. 'maintenance_mode_exclude_mode' => [
  199. 'label' => esc_html__( 'Who Can Access', 'elementor' ),
  200. 'field_args' => [
  201. 'class' => 'elementor-default-hide',
  202. 'type' => 'select',
  203. 'std' => 'logged_in',
  204. 'options' => [
  205. 'logged_in' => esc_html__( 'Logged In', 'elementor' ),
  206. 'custom' => esc_html__( 'Custom', 'elementor' ),
  207. ],
  208. ],
  209. ],
  210. 'maintenance_mode_exclude_roles' => [
  211. 'label' => esc_html__( 'Roles', 'elementor' ),
  212. 'field_args' => [
  213. 'class' => 'elementor-default-hide',
  214. 'type' => 'checkbox_list_roles',
  215. ],
  216. 'setting_args' => [ __NAMESPACE__ . '\Settings_Validations', 'checkbox_list' ],
  217. ],
  218. 'maintenance_mode_template_id' => [
  219. 'label' => esc_html__( 'Choose Template', 'elementor' ),
  220. 'field_args' => [
  221. 'class' => 'elementor-default-hide',
  222. 'type' => 'select',
  223. 'std' => '',
  224. 'show_select' => true,
  225. 'options' => $templates_options,
  226. 'desc' => $template_description,
  227. ],
  228. ],
  229. ],
  230. ],
  231. ],
  232. ]
  233. );
  234. }
  235. /**
  236. * Add menu in admin bar.
  237. *
  238. * Adds "Maintenance Mode" items to the WordPress admin bar.
  239. *
  240. * Fired by `admin_bar_menu` filter.
  241. *
  242. * @since 1.4.0
  243. * @access public
  244. *
  245. * @param \WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance, passed by reference.
  246. */
  247. public function add_menu_in_admin_bar( \WP_Admin_Bar $wp_admin_bar ) {
  248. $wp_admin_bar->add_node( [
  249. 'id' => 'elementor-maintenance-on',
  250. 'title' => esc_html__( 'Maintenance Mode ON', 'elementor' ),
  251. 'href' => Tools::get_url() . '#tab-maintenance_mode',
  252. ] );
  253. $document = Plugin::$instance->documents->get( self::get( 'template_id' ) );
  254. $wp_admin_bar->add_node( [
  255. 'id' => 'elementor-maintenance-edit',
  256. 'parent' => 'elementor-maintenance-on',
  257. 'title' => esc_html__( 'Edit Template', 'elementor' ),
  258. 'href' => $document ? $document->get_edit_url() : '',
  259. ] );
  260. }
  261. /**
  262. * Print style.
  263. *
  264. * Adds custom CSS to the HEAD html tag. The CSS that emphasise the maintenance
  265. * mode with red colors.
  266. *
  267. * Fired by `admin_head` and `wp_head` filters.
  268. *
  269. * @since 1.4.0
  270. * @access public
  271. */
  272. public function print_style() {
  273. ?>
  274. <style>#wp-admin-bar-elementor-maintenance-on > a { background-color: #dc3232; }
  275. #wp-admin-bar-elementor-maintenance-on > .ab-item:before { content: "\f160"; top: 2px; }</style>
  276. <?php
  277. }
  278. public function on_update_mode( $old_value, $value ) {
  279. if ( $old_value !== $value ) {
  280. do_action( 'elementor/maintenance_mode/mode_changed', $old_value, $value );
  281. }
  282. }
  283. /**
  284. * Maintenance mode constructor.
  285. *
  286. * Initializing Elementor maintenance mode.
  287. *
  288. * @since 1.4.0
  289. * @access public
  290. */
  291. public function __construct() {
  292. add_action( 'update_option_elementor_maintenance_mode_mode', [ $this, 'on_update_mode' ], 10, 2 );
  293. $is_enabled = (bool) self::get( 'mode' ) && (bool) self::get( 'template_id' );
  294. if ( is_admin() ) {
  295. $page_id = Tools::PAGE_ID;
  296. add_action( "elementor/admin/after_create_settings/{$page_id}", [ $this, 'register_settings_fields' ] );
  297. }
  298. if ( ! $is_enabled ) {
  299. return;
  300. }
  301. add_action( 'admin_bar_menu', [ $this, 'add_menu_in_admin_bar' ], 300 );
  302. add_action( 'admin_head', [ $this, 'print_style' ] );
  303. add_action( 'wp_head', [ $this, 'print_style' ] );
  304. // Priority = 11 that is *after* WP default filter `redirect_canonical` in order to avoid redirection loop.
  305. add_action( 'template_redirect', [ $this, 'template_redirect' ], 11 );
  306. }
  307. /**
  308. * Print Template Description
  309. *
  310. * Prints the template description
  311. *
  312. * @since 2.2.0
  313. * @access private
  314. */
  315. private function print_template_description() {
  316. $template_id = self::get( 'template_id' );
  317. $edit_url = '';
  318. if ( $template_id && get_post( $template_id ) ) {
  319. $edit_url = Plugin::$instance->documents->get( $template_id )->get_edit_url();
  320. }
  321. ?>
  322. <a target="_blank" class="elementor-edit-template" style="display: none" href="<?php echo esc_url( $edit_url ); ?>"><?php echo esc_html__( 'Edit Template', 'elementor' ); ?></a>
  323. <div class="elementor-maintenance-mode-error"><?php echo esc_html__( 'To enable maintenance mode you have to set a template for the maintenance mode page.', 'elementor' ); ?></div>
  324. <div class="elementor-maintenance-mode-error">
  325. <?php
  326. printf(
  327. /* translators: %1$s Link open tag, %2$s: Link close tag. */
  328. esc_html__( 'Select one or go ahead and %1$screate one%2$s now.', 'elementor' ),
  329. '<a target="_blank" href="' . esc_url( admin_url( 'post-new.php?post_type=' . Source_Local::CPT ) ) . '">',
  330. '</a>'
  331. );
  332. ?>
  333. </div>
  334. <?php
  335. }
  336. }