管理画面と閲覧画面で共通に利用しているサブルーチンです。
コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
class Hk_Sitemap_Common { // ------ 共通プロパティ ---------- public $id_strs = array(); // IDstr public $id_data = array(); // 固定ページ・カテゴリのデータ public $ex_pages = array(); // 除外する固定ページのID public $ex_cats = array(); // 除外するカテゴリのID public $ex_s_pages = array(); // 検索から除外する固定ページのID public $ex_s_cats = array(); // 検索から除外するカテゴリのID public $ex_page_top = array(); // 除外する固定ページ LEVEL 1 の id_str public $ex_cat_top = array(); // 除外するカテゴリ LEVEL 1 の id_str public $new_page_cat = array(); // 新規ページ・カテゴリの id_str // 変数初期化 function init_var( $hide_empty ) { // フロントページID // フロントページのタイプ $show_on_front = get_option( 'show_on_front', '' ); // フロントページ=固定ページの場合 Show_on_front : page if ( $show_on_front == 'page' ) { $page_on_front = get_option( 'page_on_front', '' ); $page_for_posts = get_option( 'page_for_posts', '' ); // 固定ページ・フロントページの場合 Page_on_front : 0以外 if ( $page_on_front != 0 ) $front_page_id = $page_on_front; // 固定ページ・投稿ページの場合 Page_for_posts : 0以外 else if ( $page_for_posts != 0 ) $front_page_id = $page_for_posts; else $front_page_id = 0; } // フロントページ=最新の投稿の場合 Show_on_front : posts else $front_page_id = 0; // フロントページタイトル if ( $front_page_id != 0 ) $title = get_the_title( $front_page_id ); else $title = hk_func_site_name(); // フロントページURL $home_url = get_home_url(); // id_str データ配列初期化 $this->id_strs = array( 'top', ); // 固定ページ・カテゴリのデータ配列初期化 $this->id_data = array(); // フロントページデータ $this->id_data['top'] = array( 'id' => $front_page_id, 'type' => '', 'title' => $title, 'alias' => $title, 'link' => $home_url, 'order' => 0, 'sort' => '', 'parent' => '', 'child' => array(), ); // 固定ページ一覧取得 ID順 $args = array( 'sort_column' => 'ID', 'sort_order' => 'ASC', 'hierarchical' => 0, 'post_status' => 'publish', ); $pages = get_pages( $args ); // 各固定ページの配列初期設定 $order = 0; foreach( $pages as $page ) { $id = $page->ID; // フロントページの場合 スキップ if ( $id == $front_page_id ) continue; // パスワード保護の場合 スキップ $passwd = $page->post_password; if ( $passwd ) continue; // データ取り込み ++$order; $id_str = 'page_'.$id; $title = $page->post_title; if ( $title == '' ) $title = '(無題)'; $link = get_page_link( $id ); // id_strs 配列に追加 $this->id_strs[] = $id_str; // 親ページID $parent = $page->post_parent; // フロントページの子ページの場合 if ( $parent == $front_page_id ) $parent = 0; // 親ページの場合 if ( $parent == 0 ) $parent_str = 'top'; // 子ページ以降の場合 else $parent_str = 'page_'.$parent; // データセット $this->id_data[ $id_str ] = array( 'id' => $id, 'type' => 'page', 'title' => $title, 'alias' => $title, 'link' => $link, 'order' => $order, 'sort' => '', 'parent' => $parent_str, 'child' => array(), ); } unset( $page ); // カテゴリ一覧取得 ID順 $args = array( 'orderby' => 'id', 'order' => 'ASC', 'hide_empty' => $hide_empty, ); $categories = get_categories( $args ); // 各カテゴリの配列初期設定 foreach( $categories as $category ) { ++$order; $id = $category->cat_ID; $id_str = 'cat_'.$id; $title = $category->cat_name; $link = get_category_link( $id ); $sort = '4'; // ソート方法:投稿日降順 // id_strs 配列に追加 $this->id_strs[] = $id_str; // 親カテゴリID $parent = $category->parent; // 親カテゴリの場合 if ( $parent == 0 ) $parent_str = 'top'; // 子カテゴリ以降の場合 else $parent_str = 'cat_'.$parent; // データセット $this->id_data[ $id_str ] = array( 'id' => $id, 'type' => 'cat', 'title' => $title, 'alias' => $title, 'link' => $link, 'order' => $order, 'sort' => $sort, 'parent' => $parent_str, 'child' => array(), ); } unset( $category ); // 子データ配列作成 foreach( $this->id_strs as $id_str ) { if ( $id_str == 'top' ) continue; $parent_str = $this->id_data[ $id_str ]['parent']; $this->id_data[ $parent_str ]['child'][] = $id_str; } unset( $id_str ); return; } // DB読み出し // 戻り値 true :正常終了 // false:サイトマップ未設定 function db_read() { $this->new_page_cat = array(); $this->ex_s_pages = array(); $this->ex_s_cats = array(); // サイトマップ設定済みかチェック $db_data = get_option( 'hk_sitemap_id_data', '' ); if ( $db_data == '' ) return false; // key 書き換え $db_data = str_replace( 'id_str_csv=', 'id_strs=', $db_data ); $db_data = str_replace( 'alias_csv=', 'aliases=', $db_data ); $db_data = str_replace( 'order_csv=', 'orders=', $db_data ); $db_data = str_replace( 'sort_csv=', 'sorts=', $db_data ); // 分解 $key_value = hk_func_data_explode_full( $db_data ); // $id_strs, $aliases, $orders, $sorts, extract( $key_value ); // 初期値を置き換え $count_db = count( $id_strs ); for ( $i=0; $i<$count_db; $i++ ) { $id_str = $id_strs[ $i ]; // 該当ID(id_str)が無ければスキップ (削除された場合) if ( ! isset( $this->id_data[ $id_str ] ) ) continue; // 別名 $alias = $aliases[ $i ]; $alias = hk_func_hex2spchar( $alias ); // TOP if ( $id_str == 'top' ) $this->id_data['top']['alias'] = $alias; // その他 else { $this->id_data[ $id_str ]['alias'] = $alias; $this->id_data[ $id_str ]['order'] = $orders[ $i ]; $this->id_data[ $id_str ]['sort'] = $sorts[ $i ]; } } // 新ページ・カテゴリ $count_init = count( $this->id_strs ); $order = max( $count_db, $count_init ) + 1; foreach( $this->id_strs as $id_str ) { // DBの配列中になければ新ページ・カテゴリ if ( ! in_array( $id_str, $id_strs ) ) { // 配列作成 $this->new_page_cat[] = $id_str; // 最終順位にデータ書き換え $this->id_data[ $id_str ]['order'] = $order++; } } unset( $id_str ); // 検索から除外する固定ページのID・カテゴリのID $db_data = get_option( 'hk_sitemap_etc', '' ); // 分解 $key_value = hk_func_data_explode_half( $db_data ); // $ex_s_page_csv, $ex_s_cat_csv extract( $key_value ); // 存在チェック ページ $ex_s_pages_new = array(); if ( $ex_s_page_csv != '' ) { $ex_s_pages = explode( ',', $ex_s_page_csv ); foreach( $ex_s_pages as $id ) { $id_str = 'page_'.$id; if ( isset( $this->id_data[ $id_str ] ) ) $ex_s_pages_new[] = $id; } unset( $id ); } $this->ex_s_pages = $ex_s_pages_new; // 存在チェック カテゴリ $ex_s_cats_new = array(); if ( $ex_s_cat_csv != '' ) { $ex_s_cats = explode( ',', $ex_s_cat_csv ); foreach( $ex_s_cats as $id ) { $id_str = 'cat_'.$id; if ( isset( $this->id_data[ $id_str ] ) ) $ex_s_cats_new[] = $id; } unset( $id ); } $this->ex_s_cats = $ex_s_cats_new; return true; } // 除外ページ・カテゴリの処理 function exclude_page_cat() { $this->ex_pages = array(); $this->ex_cats = array(); $this->ex_page_top = array(); $this->ex_cat_top = array(); // 子の処理 if ( empty( $this->id_data['top']['child'] ) ) return; $this->exclude_page_cat_recursive( 'top' ); return; } // 除外ページ・カテゴリの再帰処理 private function exclude_page_cat_recursive( $id_str ) { // 子の処理 if ( empty( $this->id_data[ $id_str ]['child'] ) ) return; $children = $this->id_data[ $id_str ]['child']; foreach( $children as $child ) { // 除外 if ( $this->id_data[ $child ]['order'] === '' ) { // 子配列から削除 $new_children = $this->id_data[ $id_str ]['child']; $key = array_search( $child, $new_children ); unset( $new_children[ $key ] ); $new_children = array_merge( $new_children ); $this->id_data[ $id_str ]['child'] = $new_children; // 子を除外 LEVEL 1 に追加 $type = $this->id_data[ $child ]['type']; if ( $type == 'page' ) $this->ex_page_top[] = $child; else $this->ex_cat_top[] = $child; // 子・孫グループ一括除外 $this->exclude_group_recursive( $child ); } else $this->exclude_page_cat_recursive( $child ); } unset( $child ); return; } // 除外ページ・カテゴリの再帰処理(子・孫グループ一括除外) private function exclude_group_recursive( $id_str ) { // データ $this->id_data[ $id_str ]['order'] = ''; $type = $this->id_data[ $id_str ]['type']; $id = $this->id_data[ $id_str ]['id']; // 除外ページ・カテゴリの配列へ追加 if ( $type == 'page' ) $this->ex_pages[] = $id; else $this->ex_cats[] = $id; // 子の処理 if ( empty( $this->id_data[ $id_str ]['child'] ) ) return; $children = $this->id_data[ $id_str ]['child']; // 子の再帰処理 foreach( $children as $child ) $this->exclude_group_recursive( $child ); unset( $child ); return; } // サイト構造解析 再帰処理 function site_structure_recursive( $children, &$order_new, &$level, $map_order ) { // ID-ORDER 配列作成 $id_order = array(); foreach( $children as $child ) $id_order[ $child ] = $this->id_data[ $child ]['order']; unset( $child ); // ソート asort( $id_order ); // サイト構造 foreach( $id_order as $child => $value ) { // order の再付番 $this->id_data[ $child ]['order'] = $order_new++; // データ $map_order[ $child ] = $level; // 孫の再帰処理 $grandchildren = $this->id_data[ $child ]['child']; if ( ! empty( $grandchildren ) ) { ++$level; $map_order = $this->site_structure_recursive( $grandchildren, $order_new, $level, $map_order ); --$level; } } unset( $child ); unset( $value ); return $map_order; } }
説明
下記関数に関しては“共通関数”をご覧ください。
hk_func_site_name
hk_func_data_explode_full
hk_func_data_explode_half
hk_func_hex2spchar
このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。