Skip to content

Commit

Permalink
Tagging 0.9.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanrice committed Aug 9, 2013
1 parent 33940b3 commit 4e6fc0b
Show file tree
Hide file tree
Showing 6 changed files with 664 additions and 0 deletions.
181 changes: 181 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
/**
* This registers the settings field and adds empty array to the options table
*/
add_action('admin_init', 'register_ss_settings');
function register_ss_settings() {
register_setting(SS_SETTINGS_FIELD, SS_SETTINGS_FIELD);
add_option(SS_SETTINGS_FIELD, '__return_empty_array', '', 'yes');
}

/**
* This function adds our "Simple Sidebars" submenu item
*/
add_action('admin_menu', 'ss_settings_init', 15);
function ss_settings_init() {

add_submenu_page('genesis', __('Simple Sidebars','ss'), __('Simple Sidebars','ss'), 'manage_options', 'simple-sidebars', 'ss_settings_admin');

}

add_action('admin_init', 'ss_action_functions');
function ss_action_functions() {

if ( !isset( $_REQUEST['page'] ) || $_REQUEST['page'] != 'simple-sidebars' ) {
return;
}

/**
* This section handles the data if a new sidebar is created
*/
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'create' ) {
ss_create_sidebar( $_POST['new_sidebar'] );
}

/**
* This section will handle the data if a sidebar is deleted
*/
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete' && isset( $_REQUEST['id'] ) ) {
ss_delete_sidebar( $_REQUEST['id'] );
}

/**
* This section will handle the data if a sidebar is to be modified
*/
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'edit' && !isset( $_REQUEST['id'] ) ) {
ss_edit_sidebar( $_POST['edit_sidebar'] );
}
}

/**
* This function is what actually gets output to the page.
* It handles the markup, builds the form, etc.
*/
function ss_settings_admin() { ?>

<div class="wrap">

<?php
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'edit' ) :

$_sidebars = get_option( SS_SETTINGS_FIELD );

if ( array_key_exists( $_REQUEST['id'], (array)$_sidebars ) ) {
$_sidebar = stripslashes_deep( $_sidebars[$_REQUEST['id']] );
} else {
wp_die( __('Nice try, partner. But that sidebar doesn\'t exist. Click back and try again.', 'ss') );
}

?>

<?php screen_icon('themes'); ?>
<h2><?php _e('Edit Sidebar', 'ss'); ?></h2>

<form method="post" action="<?php echo admin_url( 'admin.php?page=simple-sidebars&amp;action=edit' ); ?>">
<?php wp_nonce_field('simple-sidebars-action_edit-sidebar'); ?>
<table class="form-table">

<tr class="form-field">
<th scope="row" valign="top"><label for="edit_sidebar[name]"><?php _e('Name', 'ss'); ?></label></th>
<td><input name="edit_sidebar[name]" id="edit_sidebar[name]" type="text" value="<?php echo esc_html( $_sidebar['name'] ); ?>" size="40" />
<p class="description"><?php _e('A recognizable name for your new sidebar widget area', 'ss'); ?></p></td>
</tr>

<tr class="form-field">
<th scope="row" valign="top"><label for="edit_sidebar[id]"><?php _e('ID', 'ss'); ?></label></th>
<td>
<input type="text" value="<?php echo esc_html( $_REQUEST['id'] ); ?>" size="40" disabled="disabled" />
<input name="edit_sidebar[id]" id="edit_sidebar[id]" type="hidden" value="<?php echo esc_html( $_REQUEST['id'] ); ?>" size="40" />
<p class="description"><?php _e('The unique ID is used to register the sidebar widget area (cannot be changed)', 'ss'); ?></p></td>
</tr>

<tr class="form-field">
<th scope="row" valign="top"><label for="edit_sidebar[description]"><?php _e('Description', 'ss'); ?></label></th>
<td><textarea name="edit_sidebar[description]" id="edit_sidebar[description]" rows="3" cols="50" style="width: 97%;"><?php echo esc_html( $_sidebar['description'] ); ?></textarea></td>
</tr>

</table>

<p class="submit"><input type="submit" class="button-primary" name="submit" value="<?php _e('Update', 'ee'); ?>" /></p>

</form>

<?php else : ?>

<?php screen_icon('themes'); ?>
<h2><?php _e('Genesis - Simple Sidebars', 'ss'); ?></h2>

<div id="col-container">

<div id="col-right">
<div class="col-wrap">

<h3><?php _e('Current Sidebars', 'ss'); ?></h3>
<table class="widefat tag fixed" cellspacing="0">
<thead>
<tr>
<th scope="col" id="name" class="manage-column column-name"><?php _e('Name', 'ss'); ?></th>
<th scope="col" class="manage-column column-slug"><?php _e('ID', 'ss'); ?></th>
<th scope="col" id="description" class="manage-column column-description"><?php _e('Description', 'ss'); ?></th>
</tr>
</thead>

<tfoot>
<tr>
<th scope="col" class="manage-column column-name"><?php _e('Name', 'ss'); ?></th>
<th scope="col" class="manage-column column-slug"><?php _e('ID', 'ss'); ?></th>
<th scope="col" class="manage-column column-description"><?php _e('Description', 'ss'); ?></th>
</tr>
</tfoot>

<tbody id="the-list" class="list:tag">

<?php ss_sidebar_table_rows(); ?>

</tbody>
</table>

</div>
</div><!-- /col-right -->

<div id="col-left">
<div class="col-wrap">


<div class="form-wrap">
<h3><?php _e('Add New Sidebar', 'ss'); ?></h3>

<form method="post" action="<?php echo admin_url( 'admin.php?page=simple-sidebars&amp;action=create' ); ?>">
<?php wp_nonce_field('simple-sidebars-action_create-sidebar'); ?>

<div class="form-field form-required">
<label for="sidebar-name"><?php _e('Name', 'ss'); ?></label>
<input name="new_sidebar[name]" id="sidebar-name" type="text" value="" size="40" aria-required="true" />
<p><?php _e('A recognizable name for your new sidebar widget area', 'ss'); ?></p>
</div>

<div class="form-field">
<label for="sidebar-id"><?php _e('ID', 'ss'); ?></label>
<input name="new_sidebar[id]" id="sidebar-id" type="text" value="" size="40" />
<p><?php _e('The unique ID is used to register the sidebar widget area', 'ss'); ?></p>
</div>

<div class="form-field">
<label for="sidebar-description"><?php _e('Description', 'ss'); ?></label>
<textarea name="new_sidebar[description]" id="sidebar-description" rows="5" cols="40"></textarea>
</div>

<p class="submit"><input type="submit" class="button" name="submit" id="submit" value="<?php _e('Add New Sidebar', 'ss'); ?>" /></p>
</form></div>

</div>
</div><!-- /col-left -->

</div><!-- /col-container -->

<?php endif; ?>

</div><!-- /wrap -->

<?php
}
181 changes: 181 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php
function ss_sidebar_table_rows() {
global $wp_registered_sidebars;

$_sidebars = $wp_registered_sidebars;

$alt = true;

foreach ( (array)$_sidebars as $id => $info ) { ?>

<?php
$is_editable = isset( $info['editable'] ) && $info['editable'] ? true : false;
?>

<tr <?php if ( $alt ) { echo 'class="alternate"'; $alt = false; } else { $alt = true; } ?>>
<td class="name column-name">
<?php
if ( $is_editable ) {
printf( '<a class="row-title" href="%s" title="Edit %s">%s</a>', admin_url('admin.php?page=simple-sidebars&amp;action=edit&amp;id=' . esc_html( $id ) ), esc_html( $info['name'] ), esc_html( $info['name'] ) );
} else {
printf( '<strong class="row-title">%s</strong>', esc_html( $info['name'] ) );
}
?>

<?php if ( $is_editable ) : ?>
<br />
<div class="row-actions">
<span class="edit"><a href="<?php echo admin_url('admin.php?page=simple-sidebars&amp;action=edit&amp;id=' . esc_html( $id ) ); ?>"><?php _e('Edit', 'ss'); ?></a> | </span>
<span class="delete"><a class="delete-tag" href="<?php echo wp_nonce_url( admin_url( 'admin.php?page=simple-sidebars&amp;action=delete&amp;id=' . esc_html( $id ) ), 'simple-sidebars-action_delete-sidebar' ); ?>"><?php _e('Delete', 'ss'); ?></a></span>
</div>
<?php endif; ?>

</td>
<td class="slug column-slug"><?php echo esc_html( $id ); ?></td>
<td class="description column-description"><?php echo esc_html( $info['description'] )?></td>
</tr>

<?php
}

}

function ss_create_sidebar( $args = array() ) {

if ( empty( $args['name'] ) || empty( $args['id'] ) ) {
wp_die( ss_error_message(1) );
exit;
}

// nonce verification
check_admin_referer('simple-sidebars-action_create-sidebar');

$db = (array)get_option(SS_SETTINGS_FIELD);
$new = array(
sanitize_title_with_dashes( $args['id'] ) => array(
'name' => esc_html( $args['name'] ),
'description' => esc_html( $args['description'] )
)
);

if ( array_key_exists( $args['id'], $db ) ) {
wp_die( ss_error_message(2) );
exit;
}

$_sidebars = wp_parse_args( $new, $db );

update_option( SS_SETTINGS_FIELD, $_sidebars );
wp_redirect( admin_url('admin.php?page=simple-sidebars&created=true') );
exit;

}

function ss_edit_sidebar( $args = array() ) {

if ( empty( $args['name'] ) || empty( $args['id'] ) ) {
wp_die( ss_error_message(3) );
exit;
}

// nonce verification
check_admin_referer('simple-sidebars-action_edit-sidebar');

$db = (array)get_option(SS_SETTINGS_FIELD);
$new = array(
sanitize_title_with_dashes( $args['id'] ) => array(
'name' => esc_html( $args['name'] ),
'description' => esc_html( $args['description'] )
)
);

if ( !array_key_exists( $args['id'], $db ) ) {
wp_die( ss_error_message(3) );
exit;
}

$_sidebars = wp_parse_args( $new, $db );

update_option( SS_SETTINGS_FIELD, $_sidebars );
wp_redirect( admin_url('admin.php?page=simple-sidebars&edited=true') );
exit;

}

function ss_delete_sidebar( $id = '' ) {

if ( empty( $id ) ) {
wp_die( ss_error_message(4) );
exit;
}

// nonce verification
check_admin_referer('simple-sidebars-action_delete-sidebar');

$_sidebars = (array)get_option( SS_SETTINGS_FIELD );

if ( !isset( $_sidebars[$id] ) ) {
wp_die( ss_error_message(4) );
exit;
}

unset( $_sidebars[$id] );

update_option( SS_SETTINGS_FIELD, $_sidebars );
wp_redirect( admin_url('admin.php?page=simple-sidebars&deleted=true') );
exit;

}

function ss_error_message( $error = false ) {

if ( !$error ) return false;

switch( (int)$error ) {

case 1:
return __('Oops! Please choose a valid Name and ID for this sidebar', 'ss');
break;
case 2:
return __('Oops! That sidebar ID already exists');
break;
case 3:
return __('Oops! You are trying to edit a sidebar that does not exist, or is not editable', 'ss');
break;
case 4:
return __('Oops! You are trying to delete a sidebar that does not exist, or cannot be deleted', 'ss');
break;
default:
return __('Oops! Something went wrong. Try again.', 'ss');

}

}

add_action('admin_notices', 'ss_success_message');
function ss_success_message() {

if ( !isset( $_REQUEST['page'] ) || $_REQUEST['page'] != 'simple-sidebars' ) {
return;
}

$format = '<div id="message" class="updated"><p><strong>%s</strong></p></div>';

if ( isset( $_REQUEST['created'] ) && $_REQUEST['created'] === 'true' ) {
printf( $format, __('New sidebar successfully created!', 'ss') );
return;
}

if ( isset( $_REQUEST['edited'] ) && $_REQUEST['edited'] === 'true' ) {
printf( $format, __('Sidebar successfully edited!', 'ss') );
return;
}

if ( isset( $_REQUEST['deleted'] ) && $_REQUEST['deleted'] === 'true' ) {
printf( $format, __('Sidebar successfully deleted.', 'ss') );
return;
}

return;

}
Loading

0 comments on commit 4e6fc0b

Please sign in to comment.