Plugin Directory

Changeset 1493317


Ignore:
Timestamp:
09/09/2016 03:03:21 PM (10 years ago)
Author:
ShinichiN
Message:

Version 0.7. Now you can switch sidebar post type wise.

Location:
dynamically-dynamic-sidebar
Files:
1 added
9 edited
13 copied

Legend:

Unmodified
Added
Removed
  • dynamically-dynamic-sidebar/tags/0.7/admin-main.php

    r1417566 r1493317  
    11<?php
    22
    3 // ページとメニューの登録
     3/**
     4 * Register the admin page and the menu to it.
     5 *
     6 */
    47add_action( 'admin_menu', 'dds_add_theme_page' );
    58function dds_add_theme_page() {
    69    add_theme_page(
    7         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
    8         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
     10        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
     11        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
    912        'edit_theme_options',
    1013        'dynamically-dynamic-sidebar',
     
    1316}
    1417
    15 // 管理画面の出力
     18/**
     19 * Outputs the admin page.
     20 * Processes the posted data.
     21 *
     22 */
    1623function dds_output_admin_panel() {
    1724
    18     // post された
     25    // Anything $_POST ed
    1926    if ( ! empty( $_POST ) ) {
    2027
    21         // リファラをチェック
     28        // Let's do check
    2229        check_admin_referer( 'dynamically-dynamic-sidebar' );
    2330
    24         // ポストされてきたもの
     31        // Posted values.
    2532        $posted_sidebars = $_POST["dds-widget-areas"];
    2633
    27         $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // から削除
    28         $posted_sidebars = array_unique( $posted_sidebars ); // 重複排除
    29         $posted_sidebars = array_values( $posted_sidebars ); // キーがジャンプするのを防ぐ
    30         $posted_sidebars = stripslashes_deep( $posted_sidebars ); // スラッシュ問題の解決
     34        $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // Strip null, false, 0, and empty.
     35        $posted_sidebars = array_unique( $posted_sidebars ); // Strip the doubled ones.
     36        $posted_sidebars = array_values( $posted_sidebars ); // No jumps for the keys.
     37        $posted_sidebars = stripslashes_deep( $posted_sidebars ); // Handler for the quotes escaping slashes.
    3138
    3239        $dds_sidebars = array();
    3340        foreach ( $posted_sidebars as $ps ) {
     41
    3442            $key = sanitize_title( $ps );
    3543            $val = esc_attr( $ps );
    3644            $dds_sidebars[$key] = $val;
    37         }
    38 
    39         // 保存
     45
     46        }
     47
     48        // Save the data.
    4049        if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) {
     50
    4151            update_option( 'dds_sidebars', $dds_sidebars );
     52
    4253        }
    4354
    4455        $posted_target = $_POST["dds_target_widget_area"];
    4556        if ( $posted_target ) {
     57
    4658            update_option( 'dds_target_widget_area', $posted_target );
     59
    4760        } else {
     61
    4862            delete_option( 'dds_target_widget_area' );
     63
     64        }
     65
     66        $posted_widget_area_for_post_types = $_POST["dds_area_for_post_types"];
     67        if ( $posted_widget_area_for_post_types ) {
     68
     69            update_option( 'dds_area_for_post_types', $posted_widget_area_for_post_types );
     70
     71        } else {
     72
     73            delete_option( 'dds_area_for_post_types' );
     74
    4975        }
    5076
     
    5379    $dds_sidebars = get_option( 'dds_sidebars' );
    5480    $dds_target   = get_option( 'dds_target_widget_area' );
     81    $dds_a_f_pts  = get_option( 'dds_area_for_post_types' );
     82
     83
    5584?>
    5685<div class="wrap">
     
    6190    <tbody>
    6291        <tr>
    63             <th scope="row"><?php _e( 'Target widget area to switch', 'dynamically-dynamic-sidebar' ); ?></th>
     92            <th scope="row"><?php _e( 'Target Widget Area', 'dynamically-dynamic-sidebar' ); ?></th>
    6493            <td>
     94                <p class="description">Choose the widget area to switch with custom widget areas.</p>
     95                <br />
    6596                <label for="dds_target_widget_area">
    6697                    <select name="dds_target_widget_area" id="dds_target_widget_area">
     
    69100                            global $wp_registered_sidebars;
    70101
    71                             // here's the list of the widget areas.
     102                            // Here's the list of the widget areas.
    72103                            $registered = $wp_registered_sidebars;
    73                             // we want only the ones registered by other theme(plugins)l
     104                            // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin.
    74105                            foreach ( $dds_sidebars as $key => $val ) {
    75106                                unset( $registered[$key] );
     
    93124        </tr>
    94125        <tr>
    95             <th scope="row"><?php _e( 'Manage Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     126            <th scope="row"><?php _e( 'Custom Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     127            <td style="vertical-align: top;" width="25%">
     128                <?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?> <label for="dds-new"><input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     129            </td>
    96130            <td>
    97131                <?php
    98132                if ( $dds_sidebars ) {
     133                    ?>
     134                    <p class="description">Add and edit the name of custom widget areas.</p>
     135                    <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
     136                    <br />
     137                    <?php
    99138                    foreach ( $dds_sidebars as $key => $val ) {
    100139                        ?>
     
    102141                        <?php
    103142                    }
    104                     ?>
    105                     <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
    106                     <?php
    107143                } else {
    108144                    _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' );
     
    112148        </tr>
    113149        <tr>
    114             <th scope="row"><?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?></th>
     150            <th scope="row"><?php _e( 'Post type Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
    115151            <td>
    116                 <label for="dds-new"><?php _e( 'Add New', 'dynamically-dynamic-sidebar' ); ?> <input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     152                <p class="description">Assign widget areas to your post types.</p>
     153                <br />
     154                <?php
     155                    // https://developer.wordpress.org/reference/functions/get_post_types/
     156                    $args = array(
     157                        'public' => true,
     158                    );
     159                    $registered_post_types = get_post_types(
     160                        $args,
     161                        "object",
     162                        "and"
     163                    );
     164                    unset( $registered_post_types["attachment"] );
     165
     166                    echo "<table>";
     167
     168                    foreach ( $registered_post_types as $key => $val ) {
     169                        ?>
     170                        <tr>
     171                            <th>
     172                                <label for="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]"><?php echo esc_attr( $val->label ); ?></label>
     173                            </th>
     174                            <td>
     175                                <select name="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]" id="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]">
     176                                    <option value="dds-default"><?php _e( 'Default', 'dynamically-dynamic-sidebar' ); ?></option>
     177                                    <?php foreach ( $dds_sidebars as $dds_key => $dds_val ) {
     178                                        ?>
     179                                        <option
     180                                            value="<?php echo esc_attr( $dds_key ); ?>"
     181                                            <?php
     182                                            if ( isset($dds_a_f_pts[$key]) && esc_attr( $dds_a_f_pts[$key] ) === $dds_key ) {
     183                                            ?>
     184                                                selected="selected"
     185                                            <?php
     186                                            }
     187                                            ?>
     188                                        ><?php echo esc_html( $dds_val ); ?></option>
     189                                        <?php
     190                                    } ?>
     191                                </select>
     192                            </td>
     193                        </tr>
     194                        <?php
     195                    }
     196                    echo "</table>";
     197                ?>
    117198            </td>
    118199        </tr>
    119200    </tbody>
    120201</table>
    121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widget settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
     202<p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
    122203<?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?>
    123204</form>
  • dynamically-dynamic-sidebar/tags/0.7/admin-post.php

    r1417567 r1493317  
    11<?php
    22
     3/**
     4 * Adds metabox to the post editor.
     5 *
     6 */
    37add_action( 'add_meta_boxes', 'dds_add_meta_box' );
    48function dds_add_meta_box() {
     
    2630}
    2731
    28 // ポスト用のメタボックス
     32/**
     33 * The output of the metabox.
     34 *
     35 */
    2936function dds_post_meta_boxes( $post ) {
    3037
     
    3239    $the_area   = get_post_meta( $post->ID, 'dds_widget_area', true );
    3340
     41    // This outputs an alert or an info in the contexts
     42    // , where the assigned term has an sidebar widget area.
    3443    dds_alert_term_widget( $post );
    3544
     
    5160}
    5261
     62/**
     63 * Used in the meta box for a post,
     64 * this function inform users that the
     65 * post will have a custom widget area
     66 * set by the term it's allocated.
     67 *
     68 */
    5369function dds_alert_term_widget( $post ) {
    5470
     
    5773    if ( $widget_by_term ) {
    5874
    59         $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to <strong>"%2$s"</strong> in <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
     75        $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to the term <strong>"%2$s"</strong> of the taxonomy <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
    6076
    6177        printf(
     
    6682        );
    6783
    68         echo '<p class="dds-notice">You can override it by choosing another one here.</p>';
     84        echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>';
    6985
    7086    } else {
     
    7692}
    7793
    78 
     94/**
     95 * Save the data sent from the meta box.
     96 *
     97 */
    7998add_action( 'save_post', 'dds_save_post_widget' );
    8099function dds_save_post_widget( $post_id ) {
     
    103122}
    104123
     124
     125/**
     126 * In the list table of the posts/pages/custom post types,
     127 * the widget areas will be shown.
     128 *
     129 */
     130
    105131// posts and custom post types
    106132add_filter( 'manage_posts_columns',            'dds_add_posts_table_column'       );
     
    108134
    109135// page
    110 add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10);
    111 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2);
     136add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10 );
     137add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells',  10, 2 );
    112138
    113139function dds_add_posts_table_column( $columns ) {
     
    122148    }
    123149
     150    // When the area comes from the post
    124151    $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true );
    125152    $widget_name    = dds_get_widget_name_by_id($widget_by_post);
     
    130157
    131158    } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) {
     159        // when the area comes from a term.
    132160
    133161        $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )';
  • dynamically-dynamic-sidebar/tags/0.7/admin-term.php

    r1417566 r1493317  
    22
    33// 各タクソノミで出力と保存をするためにフックで登録
     4/**
     5 * Hooks the actions to display and save
     6 * fields on the term list/edit screens.
     7 *
     8 */
    49add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' );
    510function dds_do_meta_for_all_taxonomies() {
     
    813
    914    foreach ( $taxonomies as $t ) {
    10         add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields' );  // 新規追加時に出力
    11         add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // 編集時に出力
    12         add_action( 'created_' . $t, 'dds_update_term_meta' );              // 新規追加時に実行
    13         add_action( 'edited_'  . $t, 'dds_update_term_meta' );              // 編集時に実行
     15        add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields'  ); // output on add screen
     16        add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // output on edit screen
     17        add_action( 'created_' . $t,         'dds_update_term_meta'      ); // fires on add screen
     18        add_action( 'edited_'  . $t,         'dds_update_term_meta'      ); // fires on edit screen
    1419    }
    1520
     
    1722
    1823
    19 // タームの編集画面にフォームを追加
     24/**
     25 * Output the form on edit screen.
     26 *
     27 */
    2028function dds_term_edit_meta_fields( $taxonomy ) {
    2129
     
    4755}
    4856
    49 // タームの新規追加画面にフォームを追加
     57/**
     58 * Output the form on add screen.
     59 *
     60 */
    5061function dds_term_add_meta_fields( $taxonomy ) {
    5162
     
    6879
    6980
    70 // タームメタの保存
     81/**
     82 * Save the term meta on save.
     83 *
     84 */
    7185function dds_update_term_meta( $term_id ) {
    7286
     
    94108}
    95109
    96 // タームのリストに割り当てられたウィジェットを出す
     110/**
     111 * Add a column to the list table of the terms.
     112 *
     113 */
    97114add_action( 'admin_init', 'dds_fire_term_table_funcs' );
    98115function dds_fire_term_table_funcs() {
     
    103120        add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身
    104121    }
     122
    105123}
    106124
     125/**
     126 * Output the th
     127 */
    107128function dds_add_term_table_column( $columns ) {
    108     $columns["dds_widget_column"] = "Sidebar";
     129
     130    $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' );
    109131    return $columns;
     132
    110133}
    111134
     135/**
     136 * Output the allocated area name in the cells.
     137 */
    112138function dds_add_term_table_cells( $content, $column_name, $term_id ) {
     139
    113140    if ( 'dds_widget_column' === $column_name ) {
    114141        $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true );
     
    118145        }
    119146    }
     147
    120148    return $content;
     149
    121150}
    122 
    123 
    124 
    125 
    126 
    127 
  • dynamically-dynamic-sidebar/tags/0.7/dynamically-dynamic-sidebar.php

    r1417575 r1493317  
    22/*
    33Plugin Name: Dynamically Dynamic Sidebar
    4 Version: 0.6
     4Version: 0.7
    55Description: This plugin enables you to create unlimited widget area and use them for posts, pages, categories, tags and so on.
    66Author: Shinichi Nishikawa
     
    1313$dds_wp_version = $GLOBALS['wp_version'];
    1414
     15// This plugin requires WP version greater than 4.4.
    1516if ( version_compare( (float)$dds_wp_version, '4.4', '<' ) ) {
     17
    1618    add_action( 'admin_notices', 'dds_admin_error_notice' );
    1719    return false;
     20
    1821} else {
    1922
     
    2932
    3033function dds_admin_error_notice() {
    31     ?><div class="error"><p><?php _e( '"Dynamically Dynamic Sidebar" plugin utilizes Term Meta API which was introduced in WordPress version 4.4. You need to upgrade your WordPress to activate the plugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php
     34    ?><div class="error"><p><?php _e( '"Dynamically Dynamic Sidebar" plugin utilizes Term Meta API, which was introduced in WordPress version 4.4. You need to upgrade your WordPress to use this plugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php
    3235}
  • dynamically-dynamic-sidebar/tags/0.7/functions.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Returns the taxonomies which are public.
     5 *
     6 */
    37function dds_get_taxonomies() {
    48
     
    1822}
    1923
     24
     25
    2026/**
    21  * Returns an array of area FIRST FOUND.
     27 * Given the post ID, looking into the post type of the post,
     28 * returns an array of the area FIRST FOUND.
     29 * If no area is assigned, returns false.
     30 *
     31 * @param  int   $post          post id
     32 * @return array $area_term_arr an array of widget_id, widget_name
     33                                and term object detemining the widget id.
     34 */
     35function dds_get_widget_of_post_type( $post ) {
     36
     37    if ( ! $post ) {
     38        return false;
     39    }
     40
     41    $dds_post_type = $post->post_type;
     42    $registered_posttypes_widgets = get_option( "dds_area_for_post_types" );
     43    if ( isset( $registered_posttypes_widgets[$dds_post_type] ) ) {
     44        $by_post_type = $registered_posttypes_widgets[$dds_post_type];
     45    } else {
     46        $by_post_type = false;
     47    }
     48
     49    return $by_post_type;
     50
     51
     52}
     53
     54
     55/**
     56 * Given the post ID, looking into the terms of the post,
     57 * returns an array of the area FIRST FOUND.
    2258 * If no area is assigned, returns false.
    2359 *
     
    2864function dds_get_widget_of_post_by_term( $post ) {
    2965
    30     // マスター
     66    // Init
    3167    $terms         = array();
    3268    $ancestors     = array();
    3369    $area_term_arr = array();
    3470
    35     // 登録されているタクソノミ
     71    // Getting all the taxonomies, which are public and has the UI.
    3672    $taxonomies = dds_get_taxonomies();
    3773
    38     // 各タクソノミのタームを取得
     74    // Loop through all the taxonomy terms.
    3975    foreach ( $taxonomies as $taxonomy ) {
    4076
    41         $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得
     77        $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms.
    4278
    4379        // タームに属していればマスターに入れる
     
    4884    }
    4985
    50     // 直接のタームをチェック
     86    // Pass the term array and get the widget area back.
    5187    $area_term_arr = dds_check_term_arrays_allocated_area( $terms );
    5288
     
    5591    }
    5692
    57     // タームの親をチェックしないと。。
     93    // Check the ancestors too.
    5894    foreach ( $terms as $t ) {
    5995
     
    69105    }
    70106
     107    // Pass the parent term array and get the widget area back.
    71108    $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors );
    72109
     
    84121 *
    85122 *
    86  *
    87123 * @param  int|object $terms         term id or term object
    88124 * @return array      $area_term_arr $area_term_arr an array of widget_id, widget_name
     
    97133    foreach ( $terms as $t ) {
    98134
    99         // object でも id でも id として扱う
     135        // Let's handle ids only.
    100136        if ( is_object( $t ) ) {
    101137            $term_id = $t->term_id;
     
    105141
    106142        $area_id = get_term_meta( $term_id, 'dds_widget_area', true );
    107         // もしあれば、配列を作って、ループを抜ける
    108         if ( $area_id ) {
    109143
    110             // ユーザ作成のエリア
     144        if ( $area_id ) { // if exists, create an array and escape from the loop.
     145
     146            // the array of the user custom areas.
    111147            $allocatable_widgets  = get_option( 'dds_sidebars' );
    112148
  • dynamically-dynamic-sidebar/tags/0.7/inc/admin-main.php

    r1287029 r1493317  
    11<?php
    22
    3 // ページとメニューの登録
     3/**
     4 * Register the admin page and the menu to it.
     5 *
     6 */
    47add_action( 'admin_menu', 'dds_add_theme_page' );
    58function dds_add_theme_page() {
    69    add_theme_page(
    7         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
    8         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
     10        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
     11        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
    912        'edit_theme_options',
    1013        'dynamically-dynamic-sidebar',
     
    1316}
    1417
    15 // 管理画面の出力
     18/**
     19 * Outputs the admin page.
     20 * Processes the posted data.
     21 *
     22 */
    1623function dds_output_admin_panel() {
    1724
    18     // post された
     25    // Anything $_POST ed
    1926    if ( ! empty( $_POST ) ) {
    2027
    21         // リファラをチェック
     28        // Let's do check
    2229        check_admin_referer( 'dynamically-dynamic-sidebar' );
    2330
    24         // ポストされてきたもの
     31        // Posted values.
    2532        $posted_sidebars = $_POST["dds-widget-areas"];
    2633
    27         $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // から削除
    28         $posted_sidebars = array_unique( $posted_sidebars ); // 重複排除
    29         $posted_sidebars = array_values( $posted_sidebars ); // キーがジャンプするのを防ぐ
    30         $posted_sidebars = stripslashes_deep( $posted_sidebars ); // スラッシュ問題の解決
     34        $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // Strip null, false, 0, and empty.
     35        $posted_sidebars = array_unique( $posted_sidebars ); // Strip the doubled ones.
     36        $posted_sidebars = array_values( $posted_sidebars ); // No jumps for the keys.
     37        $posted_sidebars = stripslashes_deep( $posted_sidebars ); // Handler for the quotes escaping slashes.
    3138
    3239        $dds_sidebars = array();
    3340        foreach ( $posted_sidebars as $ps ) {
     41
    3442            $key = sanitize_title( $ps );
    3543            $val = esc_attr( $ps );
    3644            $dds_sidebars[$key] = $val;
    37         }
    38 
    39         // 保存
     45
     46        }
     47
     48        // Save the data.
    4049        if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) {
     50
    4151            update_option( 'dds_sidebars', $dds_sidebars );
     52
    4253        }
    4354
    4455        $posted_target = $_POST["dds_target_widget_area"];
    4556        if ( $posted_target ) {
     57
    4658            update_option( 'dds_target_widget_area', $posted_target );
     59
    4760        } else {
     61
    4862            delete_option( 'dds_target_widget_area' );
     63
     64        }
     65
     66        $posted_widget_area_for_post_types = $_POST["dds_area_for_post_types"];
     67        if ( $posted_widget_area_for_post_types ) {
     68
     69            update_option( 'dds_area_for_post_types', $posted_widget_area_for_post_types );
     70
     71        } else {
     72
     73            delete_option( 'dds_area_for_post_types' );
     74
    4975        }
    5076
     
    5379    $dds_sidebars = get_option( 'dds_sidebars' );
    5480    $dds_target   = get_option( 'dds_target_widget_area' );
     81    $dds_a_f_pts  = get_option( 'dds_area_for_post_types' );
     82
     83
    5584?>
    5685<div class="wrap">
     
    6190    <tbody>
    6291        <tr>
    63             <th scope="row"><?php _e( 'Target widget area to switch', 'dynamically-dynamic-sidebar' ); ?></th>
     92            <th scope="row"><?php _e( 'Target Widget Area', 'dynamically-dynamic-sidebar' ); ?></th>
    6493            <td>
     94                <p class="description">Choose the widget area to switch with custom widget areas.</p>
     95                <br />
    6596                <label for="dds_target_widget_area">
    6697                    <select name="dds_target_widget_area" id="dds_target_widget_area">
     
    69100                            global $wp_registered_sidebars;
    70101
    71                             // here's the list of the widget areas.
     102                            // Here's the list of the widget areas.
    72103                            $registered = $wp_registered_sidebars;
    73                             // we want only the ones registered by other theme(plugins)l
     104                            // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin.
    74105                            foreach ( $dds_sidebars as $key => $val ) {
    75106                                unset( $registered[$key] );
     
    93124        </tr>
    94125        <tr>
    95             <th scope="row"><?php _e( 'Manage Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     126            <th scope="row"><?php _e( 'Custom Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     127            <td style="vertical-align: top;" width="25%">
     128                <?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?> <label for="dds-new"><input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     129            </td>
    96130            <td>
    97131                <?php
    98132                if ( $dds_sidebars ) {
     133                    ?>
     134                    <p class="description">Add and edit the name of custom widget areas.</p>
     135                    <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
     136                    <br />
     137                    <?php
    99138                    foreach ( $dds_sidebars as $key => $val ) {
    100139                        ?>
     
    102141                        <?php
    103142                    }
    104                     ?>
    105                     <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
    106                     <?php
    107143                } else {
    108144                    _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' );
     
    112148        </tr>
    113149        <tr>
    114             <th scope="row"><?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?></th>
     150            <th scope="row"><?php _e( 'Post type Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
    115151            <td>
    116                 <label for="dds-new"><?php _e( 'Add New', 'dynamically-dynamic-sidebar' ); ?> <input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     152                <p class="description">Assign widget areas to your post types.</p>
     153                <br />
     154                <?php
     155                    // https://developer.wordpress.org/reference/functions/get_post_types/
     156                    $args = array(
     157                        'public' => true,
     158                    );
     159                    $registered_post_types = get_post_types(
     160                        $args,
     161                        "object",
     162                        "and"
     163                    );
     164                    unset( $registered_post_types["attachment"] );
     165
     166                    echo "<table>";
     167
     168                    foreach ( $registered_post_types as $key => $val ) {
     169                        ?>
     170                        <tr>
     171                            <th>
     172                                <label for="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]"><?php echo esc_attr( $val->label ); ?></label>
     173                            </th>
     174                            <td>
     175                                <select name="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]" id="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]">
     176                                    <option value="dds-default"><?php _e( 'Default', 'dynamically-dynamic-sidebar' ); ?></option>
     177                                    <?php foreach ( $dds_sidebars as $dds_key => $dds_val ) {
     178                                        ?>
     179                                        <option
     180                                            value="<?php echo esc_attr( $dds_key ); ?>"
     181                                            <?php
     182                                            if ( isset($dds_a_f_pts[$key]) && esc_attr( $dds_a_f_pts[$key] ) === $dds_key ) {
     183                                            ?>
     184                                                selected="selected"
     185                                            <?php
     186                                            }
     187                                            ?>
     188                                        ><?php echo esc_html( $dds_val ); ?></option>
     189                                        <?php
     190                                    } ?>
     191                                </select>
     192                            </td>
     193                        </tr>
     194                        <?php
     195                    }
     196                    echo "</table>";
     197                ?>
    117198            </td>
    118199        </tr>
    119200    </tbody>
    120201</table>
    121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widget settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
     202<p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
    122203<?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?>
    123204</form>
  • dynamically-dynamic-sidebar/tags/0.7/inc/admin-post.php

    r1417567 r1493317  
    11<?php
    22
     3/**
     4 * Adds metabox to the post editor.
     5 *
     6 */
    37add_action( 'add_meta_boxes', 'dds_add_meta_box' );
    48function dds_add_meta_box() {
     
    2630}
    2731
    28 // ポスト用のメタボックス
     32/**
     33 * The output of the metabox.
     34 *
     35 */
    2936function dds_post_meta_boxes( $post ) {
    3037
     
    3239    $the_area   = get_post_meta( $post->ID, 'dds_widget_area', true );
    3340
     41    // This outputs an alert or an info in the contexts
     42    // , where the assigned term has an sidebar widget area.
    3443    dds_alert_term_widget( $post );
    3544
     
    5160}
    5261
     62/**
     63 * Used in the meta box for a post,
     64 * this function inform users that the
     65 * post will have a custom widget area
     66 * set by the term it's allocated.
     67 *
     68 */
    5369function dds_alert_term_widget( $post ) {
    5470
     
    5773    if ( $widget_by_term ) {
    5874
    59         $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to <strong>"%2$s"</strong> in <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
     75        $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to the term <strong>"%2$s"</strong> of the taxonomy <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
    6076
    6177        printf(
     
    6682        );
    6783
    68         echo '<p class="dds-notice">You can override it by choosing another one here.</p>';
     84        echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>';
    6985
    7086    } else {
     
    7692}
    7793
    78 
     94/**
     95 * Save the data sent from the meta box.
     96 *
     97 */
    7998add_action( 'save_post', 'dds_save_post_widget' );
    8099function dds_save_post_widget( $post_id ) {
     
    103122}
    104123
     124
     125/**
     126 * In the list table of the posts/pages/custom post types,
     127 * the widget areas will be shown.
     128 *
     129 */
     130
    105131// posts and custom post types
    106132add_filter( 'manage_posts_columns',            'dds_add_posts_table_column'       );
     
    108134
    109135// page
    110 add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10);
    111 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2);
     136add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10 );
     137add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells',  10, 2 );
    112138
    113139function dds_add_posts_table_column( $columns ) {
     
    122148    }
    123149
     150    // When the area comes from the post
    124151    $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true );
    125152    $widget_name    = dds_get_widget_name_by_id($widget_by_post);
     
    130157
    131158    } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) {
     159        // when the area comes from a term.
    132160
    133161        $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )';
  • dynamically-dynamic-sidebar/tags/0.7/inc/admin-term.php

    r1287029 r1493317  
    22
    33// 各タクソノミで出力と保存をするためにフックで登録
     4/**
     5 * Hooks the actions to display and save
     6 * fields on the term list/edit screens.
     7 *
     8 */
    49add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' );
    510function dds_do_meta_for_all_taxonomies() {
     
    813
    914    foreach ( $taxonomies as $t ) {
    10         add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields' );  // 新規追加時に出力
    11         add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // 編集時に出力
    12         add_action( 'created_' . $t, 'dds_update_term_meta' );              // 新規追加時に実行
    13         add_action( 'edited_'  . $t, 'dds_update_term_meta' );              // 編集時に実行
     15        add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields'  ); // output on add screen
     16        add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // output on edit screen
     17        add_action( 'created_' . $t,         'dds_update_term_meta'      ); // fires on add screen
     18        add_action( 'edited_'  . $t,         'dds_update_term_meta'      ); // fires on edit screen
    1419    }
    1520
     
    1722
    1823
    19 // タームの編集画面にフォームを追加
     24/**
     25 * Output the form on edit screen.
     26 *
     27 */
    2028function dds_term_edit_meta_fields( $taxonomy ) {
    2129
     
    4755}
    4856
    49 // タームの新規追加画面にフォームを追加
     57/**
     58 * Output the form on add screen.
     59 *
     60 */
    5061function dds_term_add_meta_fields( $taxonomy ) {
    5162
     
    6879
    6980
    70 // タームメタの保存
     81/**
     82 * Save the term meta on save.
     83 *
     84 */
    7185function dds_update_term_meta( $term_id ) {
    7286
     
    94108}
    95109
    96 // タームのリストに割り当てられたウィジェットを出す
     110/**
     111 * Add a column to the list table of the terms.
     112 *
     113 */
    97114add_action( 'admin_init', 'dds_fire_term_table_funcs' );
    98115function dds_fire_term_table_funcs() {
     
    103120        add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身
    104121    }
     122
    105123}
    106124
     125/**
     126 * Output the th
     127 */
    107128function dds_add_term_table_column( $columns ) {
    108     $columns["dds_widget_column"] = "Sidebar";
     129
     130    $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' );
    109131    return $columns;
     132
    110133}
    111134
     135/**
     136 * Output the allocated area name in the cells.
     137 */
    112138function dds_add_term_table_cells( $content, $column_name, $term_id ) {
     139
    113140    if ( 'dds_widget_column' === $column_name ) {
    114141        $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true );
     
    118145        }
    119146    }
     147
    120148    return $content;
     149
    121150}
    122 
    123 
    124 
    125 
    126 
    127 
  • dynamically-dynamic-sidebar/tags/0.7/inc/functions.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Returns the taxonomies which are public.
     5 *
     6 */
    37function dds_get_taxonomies() {
    48
     
    1822}
    1923
     24
     25
    2026/**
    21  * Returns an array of area FIRST FOUND.
     27 * Given the post ID, looking into the post type of the post,
     28 * returns an array of the area FIRST FOUND.
     29 * If no area is assigned, returns false.
     30 *
     31 * @param  int   $post          post id
     32 * @return array $area_term_arr an array of widget_id, widget_name
     33                                and term object detemining the widget id.
     34 */
     35function dds_get_widget_of_post_type( $post ) {
     36
     37    if ( ! $post ) {
     38        return false;
     39    }
     40
     41    $dds_post_type = $post->post_type;
     42    $registered_posttypes_widgets = get_option( "dds_area_for_post_types" );
     43    if ( isset( $registered_posttypes_widgets[$dds_post_type] ) ) {
     44        $by_post_type = $registered_posttypes_widgets[$dds_post_type];
     45    } else {
     46        $by_post_type = false;
     47    }
     48
     49    return $by_post_type;
     50
     51
     52}
     53
     54
     55/**
     56 * Given the post ID, looking into the terms of the post,
     57 * returns an array of the area FIRST FOUND.
    2258 * If no area is assigned, returns false.
    2359 *
     
    2864function dds_get_widget_of_post_by_term( $post ) {
    2965
    30     // マスター
     66    // Init
    3167    $terms         = array();
    3268    $ancestors     = array();
    3369    $area_term_arr = array();
    3470
    35     // 登録されているタクソノミ
     71    // Getting all the taxonomies, which are public and has the UI.
    3672    $taxonomies = dds_get_taxonomies();
    3773
    38     // 各タクソノミのタームを取得
     74    // Loop through all the taxonomy terms.
    3975    foreach ( $taxonomies as $taxonomy ) {
    4076
    41         $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得
     77        $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms.
    4278
    4379        // タームに属していればマスターに入れる
     
    4884    }
    4985
    50     // 直接のタームをチェック
     86    // Pass the term array and get the widget area back.
    5187    $area_term_arr = dds_check_term_arrays_allocated_area( $terms );
    5288
     
    5591    }
    5692
    57     // タームの親をチェックしないと。。
     93    // Check the ancestors too.
    5894    foreach ( $terms as $t ) {
    5995
     
    69105    }
    70106
     107    // Pass the parent term array and get the widget area back.
    71108    $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors );
    72109
     
    84121 *
    85122 *
    86  *
    87123 * @param  int|object $terms         term id or term object
    88124 * @return array      $area_term_arr $area_term_arr an array of widget_id, widget_name
     
    97133    foreach ( $terms as $t ) {
    98134
    99         // object でも id でも id として扱う
     135        // Let's handle ids only.
    100136        if ( is_object( $t ) ) {
    101137            $term_id = $t->term_id;
     
    105141
    106142        $area_id = get_term_meta( $term_id, 'dds_widget_area', true );
    107         // もしあれば、配列を作って、ループを抜ける
    108         if ( $area_id ) {
    109143
    110             // ユーザ作成のエリア
     144        if ( $area_id ) { // if exists, create an array and escape from the loop.
     145
     146            // the array of the user custom areas.
    111147            $allocatable_widgets  = get_option( 'dds_sidebars' );
    112148
  • dynamically-dynamic-sidebar/tags/0.7/inc/main.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Get the registered custome sidebar areas.
     5 *
     6 * With this registration,
     7 *
     8 *
     9 * https://developer.wordpress.org/reference/functions/register_sidebar/
     10 *
     11 */
    312add_action( 'widgets_init', 'dds_widgets_init' );
    413function dds_widgets_init() {
     
    1827}
    1928
     29
     30/**
     31 * Switch the sidebar area.
     32 *
     33 * Get the desired area and swith it with
     34 * the area to be dissappear.
     35 *
     36 * The actual rendering of the sidebar area
     37 * is done by `do_action( 'dynamically_dynamic_sidebar' );`.
     38 * What this function does is
     39 *
     40 * - Get the new area.
     41 * - Set the target: Retrieve which sidebar area should be switched to a new one.
     42 * - Get html info:  Get the settings of the surrouding htmls of the area, such as `before_widget`.
     43 * - Set the html settings: Apply the htmls into the new one.
     44 * - Stop the default: Stop rendering the default area.
     45 * - Render: Render the new one.
     46 */
    2047add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    2148function dds_switch_sidebar( $is_active_sidebar, $index ) {
    2249
    23     // 投稿やタームで指定されたウィジェットエリアを取得
     50
     51    // Retrieve the desired area from post type, term, or post.
    2452    $switch = dds_get_desired_widget_area();
    2553
     54    // If none is set, do nothing.
    2655    if ( false == $switch ) {
    2756        return $is_active_sidebar;
    2857    }
    2958
    30     // スイッチングされるべきウィジェットエリアを取得
     59    // Retrieve the target area.
    3160    $dds_target = get_option( 'dds_target_widget_area' );
    3261
     62    // If none is set, do nothing.
    3363    if ( ! $dds_target ) {
    3464        return $is_active_sidebar;
    3565    }
    3666
    37     // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行
     67    // Act only when the the $index is the target area.
    3868    if ( $dds_target === $index ) {
    3969
    40         // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、
    41         // ユーザーが定義したウィジェットエリアにセットする
     70        // Retrieve the display parameters from the original
     71        // and set them to the new area.
    4272        global $wp_registered_sidebars;
    4373        if ( isset( $wp_registered_sidebars[$index] ) ) {
    44             $original_params = $wp_registered_sidebars[$index]; // この中にテーマなどで定義されたウィジェット周辺のHTML情報が入っている
     74
     75            $original_params = $wp_registered_sidebars[$index]; // the settings here.
     76
    4577        } else {
    46             // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある
    47             // その場合は何もしない
     78
     79            // !isset happens when user changes the theme and
     80            // WP loses widget area id.
    4881            return $is_active_sidebar;
    49         }
    50 
     82
     83        }
     84
     85        // Set the four html parameters.
    5186        $params = array(
    5287            'before_widget',
     
    5691        );
    5792        foreach ( $params as $p ) {
    58             // 管理画面で作られたウィジェットエリアにもそれを入れちゃう
     93
    5994            $wp_registered_sidebars[$switch][$p] = $original_params[$p];
    60         }
    61 
    62         // 止めちゃう
     95
     96        }
     97
     98        // Stop the default
    6399        $is_active_sidebar = false;
    64100
    65         // output the dynamic one.
     101        // Rendering. output the dynamic one.
    66102        do_action( 'dynamically_dynamic_sidebar' );
    67103
     
    72108}
    73109
    74 // 出力!
     110/**
     111 * This is the rendering part.
     112 *
     113 * - Get the new widget.
     114 * - Check if it's when.
     115 * - Render.
     116 *
     117 */
    75118add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' );
    76119function dynamically_dynamic_sidebar() {
    77120
    78     // 投稿やタームで指定されたウィジェットエリアを取得
     121    // dds_get_desired_widget_area get the id from various cases.
    79122    $switch = dds_get_desired_widget_area();
    80123
     
    83126    }
    84127
    85     // ユーザーが作ったやつのリスト
     128    // The list of the areas that the user set.
    86129    $registered  = get_option( 'dds_sidebars' );
    87130
    88     // ユーザー作ったやつが存在すればいよいよ実行
     131    // If the area post, term, or post type assigns exist,
     132    // render the sidebar area.
    89133    if ( array_key_exists( $switch, $registered ) ) {
    90134
    91         // is_active_sidebar が無限ループしてしまうのを防いだ
     135        // Trick to prevent is_active_sidebar loop
    92136        remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' );
    93137
    94         // 中身があれば出力。無ければ何もしない
     138        // Check if widgets are set into the area.
    95139        if ( is_active_sidebar( $switch ) ) {
    96140            dynamic_sidebar( $switch );
    97141        }
    98142
    99         // 外したのをもう一度入れておく
     143        // Get the filter back here.
    100144        add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    101145
    102146    } else {
    103147
    104         // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト
     148        // If anything goes wrong, the default.
    105149        dynamic_sidebar( 1 );
    106     }
    107 
    108 }
    109 
    110 // return widget area id (ie, sidebar-custom)
     150
     151    }
     152
     153}
     154
     155//
     156/**
     157 * Returns the widget area id (ie, sidebar-custom, sidebar-noAds)
     158 *
     159 * The priority goes as listed below.
     160 *
     161 * 1. The area set by post comes first.
     162 * 2. The area set by temrs comes next.
     163 * 3. The area set by post type comes the third.
     164 * 4. The default area comes last.
     165 *
     166 * The program flow goes as listed below.
     167 *
     168 * 1. Check the is_singluar().
     169 *    1-1. area set to the post.
     170 *    1-2. area set to the term assigned to the post.
     171 *    1-3. area set to the post type.
     172 * 2. Check the taxonomy term archives.
     173 *    2-1. area set to the term.
     174 *    2-2. area set to the parent term of the term.
     175 *    // 2-3. area set to the taxonomy.
     176 *    2-4. area set to the post type.
     177 * 3. Check the post type archive.
     178 *
     179 */
    111180function dds_get_desired_widget_area() {
    112181
    113182    $widget_area = false;
    114183
     184    // 1. When the page is_singular().
    115185    if ( is_singular() ) {
    116186
    117187        global $post;
     188        // 1-1. area is saved in the post_meta.
    118189        $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true );
    119190        if ( $widget_area ) {
     
    121192        }
    122193
     194        // 1-2. get the area id set to the term or the ancestor of the term.
    123195        $by_term = dds_get_widget_of_post_by_term( $post );
    124196        if ( $by_term ) {
     
    126198        }
    127199
     200        // 1-3. get the area id set to the post type of the post shown now.
     201        $by_post_type = dds_get_widget_of_post_type( $post );
     202        if ( $by_post_type ) {
     203            return $by_post_type;
     204        }
    128205
    129206    } elseif ( is_category() || is_tag() || is_tax() ) {
    130207
    131         // ページ表示に使われたタクソノミのオブジェクト
     208        // This is the term object which used in the query.
    132209        $queried_obj = get_queried_object();
    133210
    134         // のid
     211        // The id of the term.
    135212        $term_id     = $queried_obj->term_id;
    136213
    137         // に割当はあるか?
     214        // Chekc if the term has a custom sidebar area in its term meta.
    138215        $widget_area = get_term_meta( $term_id, 'dds_widget_area', true );
    139216
    140         // あればいいんだけど、
     217        // If a custome area exists, retun it.
    141218        if ( $widget_area ) {
    142219            return $widget_area;
    143220        }
    144221
    145         // ない場合には親をチェックせねば。
     222        // If not, check the ancestors and return the found one.
    146223        $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy );
    147224        if ( is_array( $ancestors ) ) {
     
    151228        }
    152229
     230    } elseif ( is_post_type_archive() ) {
     231
     232        $post_type = get_post_type();
     233
     234        $widget_area_arr = get_option( "dds_area_for_post_types" );
     235
     236        $widget_area = $widget_area_arr[$post_type];
     237
     238        if ( "dds-default" !== $widget_area ) {
     239            return $widget_area;
     240        }
     241
    153242    }
    154243
  • dynamically-dynamic-sidebar/tags/0.7/main.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Get the registered custome sidebar areas.
     5 *
     6 * With this registration,
     7 *
     8 *
     9 * https://developer.wordpress.org/reference/functions/register_sidebar/
     10 *
     11 */
    312add_action( 'widgets_init', 'dds_widgets_init' );
    413function dds_widgets_init() {
     
    1827}
    1928
     29
     30/**
     31 * Switch the sidebar area.
     32 *
     33 * Get the desired area and swith it with
     34 * the area to be dissappear.
     35 *
     36 * The actual rendering of the sidebar area
     37 * is done by `do_action( 'dynamically_dynamic_sidebar' );`.
     38 * What this function does is
     39 *
     40 * - Get the new area.
     41 * - Set the target: Retrieve which sidebar area should be switched to a new one.
     42 * - Get html info:  Get the settings of the surrouding htmls of the area, such as `before_widget`.
     43 * - Set the html settings: Apply the htmls into the new one.
     44 * - Stop the default: Stop rendering the default area.
     45 * - Render: Render the new one.
     46 */
    2047add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    2148function dds_switch_sidebar( $is_active_sidebar, $index ) {
    2249
    23     // 投稿やタームで指定されたウィジェットエリアを取得
     50
     51    // Retrieve the desired area from post type, term, or post.
    2452    $switch = dds_get_desired_widget_area();
    2553
     54    // If none is set, do nothing.
    2655    if ( false == $switch ) {
    2756        return $is_active_sidebar;
    2857    }
    2958
    30     // スイッチングされるべきウィジェットエリアを取得
     59    // Retrieve the target area.
    3160    $dds_target = get_option( 'dds_target_widget_area' );
    3261
     62    // If none is set, do nothing.
    3363    if ( ! $dds_target ) {
    3464        return $is_active_sidebar;
    3565    }
    3666
    37     // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行
     67    // Act only when the the $index is the target area.
    3868    if ( $dds_target === $index ) {
    3969
    40         // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、
    41         // ユーザーが定義したウィジェットエリアにセットする
     70        // Retrieve the display parameters from the original
     71        // and set them to the new area.
    4272        global $wp_registered_sidebars;
    4373        if ( isset( $wp_registered_sidebars[$index] ) ) {
    44             $original_params = $wp_registered_sidebars[$index]; // この中にテーマなどで定義されたウィジェット周辺のHTML情報が入っている
     74
     75            $original_params = $wp_registered_sidebars[$index]; // the settings here.
     76
    4577        } else {
    46             // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある
    47             // その場合は何もしない
     78
     79            // !isset happens when user changes the theme and
     80            // WP loses widget area id.
    4881            return $is_active_sidebar;
    49         }
    50 
     82
     83        }
     84
     85        // Set the four html parameters.
    5186        $params = array(
    5287            'before_widget',
     
    5691        );
    5792        foreach ( $params as $p ) {
    58             // 管理画面で作られたウィジェットエリアにもそれを入れちゃう
     93
    5994            $wp_registered_sidebars[$switch][$p] = $original_params[$p];
    60         }
    61 
    62         // 止めちゃう
     95
     96        }
     97
     98        // Stop the default
    6399        $is_active_sidebar = false;
    64100
    65         // output the dynamic one.
     101        // Rendering. output the dynamic one.
    66102        do_action( 'dynamically_dynamic_sidebar' );
    67103
     
    72108}
    73109
    74 // 出力!
     110/**
     111 * This is the rendering part.
     112 *
     113 * - Get the new widget.
     114 * - Check if it's when.
     115 * - Render.
     116 *
     117 */
    75118add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' );
    76119function dynamically_dynamic_sidebar() {
    77120
    78     // 投稿やタームで指定されたウィジェットエリアを取得
     121    // dds_get_desired_widget_area get the id from various cases.
    79122    $switch = dds_get_desired_widget_area();
    80123
     
    83126    }
    84127
    85     // ユーザーが作ったやつのリスト
     128    // The list of the areas that the user set.
    86129    $registered  = get_option( 'dds_sidebars' );
    87130
    88     // ユーザー作ったやつが存在すればいよいよ実行
     131    // If the area post, term, or post type assigns exist,
     132    // render the sidebar area.
    89133    if ( array_key_exists( $switch, $registered ) ) {
    90134
    91         // is_active_sidebar が無限ループしてしまうのを防いだ
     135        // Trick to prevent is_active_sidebar loop
    92136        remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' );
    93137
    94         // 中身があれば出力。無ければ何もしない
     138        // Check if widgets are set into the area.
    95139        if ( is_active_sidebar( $switch ) ) {
    96140            dynamic_sidebar( $switch );
    97141        }
    98142
    99         // 外したのをもう一度入れておく
     143        // Get the filter back here.
    100144        add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    101145
    102146    } else {
    103147
    104         // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト
     148        // If anything goes wrong, the default.
    105149        dynamic_sidebar( 1 );
    106     }
    107 
    108 }
    109 
    110 // return widget area id (ie, sidebar-custom)
     150
     151    }
     152
     153}
     154
     155//
     156/**
     157 * Returns the widget area id (ie, sidebar-custom, sidebar-noAds)
     158 *
     159 * The priority goes as listed below.
     160 *
     161 * 1. The area set by post comes first.
     162 * 2. The area set by temrs comes next.
     163 * 3. The area set by post type comes the third.
     164 * 4. The default area comes last.
     165 *
     166 * The program flow goes as listed below.
     167 *
     168 * 1. Check the is_singluar().
     169 *    1-1. area set to the post.
     170 *    1-2. area set to the term assigned to the post.
     171 *    1-3. area set to the post type.
     172 * 2. Check the taxonomy term archives.
     173 *    2-1. area set to the term.
     174 *    2-2. area set to the parent term of the term.
     175 *    // 2-3. area set to the taxonomy.
     176 *    2-4. area set to the post type.
     177 * 3. Check the post type archive.
     178 *
     179 */
    111180function dds_get_desired_widget_area() {
    112181
    113182    $widget_area = false;
    114183
     184    // 1. When the page is_singular().
    115185    if ( is_singular() ) {
    116186
    117187        global $post;
     188        // 1-1. area is saved in the post_meta.
    118189        $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true );
    119190        if ( $widget_area ) {
     
    121192        }
    122193
     194        // 1-2. get the area id set to the term or the ancestor of the term.
    123195        $by_term = dds_get_widget_of_post_by_term( $post );
    124196        if ( $by_term ) {
     
    126198        }
    127199
     200        // 1-3. get the area id set to the post type of the post shown now.
     201        $by_post_type = dds_get_widget_of_post_type( $post );
     202        if ( $by_post_type ) {
     203            return $by_post_type;
     204        }
    128205
    129206    } elseif ( is_category() || is_tag() || is_tax() ) {
    130207
    131         // ページ表示に使われたタクソノミのオブジェクト
     208        // This is the term object which used in the query.
    132209        $queried_obj = get_queried_object();
    133210
    134         // のid
     211        // The id of the term.
    135212        $term_id     = $queried_obj->term_id;
    136213
    137         // に割当はあるか?
     214        // Chekc if the term has a custom sidebar area in its term meta.
    138215        $widget_area = get_term_meta( $term_id, 'dds_widget_area', true );
    139216
    140         // あればいいんだけど、
     217        // If a custome area exists, retun it.
    141218        if ( $widget_area ) {
    142219            return $widget_area;
    143220        }
    144221
    145         // ない場合には親をチェックせねば。
     222        // If not, check the ancestors and return the found one.
    146223        $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy );
    147224        if ( is_array( $ancestors ) ) {
     
    151228        }
    152229
     230    } elseif ( is_post_type_archive() ) {
     231
     232        $post_type = get_post_type();
     233
     234        $widget_area_arr = get_option( "dds_area_for_post_types" );
     235
     236        $widget_area = $widget_area_arr[$post_type];
     237
     238        if ( "dds-default" !== $widget_area ) {
     239            return $widget_area;
     240        }
     241
    153242    }
    154243
  • dynamically-dynamic-sidebar/tags/0.7/readme.txt

    r1417575 r1493317  
    33Tags: widget, widget area, sidebar
    44Requires at least: 4.4
    5 Tested up to: 4.5.2
    6 Stable tag: 0.6
     5Tested up to: 4.6.1
     6Stable tag: 0.7
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • dynamically-dynamic-sidebar/trunk/dynamically-dynamic-sidebar.php

    r1417575 r1493317  
    22/*
    33Plugin Name: Dynamically Dynamic Sidebar
    4 Version: 0.6
     4Version: 0.7
    55Description: This plugin enables you to create unlimited widget area and use them for posts, pages, categories, tags and so on.
    66Author: Shinichi Nishikawa
     
    1313$dds_wp_version = $GLOBALS['wp_version'];
    1414
     15// This plugin requires WP version greater than 4.4.
    1516if ( version_compare( (float)$dds_wp_version, '4.4', '<' ) ) {
     17
    1618    add_action( 'admin_notices', 'dds_admin_error_notice' );
    1719    return false;
     20
    1821} else {
    1922
     
    2932
    3033function dds_admin_error_notice() {
    31     ?><div class="error"><p><?php _e( '"Dynamically Dynamic Sidebar" plugin utilizes Term Meta API which was introduced in WordPress version 4.4. You need to upgrade your WordPress to activate the plugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php
     34    ?><div class="error"><p><?php _e( '"Dynamically Dynamic Sidebar" plugin utilizes Term Meta API, which was introduced in WordPress version 4.4. You need to upgrade your WordPress to use this plugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php
    3235}
  • dynamically-dynamic-sidebar/trunk/inc/admin-main.php

    r1287029 r1493317  
    11<?php
    22
    3 // ページとメニューの登録
     3/**
     4 * Register the admin page and the menu to it.
     5 *
     6 */
    47add_action( 'admin_menu', 'dds_add_theme_page' );
    58function dds_add_theme_page() {
    69    add_theme_page(
    7         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
    8         __( 'Dynamically dinamic sidebar', 'dynamically-dynamic-sidebar' ),
     10        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
     11        __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ),
    912        'edit_theme_options',
    1013        'dynamically-dynamic-sidebar',
     
    1316}
    1417
    15 // 管理画面の出力
     18/**
     19 * Outputs the admin page.
     20 * Processes the posted data.
     21 *
     22 */
    1623function dds_output_admin_panel() {
    1724
    18     // post された
     25    // Anything $_POST ed
    1926    if ( ! empty( $_POST ) ) {
    2027
    21         // リファラをチェック
     28        // Let's do check
    2229        check_admin_referer( 'dynamically-dynamic-sidebar' );
    2330
    24         // ポストされてきたもの
     31        // Posted values.
    2532        $posted_sidebars = $_POST["dds-widget-areas"];
    2633
    27         $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // から削除
    28         $posted_sidebars = array_unique( $posted_sidebars ); // 重複排除
    29         $posted_sidebars = array_values( $posted_sidebars ); // キーがジャンプするのを防ぐ
    30         $posted_sidebars = stripslashes_deep( $posted_sidebars ); // スラッシュ問題の解決
     34        $posted_sidebars = array_filter( $posted_sidebars, "strlen" ); // Strip null, false, 0, and empty.
     35        $posted_sidebars = array_unique( $posted_sidebars ); // Strip the doubled ones.
     36        $posted_sidebars = array_values( $posted_sidebars ); // No jumps for the keys.
     37        $posted_sidebars = stripslashes_deep( $posted_sidebars ); // Handler for the quotes escaping slashes.
    3138
    3239        $dds_sidebars = array();
    3340        foreach ( $posted_sidebars as $ps ) {
     41
    3442            $key = sanitize_title( $ps );
    3543            $val = esc_attr( $ps );
    3644            $dds_sidebars[$key] = $val;
    37         }
    38 
    39         // 保存
     45
     46        }
     47
     48        // Save the data.
    4049        if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) {
     50
    4151            update_option( 'dds_sidebars', $dds_sidebars );
     52
    4253        }
    4354
    4455        $posted_target = $_POST["dds_target_widget_area"];
    4556        if ( $posted_target ) {
     57
    4658            update_option( 'dds_target_widget_area', $posted_target );
     59
    4760        } else {
     61
    4862            delete_option( 'dds_target_widget_area' );
     63
     64        }
     65
     66        $posted_widget_area_for_post_types = $_POST["dds_area_for_post_types"];
     67        if ( $posted_widget_area_for_post_types ) {
     68
     69            update_option( 'dds_area_for_post_types', $posted_widget_area_for_post_types );
     70
     71        } else {
     72
     73            delete_option( 'dds_area_for_post_types' );
     74
    4975        }
    5076
     
    5379    $dds_sidebars = get_option( 'dds_sidebars' );
    5480    $dds_target   = get_option( 'dds_target_widget_area' );
     81    $dds_a_f_pts  = get_option( 'dds_area_for_post_types' );
     82
     83
    5584?>
    5685<div class="wrap">
     
    6190    <tbody>
    6291        <tr>
    63             <th scope="row"><?php _e( 'Target widget area to switch', 'dynamically-dynamic-sidebar' ); ?></th>
     92            <th scope="row"><?php _e( 'Target Widget Area', 'dynamically-dynamic-sidebar' ); ?></th>
    6493            <td>
     94                <p class="description">Choose the widget area to switch with custom widget areas.</p>
     95                <br />
    6596                <label for="dds_target_widget_area">
    6697                    <select name="dds_target_widget_area" id="dds_target_widget_area">
     
    69100                            global $wp_registered_sidebars;
    70101
    71                             // here's the list of the widget areas.
     102                            // Here's the list of the widget areas.
    72103                            $registered = $wp_registered_sidebars;
    73                             // we want only the ones registered by other theme(plugins)l
     104                            // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin.
    74105                            foreach ( $dds_sidebars as $key => $val ) {
    75106                                unset( $registered[$key] );
     
    93124        </tr>
    94125        <tr>
    95             <th scope="row"><?php _e( 'Manage Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     126            <th scope="row"><?php _e( 'Custom Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
     127            <td style="vertical-align: top;" width="25%">
     128                <?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?> <label for="dds-new"><input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     129            </td>
    96130            <td>
    97131                <?php
    98132                if ( $dds_sidebars ) {
     133                    ?>
     134                    <p class="description">Add and edit the name of custom widget areas.</p>
     135                    <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
     136                    <br />
     137                    <?php
    99138                    foreach ( $dds_sidebars as $key => $val ) {
    100139                        ?>
     
    102141                        <?php
    103142                    }
    104                     ?>
    105                     <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>
    106                     <?php
    107143                } else {
    108144                    _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' );
     
    112148        </tr>
    113149        <tr>
    114             <th scope="row"><?php _e( 'Add new', 'dynamically-dynamic-sidebar' ); ?></th>
     150            <th scope="row"><?php _e( 'Post type Widget Areas', 'dynamically-dynamic-sidebar' ); ?></th>
    115151            <td>
    116                 <label for="dds-new"><?php _e( 'Add New', 'dynamically-dynamic-sidebar' ); ?> <input id="dds-new" type="text" name="dds-widget-areas[]" value=""></label>
     152                <p class="description">Assign widget areas to your post types.</p>
     153                <br />
     154                <?php
     155                    // https://developer.wordpress.org/reference/functions/get_post_types/
     156                    $args = array(
     157                        'public' => true,
     158                    );
     159                    $registered_post_types = get_post_types(
     160                        $args,
     161                        "object",
     162                        "and"
     163                    );
     164                    unset( $registered_post_types["attachment"] );
     165
     166                    echo "<table>";
     167
     168                    foreach ( $registered_post_types as $key => $val ) {
     169                        ?>
     170                        <tr>
     171                            <th>
     172                                <label for="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]"><?php echo esc_attr( $val->label ); ?></label>
     173                            </th>
     174                            <td>
     175                                <select name="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]" id="dds_area_for_post_types[<?php echo esc_attr( $key ); ?>]">
     176                                    <option value="dds-default"><?php _e( 'Default', 'dynamically-dynamic-sidebar' ); ?></option>
     177                                    <?php foreach ( $dds_sidebars as $dds_key => $dds_val ) {
     178                                        ?>
     179                                        <option
     180                                            value="<?php echo esc_attr( $dds_key ); ?>"
     181                                            <?php
     182                                            if ( isset($dds_a_f_pts[$key]) && esc_attr( $dds_a_f_pts[$key] ) === $dds_key ) {
     183                                            ?>
     184                                                selected="selected"
     185                                            <?php
     186                                            }
     187                                            ?>
     188                                        ><?php echo esc_html( $dds_val ); ?></option>
     189                                        <?php
     190                                    } ?>
     191                                </select>
     192                            </td>
     193                        </tr>
     194                        <?php
     195                    }
     196                    echo "</table>";
     197                ?>
    117198            </td>
    118199        </tr>
    119200    </tbody>
    120201</table>
    121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widget settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
     202<p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save settings.', 'dynamically-dynamic-sidebar' ); ?>" /></p>
    122203<?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?>
    123204</form>
  • dynamically-dynamic-sidebar/trunk/inc/admin-post.php

    r1417567 r1493317  
    11<?php
    22
     3/**
     4 * Adds metabox to the post editor.
     5 *
     6 */
    37add_action( 'add_meta_boxes', 'dds_add_meta_box' );
    48function dds_add_meta_box() {
     
    2630}
    2731
    28 // ポスト用のメタボックス
     32/**
     33 * The output of the metabox.
     34 *
     35 */
    2936function dds_post_meta_boxes( $post ) {
    3037
     
    3239    $the_area   = get_post_meta( $post->ID, 'dds_widget_area', true );
    3340
     41    // This outputs an alert or an info in the contexts
     42    // , where the assigned term has an sidebar widget area.
    3443    dds_alert_term_widget( $post );
    3544
     
    5160}
    5261
     62/**
     63 * Used in the meta box for a post,
     64 * this function inform users that the
     65 * post will have a custom widget area
     66 * set by the term it's allocated.
     67 *
     68 */
    5369function dds_alert_term_widget( $post ) {
    5470
     
    5773    if ( $widget_by_term ) {
    5874
    59         $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to <strong>"%2$s"</strong> in <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
     75        $format = __( '<p class="dds-notice">The widget area for this post is <strong>%1$s</strong>, which is allocated to the term <strong>"%2$s"</strong> of the taxonomy <strong>"%3$s"</strong>.</p>', 'dynamically-dynamic-sidebar' );
    6076
    6177        printf(
     
    6682        );
    6783
    68         echo '<p class="dds-notice">You can override it by choosing another one here.</p>';
     84        echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>';
    6985
    7086    } else {
     
    7692}
    7793
    78 
     94/**
     95 * Save the data sent from the meta box.
     96 *
     97 */
    7998add_action( 'save_post', 'dds_save_post_widget' );
    8099function dds_save_post_widget( $post_id ) {
     
    103122}
    104123
     124
     125/**
     126 * In the list table of the posts/pages/custom post types,
     127 * the widget areas will be shown.
     128 *
     129 */
     130
    105131// posts and custom post types
    106132add_filter( 'manage_posts_columns',            'dds_add_posts_table_column'       );
     
    108134
    109135// page
    110 add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10);
    111 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2);
     136add_filter( 'manage_page_posts_columns',       'dds_add_posts_table_column', 10 );
     137add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells',  10, 2 );
    112138
    113139function dds_add_posts_table_column( $columns ) {
     
    122148    }
    123149
     150    // When the area comes from the post
    124151    $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true );
    125152    $widget_name    = dds_get_widget_name_by_id($widget_by_post);
     
    130157
    131158    } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) {
     159        // when the area comes from a term.
    132160
    133161        $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )';
  • dynamically-dynamic-sidebar/trunk/inc/admin-term.php

    r1287029 r1493317  
    22
    33// 各タクソノミで出力と保存をするためにフックで登録
     4/**
     5 * Hooks the actions to display and save
     6 * fields on the term list/edit screens.
     7 *
     8 */
    49add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' );
    510function dds_do_meta_for_all_taxonomies() {
     
    813
    914    foreach ( $taxonomies as $t ) {
    10         add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields' );  // 新規追加時に出力
    11         add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // 編集時に出力
    12         add_action( 'created_' . $t, 'dds_update_term_meta' );              // 新規追加時に実行
    13         add_action( 'edited_'  . $t, 'dds_update_term_meta' );              // 編集時に実行
     15        add_action( $t . '_add_form_fields', 'dds_term_add_meta_fields'  ); // output on add screen
     16        add_action( $t . '_edit_form',       'dds_term_edit_meta_fields' ); // output on edit screen
     17        add_action( 'created_' . $t,         'dds_update_term_meta'      ); // fires on add screen
     18        add_action( 'edited_'  . $t,         'dds_update_term_meta'      ); // fires on edit screen
    1419    }
    1520
     
    1722
    1823
    19 // タームの編集画面にフォームを追加
     24/**
     25 * Output the form on edit screen.
     26 *
     27 */
    2028function dds_term_edit_meta_fields( $taxonomy ) {
    2129
     
    4755}
    4856
    49 // タームの新規追加画面にフォームを追加
     57/**
     58 * Output the form on add screen.
     59 *
     60 */
    5061function dds_term_add_meta_fields( $taxonomy ) {
    5162
     
    6879
    6980
    70 // タームメタの保存
     81/**
     82 * Save the term meta on save.
     83 *
     84 */
    7185function dds_update_term_meta( $term_id ) {
    7286
     
    94108}
    95109
    96 // タームのリストに割り当てられたウィジェットを出す
     110/**
     111 * Add a column to the list table of the terms.
     112 *
     113 */
    97114add_action( 'admin_init', 'dds_fire_term_table_funcs' );
    98115function dds_fire_term_table_funcs() {
     
    103120        add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身
    104121    }
     122
    105123}
    106124
     125/**
     126 * Output the th
     127 */
    107128function dds_add_term_table_column( $columns ) {
    108     $columns["dds_widget_column"] = "Sidebar";
     129
     130    $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' );
    109131    return $columns;
     132
    110133}
    111134
     135/**
     136 * Output the allocated area name in the cells.
     137 */
    112138function dds_add_term_table_cells( $content, $column_name, $term_id ) {
     139
    113140    if ( 'dds_widget_column' === $column_name ) {
    114141        $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true );
     
    118145        }
    119146    }
     147
    120148    return $content;
     149
    121150}
    122 
    123 
    124 
    125 
    126 
    127 
  • dynamically-dynamic-sidebar/trunk/inc/functions.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Returns the taxonomies which are public.
     5 *
     6 */
    37function dds_get_taxonomies() {
    48
     
    1822}
    1923
     24
     25
    2026/**
    21  * Returns an array of area FIRST FOUND.
     27 * Given the post ID, looking into the post type of the post,
     28 * returns an array of the area FIRST FOUND.
     29 * If no area is assigned, returns false.
     30 *
     31 * @param  int   $post          post id
     32 * @return array $area_term_arr an array of widget_id, widget_name
     33                                and term object detemining the widget id.
     34 */
     35function dds_get_widget_of_post_type( $post ) {
     36
     37    if ( ! $post ) {
     38        return false;
     39    }
     40
     41    $dds_post_type = $post->post_type;
     42    $registered_posttypes_widgets = get_option( "dds_area_for_post_types" );
     43    if ( isset( $registered_posttypes_widgets[$dds_post_type] ) ) {
     44        $by_post_type = $registered_posttypes_widgets[$dds_post_type];
     45    } else {
     46        $by_post_type = false;
     47    }
     48
     49    return $by_post_type;
     50
     51
     52}
     53
     54
     55/**
     56 * Given the post ID, looking into the terms of the post,
     57 * returns an array of the area FIRST FOUND.
    2258 * If no area is assigned, returns false.
    2359 *
     
    2864function dds_get_widget_of_post_by_term( $post ) {
    2965
    30     // マスター
     66    // Init
    3167    $terms         = array();
    3268    $ancestors     = array();
    3369    $area_term_arr = array();
    3470
    35     // 登録されているタクソノミ
     71    // Getting all the taxonomies, which are public and has the UI.
    3672    $taxonomies = dds_get_taxonomies();
    3773
    38     // 各タクソノミのタームを取得
     74    // Loop through all the taxonomy terms.
    3975    foreach ( $taxonomies as $taxonomy ) {
    4076
    41         $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得
     77        $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms.
    4278
    4379        // タームに属していればマスターに入れる
     
    4884    }
    4985
    50     // 直接のタームをチェック
     86    // Pass the term array and get the widget area back.
    5187    $area_term_arr = dds_check_term_arrays_allocated_area( $terms );
    5288
     
    5591    }
    5692
    57     // タームの親をチェックしないと。。
     93    // Check the ancestors too.
    5894    foreach ( $terms as $t ) {
    5995
     
    69105    }
    70106
     107    // Pass the parent term array and get the widget area back.
    71108    $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors );
    72109
     
    84121 *
    85122 *
    86  *
    87123 * @param  int|object $terms         term id or term object
    88124 * @return array      $area_term_arr $area_term_arr an array of widget_id, widget_name
     
    97133    foreach ( $terms as $t ) {
    98134
    99         // object でも id でも id として扱う
     135        // Let's handle ids only.
    100136        if ( is_object( $t ) ) {
    101137            $term_id = $t->term_id;
     
    105141
    106142        $area_id = get_term_meta( $term_id, 'dds_widget_area', true );
    107         // もしあれば、配列を作って、ループを抜ける
    108         if ( $area_id ) {
    109143
    110             // ユーザ作成のエリア
     144        if ( $area_id ) { // if exists, create an array and escape from the loop.
     145
     146            // the array of the user custom areas.
    111147            $allocatable_widgets  = get_option( 'dds_sidebars' );
    112148
  • dynamically-dynamic-sidebar/trunk/inc/main.php

    r1417575 r1493317  
    11<?php
    22
     3/**
     4 * Get the registered custome sidebar areas.
     5 *
     6 * With this registration,
     7 *
     8 *
     9 * https://developer.wordpress.org/reference/functions/register_sidebar/
     10 *
     11 */
    312add_action( 'widgets_init', 'dds_widgets_init' );
    413function dds_widgets_init() {
     
    1827}
    1928
     29
     30/**
     31 * Switch the sidebar area.
     32 *
     33 * Get the desired area and swith it with
     34 * the area to be dissappear.
     35 *
     36 * The actual rendering of the sidebar area
     37 * is done by `do_action( 'dynamically_dynamic_sidebar' );`.
     38 * What this function does is
     39 *
     40 * - Get the new area.
     41 * - Set the target: Retrieve which sidebar area should be switched to a new one.
     42 * - Get html info:  Get the settings of the surrouding htmls of the area, such as `before_widget`.
     43 * - Set the html settings: Apply the htmls into the new one.
     44 * - Stop the default: Stop rendering the default area.
     45 * - Render: Render the new one.
     46 */
    2047add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    2148function dds_switch_sidebar( $is_active_sidebar, $index ) {
    2249
    23     // 投稿やタームで指定されたウィジェットエリアを取得
     50
     51    // Retrieve the desired area from post type, term, or post.
    2452    $switch = dds_get_desired_widget_area();
    2553
     54    // If none is set, do nothing.
    2655    if ( false == $switch ) {
    2756        return $is_active_sidebar;
    2857    }
    2958
    30     // スイッチングされるべきウィジェットエリアを取得
     59    // Retrieve the target area.
    3160    $dds_target = get_option( 'dds_target_widget_area' );
    3261
     62    // If none is set, do nothing.
    3363    if ( ! $dds_target ) {
    3464        return $is_active_sidebar;
    3565    }
    3666
    37     // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行
     67    // Act only when the the $index is the target area.
    3868    if ( $dds_target === $index ) {
    3969
    40         // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、
    41         // ユーザーが定義したウィジェットエリアにセットする
     70        // Retrieve the display parameters from the original
     71        // and set them to the new area.
    4272        global $wp_registered_sidebars;
    4373        if ( isset( $wp_registered_sidebars[$index] ) ) {
    44             $original_params = $wp_registered_sidebars[$index]; // この中にテーマなどで定義されたウィジェット周辺のHTML情報が入っている
     74
     75            $original_params = $wp_registered_sidebars[$index]; // the settings here.
     76
    4577        } else {
    46             // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある
    47             // その場合は何もしない
     78
     79            // !isset happens when user changes the theme and
     80            // WP loses widget area id.
    4881            return $is_active_sidebar;
    49         }
    50 
     82
     83        }
     84
     85        // Set the four html parameters.
    5186        $params = array(
    5287            'before_widget',
     
    5691        );
    5792        foreach ( $params as $p ) {
    58             // 管理画面で作られたウィジェットエリアにもそれを入れちゃう
     93
    5994            $wp_registered_sidebars[$switch][$p] = $original_params[$p];
    60         }
    61 
    62         // 止めちゃう
     95
     96        }
     97
     98        // Stop the default
    6399        $is_active_sidebar = false;
    64100
    65         // output the dynamic one.
     101        // Rendering. output the dynamic one.
    66102        do_action( 'dynamically_dynamic_sidebar' );
    67103
     
    72108}
    73109
    74 // 出力!
     110/**
     111 * This is the rendering part.
     112 *
     113 * - Get the new widget.
     114 * - Check if it's when.
     115 * - Render.
     116 *
     117 */
    75118add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' );
    76119function dynamically_dynamic_sidebar() {
    77120
    78     // 投稿やタームで指定されたウィジェットエリアを取得
     121    // dds_get_desired_widget_area get the id from various cases.
    79122    $switch = dds_get_desired_widget_area();
    80123
     
    83126    }
    84127
    85     // ユーザーが作ったやつのリスト
     128    // The list of the areas that the user set.
    86129    $registered  = get_option( 'dds_sidebars' );
    87130
    88     // ユーザー作ったやつが存在すればいよいよ実行
     131    // If the area post, term, or post type assigns exist,
     132    // render the sidebar area.
    89133    if ( array_key_exists( $switch, $registered ) ) {
    90134
    91         // is_active_sidebar が無限ループしてしまうのを防いだ
     135        // Trick to prevent is_active_sidebar loop
    92136        remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' );
    93137
    94         // 中身があれば出力。無ければ何もしない
     138        // Check if widgets are set into the area.
    95139        if ( is_active_sidebar( $switch ) ) {
    96140            dynamic_sidebar( $switch );
    97141        }
    98142
    99         // 外したのをもう一度入れておく
     143        // Get the filter back here.
    100144        add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 );
    101145
    102146    } else {
    103147
    104         // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト
     148        // If anything goes wrong, the default.
    105149        dynamic_sidebar( 1 );
    106     }
    107 
    108 }
    109 
    110 // return widget area id (ie, sidebar-custom)
     150
     151    }
     152
     153}
     154
     155//
     156/**
     157 * Returns the widget area id (ie, sidebar-custom, sidebar-noAds)
     158 *
     159 * The priority goes as listed below.
     160 *
     161 * 1. The area set by post comes first.
     162 * 2. The area set by temrs comes next.
     163 * 3. The area set by post type comes the third.
     164 * 4. The default area comes last.
     165 *
     166 * The program flow goes as listed below.
     167 *
     168 * 1. Check the is_singluar().
     169 *    1-1. area set to the post.
     170 *    1-2. area set to the term assigned to the post.
     171 *    1-3. area set to the post type.
     172 * 2. Check the taxonomy term archives.
     173 *    2-1. area set to the term.
     174 *    2-2. area set to the parent term of the term.
     175 *    // 2-3. area set to the taxonomy.
     176 *    2-4. area set to the post type.
     177 * 3. Check the post type archive.
     178 *
     179 */
    111180function dds_get_desired_widget_area() {
    112181
    113182    $widget_area = false;
    114183
     184    // 1. When the page is_singular().
    115185    if ( is_singular() ) {
    116186
    117187        global $post;
     188        // 1-1. area is saved in the post_meta.
    118189        $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true );
    119190        if ( $widget_area ) {
     
    121192        }
    122193
     194        // 1-2. get the area id set to the term or the ancestor of the term.
    123195        $by_term = dds_get_widget_of_post_by_term( $post );
    124196        if ( $by_term ) {
     
    126198        }
    127199
     200        // 1-3. get the area id set to the post type of the post shown now.
     201        $by_post_type = dds_get_widget_of_post_type( $post );
     202        if ( $by_post_type ) {
     203            return $by_post_type;
     204        }
    128205
    129206    } elseif ( is_category() || is_tag() || is_tax() ) {
    130207
    131         // ページ表示に使われたタクソノミのオブジェクト
     208        // This is the term object which used in the query.
    132209        $queried_obj = get_queried_object();
    133210
    134         // のid
     211        // The id of the term.
    135212        $term_id     = $queried_obj->term_id;
    136213
    137         // に割当はあるか?
     214        // Chekc if the term has a custom sidebar area in its term meta.
    138215        $widget_area = get_term_meta( $term_id, 'dds_widget_area', true );
    139216
    140         // あればいいんだけど、
     217        // If a custome area exists, retun it.
    141218        if ( $widget_area ) {
    142219            return $widget_area;
    143220        }
    144221
    145         // ない場合には親をチェックせねば。
     222        // If not, check the ancestors and return the found one.
    146223        $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy );
    147224        if ( is_array( $ancestors ) ) {
     
    151228        }
    152229
     230    } elseif ( is_post_type_archive() ) {
     231
     232        $post_type = get_post_type();
     233
     234        $widget_area_arr = get_option( "dds_area_for_post_types" );
     235
     236        $widget_area = $widget_area_arr[$post_type];
     237
     238        if ( "dds-default" !== $widget_area ) {
     239            return $widget_area;
     240        }
     241
    153242    }
    154243
  • dynamically-dynamic-sidebar/trunk/readme.txt

    r1417575 r1493317  
    33Tags: widget, widget area, sidebar
    44Requires at least: 4.4
    5 Tested up to: 4.5.2
    6 Stable tag: 0.6
     5Tested up to: 4.6.1
     6Stable tag: 0.7
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.