Exemple #1
0
/**
 * jQuery Ping Hooks
 * 
 * Necessary jQuery to do ajax requests to this same file in order 
 * to commit session updates to PilotPress.
 *
 * @since 1.7.1
 *
 */
function sessionslap_face()
{
    $defaults = sessionslap_get_default_options();
    $options = get_option('plugin_sessionslap_options');
    // Update with defaults if database entries don't exist yet
    if (is_array($options)) {
        foreach ($options as $optionName => $option) {
            if (empty($option)) {
                $options[$optionName] = $defaults[$optionName];
            }
        }
    } else {
        $options = $defaults;
    }
    ?>
	<script type="text/javascript">
	jQuery(function($){
		window.sessionslap = {
			// Show alerts in top right corner?
			"alerts": <?php 
    echo $options['alerts'] == 'on' ? 1 : 0;
    ?>
,
			// Duration alerts should hang for, in ms
			"alert_hang_time": <?php 
    echo (int) $options['hang_duration'] * 1000;
    ?>
, // 5 seconds
			// Interval of time between each keep-alive ping
			"interval_time": <?php 
    echo (int) $options['interval_duration'] * (60 * 1000);
    ?>
, //1800000 == 30 min,
			"init": function(){
				window.sessionslap.pinger_interval_id = setInterval( this.pinger, this.interval_time);
			},
			"pinger": function(){
				$(document).trigger("sessionslap.ping.start");
				jQuery.ajax({
					url: "?",
					type: "GET",
					data: {
						update: true,
						r: Math.random()
					},
					success: function(data){
						if (window.sessionslap.alerts){
							window.sessionslap.alert("Your session has been updated!", true );
						}
						$(document).trigger("sessionslap.ping.end.success");
						console.log("Your session has been updated!");
					},
					error: function(data){
						if (window.sessionslap.alerts){
							window.sessionslap.alert("There was an issue with your session getting updated!");
						}
						$(document).trigger("sessionslap.ping.end.error");
						console.log("There was an issue with your session getting updated!");
					}
				});
			},
			"alert": function(msg, good){
				$alert = $("<div>").text( msg ).addClass("sessionslap-alert " + ( good ? "sessionslap-success" : "sessionslap-error" ) ).bind("click", function(e){
					$(this).stop();
				});
				$("body").append( $alert ).find(".sessionslap-alert").delay( this.alert_hang_time ).fadeOut(1500, function(e){
					$(this).remove();
				});
			}
		}
		
		<?php 
    if ($options['enabled'] == 'on') {
        ?>
		window.sessionslap.init();
		<?php 
    }
    ?>
	});
	</script>
	<?php 
}
/**
 * Input Sanitizer
 * 
 * Utilized by WordPress filters during option updates 
 * to sterilize input provided by admin. This will also 
 * report inconsistancies and invalid options to the 
 * $SESSIONSLAP_ERRORS variable
 *
 * @since 1.7.1
 *
 * @uses sessionslap_get_default_options()
 *
 * @param Array $values Dictionary of options to be sanitized. 
 * @return Array Dictionary of sanitized options.
 */
function sessionslap_validate($values)
{
    global $SESSIONSLAP_ERRORS;
    $defaults = sessionslap_get_default_options();
    $SESSIONSLAP_ERRORS = array();
    if (is_array($values)) {
        foreach ($values as $valueName => $value) {
            $error = 0;
            $value = trim($value);
            switch ($valueName) {
                // On Off value checks
                case 'enabled':
                case 'alerts':
                    if (strtolower($value) != 'on' && strtolower($value) != 'off') {
                        $error++;
                    }
                    unset($defaults[$valueName]);
                    break;
                    // Numeric value checks
                // Numeric value checks
                case 'interval_duration':
                case 'hang_duration':
                    if (!is_numeric($value)) {
                        $error++;
                    }
                    unset($defaults[$valueName]);
                    break;
            }
            if ($error) {
                $values[$valueName] = $defaults[$valueName];
                $SESSIONSLAP_ERRORS[$valueName] = array('value' => $value, 'fn' => $valueName);
            }
        }
    }
    if (count($defaults)) {
        // Add missing defaults which should trigger extras in array_diff checks
        $values = array_merge($values, $defaults);
    }
    return $values;
}