• Resolved seds94

    (@seds94)


    Hello, is it possible to automatically deactivate Maintenance mode at the end of the timer?

    Maybe with a JS listener on the timer value calling a wp action to disable maintenance mode and reload page? Is there an action to enable/disable maintenance mode, or do I need to change its value from the database?

    Any easier solution?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @seds94,

    Thanks for choosing WP Maintenance Mode!

    Currently, it’s not possible to automatically deactivate the maintenance mode when the countdown is finished. However, our developers are currently working to integrate such a feature in the plugin and hopefully it will be included in the next update of the plugin.

    Have a nice day!

    Thread Starter seds94

    (@seds94)

    Hi, thanx for your feedback.

    Done by copying WPMM $default_options and running ajax :

    if ( is_plugin_active( 'wp-maintenance-mode/wp-maintenance-mode.php') ) {
    
        /**
         * Status auto-update at the end of downcount
         */
        // Check status
        function isWpmmActive( $countdown = null ) {
            $setting = get_option( 'wpmm_settings' );
            // Active mode
            if ( 1 === $setting['general']['status'] ) {
                if ( $countdown ) {
                    // Countdown active
                    if ( 1 === $setting['modules'] ['countdown_status'] ) return true;
                    return false;
                }
                return true;
            }
            return false;
        }
        
        if ( isWpmmActive() )
        {
            // No enqueue from WPMM, use wpmm_styles to load default CSS
            add_filter( 'wpmm_styles', 'wpmm_moem_styles' );
            function wpmm_moem_styles( $styles ) {
                $styles['wpmm-moem'] = CHILD_TMPL. '/assets/css/wpmm.css';
                return $styles;
            }
        }
        
        if ( isWpmmActive( 'countdown' ) )
        {
            // Reset WP maintenance mode on ajax call
            add_action( 'wp_ajax_meom_disable_maintenance_mode', 'meom_disable_maintenance_mode' );
            add_action( 'wp_ajax_nopriv_meom_disable_maintenance_mode', 'meom_disable_maintenance_mode' );
            function meom_disable_maintenance_mode() {
                
                if ( ! isset($_POST['count']) ) return wp_send_json_error('ajax');
                
                if ( intval($_POST['count']) > 0 ) return wp_send_json_error('count');
                    
                $default_options = array( /* pasted from WPMM */ );
    
                if ( update_option( 'wpmm_settings', $default_options ) )
                    return wp_send_json_success();
                
                return wp_send_json_error('update');
            }
    
            /**
             * No enqueue from WPMM, use wpmm_footer and write JS from PHP to call admin-ajax.php
             */
            add_filter( 'wpmm_footer', 'wpmm_moem_footer', 10, 1 );
            function wpmm_moem_footer() { ?>
    <script>(function ($) {
    
        function updateWpmmOptions( count ) {
            $.ajax({
                url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
                type: "POST",
                cache: false,
                dataType: 'json',
                data: { action: 'meom_disable_maintenance_mode', count: count }
            }).done(function(response){
                if ( response.success ) location.reload()
            })
        }
    
        // Check the end of countdown
        function checkTimer() {
            let count = 0
            $('.countdown span').each(function() {
                if ( ! $(this).hasClass('separator') ) {
                    count += parseInt( $(this).text() )
                }
            })
            return count
        }
    
        $(document).ready(function () {
    
            // Call ajax when countdown == 0
            let checkCountdown = setInterval(function () {
    
                let timer = checkTimer()
    
                // On timer 0:00:00:00
                if ( 0 === timer ) {
                    
                    // Remove bounce animation
                    if ( $('.wrap').hasClass('bounce') ) $('.wrap').removeClass('bounce')
                    
                    // Slowly show loader after delay
                    $('body').prepend('<div id="loader" style="opacity:0"></div>')
                    
                    if ( $('#loader').length ) {
                        $('#loader').animate({ opacity: 1 }, 2000)
                        
                        // Call ajax
                        updateWpmmOptions( timer )
                        
                        clearTimeout(checkCountdown)
                    }
                }
            }, 1000)
    
        })
    })(jQuery)
    </script>
            <?php }
            
        }    
    }

    Hi @seds94!

    I’m glad to hear you managed to find a custom solution to integrate this option and thank you for sharing it with the community!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Action at the end of Countdown timer’ is closed to new replies.