Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created December 20, 2012 14:15
Show Gist options
  • Save lgedeon/4345519 to your computer and use it in GitHub Desktop.
Save lgedeon/4345519 to your computer and use it in GitHub Desktop.
Framework for adding an Admin Page
<?php
class Plugin_Config_Page {
function plugin_config() { // switch to construct
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'wp_ajax_plugin_refresh', array( $this, 'ajax_plugin_refresh' ) );
add_action( 'wp_ajax_plugin_toggle_schedule', array( $this, 'ajax_plugin_toggle_schedule' ) );
}
function admin_init() {
register_setting( 'plugin_option_group' , 'plugin_config', array( $this, 'sanitize_options' ) );
add_settings_section( 'default', '', '__return_false', 'plugin_config' );
add_settings_field('api', 'Base API', array( $this, 'textfield' ), 'plugin_config', 'default', array( 'name' => 'api', 'default' => '', 'size' => 'large' ) );
add_settings_field('refcode', 'Customer Refcode', array( $this, 'textfield' ), 'plugin_config', 'default', array( 'name' => 'refcode', 'default' => '9to5mac' ) );
add_settings_field('limit', 'Number of Ads (max 250)', array( $this, 'textfield' ), 'plugin_config', 'default', array( 'name' => 'limit', 'default' => '10' ) );
add_settings_field('sort', 'Sort By', array( $this, 'radiofield' ), 'plugin_config', 'default', array( 'name' => 'sort', 'default' => 'time', 'value_options' => array ( 'time', 'hotness' ) ) );
add_settings_field('cats', 'Categories (comma separated)', array( $this, 'textfield' ), 'plugin_config', 'default', array( 'name' => 'cats', 'default' => '' ) );
add_settings_field('stores', 'Stores (comma separated)', array( $this, 'textfield' ), 'plugin_config', 'default', array( 'name' => 'stores', 'default' => '' ) );
}
function admin_menu() {
add_options_page('plugin Importer', 'plugin Importer', 'manage_options', 'plugin_config', array( $this, 'options_page' ) );
}
function textfield( $args ) {
$args = wp_parse_args( $args, array( 'name' => '', 'default' => '', 'size' => 'regular' ) );
$options = get_option('plugin_config');
$fieldval = empty( $options[$args['name']] ) ? esc_attr( $args['default'] ) : esc_attr( $options[$args['name']] );
?>
<input type="text" name="plugin_config[<?php echo esc_attr( $args['name'] ); ?>]" value="<?php echo $fieldval; ?>" class="<?php echo esc_attr( $args['size'] ); ?>-text" />
<?php
}
function radiofield( $args ) {
$args = wp_parse_args( $args, array( 'name' => '', 'default' => '', 'value_options' => array ( '1', '2' ) ) );
$options = get_option('plugin_config');
$fieldval = empty( $options[$args['name']] ) ? esc_attr( $args['default'] ) : esc_attr( $options[$args['name']] );
$value_options = $args['value_options'];
foreach ( $value_options as $value_option ) {
?>
<input type="radio" name="plugin_config[<?php echo esc_attr( $args['name'] ); ?>]" value="<?php echo $value_option; ?>" <?php echo ( $value_option == $fieldval ) ? " checked": ""; ?> />
<?php
echo $value_option;
}
}
function sanitize_id_list( $ids ) {
$id_list = str_replace( ' ', '', $ids );
$id_list = explode(',', $id_list);
foreach( $id_list as $key => $id ) {
if ( ! preg_match( '#^[0-9]{1,5}$#', $id ) )
unset( $id_list[$key] );
}
$id_list = implode( ',', $id_list );
return $id_list;
}
function sanitize_options( $input ) {
$new_input['api'] = empty( $input['api'] ) ? 'http://plugin.com/synd/2.1/api.php' : esc_url( $input['api'], array( 'http', 'https' ) );
$new_input['refcode'] = empty( $input['refcode'] ) ? '9to5mac' : sanitize_key( $input['refcode'] );
$new_input['limit'] = ( empty( $input['limit'] ) || intval( $input['limit'] ) > 250 ) ? '10' : intval( $input['limit'] );
$new_input['sort'] = ( empty( $input['sort'] ) || $input['sort'] != 'hotness' ) ? 'time' : 'hotness';
$new_input['cats'] = empty( $input['cats'] ) ? '' : $this->sanitize_id_list( $input['cats'] );
$new_input['stores'] = empty( $input['stores'] ) ? '' : $this->sanitize_id_list( $input['stores'] );
return $new_input;
}
function options_page() {
$schedule = wp_next_scheduled('plugin_hook');
$is_active = (bool) get_option('plugin_is_active');
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2><?php global $title; echo $title; ?></h2>
<div class="updated" id="next-scheduled" <?php if ( ! $is_active ) echo 'style="display:none;"'; ?>><p>Next update schedule: <span><?php echo date('l jS \of F Y h:i:s A', $schedule); ?></span></p></div>
<p>
<input type="button" name="schedule_setter" id="schedule_setter" value="<?php if ( $is_active ) echo 'Disable Hourly Updates'; else echo 'Enable Hourly Updates' ?>" class="button" style="font-weight:bold;" onclick="pluginSchedule();" />
<input type="button" name="force_refresh" id="force_refresh" value="Refresh Now" class="button" onclick="pluginRefresh();" />
</p>
<form action="options.php" method="post">
<?php settings_fields( 'plugin_option_group'); ?>
<?php do_settings_sections('plugin_config'); ?>
<?php submit_button(); ?>
</form>
</div>
<script type="text/javascript">
function pluginRefresh () {
jQuery('#force_refresh').after(' <img src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" alt="loading" />');
jQuery.post( ajaxurl, { action: 'plugin_refresh' }, function(response){
jQuery('#force_refresh').siblings('img').remove();
});
}
function pluginSchedule() {
jQuery('#schedule_setter').attr('disabled',true);
jQuery.post( ajaxurl, { action: 'plugin_toggle_schedule' }, function(response){
if ( response == 1 ) {
var newlabel = 'Enable Hourly Updates';
jQuery('#next-scheduled').hide();
} else {
var newlabel = 'Disable Hourly Updates';
jQuery('#next-scheduled').show().find('span').html(response);
}
jQuery('#schedule_setter').val(newlabel).attr('disabled',false);
});
}
</script>
<?php
}
function ajax_plugin_refresh() {
plugin_collector(true);
die;
}
function ajax_plugin_toggle_schedule() {
$is_active = (bool) get_option('plugin_is_active');
update_option( 'plugin_is_active', ! $is_active );
if ( $is_active ) {
if ( $timestamp = wp_next_scheduled( 'plugin_hook' ) )
wp_unschedule_event( $timestamp, 'plugin_hook' );
echo '1';
} else {
if ( ! wp_next_scheduled( 'plugin_hook' ) )
wp_schedule_event( time(), 'hourly', 'plugin_hook' );
echo date('l jS \of F Y h:i:s A', wp_next_scheduled( 'plugin_hook' ) );
}
die;
}
}
$plugin_config = new Plugin_Config_Page(); // Pass a namespace and an array of fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment