Last active
October 6, 2024 02:26
-
-
Save hssktm/55f4a88f5f3f525f179e3ab5a75c0727 to your computer and use it in GitHub Desktop.
Maintenance Mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function custom_maintenance_mode_setting() { | |
add_settings_section( | |
'maintenance_mode_section', | |
'Maintenance Mode', | |
null, | |
'general' | |
); | |
add_settings_field( | |
'maintenance_mode', | |
'Activate Maintenance Mode', | |
'custom_maintenance_mode_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
add_settings_field( | |
'maintenance_mode_template', | |
'Select Maintenance Template', | |
'custom_maintenance_mode_template_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
add_settings_field( | |
'load_google_fonts', | |
'Load Google Fonts', | |
'custom_load_google_fonts_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
add_settings_field( | |
'load_ma_custom_fonts', | |
'Load MA Custom Fonts', | |
'custom_load_ma_custom_fonts_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
add_settings_field( | |
'css_cache', | |
'CSS Cache', | |
'custom_css_cache_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
add_settings_field( | |
'return_status_503', | |
'Return Status 503', | |
'custom_return_status_503_field_html', | |
'general', | |
'maintenance_mode_section' | |
); | |
register_setting('general', 'maintenance_mode', array( | |
'type' => 'boolean', | |
'sanitize_callback' => 'sanitize_text_field', | |
'default' => false | |
)); | |
register_setting('general', 'maintenance_mode_template', array( | |
'type' => 'integer', | |
'sanitize_callback' => 'absint', | |
'default' => 0 | |
)); | |
register_setting('general', 'load_google_fonts', array( | |
'type' => 'boolean', | |
'sanitize_callback' => 'sanitize_text_field', | |
'default' => true | |
)); | |
register_setting('general', 'load_ma_custom_fonts', array( | |
'type' => 'boolean', | |
'sanitize_callback' => 'sanitize_text_field', | |
'default' => true | |
)); | |
register_setting('general', 'css_cache', array( | |
'type' => 'boolean', | |
'sanitize_callback' => 'sanitize_text_field', | |
'default' => false | |
)); | |
register_setting('general', 'return_status_503', array( | |
'type' => 'boolean', | |
'sanitize_callback' => 'sanitize_text_field', | |
'default' => true | |
)); | |
} | |
add_action('admin_init', 'custom_maintenance_mode_setting'); | |
function custom_maintenance_mode_field_html() { | |
$checked = get_option('maintenance_mode') ? 'checked' : ''; | |
echo '<input type="checkbox" name="maintenance_mode" value="1" ' . $checked . '>'; | |
} | |
function custom_maintenance_mode_template_field_html() { | |
$selected = get_option('maintenance_mode_template'); | |
$templates = get_posts(array( | |
'post_type' => 'ct_template', | |
'post_status' => array('publish', 'draft', 'pending', 'private'), | |
'numberposts' => -1 | |
)); | |
echo '<select name="maintenance_mode_template">'; | |
echo '<option value="">Select a template</option>'; | |
foreach ($templates as $template) { | |
$template_id = $template->ID; | |
$title = $template->post_title; | |
$is_selected = $selected == $template_id ? 'selected' : ''; | |
echo "<option value='$template_id' $is_selected>$title</option>"; | |
} | |
echo '</select>'; | |
} | |
function custom_load_google_fonts_field_html() { | |
$checked = get_option('load_google_fonts') ? 'checked' : ''; | |
echo '<input type="checkbox" name="load_google_fonts" value="1" ' . $checked . '>'; | |
} | |
function custom_load_ma_custom_fonts_field_html() { | |
$checked = get_option('load_ma_custom_fonts') ? 'checked' : ''; | |
echo '<input type="checkbox" name="load_ma_custom_fonts" value="1" ' . $checked . '> (Requires installation <a href="https://www.altmann.de/en/blog-en/code-snippet-custom-fonts/" target="_blank" style="text-decoration: underline; color: inherit;">MA Custom Fonts</a>)'; | |
} | |
function custom_css_cache_field_html() { | |
$checked = get_option('css_cache') ? 'checked' : ''; | |
echo '<input type="checkbox" name="css_cache" value="1" ' . $checked . '> If you don’t see template changes quickly, activate this.'; | |
} | |
function custom_return_status_503_field_html() { | |
$checked = get_option('return_status_503') ? 'checked' : ''; | |
echo '<input type="checkbox" name="return_status_503" value="1" ' . $checked . '>'; | |
} | |
function load_referenced_svg_icons_from_oxygen($content) { | |
preg_match_all('/<use[^>]+xlink:href="#([^"]+)"/', $content, $matches); | |
$referenced_icons = array_unique($matches[1]); | |
if (empty($referenced_icons)) { | |
return ''; | |
} | |
$svg_sets = oxy_get_svg_sets(); | |
$svg_content = ''; | |
foreach ($referenced_icons as $icon_id) { | |
foreach ($svg_sets as $set) { | |
if (strpos($set, "id=\"{$icon_id}\"") !== false) { | |
$svg_content .= $set; | |
break; | |
} | |
} | |
} | |
return $svg_content; | |
} | |
function custom_maintenance_mode_content() { | |
$maintenance_page_id = get_option('maintenance_mode_template'); | |
if (get_option('maintenance_mode') && !current_user_can('administrator')) { | |
if (get_option('return_status_503')) { | |
status_header(503); | |
header('Retry-After: 3600'); | |
} | |
echo '<meta name="robots" content="noindex, nofollow">'; | |
$response = wp_remote_get(rest_url("api-oxygen/v1/oxygen-content/{$maintenance_page_id}")); | |
if (is_wp_error($response)) { | |
echo '<h1>Error loading content.</h1>'; | |
exit(); | |
} | |
$data = json_decode(wp_remote_retrieve_body($response), true); | |
$use_cache = get_option('css_cache'); | |
$cache_buster = $use_cache ? time() : ''; | |
if (isset($data['css_url'])) { | |
$css_url_with_cache = esc_url($data['css_url']) . ($use_cache ? "?cache={$cache_buster}" : ''); | |
echo '<link rel="stylesheet" href="' . $css_url_with_cache . '">'; | |
} | |
$universal_css_url = esc_url(home_url('/wp-content/uploads/oxygen/css/universal.css')); | |
echo '<link rel="stylesheet" href="' . $universal_css_url . ($use_cache ? "?cache={$cache_buster}" : '') . '">'; | |
$oxygen_css_url = esc_url(home_url('/wp-content/plugins/oxygen/component-framework/oxygen.css')); | |
echo '<link rel="stylesheet" href="' . $oxygen_css_url . ($use_cache ? "?cache={$cache_buster}" : '') . '">'; | |
ob_start(); | |
wp_head(); | |
$head_content = ob_get_clean(); | |
if (get_option('load_google_fonts')) { | |
if (preg_match('/<link[^>]+href=["\']?(https:\/\/fonts\.googleapis\.com\/css[^"\']+)["\']?[^>]*>/i', $head_content, $matches)) { | |
$google_fonts_url_with_cache = esc_url($matches[1]) . ($use_cache ? "?cache={$cache_buster}" : ''); | |
echo '<link rel="stylesheet" href="' . $google_fonts_url_with_cache . '">'; | |
} | |
} | |
if (get_option('load_ma_custom_fonts')) { | |
if (preg_match('/<link[^>]+id=["\']?ma-customfonts["\']?[^>]*>/i', $head_content, $matches)) { | |
$ma_fonts_url_with_cache = str_replace('.css', ".css" . ($use_cache ? "?cache={$cache_buster}" : ''), $matches[0]); | |
echo $ma_fonts_url_with_cache; | |
} | |
} | |
if (isset($data['content'])) { | |
echo $data['content']; | |
$svg_icons = load_referenced_svg_icons_from_oxygen($data['content']); | |
if (!empty($svg_icons)) { | |
echo '<svg xmlns="http://www.w3.org/2000/svg" style="display:none;">' . $svg_icons . '</svg>'; | |
} | |
} | |
exit(); | |
} | |
} | |
add_action('template_redirect', 'custom_maintenance_mode_content'); | |
add_action('rest_api_init', function () { | |
register_rest_route('api-oxygen/v1', '/oxygen-content/(?P<id>\d+)', array( | |
'methods' => 'GET', | |
'callback' => 'call_oxygen_content_api', | |
)); | |
}); | |
function call_oxygen_content_api($data) { | |
$post_id = $data['id']; | |
$maintenance_template_id = get_option('maintenance_mode_template'); | |
if ($post_id != $maintenance_template_id) { | |
return new WP_Error('forbidden', 'You do not have access to this page.', array('status' => 403)); | |
} | |
$post = get_post($post_id); | |
if (!$post) { | |
return new WP_Error('no_post', 'Post not found', array('status' => 404)); | |
} | |
$shortcodes = get_post_meta($post_id, '_ct_builder_shortcodes', true); | |
if (!$shortcodes) { | |
return new WP_Error('no_content', 'No Oxygen content found', array('status' => 404)); | |
} | |
$rendered_content = do_shortcode($shortcodes); | |
$css_url = home_url('/wp-content/uploads/oxygen/css/' . $post_id . '.css'); | |
return array( | |
'content' => $rendered_content, | |
'css_url' => $css_url, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment