| 1 |
<?php |
| 2 |
/** |
| 3 |
* Themify Maintenance Mode |
| 4 |
* |
| 5 |
* @package Themify |
| 6 |
*/ |
| 7 |
class Themify_Maintenance_Mode { |
| 8 |
|
| 9 |
public static function init() { |
| 10 |
if ( is_admin() ) { |
| 11 |
add_action( 'wp_ajax_themify_load_maintenance_pages', [ __CLASS__, 'wp_ajax_themify_load_maintenance_pages' ] ); |
| 12 |
} |
| 13 |
if ( self::is_enabled() ) { |
| 14 |
if ( ! is_admin() ) { |
| 15 |
/* Priority = 11 that is *after* WP default filter `redirect_canonical` in order to avoid redirection loop. */ |
| 16 |
add_action( 'template_redirect', [ __CLASS__, 'template_redirect' ], 11 ); |
| 17 |
} |
| 18 |
if ( current_user_can( 'manage_options' ) ) { |
| 19 |
if ( isset( $_GET['tf_disable_maintenance'], $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'tf_disable_maintenance' ) ) { |
| 20 |
self::disable(); |
| 21 |
} else { |
| 22 |
add_action( 'admin_bar_menu', [ __CLASS__, 'admin_bar_menu' ], 500 ); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public static function template_redirect() { |
| 29 |
$enabled = self::is_enabled(); |
| 30 |
if ( ! $enabled ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$user = wp_get_current_user(); |
| 35 |
$can_view = false; |
| 36 |
foreach ( $user->roles as $role ) { |
| 37 |
if ( $role === 'administrator' || themify_builder_get( 'setting-maintenance_access_' . $role, 'maintenance_access_' . $role ) ) { |
| 38 |
$can_view = true; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! $can_view ) { |
| 44 |
if ( 'on' === $enabled ) { |
| 45 |
$selected_value = themify_builder_get( 'setting-page_builder_maintenance_page', 'tools_maintenance_page' ); |
| 46 |
$selected_page = empty( $selected_value ) ? '' : get_page_by_path($selected_value, OBJECT, 'page'); |
| 47 |
if ( ! empty( $selected_page ) && ! is_page( $selected_value ) ) { |
| 48 |
exit( wp_redirect( get_page_link( $selected_page->ID ) ) ); |
| 49 |
} |
| 50 |
} elseif ( 'message' === $enabled ) { |
| 51 |
$message = themify_builder_get( 'setting-maintenance_message', 'tools_maintenance_message' ); |
| 52 |
wp_die( $message ); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public static function admin_bar_menu( $admin_bar ) { |
| 58 |
$admin_bar->add_menu( [ |
| 59 |
'id' => 'tf_maintenance_mode', |
| 60 |
'title' => '<span class="tf_admin_bar_tooltip">' . __( 'Warning: Maintenance Mode is enabled, website is disabled for public visitors. Be sure to deactivate this once your website is ready.', 'themify' ) . '</span>' . __( 'Maintenance Mode', 'themify' ), |
| 61 |
'meta' => [ |
| 62 |
'class' => 'tf_admin_bar_alert', |
| 63 |
], |
| 64 |
] ); |
| 65 |
$admin_bar->add_menu( [ |
| 66 |
'id' => 'tf_maintenance_mode_disable', |
| 67 |
'title' => __( 'Disable Maintenance Mode', 'themify' ), |
| 68 |
'href' => add_query_arg( [ |
| 69 |
'tf_disable_maintenance' => 1, |
| 70 |
'_wpnonce' => wp_create_nonce( 'tf_disable_maintenance' ), |
| 71 |
] ), |
| 72 |
'parent' => 'tf_maintenance_mode', |
| 73 |
] ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Load pages for maintenance page dropdown |
| 78 |
*/ |
| 79 |
public static function wp_ajax_themify_load_maintenance_pages() { |
| 80 |
check_ajax_referer( 'tf_nonce', 'nonce' ); |
| 81 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 82 |
die; |
| 83 |
} |
| 84 |
|
| 85 |
$pages = get_pages(); |
| 86 |
$selected = themify_builder_get( 'setting-page_builder_maintenance_page', 'tools_maintenance_page' ); |
| 87 |
$output = '<option></option>'; |
| 88 |
foreach ( $pages as $page ) { |
| 89 |
$val = $page->post_name; |
| 90 |
$post_parent = $page->post_parent; |
| 91 |
while ( $post_parent !== 0 ) { |
| 92 |
$post_aux = get_post( $post_parent ); |
| 93 |
$val = $post_aux->post_name.'/'.$val; |
| 94 |
$post_parent = $post_aux->post_parent; |
| 95 |
} |
| 96 |
$output .= sprintf( '<option value="%s"%s>%s</option>', |
| 97 |
esc_attr( $val ), |
| 98 |
selected( $val, $selected, false ), |
| 99 |
esc_html( $page->post_title ) |
| 100 |
); |
| 101 |
} |
| 102 |
echo $output; |
| 103 |
wp_die(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns true if maintenance mode is enabled |
| 108 |
* |
| 109 |
* @return string|false |
| 110 |
*/ |
| 111 |
public static function is_enabled() { |
| 112 |
static $is = null; |
| 113 |
if ( $is === null ) { |
| 114 |
$is = themify_builder_get( 'setting-page_builder_maintenance_mode', 'tools_maintenance_mode' ); |
| 115 |
if ( ! in_array( $is, [ 'on', 'message' ], true ) ) { |
| 116 |
$is = false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return $is; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Disable the Maintenance mode |
| 125 |
*/ |
| 126 |
public static function disable() { |
| 127 |
if ( themify_is_themify_theme() ) { |
| 128 |
$data = themify_get_data(); |
| 129 |
unset( $data['setting-page_builder_maintenance_mode'] ); |
| 130 |
themify_set_data( $data ); |
| 131 |
} |
| 132 |
else { |
| 133 |
$data = get_option( 'themify_builder_setting', [] ); |
| 134 |
if(empty($data)){ |
| 135 |
$data=[]; |
| 136 |
} |
| 137 |
unset( $data['tools_maintenance_mode'] ); |
| 138 |
update_option( 'themify_builder_setting', $data ); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
add_action( 'init', [ 'Themify_Maintenance_Mode', 'init' ] ); |