6. 2 サイトマップ 設定

 サイトマップの次の設定を行います。
 ・表示順設定
 ・表示名(別名)設定
 ・カテゴリの場合に投稿表示順設定
  タイトル昇順・降順、投稿日昇順・降順、スラッグ昇順・降順
 ・サイトマップから除外する固定ページの設定
 ・サイトマップから除外するカテゴリの設定
 ・検索から除外する固定ページの設定
 ・検索から除外するカテゴリの設定
 表示順序を空欄に、あるいは表示順序に数字以外を入力するとサイトマップから除外できます。
 別名は、メニューやローカルナビに使用します。
 「データ連係」をクリックすると、検索から除外する固定ページ・カテゴリにサイトマップから除外する固定ページ・カテゴリを追加できます。

コード

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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
class Hk_Sitemap_Admin {
    // ------ private プロパティ ------
    private $admin_message = '';        // 管理画面メッセージ
    private $ex_s_cat_csv  = '';        // 検索から除外するカテゴリのID
    private $ex_s_page_csv = '';        // 検索から除外する固定ページのID

    // 管理画面メイン
    function admin_main() {
        global $hk_map_com;
        // 変数初期化
        $hide_empty = 0;
        $hk_map_com->init_var( $hide_empty );
        // メッセージ初期化
        $this->admin_message = '';
        // 送信ボタンの名称
        $admin_next = '';
        if ( isset( $_POST["admin_next"] ) )
            $admin_next = hk_func_post_read( 'admin_next', 1 );
        // 通常のページアクセス
        if ( $admin_next == '' ) {
            // DB読み出し・初期値の置き換え
            $hk_map_com->db_read();
            $this->ex_s_page_csv = implode( ',', $hk_map_com->ex_s_pages );
            $this->ex_s_cat_csv  = implode( ',', $hk_map_com->ex_s_cats );
        }
        // 並べ替え
        // データ連係 前処理
        // 変更を保存 前処理
        else if (( $admin_next == '並べ替え' )
         || ( $admin_next == 'データ連係' )
         || ( $admin_next == '変更を保存' ))
            // フォームデータ取り込み
            $this->admin_post_data();
        // 初期化
        else if ( $admin_next == '初期化' ) {
            $this->ex_s_cat_csv  = '';
            $this->ex_s_page_csv = '';
            $hk_map_com->new_page_cat = array();
        }
        // その他
        else    wp_die('ERROR:hk-sitemap:admin_next');
        // 除外固定ページ・カテゴリの処理
        if ( $admin_next == '初期化' ) {
            $hk_map_com->ex_pages    = array();
            $hk_map_com->ex_cats     = array();
            $hk_map_com->ex_page_top = array();
            $hk_map_com->ex_cat_top  = array();
        }
        else
            $hk_map_com->exclude_page_cat();
        // サイト構造データ解析
        // 公開
        $map_order_open = array();
        $order_new = 1;
        $level     = 1;
        $children = $hk_map_com->id_data['top']['child'];
        if ( ! empty( $children ) )
            $map_order_open = $hk_map_com->site_structure_recursive( $children, $order_new, $level, $map_order_open );
        // 除外ページ
        $map_order_ex_page = array();
        $level = 1;
        $children = $hk_map_com->ex_page_top;
        if ( ! empty( $children ) )
            $map_order_ex_page = $this->site_structure_ex_recursive( $children, $level, $map_order_ex_page );
        // 除外カテゴリ
        $map_order_ex_cat = array();
        $level = 1;
        $children = $hk_map_com->ex_cat_top;
        if ( ! empty( $children ) )
            $map_order_ex_cat = $this->site_structure_ex_recursive( $children, $level, $map_order_ex_cat );
        // 後処理
        if ( $this->admin_message == '' ) {
            // 並べ替え
            if ( $admin_next == '並べ替え' )
                 $this->admin_message = '並べ替えました。';
            // 初期化
            else if ( $admin_next == '初期化' )
                $this->admin_message = '初期化しました。';
            // データ連係 後処理
            else if ( $admin_next == 'データ連係' ) {
                // 検索から除外する固定ページ
                if ( $this->ex_s_page_csv != '' )
                        $ex_s_pages = explode( ',', $this->ex_s_page_csv );
                else    $ex_s_pages = array();
                // +サイトマップから除外するページ
                $ex_s_pages = array_merge( $ex_s_pages, $hk_map_com->ex_pages );
                // 配列から重複した値を削除
                $ex_s_pages = array_unique( $ex_s_pages );
                asort( $ex_s_pages );
                $this->ex_s_page_csv = implode( ",", $ex_s_pages );
                // 検索から除外するカテゴリ
                if ( $this->ex_s_cat_csv != '' )
                        $ex_s_cats = explode( ',', $this->ex_s_cat_csv );
                else    $ex_s_cats = array();
                // +サイトマップから除外するカテゴリ
                $ex_s_cats = array_merge( $ex_s_cats, $hk_map_com->ex_cats );
                // 配列から重複した値を削除
                $ex_s_cats = array_unique( $ex_s_cats );
                asort( $ex_s_cats );
                $this->ex_s_cat_csv = implode( ",", $ex_s_cats );
                // メッセージ
                 $this->admin_message = '検索から除外する固定ページ・カテゴリのデータ連係を行いました。';
            }
            // 変更を保存 後処理
            else if ( $admin_next == '変更を保存' ) {
                // DB書き込み
                $this->db_write();
                $this->admin_message = '変更を保存しました。';
            }
        }
        // 管理画面に表示
        echo "\n\n".'<div class="hk_plugin_admin">'."\n";
        echo '<h1>統合型 サイトマップ 設定</h1>'."\n";
        if ( $this->admin_message != '' ) {
            $this->admin_message = trim( $this->admin_message );
            $this->admin_message = nl2br( $this->admin_message );
            echo '<div class="hk_message">'."\n";
            echo $this->admin_message."\n";
            echo '</div>'."\n";
        }
        // フォーム表示開始
        echo '<form method="post">'."\n";
        echo '<h2>サイトマップ設定</h2>'."\n";
        echo '<div class="hk_chap">'."\n";
        // ショートコード
        echo '  <p>ショートコード = [hk-sitemap]</p>'."\n";
        // 説明表示
        echo '  <h3>サイトマップ表示順序・表示タイトル</h3>'."\n";
        echo '  <p>表示順序・表示タイトルを変更できます。<br />'."\n";
        echo '・表示順序を空欄に、あるいは表示順序に数字以外を入力するとサイトマップから除外できます。<br />'."\n";
        echo '・タイトルの後の( )内はID番号です。</p>'."\n";
        echo '  <div class="hk_sitemap1">'."\n";
        // 新規ページ・カテゴリ
        $new_page_cat_csv = implode( ",", $hk_map_com->new_page_cat );
        echo '<input type="hidden" name="new_page_cat" value="'.$new_page_cat_csv.'" />'."\n";
        // フロントページ
        $id    = $hk_map_com->id_data['top']['id'];
        $alias = $hk_map_com->id_data['top']['alias'];
        $alias = htmlspecialchars( $alias );
        if ( $id == 0 )        $id_disp = '--';
        else                 $id_disp = $id;
        echo '    <ul>'."\n";
        echo '<li><input type="text" name="top_alias" size="40" value="'.$alias.'" /> ('.$id_disp.')'."\n";
        // 構造表示
        $this->print_site_structure( $map_order_open );
        echo "    </li></ul>\n";
        echo '  </div>'."\n";
        // サイトマップから除外する固定ページ
        echo '  <h3>サイトマップから除外する固定ページ</h3>'."\n";
        if ( empty( $hk_map_com->ex_pages ) )
            echo '  <p>ありません</p>'."\n";
        else {
            echo '  <div class="hk_sitemap1">'."\n";
            $this->print_site_structure( $map_order_ex_page );
            echo '  </div>'."\n";
        }
        // サイトマップから除外するカテゴリ
        echo '  <h3>サイトマップから除外するカテゴリ</h3>'."\n";
        if ( empty( $hk_map_com->ex_cats ) )
            echo '<p>ありません</p>'."\n";
        else {
            echo '  <div class="hk_sitemap1">'."\n";
            $this->print_site_structure( $map_order_ex_cat );
            echo '  </div>'."\n";
        }
        // SUBMIT ボタン
        echo '<p><input type="submit" name="admin_next" value="並べ替え" /></p>'."\n";
        echo '</div>'."\n";
        // 検索制限
        echo '<h2>検索制限設定</h2>'."\n";
        echo '<div class="hk_chap">'."\n";
        echo '  <h3>検索から除外する固定ページ</h3>'."\n";
        echo '  <p> 固定ページの ID を入力してください。複数の場合は「 , 」(半角カンマ)で区切ってください。<br />'."\n";
        echo ' <input type="text" name="ex_s_page_csv" size="40" value="'.$this->ex_s_page_csv.'" /> '."</p>\n";
        echo '  <h3>検索から除外するカテゴリ</h3>'."\n";
        echo '  <p> カテゴリの ID を入力してください。複数の場合は「 , 」(半角カンマ)で区切ってください。<br />'."\n";
        echo ' <input type="text" name="ex_s_cat_csv" size="40" value="'.$this->ex_s_cat_csv.'" /> '."</p>\n";
        // SUBMIT ボタン
        echo '<p><input type="submit" name="admin_next" value="データ連係" /></p>'."\n";
        echo '</div>'."\n";
        // SUBMIT ボタン
        echo '<p><input type="submit" name="admin_next" value="初期化" />   '."\n";
        echo '<input type="submit" name="admin_next" value="変更を保存" /></p>'."\n";
        echo "</form>\n";
        // END
        echo '</div>'."\n";
        return;
    }

    // POSTされたデータ群の取り込み
    private function admin_post_data() {
        global $hk_map_com;
        // IDstr 毎にデータの取り込み
        foreach( $hk_map_com->id_strs as $id_str ) {
            // 別名
            $id_str_alias = $id_str.'_alias';
            $alias = hk_func_post_read( $id_str_alias, 1 );
            if ( $alias == '' )
                $alias = $hk_map_com->id_data[ $id_str ]['title'];
            $hk_map_com->id_data[ $id_str ]['alias'] = $alias;
            if ( $id_str == 'top' )                continue;
            // 順番 (数字だけで構成されていなければ削除扱い)
            $id_str_order = $id_str.'_order';
            $order = hk_func_post_read( $id_str_order, 1 );
            if ( ! preg_match( '/^[0-9]+$/', $order ) )
                $order = '';
            $hk_map_com->id_data[ $id_str ]['order'] = $order;
            // ソート方法
            $id_str_sort = $id_str.'_sort';
            $hk_map_com->id_data[ $id_str ]['sort'] = hk_func_post_read( $id_str_sort, 1 );
        }
        unset( $id_str );
        // 新規ページ・カテゴリ
        $new_page_cat_csv = hk_func_post_read( 'new_page_cat', 1 );
        if ( $new_page_cat_csv != '' )
                $hk_map_com->new_page_cat = explode( ',', $new_page_cat_csv );
        else    $hk_map_com->new_page_cat = array();
        // 検索から除外する固定ページ
        list( $csv_data, $rtc ) = hk_func_post_csv_read( 'ex_s_page_csv', '' );
        $this->ex_s_page_csv = $csv_data;
        if ( $rtc == -1 )
            $this->admin_message .= '検索から除外する固定ページの書式が違います。'."\n";
        else {
            if ( $this->ex_s_page_csv != '' ) {
                $ex_s_pages = explode( ',', $this->ex_s_page_csv );
                foreach( $ex_s_pages as $id ) {
                    $id_str = 'page_'.$id;
                    // 存在チェック
                    if ( ! isset( $hk_map_com->id_data[ $id_str ] ) )
                        $this->admin_message .= '検索から除外する固定ページ('.$id.')が存在しません。'."\n";
                }
                unset( $id );
                if ( $this->admin_message == '' ) {
                    asort( $ex_s_pages );
                    $this->ex_s_page_csv = implode( ',', $ex_s_pages );
                }
            }
        }
        // 検索から除外するカテゴリ
        list( $csv_data, $rtc ) = hk_func_post_csv_read( 'ex_s_cat_csv', '' );
        $this->ex_s_cat_csv = $csv_data;
        if ( $rtc == -1 )
            $this->admin_message .= '検索から除外するカテゴリの書式が違います。'."\n";
        else {
            if ( $this->ex_s_cat_csv != '' ) {
                $ex_s_cats = explode( ',', $this->ex_s_cat_csv );
                foreach( $ex_s_cats as $id ) {
                    $id_str = 'cat_'.$id;
                    // 存在チェック
                    if ( ! isset( $hk_map_com->id_data[ $id_str ] ) )
                        $this->admin_message .= '検索から除外するカテゴリ('.$id.')が存在しません。'."\n";
                }
                unset( $id );
                if ( $this->admin_message == '' ) {
                    asort( $ex_s_cats );
                    $this->ex_s_cat_csv = implode( ',', $ex_s_cats );
                }
            }
        }
        return;
    }

    // サイト構造解析 除外 再帰処理
    private function site_structure_ex_recursive( $children, &$level, $map_order ) {
        global $hk_map_com;
        // ID-ORDER 配列作成
        $id_order = array();
        foreach( $children as $child )
            $id_order[ $child ] = $hk_map_com->id_data[ $child ]['id'];
        unset( $child );
        // ソート
        asort( $id_order );
        // サイト構造
        foreach( $id_order as $child => $value ) {
            // データ
            $map_order[ $child ] = $level;
            // 孫の再帰処理
            $grandchildren = $hk_map_com->id_data[ $child ]['child'];
            if ( ! empty( $grandchildren ) ) {
                ++$level;
                $map_order = $this->site_structure_ex_recursive( $grandchildren, $level, $map_order );
                --$level;
            }
        }
        unset( $child );
        unset( $value );
        return $map_order;
    }

    // DB書き込み
    private function db_write() {
        global $hk_map_com;
        // ALIAS, ORDER, SORT
        $aliases = array();
        $orders  = array();
        $sorts   = array();
        // top も書き込む
        foreach( $hk_map_com->id_strs as $id_str ) {
            $alias     = $hk_map_com->id_data[ $id_str ]['alias'];
            $aliases[] = hk_func_spchar2hex( $alias );
            $orders[]  = $hk_map_com->id_data[ $id_str ]['order'];
            $sorts[]   = $hk_map_com->id_data[ $id_str ]['sort'];
        }
        unset( $id_str );
        $id_str_csv = implode( ',', $hk_map_com->id_strs );
        $alias_csv  = implode( ',', $aliases );
        $order_csv  = implode( ',', $orders );
        $sort_csv   = implode( ',', $sorts );
        // DB 書き込み
        $db_data  = 'id_str_csv='.$id_str_csv.'&';
        $db_data .= 'alias_csv='. $alias_csv.'&';
        $db_data .= 'order_csv='. $order_csv.'&';
        $db_data .= 'sort_csv='.  $sort_csv;
        update_option( 'hk_sitemap_id_data', $db_data );
        // 検索から除外する固定ページのID・カテゴリのID
        $db_data  = 'ex_s_page_csv='.$this->ex_s_page_csv.'&';
        $db_data .= 'ex_s_cat_csv='. $this->ex_s_cat_csv;
        update_option( 'hk_sitemap_etc', $db_data );
        return;
    }

    // サイト構造表示
    private function print_site_structure( $map_order ) {
        global $hk_map_com;
        $level_now = 0;
        foreach( $map_order as $id_str => $level ) {
            if ( $level > $level_now ) {
                echo "\n    ";
                for ( $i=1; $i<=$level_now+1; $i++ )        echo '  ';
                echo "<ul>\n";
                $level_now = $level;
            }
            else if ( $level < $level_now ) {
                echo "</li>\n";
                $i_max = $level_now - $level;
                for ( $i=1; $i<=$i_max; $i++ ) {
                    echo '    ';
                    for ( $j=$i; $j<=$level_now; $j++ )     echo '  ';
                    echo "</ul></li>\n";
                }
                $level_now = $level;
            }
            else
                echo "</li>\n";
            // データ
            $type  = $hk_map_com->id_data[ $id_str ]['type'];
            $id    = $hk_map_com->id_data[ $id_str ]['id'];
            $order = $hk_map_com->id_data[ $id_str ]['order'];
            $order = htmlspecialchars( $order );
            $alias = $hk_map_com->id_data[ $id_str ]['alias'];
            $alias = htmlspecialchars( $alias );
            $sort  = $hk_map_com->id_data[ $id_str ]['sort'];
            // 順番
            echo '<li><input type="text" name="'.$id_str.'_order" size="3" maxlength="3" value="'.$order.'" /> '."\n";
            // 新規ページ・カテゴリ
            if ( in_array( $id_str, $hk_map_com->new_page_cat ) )
                echo '<span class="hk_italic">NEW</span> '."\n";
            // 別名
            echo '<input type="text" name="'.$id_str.'_alias" size="40" value="'.$alias.'" /> ('.$id.')';
            // 投稿ソート順選択
            if ( $type == 'cat' ) {
                $html = $this->sort_option_set( $id_str, $sort );
                echo " \n".$html;
            }
            else
                echo "\n".'<input type="hidden" name="'.$id_str.'_sort"  value="" />';
        }
        unset( $id_str );
        unset( $level );
        echo "</li>\n";
        $i_max = $level_now - 1;
        for ( $i=1; $i<=$i_max; $i++ ) {
            echo '    ';
            for ( $j=$i; $j<=$level_now; $j++ )        echo '  ';
            echo "</ul></li>\n";
        }
        echo "      </ul>\n";
        return;
    }

    // ソート順選択 入力欄作成
    //   戻り値:HTML
    private function sort_option_set( $id_str, $selected ) {
        $sort_option =  array(
            '1' => 'タイトル昇順',
            '2' => 'タイトル降順',
            '3' => '投稿日昇順',
            '4' => '投稿日降順',
            '5' => 'スラッグ昇順',
            '6' => 'スラッグ降順',
        );
        $html  = '';
        $html .= '<select size="1" name="'.$id_str.'_sort">'."\n";
        foreach( $sort_option as $key => $value ) {
            $html .= '  <option value="'.$key.'"';
            if ( $key == $selected )    $html .= ' selected="selected"';
            $html .= '>'.$value.'</option>'."\n";
        }
        unset( $key );
        unset( $value );
        $html .= '</select>';
        return $html;
    }
}

説明

 データベースには下記2個のデータを登録しています。
  'hk_sitemap_id_data':サイトマップの主要データ
  'hk_sitemap_etc':検索から除外する固定ページのID・カテゴリのID
 下記関数に関しては“共通関数”をご覧ください。
  hk_func_post_read
  hk_func_post_csv_read
  hk_func_spchar2hex

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

関連

更新日:2016/02/27
掲載日:2016/01/22