diff --git a/admin.php b/admin.php new file mode 100644 index 0000000..0225cbd --- /dev/null +++ b/admin.php @@ -0,0 +1,181 @@ + + +
+ + + + +

+ +
+ + + + + + + + + + + + + + + + + + +
+

+ + +

+ +

+ +
+ + + + +

+ +
+ +
+
+ +

+ + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+ +
+
+ + +
+

+ +
+ + +
+ + +

+
+ +
+ + +

+
+ +
+ + +
+ +

+
+ +
+
+ +
+ + + +
+ + $info ) { ?> + + + + > + + %s', admin_url('admin.php?page=simple-sidebars&action=edit&id=' . esc_html( $id ) ), esc_html( $info['name'] ), esc_html( $info['name'] ) ); + } else { + printf( '%s', esc_html( $info['name'] ) ); + } + ?> + + +
+
+ | + +
+ + + + + + + + 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 = '

%s

'; + + 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; + +} \ No newline at end of file diff --git a/inpost.php b/inpost.php new file mode 100644 index 0000000..c3bc3c7 --- /dev/null +++ b/inpost.php @@ -0,0 +1,84 @@ + + + + +

+ + +

+ +

+ + +

+ +ID; + + // don't try to save the data under autosave, ajax, or future post. + if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return; + if ( defined('DOING_AJAX') && DOING_AJAX ) return; + if ( defined('DOING_CRON') && DOING_CRON ) return; + + // is the user allowed to edit the post or page? + if ( ( 'page' == $_POST['post_type'] && !current_user_can('edit_page', $post->ID) ) || !current_user_can('edit_post', $post->ID ) ) + return $post->ID; + + $_sidebars = array( + '_ss_sidebar' => $_POST['_ss_sidebar'], + '_ss_sidebar_alt' => $_POST['_ss_sidebar_alt'] + ); + + // store the custom fields + foreach ( $_sidebars as $key => $value ) { + + if ( $post->post_type == 'revision' ) return; // don't try to store data during revision save + + if ( $value ) { + // save/update + update_post_meta($post->ID, $key, $value); + } else { + // delete if blank + delete_post_meta($post->ID, $key); + } + + } + +} \ No newline at end of file diff --git a/plugin.php b/plugin.php new file mode 100644 index 0000000..9676111 --- /dev/null +++ b/plugin.php @@ -0,0 +1,118 @@ +Genesis', 'ss'), 'http://www.studiopress.com/themes/genesis' ) ); + } + + if( version_compare( $theme_info['Version'], $latest, '<' ) ) { + deactivate_plugins(plugin_basename(__FILE__)); // Deactivate ourself + wp_die( sprintf( __('Sorry, you cannot activate without Genesis %s or greater', 'ss'), 'http://www.studiopress.com/support/showthread.php?t=19576', $latest ) ); + } + +} + +// Define our constants +define('SS_SETTINGS_FIELD', 'ss-settings'); +define('SS_PLUGIN_DIR', dirname(__FILE__)); + +// Include files +require_once(SS_PLUGIN_DIR . '/admin.php'); +require_once(SS_PLUGIN_DIR . '/functions.php'); +require_once(SS_PLUGIN_DIR . '/inpost.php'); +require_once(SS_PLUGIN_DIR . '/term.php'); + +/** + * This function registers the created sidebars + */ +add_action('widgets_init', 'ss_register_sidebars'); +function ss_register_sidebars() { + + $_sidebars = stripslashes_deep( get_option( SS_SETTINGS_FIELD ) ); + if ( !$_sidebars ) return; + + foreach ( (array)$_sidebars as $id => $info ) { + + register_sidebar(array( + 'name' => esc_html( $info['name'] ), + 'id' => $id, + 'description' => esc_html( $info['description'] ), + 'editable' => 1, + + 'before_widget' => '
', + 'after_widget' => "
\n", + 'before_title' => '

', + 'after_title' => "

\n" + )); + + } + +} + + +/** + * Remove the default sidebars, run some conditional logic, + * use alternate sidebars if necessary, else fallback on default sidebars. + */ +add_action('get_header', 'ss_sidebars_init'); +function ss_sidebars_init() { + remove_action('genesis_sidebar', 'genesis_do_sidebar'); + remove_action('genesis_sidebar_alt', 'genesis_do_sidebar_alt'); + add_action('genesis_sidebar', 'ss_do_sidebar'); + add_action('genesis_sidebar_alt', 'ss_do_sidebar_alt'); +} + +function ss_do_sidebar() { + + if ( is_singular() && $_sidebar = genesis_get_custom_field('_ss_sidebar') ) { + if ( dynamic_sidebar($_sidebar) ) return; + } + + if ( is_category() ) { + $term = get_term( get_query_var('cat'), 'category' ); + if( isset( $term->meta['_ss_sidebar'] ) && dynamic_sidebar( $term->meta['_ss_sidebar'] ) ) return; + } + + if ( is_tag() ) { + $term = get_term( get_query_var('tag_id'), 'post_tag' ); + if( isset( $term->meta['_ss_sidebar'] ) && dynamic_sidebar( $term->meta['_ss_sidebar'] ) ) return; + } + + genesis_do_sidebar(); + +} +function ss_do_sidebar_alt() { + + if ( is_singular() && $_sidebar_alt = genesis_get_custom_field('_ss_sidebar_alt') ) { + if ( dynamic_sidebar($_sidebar_alt) ) return; + } + + if ( is_category() ) { + $term = get_term( get_query_var('cat'), 'category' ); + if( isset( $term->meta['_ss_sidebar_alt'] ) && dynamic_sidebar( $term->meta['_ss_sidebar_alt'] ) ) return; + } + + if ( is_tag() ) { + $term = get_term( get_query_var('tag_id'), 'post_tag' ); + if( isset( $term->meta['_ss_sidebar_alt'] ) && dynamic_sidebar( $term->meta['_ss_sidebar_alt'] ) ) return; + } + + genesis_do_sidebar_alt(); + +} \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..986db2b --- /dev/null +++ b/readme.txt @@ -0,0 +1,47 @@ +=== Plugin Name === +Contributors: nathanrice +Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5553118 +Tags: hooks, genesis, genesiswp, studiopress +Requires at least: 2.9 +Tested up to: 3.0 +Stable tag: 0.9 + +This plugin allows you to create multiple, dynamic widget areas, and assign those widget areas to sidebar locations within the Genesis Theme Framework on a per post, per page, or per tag/category archive basis. + +== Description == + +This plugin allows you to create multiple, dynamic widget areas, and assign those widget areas to sidebar locations within the Genesis Theme Framework on a per post, per page, or per tag/category archive basis. + +Creating widget areas programmatically, then using conditional logic to properly assign them to sidebar locations can be a complex task for a beginner. This plugin allows you to do all this from a simple administration menu, and assign widget areas to sidebar locations with simple drop-down menus within the post/page edit screens, or when editing a tag or category. + +== Installation == + +1. Upload the entire `genesis-simple-sidebars` folder to the `/wp-content/plugins/` directory +1. DO NOT change the name of the `genesis-simple-sidebars` folder +1. Activate the plugin through the 'Plugins' menu in WordPress +1. Navigate to the `Genesis > Simple Sidebars` menu +1. Create as many new sidebar widget areas as you need +1. Choose the widget area you want to display by choosing it from the drop-down menu in the post/page or category/tag edit screen. + +== Frequently Asked Questions == + += Can I assign widget areas to locations other than the sidebars? = + +No. You can only assign them to the primary and secondary sidebars, using the plugin. + +However, once a widget area has been created, you can use hooks to programmatically display those widget areas throughout the theme. But if you're going to do that, it's very unlikely that you would want to use the plugin to create the widget areas. You might as well just create the widget areas programmatically too. + += Does this plugin give me the option of creating an entirely NEW sidebar? = + +Not in the way you're probably thinking. The markup surrounding the widget area never changes. The only thing that changes is the dynamic content that displays within the pre-existing sidebar locations. + +== Changelog == + += 0.1 = +* Initial Alpha Release + += 0.9 = +* Fixed "is not array" errors reported by users +* Added nonce verification for security purposes +* Added error and success messages +* Bump to pre-release 0.9 branch \ No newline at end of file diff --git a/term.php b/term.php new file mode 100644 index 0000000..8c79ed8 --- /dev/null +++ b/term.php @@ -0,0 +1,53 @@ +meta = wp_parse_args( $tag->meta, array( '_ss_sidebar' => '', '_ss_sidebar_alt' => '' ) ); + + // Pull custom sidebars + $_sidebars = stripslashes_deep( get_option( SS_SETTINGS_FIELD ) ); + +?> + +

+ + + + + + + + + + + + +
+ +
+ +
+ +