<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

/**
 * smd_at_work: a Textpattern CMS plugin for informing visitors of site maintenance.
 *
 * @author Stef Dawson
 * @see http://stefdawson.com/
 */
if (txpinterface === 'admin') {
    new 
smd_at_work();
} elseif (
txpinterface === 'public') {
    
register_callback('smd_at_work_init''pretext');

    if (
class_exists('\Textpattern\Tag\Registry')) {
        
Txp::get('\Textpattern\Tag\Registry')
            ->
register('smd_at_work_status')
            ->
register('smd_if_at_work');
    }

    
/**
     * Public-side initialisation.
     *
     * Only sets up the callback if:
     *  1. The plugin's toggle pref is on.
     *  2. The visitor is not logged into the admin side.
     *  3. the URL param txpcleantest is missing.
     *
     * @see smd_at_work()
     */
    
function smd_at_work_init()
    {
        if (
get_pref('smd_at_work_enabled') == '1') {
            if (
txpinterface === 'public' && !gps('txpcleantest') && !is_logged_in()) {
                
$_GET $_POST $_REQUEST = array();
                
register_callback('smd_at_work''pretext_end');
            }
        }
    }

    
/**
     * Throw an HTTP 503 error.
     */
    
function smd_at_work()
    {
        
txp_die(get_pref('smd_at_work_message''Site maintenance in progress. Please check back later.'), 503);
    }

    
/**
     * Public tag to set the maintenance status.
     *
     * Only logged-in users may set this. It is up to application
     * logic if this is restricted any further.
     */
    
function smd_at_work_status($atts = array(), $thing null)
    {
        
extract(lAtts(array(
            
'status' => null,
        ), 
$atts));

        
// Null status toggles the state.
        
if ($status === null) {
            
$status = !get_pref('smd_at_work_enabled'nulltrue);
        }

        if (
is_logged_in()) {
            
set_pref('smd_at_work_enabled', (($status) ? 0));
        }
    }

    
/**
     * Public conditional tag to determine if maintenance mode is active.
     *
     * Not so much use on the actual public site, but handy for admin-side
     * dashboards.
     */
    
function smd_if_at_work($atts = array(), $thing null)
    {
        return 
parse($thingget_pref('smd_at_work_enabled'nulltrue) == '1');
    }
}

/**
 * Admin-side class.
 */
class smd_at_work
{
    
/**
     * The plugin's event.
     *
     * @var string
     */
    
protected $smd_at_work_event __CLASS__;

    
/**
     * Constructor.
     */
    
public function __construct()
    {
        
add_privs('prefs.'.$this->smd_at_work_event'1');
        
add_privs('plugin_prefs.'.$this->smd_at_work_event'1');
        
register_callback(array($this'welcome'), 'plugin_lifecycle.'.$this->smd_at_work_event);
        
register_callback(array($this'banner'), 'admin_side''footer');
        
register_callback(array($this'install'), 'prefs'null1);
        
register_callback(array($this'options'), 'plugin_prefs.'.$this->smd_at_work_eventnull1);
    }

    
/**
     * Handler for plugin lifecycle events.
     *
     * @param string $evt Textpattern action event
     * @param string $stp Textpattern action step
     */
    
public function welcome($evt$stp)
    {
        switch (
$stp) {
            case 
'installed':
            case 
'enabled':
                
$this->install();
                break;
            case 
'deleted':
                
remove_pref(null'smd_at_work');
                
safe_delete('txp_lang'"owner = 'smd\_at\_work'");
                break;
        }
    }

    
/**
     * Display a banner to inform admins that the site is in maintenance mode.
     *
     * @param string $evt  Textpattern action event
     * @param string $stp  Textpattern action step
     * @param string $data The current footer content
     */
    
public function banner($evt$stp$data)
    {
        global 
$event$step;

        
// Force DB lookup of pref to avoid stale message on prefs screen.
        
$force = ($event === 'prefs' && $step === 'prefs_save') ? true false;
        
$link $this->prefs_link();

        if (
get_pref('smd_at_work_enabled'null$force) == '1') {
            
// Prepend the maintenance message to the footer content.
            
$data '<p class="warning" style="display:inline-block;">'.
                
gTxt('smd_at_work_admin_message', array('{url}' => $link)).
                
'</p>'.
                
n.$data;
        }

        return 
$data;
    }

    
/**
     * Fetch the admin-side prefs panel link.
     */
    
protected function prefs_link()
    {
        return 
'?event=prefs#prefs_group_smd_at_work';
    }

    
/**
     * Jump to the prefs panel.
     */
    
public function options()
    {
        
$link $this->prefs_link();

        
header('Location: ' $link);
    }

    
/**
     * Install the prefs if necessary.
     *
     * When operating under a plugin cache environment, the install lifecycle
     * event is never fired, so this is a fallback.
     *
     * The lifecycle callback remains for deletion purposes under a regular installation,
     * since the plugin cannot detect this in a cache environment.
     *
     * @see welcome()
     */
    
function install()
    {
        if (
get_pref('smd_at_work_enabled'null) === null) {
            
set_pref('smd_at_work_enabled'0'smd_at_work'PREF_PLUGIN'yesnoradio'10);
        }

        if (
get_pref('smd_at_work_message'null) === null) {
            
set_pref('smd_at_work_message''Site maintenance in progress. Please check back later.''smd_at_work'PREF_PLUGIN'pref_longtext_input'20);
        } else {
            
update_pref('smd_at_work_message'nullnullnull'pref_longtext_input');
        }
    }
}