get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", PPS_KEY_REPLACED) );
return $posts_w_sidebars;
}
// ************************************************************
// tcc_pps_add_checked
//
// Used to mark a selection as checked in HTML.
// ************************************************************
function tcc_pps_add_checked($testvalue, $value) {
if ($testvalue == $value)
return ' checked="checked" ';
}
// ************************************************************
// Methods
// ************************************************************
// ************************************************************
// pps_add_custom_box
//
// This adds the sidebar replacement selection box to the
// Edit->Page screens.
// ************************************************************
function pps_add_custom_box() {
// This security check is also verified upon save.
if ( current_user_can('edit_theme_options') )
add_meta_box('pps_sectionid', __( 'Custom Sidebar', 'tcc_pps_domain' ), 'pps_inner_custom_box', 'page', 'advanced', 'high' );
}
// ************************************************************
// pps_inner_custom_box
//
// This displays the sidebar replacement selection box
// ************************************************************
function pps_inner_custom_box($post) {
global $wp_registered_sidebars;
$replaced_sidebar=get_post_meta($post->ID, PPS_KEY_REPLACED, true);
$pps_active_on_post = (isset($wp_registered_sidebars[$replaced_sidebar]) );
echo '';
echo '
';
echo '
';
echo ' ';
// Print the list of sidebars in the drop down
foreach ($wp_registered_sidebars as $id => $sidebar) {
$description = $sidebar['description'];
if ( isset($sidebar['name']) ) {
$name = $sidebar['name'];
} else {
$name = $id;
}
// Eliminate any self created sidebars or this could get messy.
if (substr($name, 0, strlen(SIDEBAR_PREFIX) ) != SIDEBAR_PREFIX) {
echo ' ' . $name . ' - ' . $description . ' ';
}
}
echo '
';
}
// ************************************************************
// pps_save_postdata
//
// Recieves the sidebar replacement data when a page is saved.
// ************************************************************
function pps_save_postdata($post_id) {
global $wp_registered_sidebars;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
// This will eventualy need to be adjusted to deal 'edit_CUSTOMPOSTTYPE' permissions as the core does.
// Assuming, of course, I extend the plugin to handle more than pages.
if (('page' == $_POST['post_type'] ) and current_user_can('edit_theme_options')) {
if(!current_user_can('edit_page', $post_id)) return $post_id;
} else {
return $post_id;
}
// Return with no errors if the NONCE fails. This both blocks hack attacks AND prevents a save from removing
// the data if plugin modification adds security checks on meta box display that aren't duplicated here.
if (!wp_verify_nonce($_POST['pps_nonce'], plugin_basename(__FILE__))) return $post_id;
$replaced_sidebar = $_POST['tcc-pps-replace'];
$pps_active_on_post = ($_POST['tcc-pps-active'] == 1) && (isset($wp_registered_sidebars[$replaced_sidebar]));
if ($pps_active_on_post) {
update_post_meta($post_id, PPS_KEY_REPLACED, $replaced_sidebar);
} else {
delete_post_meta($post_id, PPS_KEY_REPLACED);
}
return $post_id;
}
// ************************************************************
// pps_save_postdata
//
// Adds the custom sidebar to the sidebar array as a page is
// loaded.
// ************************************************************
function pps_reg_custom_sidebar() {
global $wp_query, $wp_registered_sidebars;
if ( is_page() ) {
$post = $wp_query->get_queried_object();
$post_id = $post->ID;
$post_parent = get_post($post->post_parent);
$slug_parent = $post_parent->post_name;
$post_name = $post->post_name;
$post_title = $post->post_title;
$replaced_sidebar=get_post_meta($post->ID, PPS_KEY_REPLACED, true);
$pps_active_on_post = (isset($wp_registered_sidebars[$replaced_sidebar]) );
if ($pps_active_on_post) {
register_sidebar(array(
'name' => SIDEBAR_PREFIX . $slug_parent .'-'. $post_name,
'id' => SIDEBAR_PREFIX . $slug_parent .'-'. $post_name,
'description' => 'A custom sidebar for the post "' . $post_title . '"'));
}
}
}
// ************************************************************
// pps_reg_all_custom_sidebars
//
// Registers the custom sidebars for the admin pages.
// ************************************************************
function pps_reg_all_custom_sidebars() {
global $wp_registered_sidebars;
$posts = get_pps_post_ids();
foreach ($posts as $post_id) {
$post = get_post($post_id);
$post_parent = get_post($post->post_parent);
$slug_parent = $post_parent->post_name;
register_sidebar(array(
'name' => SIDEBAR_PREFIX . $slug_parent .'-'. $post->post_name,
'id' => SIDEBAR_PREFIX . $slug_parent .'-'. $post->post_name,
'description' => 'A custom sidebar for the post "' . $post->post_title . '"'));
}
}
// ************************************************************
// pps_hijack_sidebars
//
// Replaces the content but not the design of sidebars during
// the page display.
// ************************************************************
function pps_hijack_sidebars($query) {
global $post, $wp_registered_sidebars, $_wp_sidebars_widgets;
$hostpost = $post;
$post_id = $hostpost->ID;
$post_name = $hostpost->post_name;
$host_sidebar = get_post_meta($hostpost->ID, PPS_KEY_REPLACED, true);
while (($host_sidebar == '') and ( $hostpost->post_parent > 0 )) {
$hostpost = get_post($hostpost->post_parent);
$host_sidebar = get_post_meta($hostpost->ID, PPS_KEY_REPLACED, true);
$post_id = $hostpost->ID;
$post_name = $hostpost->post_name;
}
$post_parent = get_post($hostpost->post_parent);
$slug_parent = $post_parent->post_name;
$parasite_sidebar = SIDEBAR_PREFIX . $slug_parent .'-'. $post_name;
if (isset($_wp_sidebars_widgets[$parasite_sidebar])) {
$_wp_sidebars_widgets[$host_sidebar] = $_wp_sidebars_widgets[$parasite_sidebar];
}
}
// ************************************************************
// Executable Code
// ************************************************************
// Add the edit field to the Page Edit screens
add_action('admin_menu', 'pps_add_custom_box');
// Process the custom PPS values submitted when saving pages.
add_action('save_post', 'pps_save_postdata');
// Register the custom sidebars that have been created for the pages.
add_action('admin_init', 'pps_reg_all_custom_sidebars');
// When a page is dipslayed, check for a custom sidebar and if it exists hijack the standard sidebar
add_filter( 'wp', 'pps_hijack_sidebars' );
?>