diff --git a/genesis-simple-sidebars.php b/genesis-simple-sidebars.php
new file mode 100644
index 0000000..962ba45
--- /dev/null
+++ b/genesis-simple-sidebars.php
@@ -0,0 +1,190 @@
+plugin_dir_url = plugin_dir_url( __FILE__ );
+ $this->plugin_dir_path = plugin_dir_path( __FILE__ );
+
+ // For backward compatibility
+ define( 'SS_PLUGIN_DIR', $this->plugin_dir_path );
+
+ }
+
+ /**
+ * Initialize.
+ *
+ * @since 2.1.0
+ */
+ public function init() {
+
+ $this->load_plugin_textdomain();
+
+ add_action( 'admin_notices', array( $this, 'requirements_notice' ) );
+
+ $this->includes();
+ $this->instantiate();
+
+ }
+
+ /**
+ * Show admin notice if minimum requirements aren't met.
+ *
+ * @since 2.1.0
+ */
+ public function requirements_notice() {
+
+ if ( ! defined( 'PARENT_THEME_VERSION' ) || ! version_compare( PARENT_THEME_VERSION, $this->min_genesis_version, '>=' ) ) {
+
+ $action = defined( 'PARENT_THEME_VERSION' ) ? __( 'upgrade to', 'genesis-simple-sidebars' ) : __( 'install and activate', 'genesis-simple-sidebars' );
+
+ $message = sprintf( __( 'Genesis Simple Sidebars requires WordPress %s and Genesis %s, or greater. Please %s the latest version of Genesis to use this plugin.', 'genesis-simple-sidebars' ), $this->min_wp_version, $this->min_genesis_version, $action, 'http://my.studiopress.com/?download_id=91046d629e74d525b3f2978e404e7ffa' );
+ echo '
';
+
+ }
+
+ }
+
+ /**
+ * Load the plugin textdomain, for translation.
+ *
+ * @since 2.1.0
+ */
+ public function load_plugin_textdomain() {
+ load_plugin_textdomain( $this->plugin_textdomain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
+ }
+
+ /**
+ * All general includes.
+ *
+ * @since 2.1.0
+ */
+ public function includes() {
+
+ require_once( $this->plugin_dir_path . 'includes/functions.php' );
+ require_once( $this->plugin_dir_path . 'includes/deprecated.php' );
+
+ }
+
+ /**
+ * Include the class file, instantiate the classes, create objects.
+ *
+ * @since 2.1.0
+ */
+ public function instantiate() {
+
+ add_action( 'genesis_setup', array( $this, 'genesis_dependencies' ) );
+
+ }
+
+ /**
+ * Load and instantiate any Genesis dependencies.
+ *
+ * @since 2.1.0
+ */
+ public function genesis_dependencies() {
+
+ require_once( $this->plugin_dir_path . 'includes/class-genesis-simple-sidebars-core.php' );
+ $this->core = new Genesis_Simple_Sidebars_Core;
+ $this->core->init();
+
+ // Anything beyond this point should only be loaded if in the admin.
+ if ( ! is_admin() ) {
+ return;
+ }
+
+ require_once( $this->plugin_dir_path . 'includes/class-genesis-simple-sidebars-entry.php' );
+ $this->entry = new Genesis_Simple_Sidebars_Entry;
+ $this->entry->init();
+
+ require_once( $this->plugin_dir_path . 'includes/class-genesis-simple-sidebars-term.php' );
+ $this->term = new Genesis_Simple_Sidebars_Term;
+ $this->term->init();
+
+ require_once( $this->plugin_dir_path . 'includes/class-genesis-simple-sidebars-admin.php' );
+ $this->admin = new Genesis_Simple_Sidebars_Admin;
+ $this->admin->admin_menu();
+
+ // For backward compatibility
+ global $_genesis_simple_sidebars;
+ $_genesis_simple_sidebars = $this->admin;
+
+ }
+
+}
+
+/**
+ * Helper function to retrieve the static object without using globals.
+ *
+ * @since 2.1.0
+ */
+function Genesis_Simple_Sidebars() {
+
+ static $object;
+
+ if ( null == $object ) {
+ $object = new Genesis_Simple_Sidebars;
+ }
+
+ return $object;
+}
+/**
+ * Initialize the object on `plugins_loaded`.
+ */
+add_action( 'plugins_loaded', array( Genesis_Simple_Sidebars(), 'init' ) );
diff --git a/includes/admin.php b/includes/class-genesis-simple-sidebars-admin.php
similarity index 76%
rename from includes/admin.php
rename to includes/class-genesis-simple-sidebars-admin.php
index f045e32..d2a572a 100644
--- a/includes/admin.php
+++ b/includes/class-genesis-simple-sidebars-admin.php
@@ -13,6 +13,25 @@
*/
class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
+ /**
+ * Settings field.
+ *
+ * @since 2.1.0
+ */
+ public $settings_field;
+
+ /**
+ * Constructor.
+ */
+ public function __construct() {
+
+ $this->settings_field = Genesis_Simple_Sidebars()->settings_field;
+
+ // For backward compatibility
+ define( 'SS_SETTINGS_FIELD', $this->settings_field );
+
+ }
+
/**
* Create an admin menu item and settings page.
*
@@ -22,7 +41,7 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
*
* @see Genesis_Admin_Import_Export::actions() Handle creating, editing, and deleting sidebars.
*/
- public function __construct() {
+ public function admin_menu() {
$page_id = 'simple-sidebars';
@@ -34,14 +53,12 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
)
);
- //* Empty, as we'll be building the page manually
+ // Empty, as we'll be building the page manually
$page_ops = array();
- $settings_field = SS_SETTINGS_FIELD;
+ $this->create( $page_id, $menu_ops, $page_ops, $this->settings_field );
- $this->create( $page_id, $menu_ops, $page_ops, $settings_field );
-
- //* Simpe Sidebar actions (create, edit, or delete)
+ // Simpe Sidebar actions (create, edit, or delete)
add_action( 'admin_init', array( $this, 'actions' ) );
}
@@ -56,14 +73,14 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
*/
public function admin() {
- $_sidebars = get_option( $this->settings_field );
-
echo '';
- if ( isset( $_REQUEST['action'] ) && 'edit' == $_REQUEST['action'] )
- require_once( SS_PLUGIN_DIR . '/includes/views/edit.php' );
- else
- require_once( SS_PLUGIN_DIR . '/includes/views/main.php' );
+ if ( isset( $_REQUEST['action'] ) && 'edit' == $_REQUEST['action'] ) {
+ require_once( Genesis_Simple_Sidebars()->plugin_dir_path . '/includes/views/admin-edit.php' );
+ }
+ else {
+ require_once( Genesis_Simple_Sidebars()->plugin_dir_path . '/includes/views/admin-main.php' );
+ }
echo '
';
@@ -129,8 +146,9 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
*/
public function actions() {
- if ( ! genesis_is_menu_page( 'simple-sidebars' ) )
+ if ( ! genesis_is_menu_page( 'simple-sidebars' ) ) {
return;
+ }
/**
* This section handles the data if a new sidebar is created
@@ -164,8 +182,9 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
*/
public function notices() {
- if ( ! genesis_is_menu_page( 'simple-sidebars' ) )
+ if ( ! genesis_is_menu_page( 'simple-sidebars' ) ) {
return;
+ }
$pattern = '';
@@ -196,34 +215,50 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
*/
protected function create_sidebar( $args = array() ) {
- if ( empty( $args['name'] ) || empty( $args['id'] ) ) {
+ if ( empty( $args['name'] ) ) {
wp_die( $this->error( 1 ) );
exit;
}
- //* nonce verification
+ // nonce verification
check_admin_referer( 'simple-sidebars-action_create-sidebar' );
- //* WP changes a numeric sidebar id to sidebar-id which makes it inaccessible to the user
- if ( is_numeric( $args['id'] ) )
- $args['id'] = sanitize_title_with_dashes( $args['name'] );
+ $db = (array) get_option( $this->settings_field );
+
+ // Change empty or numeric IDs to the name, lowercased and separated by dashes.
+ if ( empty( $args['id'] ) || is_numeric( $args['id'] ) ) {
+ $args['id'] = $args['name'];
+ }
+
+ // Strip all but alphanumeric, sanitize with dashes.
+ $id = preg_replace( "/[^a-zA-Z0-9 -]+/", "", sanitize_title_with_dashes( $args['id'] ) );
+
+ // Preface numeric IDs with 'sidebar-'.
+ $id = is_numeric( $id ) ? 'gss-sidebar-' . $id : $id;
+
+ // If empty after all the sanitizing ...
+ if ( ! $id || is_registered_sidebar( $id ) ) {
+ $n = count( $db ) + 1;
+ do {
+ $id = 'gss-sidebar-' . $n++;
+ } while ( is_registered_sidebar( $id ) );
+ }
- $db = (array) get_option( SS_SETTINGS_FIELD );
$new = array(
- sanitize_title_with_dashes( $args['id'] ) => array(
- 'name' => esc_html( $args['name'] ),
+ $id => array(
+ 'name' => esc_html( $args['name'] ),
'description' => esc_html( $args['description'] )
- )
+ ),
);
- if ( array_key_exists( $args['id'], $db ) ) {
+ if ( array_key_exists( $id, $db ) ) {
wp_die( $this->error( 2 ) );
exit;
}
$_sidebars = wp_parse_args( $new, $db );
- update_option( SS_SETTINGS_FIELD, $_sidebars );
+ update_option( $this->settings_field, $_sidebars );
wp_redirect( admin_url( 'admin.php?page=simple-sidebars&created=true' ) );
exit;
@@ -242,17 +277,13 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
exit;
}
- //* nonce verification
+ // nonce verification
check_admin_referer( 'simple-sidebars-action_edit-sidebar' );
- //* WP changes a numeric sidebar id to sidebar-id which makes it inaccessible to the user
- if ( is_numeric( $args['id'] ) )
- $args['id'] = sanitize_title_with_dashes( $args['name'] );
-
- $db = (array) get_option( SS_SETTINGS_FIELD );
+ $db = (array) get_option( $this->settings_field );
$new = array(
- sanitize_title_with_dashes( $args['id'] ) => array(
- 'name' => esc_html( $args['name'] ),
+ $args['id'] => array(
+ 'name' => esc_html( $args['name'] ),
'description' => esc_html( $args['description'] )
)
);
@@ -264,7 +295,7 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
$_sidebars = wp_parse_args( $new, $db );
- update_option( SS_SETTINGS_FIELD, $_sidebars );
+ update_option( $this->settings_field, $_sidebars );
wp_redirect( admin_url( 'admin.php?page=simple-sidebars&edited=true' ) );
exit;
@@ -283,10 +314,10 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
exit;
}
- //* nonce verification
+ // nonce verification
check_admin_referer( 'simple-sidebars-action_delete-sidebar' );
- $_sidebars = (array) get_option( SS_SETTINGS_FIELD );
+ $_sidebars = (array) get_option( $this->settings_field );
if ( ! isset( $_sidebars[$id] ) ) {
wp_die( $this->error( 4 ) );
@@ -295,7 +326,7 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
unset( $_sidebars[$id] );
- update_option( SS_SETTINGS_FIELD, $_sidebars );
+ update_option( $this->settings_field, $_sidebars );
wp_redirect( admin_url( 'admin.php?page=simple-sidebars&deleted=true' ) );
exit;
@@ -316,7 +347,7 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
switch( (int) $error ) {
case 1:
- return __( 'Oops! Please choose a valid Name and ID for this sidebar', 'genesis-simple-sidebars' );
+ return __( 'Oops! Please choose a valid Name for this sidebar', 'genesis-simple-sidebars' );
break;
case 2:
return __( 'Oops! That sidebar ID already exists', 'genesis-simple-sidebars' );
@@ -335,17 +366,3 @@ class Genesis_Simple_Sidebars_Admin extends Genesis_Admin_Basic {
}
}
-
-add_action( 'genesis_admin_menu', 'simplesidebars_settings_menu' );
-/**
- * Instantiate the class to create the menu.
- *
- * @since 1.0.0
- */
-function simplesidebars_settings_menu() {
-
- global $_genesis_simple_sidebars_admin;
-
- $_genesis_simple_sidebars_admin = new Genesis_Simple_Sidebars_Admin;
-
-}
diff --git a/includes/class-genesis-simple-sidebars-core.php b/includes/class-genesis-simple-sidebars-core.php
new file mode 100644
index 0000000..72766b9
--- /dev/null
+++ b/includes/class-genesis-simple-sidebars-core.php
@@ -0,0 +1,199 @@
+get_sidebars();
+
+ if ( ! $sidebars ) {
+ return;
+ }
+
+ // Cycle through created sidebars, register them as widget areas
+ foreach ( (array) $sidebars as $id => $info ) {
+
+ if ( ! isset( $info['name'] ) || ! isset( $info['description'] ) ) {
+ continue;
+ }
+
+ genesis_register_sidebar( array(
+ 'name' => esc_html( $info['name'] ),
+ 'id' => $id,
+ 'description' => esc_html( $info['description'] ),
+ 'editable' => 1,
+ ) );
+
+ }
+
+ }
+
+ /**
+ * Filter the widgets in each widget area.
+ *
+ * @since 2.1.0
+ */
+ public function sidebars_widgets_filter( $widgets ) {
+
+ $sidebars = array();
+
+ if ( is_singular() ) {
+
+ $sidebars = array(
+ 'sidebar' => genesis_get_custom_field( '_ss_sidebar' ),
+ 'sidebar-alt' => genesis_get_custom_field( '_ss_sidebar_alt' ),
+ 'header-right' => genesis_get_custom_field( '_ss_header' ),
+ );
+
+ }
+
+ if ( is_tax() || is_category() || is_tag() ) {
+
+ $sidebars = array(
+ 'sidebar' => get_term_meta( get_queried_object()->term_id, '_ss_sidebar', true ),
+ 'sidebar-alt' => get_term_meta( get_queried_object()->term_id, '_ss_sidebar_alt', true ),
+ 'header-right' => get_term_meta( get_queried_object()->term_id, '_ss_header', true ),
+ );
+
+ }
+
+ $widgets = $this->swap_widgets( $widgets, $sidebars );
+
+ return $widgets;
+
+ }
+
+ /**
+ * Take the $widgets array and swap the contents of each widget area with a custom widget area, if specified.
+ *
+ * @since 2.1.0
+ */
+ public function swap_widgets( $widgets, $sidebars ) {
+
+ if ( is_admin() ) {
+ return $widgets;
+ }
+
+ foreach ( (array) $sidebars as $old_sidebar => $new_sidebar ) {
+
+ if ( ! is_registered_sidebar( $old_sidebar ) ) {
+ continue;
+ }
+
+ if ( $new_sidebar && ! empty( $widgets[ $new_sidebar ] ) ) {
+ $widgets[ $old_sidebar ] = $widgets[ $new_sidebar ];
+ }
+
+ }
+
+ return $widgets;
+
+ }
+
+ /**
+ * Get all custom registered sidebars.
+ *
+ * @since 2.1.0
+ */
+ public function get_sidebars( $cache = true ) {
+
+ if ( ! $cache ) {
+ return stripslashes_deep( get_option( Genesis_Simple_Sidebars()->settings_field ) );
+ }
+
+ if ( is_null( $this->sidebars ) ) {
+ $this->sidebars = stripslashes_deep( get_option( Genesis_Simple_Sidebars()->settings_field ) );
+ }
+
+ return $this->sidebars;
+
+ }
+
+ /**
+ * Return taxonomy ids.
+ *
+ * Helper function to return the array keys from a taxonomy query.
+ *
+ * @since 2.1.0
+ */
+ public function get_public_taxonomies() {
+
+ if ( is_null( $this->public_taxonomies ) ) {
+ $this->public_taxonomies = get_taxonomies( array(
+ 'show_ui' => true,
+ 'public' => true,
+ ) );
+ }
+
+ $this->public_taxonomies = apply_filters( 'genesis_simple_sidebars_taxonomies', array_keys( $this->public_taxonomies ) );
+
+ return $this->public_taxonomies;
+
+ }
+
+ /**
+ * Determines if this site has disabled 3 column layouts or not.
+ *
+ * @since 2.1.0
+ */
+ public function has_3_column_layout() {
+
+ $layouts = genesis_get_layouts();
+
+ $three_column_layouts = array(
+ 'content-sidebar-sidebar',
+ 'sidebar-content-sidebar',
+ 'sidebar-sidebar-content',
+ );
+
+ foreach ( $three_column_layouts as $layout ) {
+ if ( array_key_exists( $layout, $layouts ) ) {
+ return true;
+ }
+ }
+
+ return false;
+
+ }
+
+}
diff --git a/includes/class-genesis-simple-sidebars-entry.php b/includes/class-genesis-simple-sidebars-entry.php
new file mode 100644
index 0000000..f89c8b5
--- /dev/null
+++ b/includes/class-genesis-simple-sidebars-entry.php
@@ -0,0 +1,61 @@
+ true ) ) as $type ) {
+
+ if ( post_type_supports( $type, 'genesis-simple-sidebars' ) || $type == 'post' || $type == 'page' ) {
+ add_meta_box( 'ss_inpost_metabox', __( 'Sidebar Selection', 'genesis-simple-sidebars' ), array( $this, 'metabox_content' ), $type, 'side', 'low' );
+ }
+
+ }
+
+ }
+
+ /**
+ * Include the metabox content.
+ *
+ * @since 2.1.0
+ */
+ public function metabox_content() {
+
+ require_once( Genesis_Simple_Sidebars()->plugin_dir_path . 'includes/views/entry-metabox-content.php' );
+
+ }
+
+ /**
+ * Save the metabox fields when the entry is saved.
+ *
+ * @since 2.1.0
+ */
+ public function metabox_save( $post_id, $post ) {
+
+ if ( ! isset( $_POST['genesis_simple_sidebars'] ) ) {
+ return;
+ }
+
+ $data = wp_parse_args( $_POST['genesis_simple_sidebars'], array(
+ '_ss_header' => '',
+ '_ss_sidebar' => '',
+ '_ss_sidebar_alt' => '',
+ ) );
+
+ genesis_save_custom_fields( $data, 'genesis-simple-sidebars-save-entry', 'genesis-simple-sidebars-save-entry-nonce', $post );
+
+ }
+
+}
diff --git a/includes/class-genesis-simple-sidebars-term.php b/includes/class-genesis-simple-sidebars-term.php
new file mode 100644
index 0000000..af9f64b
--- /dev/null
+++ b/includes/class-genesis-simple-sidebars-term.php
@@ -0,0 +1,31 @@
+core->get_public_taxonomies();
+
+ if ( ! empty( $taxonomies ) && is_admin() && is_array( $taxonomies ) ) {
+
+ foreach ( $taxonomies as $tax ) {
+ add_action( "{$tax}_edit_form", array( $this, 'term_sidebar_form' ), 9, 2 );
+ }
+
+ }
+
+ }
+
+ public function term_sidebar_form( $tag, $taxonomy ) {
+
+ require_once( Genesis_Simple_Sidebars()->plugin_dir_path . 'includes/views/term-edit-sidebar-form.php' );
+
+ }
+
+}
diff --git a/includes/deprecated.php b/includes/deprecated.php
new file mode 100644
index 0000000..ae3aedc
--- /dev/null
+++ b/includes/deprecated.php
@@ -0,0 +1,153 @@
+ true ) ) as $type ) {
-
- if ( post_type_supports( $type, 'genesis-simple-sidebars' ) || $type == 'post' || $type == 'page' ) {
- add_meta_box( 'ss_inpost_metabox', __( 'Sidebar Selection', 'genesis-simple-sidebars' ), 'ss_inpost_metabox', $type, 'side', 'low' );
- }
-
- }
-
-}
-
-function ss_inpost_metabox() {
-
- $_sidebars = stripslashes_deep( get_option( SS_SETTINGS_FIELD ) );
- global $wp_registered_sidebars;
-
-?>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-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 ) {
-
- //* don't try to store data during revision save
- if ( 'revision' === $post->post_type )
- return;
-
- if ( $value ) {
- update_post_meta($post->ID, $key, $value);
- } else {
- delete_post_meta($post->ID, $key);
- }
-
- }
-
-}
diff --git a/includes/term.php b/includes/term.php
deleted file mode 100644
index 74d08b6..0000000
--- a/includes/term.php
+++ /dev/null
@@ -1,69 +0,0 @@
-meta = wp_parse_args( $tag->meta, array( '_ss_sidebar' => '', '_ss_sidebar_alt' => '' ) );
-
- //* Pull custom sidebars
- $_sidebars = stripslashes_deep( get_option( SS_SETTINGS_FIELD ) );
-
-?>
-
-
-
-
-core->get_sidebars();
+
+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.', 'genesis-simple-sidebars' ) );
}
-
-screen_icon( 'themes' ); ?>
-
+?>
+