Changeset 1493317
- Timestamp:
- 09/09/2016 03:03:21 PM (10 years ago)
- Location:
- dynamically-dynamic-sidebar
- Files:
-
- 1 added
- 9 edited
- 13 copied
-
tags/0.7 (added)
-
tags/0.7/admin-main.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/admin-main.php) (8 diffs)
-
tags/0.7/admin-post.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/admin-post.php) (11 diffs)
-
tags/0.7/admin-term.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/admin-term.php) (8 diffs)
-
tags/0.7/dynamically-dynamic-sidebar.php (copied) (copied from dynamically-dynamic-sidebar/trunk/dynamically-dynamic-sidebar.php) (3 diffs)
-
tags/0.7/functions.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/functions.php) (9 diffs)
-
tags/0.7/inc (copied) (copied from dynamically-dynamic-sidebar/trunk/inc)
-
tags/0.7/inc/admin-main.php (modified) (8 diffs)
-
tags/0.7/inc/admin-post.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/admin-post.php) (11 diffs)
-
tags/0.7/inc/admin-term.php (modified) (8 diffs)
-
tags/0.7/inc/functions.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/functions.php) (9 diffs)
-
tags/0.7/inc/main.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/main.php) (8 diffs)
-
tags/0.7/main.php (copied) (copied from dynamically-dynamic-sidebar/trunk/inc/main.php) (8 diffs)
-
tags/0.7/readme-ja.md (copied) (copied from dynamically-dynamic-sidebar/trunk/readme-ja.md)
-
tags/0.7/readme.md (copied) (copied from dynamically-dynamic-sidebar/trunk/readme.md)
-
tags/0.7/readme.txt (copied) (copied from dynamically-dynamic-sidebar/trunk/readme.txt) (1 diff)
-
trunk/dynamically-dynamic-sidebar.php (modified) (3 diffs)
-
trunk/inc/admin-main.php (modified) (8 diffs)
-
trunk/inc/admin-post.php (modified) (11 diffs)
-
trunk/inc/admin-term.php (modified) (8 diffs)
-
trunk/inc/functions.php (modified) (9 diffs)
-
trunk/inc/main.php (modified) (8 diffs)
-
trunk/readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
dynamically-dynamic-sidebar/tags/0.7/admin-main.php
r1417566 r1493317 1 1 <?php 2 2 3 // ページとメニューの登録 3 /** 4 * Register the admin page and the menu to it. 5 * 6 */ 4 7 add_action( 'admin_menu', 'dds_add_theme_page' ); 5 8 function dds_add_theme_page() { 6 9 add_theme_page( 7 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),8 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),10 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 11 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 9 12 'edit_theme_options', 10 13 'dynamically-dynamic-sidebar', … … 13 16 } 14 17 15 // 管理画面の出力 18 /** 19 * Outputs the admin page. 20 * Processes the posted data. 21 * 22 */ 16 23 function dds_output_admin_panel() { 17 24 18 // post された25 // Anything $_POST ed 19 26 if ( ! empty( $_POST ) ) { 20 27 21 // リファラをチェック28 // Let's do check 22 29 check_admin_referer( 'dynamically-dynamic-sidebar' ); 23 30 24 // ポストされてきたもの31 // Posted values. 25 32 $posted_sidebars = $_POST["dds-widget-areas"]; 26 33 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. 31 38 32 39 $dds_sidebars = array(); 33 40 foreach ( $posted_sidebars as $ps ) { 41 34 42 $key = sanitize_title( $ps ); 35 43 $val = esc_attr( $ps ); 36 44 $dds_sidebars[$key] = $val; 37 } 38 39 // 保存 45 46 } 47 48 // Save the data. 40 49 if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) { 50 41 51 update_option( 'dds_sidebars', $dds_sidebars ); 52 42 53 } 43 54 44 55 $posted_target = $_POST["dds_target_widget_area"]; 45 56 if ( $posted_target ) { 57 46 58 update_option( 'dds_target_widget_area', $posted_target ); 59 47 60 } else { 61 48 62 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 49 75 } 50 76 … … 53 79 $dds_sidebars = get_option( 'dds_sidebars' ); 54 80 $dds_target = get_option( 'dds_target_widget_area' ); 81 $dds_a_f_pts = get_option( 'dds_area_for_post_types' ); 82 83 55 84 ?> 56 85 <div class="wrap"> … … 61 90 <tbody> 62 91 <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> 64 93 <td> 94 <p class="description">Choose the widget area to switch with custom widget areas.</p> 95 <br /> 65 96 <label for="dds_target_widget_area"> 66 97 <select name="dds_target_widget_area" id="dds_target_widget_area"> … … 69 100 global $wp_registered_sidebars; 70 101 71 // here's the list of the widget areas.102 // Here's the list of the widget areas. 72 103 $registered = $wp_registered_sidebars; 73 // we want only the ones registered by other theme(plugins)l104 // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin. 74 105 foreach ( $dds_sidebars as $key => $val ) { 75 106 unset( $registered[$key] ); … … 93 124 </tr> 94 125 <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> 96 130 <td> 97 131 <?php 98 132 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 99 138 foreach ( $dds_sidebars as $key => $val ) { 100 139 ?> … … 102 141 <?php 103 142 } 104 ?>105 <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>106 <?php107 143 } else { 108 144 _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' ); … … 112 148 </tr> 113 149 <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> 115 151 <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 ?> 117 198 </td> 118 199 </tr> 119 200 </tbody> 120 201 </table> 121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widgetsettings.', '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> 122 203 <?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?> 123 204 </form> -
dynamically-dynamic-sidebar/tags/0.7/admin-post.php
r1417567 r1493317 1 1 <?php 2 2 3 /** 4 * Adds metabox to the post editor. 5 * 6 */ 3 7 add_action( 'add_meta_boxes', 'dds_add_meta_box' ); 4 8 function dds_add_meta_box() { … … 26 30 } 27 31 28 // ポスト用のメタボックス 32 /** 33 * The output of the metabox. 34 * 35 */ 29 36 function dds_post_meta_boxes( $post ) { 30 37 … … 32 39 $the_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 33 40 41 // This outputs an alert or an info in the contexts 42 // , where the assigned term has an sidebar widget area. 34 43 dds_alert_term_widget( $post ); 35 44 … … 51 60 } 52 61 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 */ 53 69 function dds_alert_term_widget( $post ) { 54 70 … … 57 73 if ( $widget_by_term ) { 58 74 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' ); 60 76 61 77 printf( … … 66 82 ); 67 83 68 echo '<p class="dds-notice">You can override it by choosing anotherone here.</p>';84 echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>'; 69 85 70 86 } else { … … 76 92 } 77 93 78 94 /** 95 * Save the data sent from the meta box. 96 * 97 */ 79 98 add_action( 'save_post', 'dds_save_post_widget' ); 80 99 function dds_save_post_widget( $post_id ) { … … 103 122 } 104 123 124 125 /** 126 * In the list table of the posts/pages/custom post types, 127 * the widget areas will be shown. 128 * 129 */ 130 105 131 // posts and custom post types 106 132 add_filter( 'manage_posts_columns', 'dds_add_posts_table_column' ); … … 108 134 109 135 // 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);136 add_filter( 'manage_page_posts_columns', 'dds_add_posts_table_column', 10 ); 137 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2 ); 112 138 113 139 function dds_add_posts_table_column( $columns ) { … … 122 148 } 123 149 150 // When the area comes from the post 124 151 $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true ); 125 152 $widget_name = dds_get_widget_name_by_id($widget_by_post); … … 130 157 131 158 } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) { 159 // when the area comes from a term. 132 160 133 161 $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )'; -
dynamically-dynamic-sidebar/tags/0.7/admin-term.php
r1417566 r1493317 2 2 3 3 // 各タクソノミで出力と保存をするためにフックで登録 4 /** 5 * Hooks the actions to display and save 6 * fields on the term list/edit screens. 7 * 8 */ 4 9 add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' ); 5 10 function dds_do_meta_for_all_taxonomies() { … … 8 13 9 14 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 14 19 } 15 20 … … 17 22 18 23 19 // タームの編集画面にフォームを追加 24 /** 25 * Output the form on edit screen. 26 * 27 */ 20 28 function dds_term_edit_meta_fields( $taxonomy ) { 21 29 … … 47 55 } 48 56 49 // タームの新規追加画面にフォームを追加 57 /** 58 * Output the form on add screen. 59 * 60 */ 50 61 function dds_term_add_meta_fields( $taxonomy ) { 51 62 … … 68 79 69 80 70 // タームメタの保存 81 /** 82 * Save the term meta on save. 83 * 84 */ 71 85 function dds_update_term_meta( $term_id ) { 72 86 … … 94 108 } 95 109 96 // タームのリストに割り当てられたウィジェットを出す 110 /** 111 * Add a column to the list table of the terms. 112 * 113 */ 97 114 add_action( 'admin_init', 'dds_fire_term_table_funcs' ); 98 115 function dds_fire_term_table_funcs() { … … 103 120 add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身 104 121 } 122 105 123 } 106 124 125 /** 126 * Output the th 127 */ 107 128 function dds_add_term_table_column( $columns ) { 108 $columns["dds_widget_column"] = "Sidebar"; 129 130 $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' ); 109 131 return $columns; 132 110 133 } 111 134 135 /** 136 * Output the allocated area name in the cells. 137 */ 112 138 function dds_add_term_table_cells( $content, $column_name, $term_id ) { 139 113 140 if ( 'dds_widget_column' === $column_name ) { 114 141 $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true ); … … 118 145 } 119 146 } 147 120 148 return $content; 149 121 150 } 122 123 124 125 126 127 -
dynamically-dynamic-sidebar/tags/0.7/dynamically-dynamic-sidebar.php
r1417575 r1493317 2 2 /* 3 3 Plugin Name: Dynamically Dynamic Sidebar 4 Version: 0. 64 Version: 0.7 5 5 Description: This plugin enables you to create unlimited widget area and use them for posts, pages, categories, tags and so on. 6 6 Author: Shinichi Nishikawa … … 13 13 $dds_wp_version = $GLOBALS['wp_version']; 14 14 15 // This plugin requires WP version greater than 4.4. 15 16 if ( version_compare( (float)$dds_wp_version, '4.4', '<' ) ) { 17 16 18 add_action( 'admin_notices', 'dds_admin_error_notice' ); 17 19 return false; 20 18 21 } else { 19 22 … … 29 32 30 33 function 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 theplugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php34 ?><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 32 35 } -
dynamically-dynamic-sidebar/tags/0.7/functions.php
r1417575 r1493317 1 1 <?php 2 2 3 /** 4 * Returns the taxonomies which are public. 5 * 6 */ 3 7 function dds_get_taxonomies() { 4 8 … … 18 22 } 19 23 24 25 20 26 /** 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 */ 35 function 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. 22 58 * If no area is assigned, returns false. 23 59 * … … 28 64 function dds_get_widget_of_post_by_term( $post ) { 29 65 30 // マスター66 // Init 31 67 $terms = array(); 32 68 $ancestors = array(); 33 69 $area_term_arr = array(); 34 70 35 // 登録されているタクソノミ71 // Getting all the taxonomies, which are public and has the UI. 36 72 $taxonomies = dds_get_taxonomies(); 37 73 38 // 各タクソノミのタームを取得74 // Loop through all the taxonomy terms. 39 75 foreach ( $taxonomies as $taxonomy ) { 40 76 41 $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得77 $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms. 42 78 43 79 // タームに属していればマスターに入れる … … 48 84 } 49 85 50 // 直接のタームをチェック86 // Pass the term array and get the widget area back. 51 87 $area_term_arr = dds_check_term_arrays_allocated_area( $terms ); 52 88 … … 55 91 } 56 92 57 // タームの親をチェックしないと。。93 // Check the ancestors too. 58 94 foreach ( $terms as $t ) { 59 95 … … 69 105 } 70 106 107 // Pass the parent term array and get the widget area back. 71 108 $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors ); 72 109 … … 84 121 * 85 122 * 86 *87 123 * @param int|object $terms term id or term object 88 124 * @return array $area_term_arr $area_term_arr an array of widget_id, widget_name … … 97 133 foreach ( $terms as $t ) { 98 134 99 // object でも id でも id として扱う135 // Let's handle ids only. 100 136 if ( is_object( $t ) ) { 101 137 $term_id = $t->term_id; … … 105 141 106 142 $area_id = get_term_meta( $term_id, 'dds_widget_area', true ); 107 // もしあれば、配列を作って、ループを抜ける108 if ( $area_id ) {109 143 110 // ユーザ作成のエリア 144 if ( $area_id ) { // if exists, create an array and escape from the loop. 145 146 // the array of the user custom areas. 111 147 $allocatable_widgets = get_option( 'dds_sidebars' ); 112 148 -
dynamically-dynamic-sidebar/tags/0.7/inc/admin-main.php
r1287029 r1493317 1 1 <?php 2 2 3 // ページとメニューの登録 3 /** 4 * Register the admin page and the menu to it. 5 * 6 */ 4 7 add_action( 'admin_menu', 'dds_add_theme_page' ); 5 8 function dds_add_theme_page() { 6 9 add_theme_page( 7 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),8 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),10 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 11 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 9 12 'edit_theme_options', 10 13 'dynamically-dynamic-sidebar', … … 13 16 } 14 17 15 // 管理画面の出力 18 /** 19 * Outputs the admin page. 20 * Processes the posted data. 21 * 22 */ 16 23 function dds_output_admin_panel() { 17 24 18 // post された25 // Anything $_POST ed 19 26 if ( ! empty( $_POST ) ) { 20 27 21 // リファラをチェック28 // Let's do check 22 29 check_admin_referer( 'dynamically-dynamic-sidebar' ); 23 30 24 // ポストされてきたもの31 // Posted values. 25 32 $posted_sidebars = $_POST["dds-widget-areas"]; 26 33 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. 31 38 32 39 $dds_sidebars = array(); 33 40 foreach ( $posted_sidebars as $ps ) { 41 34 42 $key = sanitize_title( $ps ); 35 43 $val = esc_attr( $ps ); 36 44 $dds_sidebars[$key] = $val; 37 } 38 39 // 保存 45 46 } 47 48 // Save the data. 40 49 if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) { 50 41 51 update_option( 'dds_sidebars', $dds_sidebars ); 52 42 53 } 43 54 44 55 $posted_target = $_POST["dds_target_widget_area"]; 45 56 if ( $posted_target ) { 57 46 58 update_option( 'dds_target_widget_area', $posted_target ); 59 47 60 } else { 61 48 62 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 49 75 } 50 76 … … 53 79 $dds_sidebars = get_option( 'dds_sidebars' ); 54 80 $dds_target = get_option( 'dds_target_widget_area' ); 81 $dds_a_f_pts = get_option( 'dds_area_for_post_types' ); 82 83 55 84 ?> 56 85 <div class="wrap"> … … 61 90 <tbody> 62 91 <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> 64 93 <td> 94 <p class="description">Choose the widget area to switch with custom widget areas.</p> 95 <br /> 65 96 <label for="dds_target_widget_area"> 66 97 <select name="dds_target_widget_area" id="dds_target_widget_area"> … … 69 100 global $wp_registered_sidebars; 70 101 71 // here's the list of the widget areas.102 // Here's the list of the widget areas. 72 103 $registered = $wp_registered_sidebars; 73 // we want only the ones registered by other theme(plugins)l104 // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin. 74 105 foreach ( $dds_sidebars as $key => $val ) { 75 106 unset( $registered[$key] ); … … 93 124 </tr> 94 125 <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> 96 130 <td> 97 131 <?php 98 132 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 99 138 foreach ( $dds_sidebars as $key => $val ) { 100 139 ?> … … 102 141 <?php 103 142 } 104 ?>105 <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>106 <?php107 143 } else { 108 144 _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' ); … … 112 148 </tr> 113 149 <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> 115 151 <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 ?> 117 198 </td> 118 199 </tr> 119 200 </tbody> 120 201 </table> 121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widgetsettings.', '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> 122 203 <?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?> 123 204 </form> -
dynamically-dynamic-sidebar/tags/0.7/inc/admin-post.php
r1417567 r1493317 1 1 <?php 2 2 3 /** 4 * Adds metabox to the post editor. 5 * 6 */ 3 7 add_action( 'add_meta_boxes', 'dds_add_meta_box' ); 4 8 function dds_add_meta_box() { … … 26 30 } 27 31 28 // ポスト用のメタボックス 32 /** 33 * The output of the metabox. 34 * 35 */ 29 36 function dds_post_meta_boxes( $post ) { 30 37 … … 32 39 $the_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 33 40 41 // This outputs an alert or an info in the contexts 42 // , where the assigned term has an sidebar widget area. 34 43 dds_alert_term_widget( $post ); 35 44 … … 51 60 } 52 61 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 */ 53 69 function dds_alert_term_widget( $post ) { 54 70 … … 57 73 if ( $widget_by_term ) { 58 74 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' ); 60 76 61 77 printf( … … 66 82 ); 67 83 68 echo '<p class="dds-notice">You can override it by choosing anotherone here.</p>';84 echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>'; 69 85 70 86 } else { … … 76 92 } 77 93 78 94 /** 95 * Save the data sent from the meta box. 96 * 97 */ 79 98 add_action( 'save_post', 'dds_save_post_widget' ); 80 99 function dds_save_post_widget( $post_id ) { … … 103 122 } 104 123 124 125 /** 126 * In the list table of the posts/pages/custom post types, 127 * the widget areas will be shown. 128 * 129 */ 130 105 131 // posts and custom post types 106 132 add_filter( 'manage_posts_columns', 'dds_add_posts_table_column' ); … … 108 134 109 135 // 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);136 add_filter( 'manage_page_posts_columns', 'dds_add_posts_table_column', 10 ); 137 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2 ); 112 138 113 139 function dds_add_posts_table_column( $columns ) { … … 122 148 } 123 149 150 // When the area comes from the post 124 151 $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true ); 125 152 $widget_name = dds_get_widget_name_by_id($widget_by_post); … … 130 157 131 158 } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) { 159 // when the area comes from a term. 132 160 133 161 $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )'; -
dynamically-dynamic-sidebar/tags/0.7/inc/admin-term.php
r1287029 r1493317 2 2 3 3 // 各タクソノミで出力と保存をするためにフックで登録 4 /** 5 * Hooks the actions to display and save 6 * fields on the term list/edit screens. 7 * 8 */ 4 9 add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' ); 5 10 function dds_do_meta_for_all_taxonomies() { … … 8 13 9 14 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 14 19 } 15 20 … … 17 22 18 23 19 // タームの編集画面にフォームを追加 24 /** 25 * Output the form on edit screen. 26 * 27 */ 20 28 function dds_term_edit_meta_fields( $taxonomy ) { 21 29 … … 47 55 } 48 56 49 // タームの新規追加画面にフォームを追加 57 /** 58 * Output the form on add screen. 59 * 60 */ 50 61 function dds_term_add_meta_fields( $taxonomy ) { 51 62 … … 68 79 69 80 70 // タームメタの保存 81 /** 82 * Save the term meta on save. 83 * 84 */ 71 85 function dds_update_term_meta( $term_id ) { 72 86 … … 94 108 } 95 109 96 // タームのリストに割り当てられたウィジェットを出す 110 /** 111 * Add a column to the list table of the terms. 112 * 113 */ 97 114 add_action( 'admin_init', 'dds_fire_term_table_funcs' ); 98 115 function dds_fire_term_table_funcs() { … … 103 120 add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身 104 121 } 122 105 123 } 106 124 125 /** 126 * Output the th 127 */ 107 128 function dds_add_term_table_column( $columns ) { 108 $columns["dds_widget_column"] = "Sidebar"; 129 130 $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' ); 109 131 return $columns; 132 110 133 } 111 134 135 /** 136 * Output the allocated area name in the cells. 137 */ 112 138 function dds_add_term_table_cells( $content, $column_name, $term_id ) { 139 113 140 if ( 'dds_widget_column' === $column_name ) { 114 141 $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true ); … … 118 145 } 119 146 } 147 120 148 return $content; 149 121 150 } 122 123 124 125 126 127 -
dynamically-dynamic-sidebar/tags/0.7/inc/functions.php
r1417575 r1493317 1 1 <?php 2 2 3 /** 4 * Returns the taxonomies which are public. 5 * 6 */ 3 7 function dds_get_taxonomies() { 4 8 … … 18 22 } 19 23 24 25 20 26 /** 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 */ 35 function 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. 22 58 * If no area is assigned, returns false. 23 59 * … … 28 64 function dds_get_widget_of_post_by_term( $post ) { 29 65 30 // マスター66 // Init 31 67 $terms = array(); 32 68 $ancestors = array(); 33 69 $area_term_arr = array(); 34 70 35 // 登録されているタクソノミ71 // Getting all the taxonomies, which are public and has the UI. 36 72 $taxonomies = dds_get_taxonomies(); 37 73 38 // 各タクソノミのタームを取得74 // Loop through all the taxonomy terms. 39 75 foreach ( $taxonomies as $taxonomy ) { 40 76 41 $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得77 $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms. 42 78 43 79 // タームに属していればマスターに入れる … … 48 84 } 49 85 50 // 直接のタームをチェック86 // Pass the term array and get the widget area back. 51 87 $area_term_arr = dds_check_term_arrays_allocated_area( $terms ); 52 88 … … 55 91 } 56 92 57 // タームの親をチェックしないと。。93 // Check the ancestors too. 58 94 foreach ( $terms as $t ) { 59 95 … … 69 105 } 70 106 107 // Pass the parent term array and get the widget area back. 71 108 $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors ); 72 109 … … 84 121 * 85 122 * 86 *87 123 * @param int|object $terms term id or term object 88 124 * @return array $area_term_arr $area_term_arr an array of widget_id, widget_name … … 97 133 foreach ( $terms as $t ) { 98 134 99 // object でも id でも id として扱う135 // Let's handle ids only. 100 136 if ( is_object( $t ) ) { 101 137 $term_id = $t->term_id; … … 105 141 106 142 $area_id = get_term_meta( $term_id, 'dds_widget_area', true ); 107 // もしあれば、配列を作って、ループを抜ける108 if ( $area_id ) {109 143 110 // ユーザ作成のエリア 144 if ( $area_id ) { // if exists, create an array and escape from the loop. 145 146 // the array of the user custom areas. 111 147 $allocatable_widgets = get_option( 'dds_sidebars' ); 112 148 -
dynamically-dynamic-sidebar/tags/0.7/inc/main.php
r1417575 r1493317 1 1 <?php 2 2 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 */ 3 12 add_action( 'widgets_init', 'dds_widgets_init' ); 4 13 function dds_widgets_init() { … … 18 27 } 19 28 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 */ 20 47 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 21 48 function dds_switch_sidebar( $is_active_sidebar, $index ) { 22 49 23 // 投稿やタームで指定されたウィジェットエリアを取得 50 51 // Retrieve the desired area from post type, term, or post. 24 52 $switch = dds_get_desired_widget_area(); 25 53 54 // If none is set, do nothing. 26 55 if ( false == $switch ) { 27 56 return $is_active_sidebar; 28 57 } 29 58 30 // スイッチングされるべきウィジェットエリアを取得59 // Retrieve the target area. 31 60 $dds_target = get_option( 'dds_target_widget_area' ); 32 61 62 // If none is set, do nothing. 33 63 if ( ! $dds_target ) { 34 64 return $is_active_sidebar; 35 65 } 36 66 37 // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行67 // Act only when the the $index is the target area. 38 68 if ( $dds_target === $index ) { 39 69 40 // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、41 // ユーザーが定義したウィジェットエリアにセットする70 // Retrieve the display parameters from the original 71 // and set them to the new area. 42 72 global $wp_registered_sidebars; 43 73 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 45 77 } else { 46 // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある 47 // その場合は何もしない 78 79 // !isset happens when user changes the theme and 80 // WP loses widget area id. 48 81 return $is_active_sidebar; 49 } 50 82 83 } 84 85 // Set the four html parameters. 51 86 $params = array( 52 87 'before_widget', … … 56 91 ); 57 92 foreach ( $params as $p ) { 58 // 管理画面で作られたウィジェットエリアにもそれを入れちゃう 93 59 94 $wp_registered_sidebars[$switch][$p] = $original_params[$p]; 60 } 61 62 // 止めちゃう 95 96 } 97 98 // Stop the default 63 99 $is_active_sidebar = false; 64 100 65 // output the dynamic one.101 // Rendering. output the dynamic one. 66 102 do_action( 'dynamically_dynamic_sidebar' ); 67 103 … … 72 108 } 73 109 74 // 出力! 110 /** 111 * This is the rendering part. 112 * 113 * - Get the new widget. 114 * - Check if it's when. 115 * - Render. 116 * 117 */ 75 118 add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' ); 76 119 function dynamically_dynamic_sidebar() { 77 120 78 // 投稿やタームで指定されたウィジェットエリアを取得121 // dds_get_desired_widget_area get the id from various cases. 79 122 $switch = dds_get_desired_widget_area(); 80 123 … … 83 126 } 84 127 85 // ユーザーが作ったやつのリスト128 // The list of the areas that the user set. 86 129 $registered = get_option( 'dds_sidebars' ); 87 130 88 // ユーザー作ったやつが存在すればいよいよ実行 131 // If the area post, term, or post type assigns exist, 132 // render the sidebar area. 89 133 if ( array_key_exists( $switch, $registered ) ) { 90 134 91 // is_active_sidebar が無限ループしてしまうのを防いだ135 // Trick to prevent is_active_sidebar loop 92 136 remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' ); 93 137 94 // 中身があれば出力。無ければ何もしない138 // Check if widgets are set into the area. 95 139 if ( is_active_sidebar( $switch ) ) { 96 140 dynamic_sidebar( $switch ); 97 141 } 98 142 99 // 外したのをもう一度入れておく143 // Get the filter back here. 100 144 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 101 145 102 146 } else { 103 147 104 // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト148 // If anything goes wrong, the default. 105 149 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 */ 111 180 function dds_get_desired_widget_area() { 112 181 113 182 $widget_area = false; 114 183 184 // 1. When the page is_singular(). 115 185 if ( is_singular() ) { 116 186 117 187 global $post; 188 // 1-1. area is saved in the post_meta. 118 189 $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 119 190 if ( $widget_area ) { … … 121 192 } 122 193 194 // 1-2. get the area id set to the term or the ancestor of the term. 123 195 $by_term = dds_get_widget_of_post_by_term( $post ); 124 196 if ( $by_term ) { … … 126 198 } 127 199 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 } 128 205 129 206 } elseif ( is_category() || is_tag() || is_tax() ) { 130 207 131 // ページ表示に使われたタクソノミのオブジェクト208 // This is the term object which used in the query. 132 209 $queried_obj = get_queried_object(); 133 210 134 // のid211 // The id of the term. 135 212 $term_id = $queried_obj->term_id; 136 213 137 // に割当はあるか?214 // Chekc if the term has a custom sidebar area in its term meta. 138 215 $widget_area = get_term_meta( $term_id, 'dds_widget_area', true ); 139 216 140 // あればいいんだけど、217 // If a custome area exists, retun it. 141 218 if ( $widget_area ) { 142 219 return $widget_area; 143 220 } 144 221 145 // ない場合には親をチェックせねば。222 // If not, check the ancestors and return the found one. 146 223 $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy ); 147 224 if ( is_array( $ancestors ) ) { … … 151 228 } 152 229 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 153 242 } 154 243 -
dynamically-dynamic-sidebar/tags/0.7/main.php
r1417575 r1493317 1 1 <?php 2 2 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 */ 3 12 add_action( 'widgets_init', 'dds_widgets_init' ); 4 13 function dds_widgets_init() { … … 18 27 } 19 28 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 */ 20 47 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 21 48 function dds_switch_sidebar( $is_active_sidebar, $index ) { 22 49 23 // 投稿やタームで指定されたウィジェットエリアを取得 50 51 // Retrieve the desired area from post type, term, or post. 24 52 $switch = dds_get_desired_widget_area(); 25 53 54 // If none is set, do nothing. 26 55 if ( false == $switch ) { 27 56 return $is_active_sidebar; 28 57 } 29 58 30 // スイッチングされるべきウィジェットエリアを取得59 // Retrieve the target area. 31 60 $dds_target = get_option( 'dds_target_widget_area' ); 32 61 62 // If none is set, do nothing. 33 63 if ( ! $dds_target ) { 34 64 return $is_active_sidebar; 35 65 } 36 66 37 // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行67 // Act only when the the $index is the target area. 38 68 if ( $dds_target === $index ) { 39 69 40 // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、41 // ユーザーが定義したウィジェットエリアにセットする70 // Retrieve the display parameters from the original 71 // and set them to the new area. 42 72 global $wp_registered_sidebars; 43 73 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 45 77 } else { 46 // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある 47 // その場合は何もしない 78 79 // !isset happens when user changes the theme and 80 // WP loses widget area id. 48 81 return $is_active_sidebar; 49 } 50 82 83 } 84 85 // Set the four html parameters. 51 86 $params = array( 52 87 'before_widget', … … 56 91 ); 57 92 foreach ( $params as $p ) { 58 // 管理画面で作られたウィジェットエリアにもそれを入れちゃう 93 59 94 $wp_registered_sidebars[$switch][$p] = $original_params[$p]; 60 } 61 62 // 止めちゃう 95 96 } 97 98 // Stop the default 63 99 $is_active_sidebar = false; 64 100 65 // output the dynamic one.101 // Rendering. output the dynamic one. 66 102 do_action( 'dynamically_dynamic_sidebar' ); 67 103 … … 72 108 } 73 109 74 // 出力! 110 /** 111 * This is the rendering part. 112 * 113 * - Get the new widget. 114 * - Check if it's when. 115 * - Render. 116 * 117 */ 75 118 add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' ); 76 119 function dynamically_dynamic_sidebar() { 77 120 78 // 投稿やタームで指定されたウィジェットエリアを取得121 // dds_get_desired_widget_area get the id from various cases. 79 122 $switch = dds_get_desired_widget_area(); 80 123 … … 83 126 } 84 127 85 // ユーザーが作ったやつのリスト128 // The list of the areas that the user set. 86 129 $registered = get_option( 'dds_sidebars' ); 87 130 88 // ユーザー作ったやつが存在すればいよいよ実行 131 // If the area post, term, or post type assigns exist, 132 // render the sidebar area. 89 133 if ( array_key_exists( $switch, $registered ) ) { 90 134 91 // is_active_sidebar が無限ループしてしまうのを防いだ135 // Trick to prevent is_active_sidebar loop 92 136 remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' ); 93 137 94 // 中身があれば出力。無ければ何もしない138 // Check if widgets are set into the area. 95 139 if ( is_active_sidebar( $switch ) ) { 96 140 dynamic_sidebar( $switch ); 97 141 } 98 142 99 // 外したのをもう一度入れておく143 // Get the filter back here. 100 144 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 101 145 102 146 } else { 103 147 104 // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト148 // If anything goes wrong, the default. 105 149 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 */ 111 180 function dds_get_desired_widget_area() { 112 181 113 182 $widget_area = false; 114 183 184 // 1. When the page is_singular(). 115 185 if ( is_singular() ) { 116 186 117 187 global $post; 188 // 1-1. area is saved in the post_meta. 118 189 $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 119 190 if ( $widget_area ) { … … 121 192 } 122 193 194 // 1-2. get the area id set to the term or the ancestor of the term. 123 195 $by_term = dds_get_widget_of_post_by_term( $post ); 124 196 if ( $by_term ) { … … 126 198 } 127 199 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 } 128 205 129 206 } elseif ( is_category() || is_tag() || is_tax() ) { 130 207 131 // ページ表示に使われたタクソノミのオブジェクト208 // This is the term object which used in the query. 132 209 $queried_obj = get_queried_object(); 133 210 134 // のid211 // The id of the term. 135 212 $term_id = $queried_obj->term_id; 136 213 137 // に割当はあるか?214 // Chekc if the term has a custom sidebar area in its term meta. 138 215 $widget_area = get_term_meta( $term_id, 'dds_widget_area', true ); 139 216 140 // あればいいんだけど、217 // If a custome area exists, retun it. 141 218 if ( $widget_area ) { 142 219 return $widget_area; 143 220 } 144 221 145 // ない場合には親をチェックせねば。222 // If not, check the ancestors and return the found one. 146 223 $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy ); 147 224 if ( is_array( $ancestors ) ) { … … 151 228 } 152 229 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 153 242 } 154 243 -
dynamically-dynamic-sidebar/tags/0.7/readme.txt
r1417575 r1493317 3 3 Tags: widget, widget area, sidebar 4 4 Requires at least: 4.4 5 Tested up to: 4. 5.26 Stable tag: 0. 65 Tested up to: 4.6.1 6 Stable tag: 0.7 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html -
dynamically-dynamic-sidebar/trunk/dynamically-dynamic-sidebar.php
r1417575 r1493317 2 2 /* 3 3 Plugin Name: Dynamically Dynamic Sidebar 4 Version: 0. 64 Version: 0.7 5 5 Description: This plugin enables you to create unlimited widget area and use them for posts, pages, categories, tags and so on. 6 6 Author: Shinichi Nishikawa … … 13 13 $dds_wp_version = $GLOBALS['wp_version']; 14 14 15 // This plugin requires WP version greater than 4.4. 15 16 if ( version_compare( (float)$dds_wp_version, '4.4', '<' ) ) { 17 16 18 add_action( 'admin_notices', 'dds_admin_error_notice' ); 17 19 return false; 20 18 21 } else { 19 22 … … 29 32 30 33 function 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 theplugin.', 'dynamically-dynamic-sidebar' ); ?></p></div><?php34 ?><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 32 35 } -
dynamically-dynamic-sidebar/trunk/inc/admin-main.php
r1287029 r1493317 1 1 <?php 2 2 3 // ページとメニューの登録 3 /** 4 * Register the admin page and the menu to it. 5 * 6 */ 4 7 add_action( 'admin_menu', 'dds_add_theme_page' ); 5 8 function dds_add_theme_page() { 6 9 add_theme_page( 7 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),8 __( 'Dynamically d inamic sidebar', 'dynamically-dynamic-sidebar' ),10 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 11 __( 'Dynamically dynamic sidebar', 'dynamically-dynamic-sidebar' ), 9 12 'edit_theme_options', 10 13 'dynamically-dynamic-sidebar', … … 13 16 } 14 17 15 // 管理画面の出力 18 /** 19 * Outputs the admin page. 20 * Processes the posted data. 21 * 22 */ 16 23 function dds_output_admin_panel() { 17 24 18 // post された25 // Anything $_POST ed 19 26 if ( ! empty( $_POST ) ) { 20 27 21 // リファラをチェック28 // Let's do check 22 29 check_admin_referer( 'dynamically-dynamic-sidebar' ); 23 30 24 // ポストされてきたもの31 // Posted values. 25 32 $posted_sidebars = $_POST["dds-widget-areas"]; 26 33 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. 31 38 32 39 $dds_sidebars = array(); 33 40 foreach ( $posted_sidebars as $ps ) { 41 34 42 $key = sanitize_title( $ps ); 35 43 $val = esc_attr( $ps ); 36 44 $dds_sidebars[$key] = $val; 37 } 38 39 // 保存 45 46 } 47 48 // Save the data. 40 49 if ( isset( $dds_sidebars ) && is_array( $dds_sidebars ) ) { 50 41 51 update_option( 'dds_sidebars', $dds_sidebars ); 52 42 53 } 43 54 44 55 $posted_target = $_POST["dds_target_widget_area"]; 45 56 if ( $posted_target ) { 57 46 58 update_option( 'dds_target_widget_area', $posted_target ); 59 47 60 } else { 61 48 62 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 49 75 } 50 76 … … 53 79 $dds_sidebars = get_option( 'dds_sidebars' ); 54 80 $dds_target = get_option( 'dds_target_widget_area' ); 81 $dds_a_f_pts = get_option( 'dds_area_for_post_types' ); 82 83 55 84 ?> 56 85 <div class="wrap"> … … 61 90 <tbody> 62 91 <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> 64 93 <td> 94 <p class="description">Choose the widget area to switch with custom widget areas.</p> 95 <br /> 65 96 <label for="dds_target_widget_area"> 66 97 <select name="dds_target_widget_area" id="dds_target_widget_area"> … … 69 100 global $wp_registered_sidebars; 70 101 71 // here's the list of the widget areas.102 // Here's the list of the widget areas. 72 103 $registered = $wp_registered_sidebars; 73 // we want only the ones registered by other theme(plugins)l104 // We want only the ones registered by themes and plugins. Not the user custom sidebar by this very plugin. 74 105 foreach ( $dds_sidebars as $key => $val ) { 75 106 unset( $registered[$key] ); … … 93 124 </tr> 94 125 <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> 96 130 <td> 97 131 <?php 98 132 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 99 138 foreach ( $dds_sidebars as $key => $val ) { 100 139 ?> … … 102 141 <?php 103 142 } 104 ?>105 <p class="description"><?php _e( 'Make the field blank to delete your areas.', 'dynamically-dynamic-sidebar' ); ?></p>106 <?php107 143 } else { 108 144 _e( 'There\'s no dynamic widget areas yet.', 'dynamically-dynamic-sidebar' ); … … 112 148 </tr> 113 149 <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> 115 151 <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 ?> 117 198 </td> 118 199 </tr> 119 200 </tbody> 120 201 </table> 121 <p class="submit"><input type="submit" id="submit" class="button button-primary" value="<?php _e( 'Save dynamically dynamic widgetsettings.', '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> 122 203 <?php wp_nonce_field( "dynamically-dynamic-sidebar" ); ?> 123 204 </form> -
dynamically-dynamic-sidebar/trunk/inc/admin-post.php
r1417567 r1493317 1 1 <?php 2 2 3 /** 4 * Adds metabox to the post editor. 5 * 6 */ 3 7 add_action( 'add_meta_boxes', 'dds_add_meta_box' ); 4 8 function dds_add_meta_box() { … … 26 30 } 27 31 28 // ポスト用のメタボックス 32 /** 33 * The output of the metabox. 34 * 35 */ 29 36 function dds_post_meta_boxes( $post ) { 30 37 … … 32 39 $the_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 33 40 41 // This outputs an alert or an info in the contexts 42 // , where the assigned term has an sidebar widget area. 34 43 dds_alert_term_widget( $post ); 35 44 … … 51 60 } 52 61 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 */ 53 69 function dds_alert_term_widget( $post ) { 54 70 … … 57 73 if ( $widget_by_term ) { 58 74 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' ); 60 76 61 77 printf( … … 66 82 ); 67 83 68 echo '<p class="dds-notice">You can override it by choosing anotherone here.</p>';84 echo '<p class="dds-notice">You can override the sidebar of this post by choosing one here.</p>'; 69 85 70 86 } else { … … 76 92 } 77 93 78 94 /** 95 * Save the data sent from the meta box. 96 * 97 */ 79 98 add_action( 'save_post', 'dds_save_post_widget' ); 80 99 function dds_save_post_widget( $post_id ) { … … 103 122 } 104 123 124 125 /** 126 * In the list table of the posts/pages/custom post types, 127 * the widget areas will be shown. 128 * 129 */ 130 105 131 // posts and custom post types 106 132 add_filter( 'manage_posts_columns', 'dds_add_posts_table_column' ); … … 108 134 109 135 // 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);136 add_filter( 'manage_page_posts_columns', 'dds_add_posts_table_column', 10 ); 137 add_action( 'manage_page_posts_custom_column', 'dds_add_posts_table_cells', 10, 2 ); 112 138 113 139 function dds_add_posts_table_column( $columns ) { … … 122 148 } 123 149 150 // When the area comes from the post 124 151 $widget_by_post = get_post_meta( $post_id, 'dds_widget_area', true ); 125 152 $widget_name = dds_get_widget_name_by_id($widget_by_post); … … 130 157 131 158 } elseif ( $widget_by_term = dds_get_widget_of_post_by_term( $post_id ) ) { 159 // when the area comes from a term. 132 160 133 161 $format = '<strong>%1$s</strong><br>(from %2$s of %3$s )'; -
dynamically-dynamic-sidebar/trunk/inc/admin-term.php
r1287029 r1493317 2 2 3 3 // 各タクソノミで出力と保存をするためにフックで登録 4 /** 5 * Hooks the actions to display and save 6 * fields on the term list/edit screens. 7 * 8 */ 4 9 add_action( 'admin_init', 'dds_do_meta_for_all_taxonomies' ); 5 10 function dds_do_meta_for_all_taxonomies() { … … 8 13 9 14 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 14 19 } 15 20 … … 17 22 18 23 19 // タームの編集画面にフォームを追加 24 /** 25 * Output the form on edit screen. 26 * 27 */ 20 28 function dds_term_edit_meta_fields( $taxonomy ) { 21 29 … … 47 55 } 48 56 49 // タームの新規追加画面にフォームを追加 57 /** 58 * Output the form on add screen. 59 * 60 */ 50 61 function dds_term_add_meta_fields( $taxonomy ) { 51 62 … … 68 79 69 80 70 // タームメタの保存 81 /** 82 * Save the term meta on save. 83 * 84 */ 71 85 function dds_update_term_meta( $term_id ) { 72 86 … … 94 108 } 95 109 96 // タームのリストに割り当てられたウィジェットを出す 110 /** 111 * Add a column to the list table of the terms. 112 * 113 */ 97 114 add_action( 'admin_init', 'dds_fire_term_table_funcs' ); 98 115 function dds_fire_term_table_funcs() { … … 103 120 add_filter( 'manage_' . $taxonomy . '_custom_column', 'dds_add_term_table_cells', 10, 3 ); // 中身 104 121 } 122 105 123 } 106 124 125 /** 126 * Output the th 127 */ 107 128 function dds_add_term_table_column( $columns ) { 108 $columns["dds_widget_column"] = "Sidebar"; 129 130 $columns["dds_widget_column"] = __( 'Sidebar', 'dynamically-dynamic-sidebar' ); 109 131 return $columns; 132 110 133 } 111 134 135 /** 136 * Output the allocated area name in the cells. 137 */ 112 138 function dds_add_term_table_cells( $content, $column_name, $term_id ) { 139 113 140 if ( 'dds_widget_column' === $column_name ) { 114 141 $allocated_widget_key = get_term_meta( $term_id, 'dds_widget_area', true ); … … 118 145 } 119 146 } 147 120 148 return $content; 149 121 150 } 122 123 124 125 126 127 -
dynamically-dynamic-sidebar/trunk/inc/functions.php
r1417575 r1493317 1 1 <?php 2 2 3 /** 4 * Returns the taxonomies which are public. 5 * 6 */ 3 7 function dds_get_taxonomies() { 4 8 … … 18 22 } 19 23 24 25 20 26 /** 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 */ 35 function 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. 22 58 * If no area is assigned, returns false. 23 59 * … … 28 64 function dds_get_widget_of_post_by_term( $post ) { 29 65 30 // マスター66 // Init 31 67 $terms = array(); 32 68 $ancestors = array(); 33 69 $area_term_arr = array(); 34 70 35 // 登録されているタクソノミ71 // Getting all the taxonomies, which are public and has the UI. 36 72 $taxonomies = dds_get_taxonomies(); 37 73 38 // 各タクソノミのタームを取得74 // Loop through all the taxonomy terms. 39 75 foreach ( $taxonomies as $taxonomy ) { 40 76 41 $term_obj = get_the_terms( $post, $taxonomy ); // タームを取得77 $term_obj = get_the_terms( $post, $taxonomy ); // Get "THE" terms. 42 78 43 79 // タームに属していればマスターに入れる … … 48 84 } 49 85 50 // 直接のタームをチェック86 // Pass the term array and get the widget area back. 51 87 $area_term_arr = dds_check_term_arrays_allocated_area( $terms ); 52 88 … … 55 91 } 56 92 57 // タームの親をチェックしないと。。93 // Check the ancestors too. 58 94 foreach ( $terms as $t ) { 59 95 … … 69 105 } 70 106 107 // Pass the parent term array and get the widget area back. 71 108 $area_term_arr = dds_check_term_arrays_allocated_area( $ancestors ); 72 109 … … 84 121 * 85 122 * 86 *87 123 * @param int|object $terms term id or term object 88 124 * @return array $area_term_arr $area_term_arr an array of widget_id, widget_name … … 97 133 foreach ( $terms as $t ) { 98 134 99 // object でも id でも id として扱う135 // Let's handle ids only. 100 136 if ( is_object( $t ) ) { 101 137 $term_id = $t->term_id; … … 105 141 106 142 $area_id = get_term_meta( $term_id, 'dds_widget_area', true ); 107 // もしあれば、配列を作って、ループを抜ける108 if ( $area_id ) {109 143 110 // ユーザ作成のエリア 144 if ( $area_id ) { // if exists, create an array and escape from the loop. 145 146 // the array of the user custom areas. 111 147 $allocatable_widgets = get_option( 'dds_sidebars' ); 112 148 -
dynamically-dynamic-sidebar/trunk/inc/main.php
r1417575 r1493317 1 1 <?php 2 2 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 */ 3 12 add_action( 'widgets_init', 'dds_widgets_init' ); 4 13 function dds_widgets_init() { … … 18 27 } 19 28 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 */ 20 47 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 21 48 function dds_switch_sidebar( $is_active_sidebar, $index ) { 22 49 23 // 投稿やタームで指定されたウィジェットエリアを取得 50 51 // Retrieve the desired area from post type, term, or post. 24 52 $switch = dds_get_desired_widget_area(); 25 53 54 // If none is set, do nothing. 26 55 if ( false == $switch ) { 27 56 return $is_active_sidebar; 28 57 } 29 58 30 // スイッチングされるべきウィジェットエリアを取得59 // Retrieve the target area. 31 60 $dds_target = get_option( 'dds_target_widget_area' ); 32 61 62 // If none is set, do nothing. 33 63 if ( ! $dds_target ) { 34 64 return $is_active_sidebar; 35 65 } 36 66 37 // 今 is_active_sidebar されているものが、ターゲットの場合にだけ実行67 // Act only when the the $index is the target area. 38 68 if ( $dds_target === $index ) { 39 69 40 // ウィジェットエリアの表示用のパラメータを元のところから持ってきて、41 // ユーザーが定義したウィジェットエリアにセットする70 // Retrieve the display parameters from the original 71 // and set them to the new area. 42 72 global $wp_registered_sidebars; 43 73 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 45 77 } else { 46 // テーマを切り替えるなどして、ターゲットに指定されていたウィジェットエリアのIDがなくなることがある 47 // その場合は何もしない 78 79 // !isset happens when user changes the theme and 80 // WP loses widget area id. 48 81 return $is_active_sidebar; 49 } 50 82 83 } 84 85 // Set the four html parameters. 51 86 $params = array( 52 87 'before_widget', … … 56 91 ); 57 92 foreach ( $params as $p ) { 58 // 管理画面で作られたウィジェットエリアにもそれを入れちゃう 93 59 94 $wp_registered_sidebars[$switch][$p] = $original_params[$p]; 60 } 61 62 // 止めちゃう 95 96 } 97 98 // Stop the default 63 99 $is_active_sidebar = false; 64 100 65 // output the dynamic one.101 // Rendering. output the dynamic one. 66 102 do_action( 'dynamically_dynamic_sidebar' ); 67 103 … … 72 108 } 73 109 74 // 出力! 110 /** 111 * This is the rendering part. 112 * 113 * - Get the new widget. 114 * - Check if it's when. 115 * - Render. 116 * 117 */ 75 118 add_action( 'dynamically_dynamic_sidebar', 'dynamically_dynamic_sidebar' ); 76 119 function dynamically_dynamic_sidebar() { 77 120 78 // 投稿やタームで指定されたウィジェットエリアを取得121 // dds_get_desired_widget_area get the id from various cases. 79 122 $switch = dds_get_desired_widget_area(); 80 123 … … 83 126 } 84 127 85 // ユーザーが作ったやつのリスト128 // The list of the areas that the user set. 86 129 $registered = get_option( 'dds_sidebars' ); 87 130 88 // ユーザー作ったやつが存在すればいよいよ実行 131 // If the area post, term, or post type assigns exist, 132 // render the sidebar area. 89 133 if ( array_key_exists( $switch, $registered ) ) { 90 134 91 // is_active_sidebar が無限ループしてしまうのを防いだ135 // Trick to prevent is_active_sidebar loop 92 136 remove_filter( 'is_active_sidebar', 'dds_switch_sidebar' ); 93 137 94 // 中身があれば出力。無ければ何もしない138 // Check if widgets are set into the area. 95 139 if ( is_active_sidebar( $switch ) ) { 96 140 dynamic_sidebar( $switch ); 97 141 } 98 142 99 // 外したのをもう一度入れておく143 // Get the filter back here. 100 144 add_filter( 'is_active_sidebar', 'dds_switch_sidebar', 10, 2 ); 101 145 102 146 } else { 103 147 104 // ユーザーが投稿やタームで指定したウィジェットエリアが何かの拍子に消えてしまっていたらデフォルト148 // If anything goes wrong, the default. 105 149 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 */ 111 180 function dds_get_desired_widget_area() { 112 181 113 182 $widget_area = false; 114 183 184 // 1. When the page is_singular(). 115 185 if ( is_singular() ) { 116 186 117 187 global $post; 188 // 1-1. area is saved in the post_meta. 118 189 $widget_area = get_post_meta( $post->ID, 'dds_widget_area', true ); 119 190 if ( $widget_area ) { … … 121 192 } 122 193 194 // 1-2. get the area id set to the term or the ancestor of the term. 123 195 $by_term = dds_get_widget_of_post_by_term( $post ); 124 196 if ( $by_term ) { … … 126 198 } 127 199 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 } 128 205 129 206 } elseif ( is_category() || is_tag() || is_tax() ) { 130 207 131 // ページ表示に使われたタクソノミのオブジェクト208 // This is the term object which used in the query. 132 209 $queried_obj = get_queried_object(); 133 210 134 // のid211 // The id of the term. 135 212 $term_id = $queried_obj->term_id; 136 213 137 // に割当はあるか?214 // Chekc if the term has a custom sidebar area in its term meta. 138 215 $widget_area = get_term_meta( $term_id, 'dds_widget_area', true ); 139 216 140 // あればいいんだけど、217 // If a custome area exists, retun it. 141 218 if ( $widget_area ) { 142 219 return $widget_area; 143 220 } 144 221 145 // ない場合には親をチェックせねば。222 // If not, check the ancestors and return the found one. 146 223 $ancestors = get_ancestors( $term_id, $queried_obj->taxonomy ); 147 224 if ( is_array( $ancestors ) ) { … … 151 228 } 152 229 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 153 242 } 154 243 -
dynamically-dynamic-sidebar/trunk/readme.txt
r1417575 r1493317 3 3 Tags: widget, widget area, sidebar 4 4 Requires at least: 4.4 5 Tested up to: 4. 5.26 Stable tag: 0. 65 Tested up to: 4.6.1 6 Stable tag: 0.7 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset
for help on using the changeset viewer.