WordPress.org

Plugin Directory

Changeset 561412


Ignore:
Timestamp:
06/21/12 00:07:38 (22 months ago)
Author:
BrianLayman
Message:

Version 2.0 initial check in.

Location:
per-page-sidebars/trunk
Files:
5 added
4 edited

Legend:

Unmodified
Added
Removed
  • per-page-sidebars/trunk/per-page-sidebars.php

    r280808 r561412  
    44Plugin URI: http://TheCodeCave.com/plugins/per-page-sidebars 
    55Description: Allows the creation and display of custom sidebars for each page on your site. 
    6 Version: 1.0 
     6Version: 2.0.0 
    77Author: Brian Layman 
    88Author URI: http://eHermitsInc.com 
    99License: GPLv2 
    10 Requires: 2.5 
     10Requires: 3.1.0 
    1111Original Design Notes:  
    1212    In an Edit Page screen display a radio of all registered sidebars, save the choice it to postmeta.  
     
    1919*/ 
    2020 
    21 /*  Copyright 2010 Brian Layman (email : brian@ehermitsinc.com) 
     21/*  Copyright 2012 Brian Layman ( email : plugins@ehermitsinc.com ) 
    2222 
    2323        This program is free software; you can redistribute it and/or modify 
     
    3838// Constants 
    3939// ************************************************************ 
    40 define('PPS_KEY_REPLACED', '_tcc-pps-replace'); 
    41 define('SIDEBAR_PREFIX', 'PPS-'); 
     40define( 'PPS_KEY_REPLACED', '_tcc-pps-replace' ); 
     41define( 'SIDEBAR_PREFIX', 'PPS-' ); 
    4242 
    4343// ************************************************************ 
     
    5050function get_pps_post_ids() { 
    5151    global $wpdb; 
    52     $posts_w_sidebars = $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", PPS_KEY_REPLACED) ); 
     52    $posts_w_sidebars = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", PPS_KEY_REPLACED ) ); 
    5353    return $posts_w_sidebars; 
    5454} 
     
    5959//  Used to mark a selection as checked in HTML. 
    6060// ************************************************************ 
    61 function tcc_pps_add_checked($testvalue, $value) { 
    62     if ($testvalue == $value)  
     61function tcc_pps_add_checked( $testvalue, $value ) { 
     62    if ( $testvalue == $value )  
    6363        return ' checked="checked" '; 
    6464} 
     
    7676function pps_add_custom_box() { 
    7777    // This security check is also verified upon save. 
    78     if ( current_user_can('edit_theme_options') ) 
    79         add_meta_box('pps_sectionid', __( 'Custom Sidebar', 'tcc_pps_domain' ), 'pps_inner_custom_box', 'page', 'advanced', 'high' ); 
     78    if ( current_user_can( 'edit_theme_options' ) ) { 
     79        add_meta_box( 'pps_sectionid', __( 'Custom Sidebar', 'tcc_pps_domain' ), 'pps_inner_custom_box', 'post', 'advanced', 'high' ); 
     80        add_meta_box( 'pps_sectionid', __( 'Custom Sidebar', 'tcc_pps_domain' ), 'pps_inner_custom_box', 'page', 'advanced', 'high' ); 
     81    } 
    8082} 
    8183 
     
    8587//  This displays the sidebar replacement selection box 
    8688// ************************************************************ 
    87 function pps_inner_custom_box($post) { 
     89function pps_inner_custom_box( $post ) { 
    8890    global $wp_registered_sidebars; 
    89     $replaced_sidebar=get_post_meta($post->ID, PPS_KEY_REPLACED, true); 
    90     $pps_active_on_post = (isset($wp_registered_sidebars[$replaced_sidebar]) ); 
    91  
    92     echo '<input type="hidden" id="pps_nonce" name="pps_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__) ) . '" />'; 
     91    $replaced_sidebar=get_post_meta( $post->ID, PPS_KEY_REPLACED, true ); 
     92    $pps_active_on_post = ( isset( $wp_registered_sidebars[ $replaced_sidebar ] ) ); 
     93 
     94    echo '<input type="hidden" id="pps_nonce" name="pps_nonce" value="' . wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />'; 
    9395    echo '<input type="checkbox" value="1" id="tcc-pps-active" name="tcc-pps-active" class="tcc-pps-checkbox" ' . 
    94         tcc_pps_add_checked($pps_active_on_post, true) . '/> <label for="tcc-pps-active">' . __('Activate Custom Sidebar?', 'tcc_pps_domain' ) .  
     96        tcc_pps_add_checked( $pps_active_on_post, true ) . '/> <label for="tcc-pps-active">' . __( 'Activate Custom Sidebar?', 'tcc_pps_domain' ) .  
    9597        '</label><br/><br/>'; 
    9698    echo '<div class="tcc-pps-radio-replace-buttons">'; 
    97     echo '  <label for="tcc-pps-replace">' . __('Select a sidebar to replace:', 'tcc_pps_domain' ) . '</label><br/>'; 
     99    echo '  <label for="tcc-pps-replace">' . __( 'Select a sidebar to replace:', 'tcc_pps_domain' ) . '</label><br/>'; 
    98100    // Print the list of sidebars in the drop down 
    99     foreach ($wp_registered_sidebars as $id => $sidebar) { 
    100         $description = $sidebar['description']; 
    101         if ( isset($sidebar['name']) ) { 
    102             $name = $sidebar['name']; 
     101    foreach ( $wp_registered_sidebars as $id => $sidebar ) { 
     102        $description = $sidebar[ 'description' ]; 
     103        if ( isset( $sidebar[ 'name' ] ) ) { 
     104            $name = $sidebar[ 'name' ]; 
    103105        } else { 
    104106            $name = $id; 
     
    106108 
    107109        // Eliminate any self created sidebars or this could get messy. 
    108         if (substr($name, 0, strlen(SIDEBAR_PREFIX) ) != SIDEBAR_PREFIX) { 
     110        if ( substr( $name, 0, strlen( SIDEBAR_PREFIX ) ) != SIDEBAR_PREFIX ) { 
    109111            echo '          <span class="tcc-pps-radio-option"><input type="radio" name="tcc-pps-replace" value="' . $id .  
    110             '" class="tcc-pps-radio" ' . tcc_pps_add_checked($replaced_sidebar, $id) . '/> ' . $name . ' - ' . $description . '</span><br/>'; 
     112            '" class="tcc-pps-radio" ' . tcc_pps_add_checked( $replaced_sidebar, $id ) . '/> ' . $name . ' - ' . $description . '</span><br/>'; 
    111113        } 
    112114    } 
     
    119121//  Recieves the sidebar replacement data when a page is saved. 
    120122// ************************************************************ 
    121 function pps_save_postdata($post_id) { 
     123function pps_save_postdata( $post_id ) { 
    122124    global $wp_registered_sidebars; 
    123     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id; 
     125    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id; 
    124126 
    125127    // This will eventualy need to be adjusted to deal 'edit_CUSTOMPOSTTYPE' permissions as the core does. 
    126128    // Assuming, of course, I extend the plugin to handle more than pages. 
    127     if (('page' == $_POST['post_type'] ) and current_user_can('edit_theme_options')) { 
    128         if(!current_user_can('edit_page', $post_id)) return $post_id; 
     129    if ( ( current_user_can( 'edit_theme_options' ) ) &&  
     130            ( ( ( 'page' == $_POST[ 'post_type' ] ) && current_user_can( 'edit_page', $post_id ) ) || 
     131              ( ( 'post' == $_POST[ 'post_type' ] ) && current_user_can( 'edit_post', $post_id ) ) ) ){ 
     132        // Fall through to the rest of the function 
    129133    } else { 
     134        // Only save on posts/pages if you have the correct rights. Otherwise just return the id without taking an action. 
    130135        return $post_id; 
    131136    } 
     
    133138    // Return with no errors if the NONCE fails. This both blocks hack attacks AND prevents a save from removing 
    134139    // the data if plugin modification adds security checks on meta box display that aren't duplicated here. 
    135     if (!wp_verify_nonce($_POST['pps_nonce'], plugin_basename(__FILE__))) return $post_id; 
    136      
    137     $replaced_sidebar = $_POST['tcc-pps-replace']; 
    138     $pps_active_on_post =  ($_POST['tcc-pps-active'] == 1) && (isset($wp_registered_sidebars[$replaced_sidebar])); 
    139      
    140     if ($pps_active_on_post) { 
    141         update_post_meta($post_id, PPS_KEY_REPLACED, $replaced_sidebar); 
     140    if ( !wp_verify_nonce( $_POST[ 'pps_nonce' ], plugin_basename( __FILE__ ) ) ) return $post_id; 
     141     
     142    $replaced_sidebar = $_POST[ 'tcc-pps-replace' ]; 
     143    $pps_active_on_post =  ( $_POST[ 'tcc-pps-active' ] == 1 ) && ( isset( $wp_registered_sidebars[ $replaced_sidebar ] ) ); 
     144     
     145    if ( $pps_active_on_post ) { 
     146        update_post_meta( $post_id, PPS_KEY_REPLACED, $replaced_sidebar ); 
    142147    } else { 
    143         delete_post_meta($post_id, PPS_KEY_REPLACED); 
     148        delete_post_meta( $post_id, PPS_KEY_REPLACED ); 
    144149    } 
    145150    return $post_id; 
     
    154159function pps_reg_custom_sidebar() { 
    155160    global $wp_query, $wp_registered_sidebars; 
    156     if ( is_page() ) { 
     161    if ( is_page() || is_post() ) { 
    157162        $post = $wp_query->get_queried_object(); 
    158163        $post_id = $post->ID; 
    159164        $post_name = $post->post_name; 
    160165        $post_title = $post->post_title; 
    161         $replaced_sidebar=get_post_meta($post->ID, PPS_KEY_REPLACED, true); 
    162  
    163         $pps_active_on_post = (isset($wp_registered_sidebars[$replaced_sidebar]) ); 
    164         if ($pps_active_on_post) { 
    165             register_sidebar(array( 
     166        $replaced_sidebar=get_post_meta( $post->ID, PPS_KEY_REPLACED, true ); 
     167 
     168        $pps_active_on_post = ( isset( $wp_registered_sidebars[ $replaced_sidebar ] ) ); 
     169        if ( $pps_active_on_post ) { 
     170            register_sidebar( array(  
    166171                'name' => SIDEBAR_PREFIX . $post_name, 
    167172                'id' => SIDEBAR_PREFIX . $post_name, 
    168                 'description' => 'A custom sidebar for the post "' . $post_title . '"')); 
     173                'description' => 'A custom sidebar for the post "' . $post_title . '"' ) ); 
    169174        } 
    170175    } 
     
    180185    $posts = get_pps_post_ids(); 
    181186 
    182     foreach ($posts as $post_id) { 
    183         $post = get_post($post_id); 
    184         register_sidebar(array( 
     187    foreach ( $posts as $post_id ) { 
     188        $post = get_post( $post_id ); 
     189        register_sidebar( array(  
    185190            'name' => SIDEBAR_PREFIX . $post->post_name, 
    186191            'id' => SIDEBAR_PREFIX . $post->post_name, 
    187             'description' => 'A custom sidebar for the post "' . $post->post_title . '"')); 
     192            'description' => 'A custom sidebar for the post "' . $post->post_title . '"' ) ); 
    188193    } 
    189194} 
     
    195200//  the page display. 
    196201// ************************************************************ 
    197 function pps_hijack_sidebars($query) { 
    198     global $post, $wp_registered_sidebars, $_wp_sidebars_widgets; 
    199     $hostpost = $post; 
     202function pps_hijack_sidebars( $query ) { 
     203    global $post, $wp_registered_sidebars, $_wp_sidebars_widgets, $wp_query; 
     204     
     205    if ( $wp_query->is_posts_page ) { 
     206        $hostpost = get_post( $wp_query->get_queried_object_id() ); 
     207    } else { 
     208        $hostpost = $post; 
     209    } 
     210 
    200211    $post_id = $hostpost->ID;    
    201212    $post_name = $hostpost->post_name; 
    202     $host_sidebar = get_post_meta($hostpost->ID, PPS_KEY_REPLACED, true); 
    203     while (($host_sidebar == '') and  ( $hostpost->post_parent > 0 )) { 
    204         $hostpost = get_post($hostpost->post_parent); 
    205         $host_sidebar = get_post_meta($hostpost->ID, PPS_KEY_REPLACED, true); 
     213    $host_sidebar = get_post_meta( $hostpost->ID, PPS_KEY_REPLACED, true ); 
     214 
     215    while ( ( $host_sidebar == '' ) and  ( $hostpost->post_parent > 0 ) ) { 
     216        $hostpost = get_post( $hostpost->post_parent ); 
     217        $host_sidebar = get_post_meta( $hostpost->ID, PPS_KEY_REPLACED, true ); 
    206218        $post_id = $hostpost->ID;     
    207         $post_name = $hostpost->post_name; 
     219        $post_name = $hostpost->post_name;       
    208220    }  
     221     
    209222    $parasite_sidebar = SIDEBAR_PREFIX . $post_name; 
    210     if (isset($_wp_sidebars_widgets[$parasite_sidebar])) { 
    211         $_wp_sidebars_widgets[$host_sidebar] = $_wp_sidebars_widgets[$parasite_sidebar]; 
     223    if ( isset( $_wp_sidebars_widgets[ $parasite_sidebar ] ) ) { 
     224        $_wp_sidebars_widgets[ $host_sidebar ] = $_wp_sidebars_widgets[ $parasite_sidebar ]; 
    212225    } 
    213226} 
     
    217230// ************************************************************ 
    218231// Add the edit field to the Page Edit screens 
    219 add_action('admin_menu', 'pps_add_custom_box');  
     232add_action( 'admin_menu', 'pps_add_custom_box' );  
    220233 
    221234// Process the custom PPS values submitted when saving pages. 
    222 add_action('save_post', 'pps_save_postdata'); 
     235add_action( 'save_post', 'pps_save_postdata' ); 
    223236 
    224237// Register the custom sidebars that have been created for the pages. 
    225 add_action('admin_init', 'pps_reg_all_custom_sidebars'); 
     238add_action( 'admin_init', 'pps_reg_all_custom_sidebars' ); 
    226239 
    227240// When a page is dipslayed, check for a custom sidebar and if it exists hijack the standard sidebar 
    228241add_filter( 'wp', 'pps_hijack_sidebars' );   
    229  
    230 ?> 
  • per-page-sidebars/trunk/readme.txt

    r280808 r561412  
    22Contributors: BrianLayman 
    33Tags: posts, pages, sidebars, widgets 
    4 Donate: http://thecodecave.com/did 
    5 Requires at least: 2.5 
    6 Tested up to: 3.0.1 
    7 Stable tag: 1.0 
     4Donate: http://thecodecave.com/donate 
     5Requires at least: 3.1 
     6Tested up to: 3.4 
     7Stable tag: 2.0.0 
    88 
    99The Per Page Sidebars (PPS) plugin allows blog administrators to create a unique sidebar for each Page. No template editing is required. 
    1010 
    1111== Description == 
    12 This plugin allows the creation and display of custom sidebars for any page on your site. On each page, you can choose which of your current theme's sidebars will be replaced. The replacement works for all descendants of a replaced page.  
     12This plugin allows the creation and display of custom sidebars for any page (or post) on your site. On each page, you can choose which of your current theme's sidebars will be replaced. The replacement works for all descendants of a replaced page.  
    1313 
    1414In this fashion you can: 
     
    2121== Installation == 
    2222 
    23 1. Upload the `tcc-pps` directory to the `/wp-content/plugins/` directory. 
    24 2. Activate the plugin through the 'Plugins' menu in WordPress. 
    25 3. Modify the display settings as needed on the Settings > Per Page Sidebar page. 
     23The installation process. 
     24 
     251. Upload  to the `/wp-content/plugins/` directory. Or directly upload from your Plugin management page. 
     262. Activate the plugin through the 'Plugins' menu in WordPress 
    2627 
    2728== Screenshots == 
    2829 
    29 1. custom_sidebar.png 
    30 2. widget-box.png 
     301. This is the metabox that appears on any page or post you are editing. It shows a list of all sidebars/widget areas built into the current theme. To replace one of them with a custom sidebar, you just need to mark the sidebar you are to replace. 
     31 
     322. Under Appearance->Widgets, for each page or post on you've activated a custom sidebar, you will see a widget area like this one - named after the post or page on which it will be used. 
     33 
     343. This plugin adds a "Custom Sidebar" metabox to the edit screens for pages and posts. Leaving the "Activate Custom Sidebar" box unchecked means that the plugin does not change the behavior of that page/post. 
     35 
     364. This screenshot shows a typical blog running the 2010 theme and displaying the default sidebar. 
     37 
     385. On the edit screen for any page or post, to create a custom sidebar, simply check "Activate Custom Sidebar" and choose the sidebar you will be replacing. 
     39 
     406. When you activate a custom sidebar on a page or post, the Appearance->Widgets screen gets a widget area created for that page or post. 
     41 
     427. Any widgets you put in the custom sidebar's widget area will be displayed with that page or post, completely replacing the chosen sidebar. 
    3143 
    3244== Use == 
     
    3749 
    3850That’s it. No code. No CSS changes. No custom templates. 
     51 
     52This video describes the usage of the plugin: 
     53 `[youtube http://www.youtube.com/watch?v=_U2XfrU7nZQ]` 
    3954 
    4055== Frequently Asked Questions == 
     
    4863 
    4964= Can I replace sidebars on posts or custom post types? =  
    50 Currently only one pages can have their sidebars replaced.  If this is a disired feature, leave comments requesting it and I can add the feature fairly easily. 
     65YES! Version 2 of the plugin allows sidebars on posts.  However, custom post types are not yet supported.  If this is a disired feature, leave comments requesting it and I can add the feature fairly easily. 
    5166 
    5267= Can my authors & contributors replace the sidebar on a page? =  
     
    5570== Changelog == 
    5671 
    57 = 0.1 = 
     72= 1.0.0 = 
     73 
    5874* Initial release. 
     75 
     76= 2.0.0 = 
     77 
     78* Added the ability to activate custom sidebars on posts 
     79* Fixed a bug that caused the wrong custom sidebar to be displayed on the page that was assigned as the Post page 
     80* Added screenshots and video to the documentation of the plugin 
Note: See TracChangeset for help on using the changeset viewer.