7. 2 フォームメール 設定メニュー(1)

 自作のフォームメールプラグインの管理画面に設定メニューを表示するコードの前半です。フォームの一覧表示と新規作成、編集、コピー、削除の機能選択と削除の実行部です。
 一覧には、“使用中のページ・投稿”も表示します。

コード

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
class Hk_Form_Mail_Admin_1 {
    private $index_message = '';    // 管理画面メッセージ

    // 管理画面メイン
    //   フォーム選択, フォーム削除
    function admin_main() {
        global $hk_form_mail;
        global $hk_form_mail_admin_2;
        // 送信ボタン
        if ( ! isset( $_POST['admin_next'] ) )
                $admin_next = 'フォーム選択';
        else    $admin_next = hk_func_post_read( 'admin_next', 1 );
        // フォーム選択
        if ( $admin_next == 'フォーム選択' ) {
            $this->index_message = '';
            $this->show_form_select();
            return;
        }
        // フォーム番号取り込み
        if ( ! isset( $_POST['form_no'] ) )
                $form_no = 0;
        else    $form_no = hk_func_post_read( 'form_no', 1 );
        // フォーム削除
        if ( $admin_next == '削除' ) {
            if ( $form_no == 0 ) {
                $this->index_message = 'フォームを選択してください';
                $this->show_form_select();
                return;
            }
            // データベースからフォームデータの読み込み
            $form_data = $hk_form_mail->db_read( $form_no );
            if ( $form_data == '' ) {
                $this->index_message = 'すでに削除されています';
                $this->show_form_select();
                return;
            }
            // 削除の確認画面表示
            $this->show_del_confirm( $form_no, $form_data );
            return;
        }
        // フォーム削除実行 = データベースより削除
        if ( $admin_next == '削除実行' ) {
            $hk_form_mail->db_delete( $form_no );
            $this->index_message = '削除しました';
            $this->show_form_select();
            return;
        }
        // 編集, コピー:エラーがなければ、admin_2 へ
        // 新規作成, 初期化, 項目追加, 並び替え, 変更を保存:admin_2 へ
        if (( $admin_next == '編集' ) || ( $admin_next == 'コピー' )) {
            if ( $form_no == 0 ) {
                $this->index_message = 'フォームを選択してください';
                $this->show_form_select();
                return;
            }
        }
        if ( $admin_next == 'コピー' ) {
            // データベースからフォームデータの読み込み
            $form_data = $hk_form_mail->db_read( $form_no );
            if ( $form_data == '' ) {
                $this->index_message = 'コピーするデータがありません';
                $this->show_form_select();
                return;
            }
        }
        // admin_2 へ
        $hk_form_mail_admin_2->admin_main_2( $form_no, $admin_next );
        return;
    }

    // フォーム選択画面表示
    private function show_form_select() {
        global $hk_form_mail;
        // 表示開始
        echo "\n\n".'<div class="hk_plugin_admin">'."\n";
        echo '  <h1>フォームメール 設定</h1>'."\n";
        // メッセージ表示
        if ( $this->index_message ) {
            $this->index_message = trim( $this->index_message );
            $this->index_message = nl2br( $this->index_message );
            echo '  <div class="hk_message">'."\n";
            echo $this->index_message."\n";
            echo '  </div>'."\n";
        }
        // フォーム選択表示開始
        echo '  <form method="post">'."\n";
        echo '    <div class="hk_chap">'."\n";
        echo '      <h3>フォーム選択</h3>'."\n";
        // データベースからフォーム実在数の読み込み
        $form_exist = $hk_form_mail->db_read_index();
        if ( $form_exist == 0 )
            echo '<p>設定されたフォームはありません</p>'."\n";
        else {
            echo '<table><tbody>'."\n";
            echo '<tr>'."\n";
            echo '  <th>NO.</th>'."\n";
            echo '  <th>選択</th>'."\n";
            echo '  <th>名称</th>'."\n";
            echo '  <th>ショートコード</th>'."\n";
            echo '  <th>使用中のページ・投稿</th>'."\n";
            echo '</tr>'."\n";
            for ( $i=1; $i<=$form_exist; $i++ ) {
                echo '<tr>'."\n";
                echo '  <td>NO.'.$i.'</td>'."\n";
                echo '  <td class="hk_centerRL"><input type="radio" name="form_no" value="'.$i.'" /></td>'."\n";
                // ショートコード
                $short_code = '[hk-form-mail form_no='.$i.']';
                // 使用中のページ・投稿
                $page_post = '';
                $args = array(
                    'post_type'      => 'any',
                    'post_status'    => 'any',
                    'orderby'        => 'date',
                    'order'          => 'DESC',
                    's'              => $short_code,
                    'posts_per_page' => -1,
                );
                $my_query = new WP_Query( $args );
                if ( $my_query->have_posts() ) {
                    $page_posts = array();
                    while ( $my_query->have_posts() ) {
                        $my_query->the_post();
                        $post = get_post();
                        if ( isset( $post->post_title ) ) {
                            $post_title = $post->post_title;
                            $post_title = htmlspecialchars( $post_title );
                            $page_posts[] = $post_title;
                        }
                    }
                    wp_reset_postdata();
                    $page_post = implode( '<br />', $page_posts );
                }
                // データベースからフォームデータの読み込み
                $form_data = $hk_form_mail->db_read( $i );
                if ( $form_data ) {
                    $form_name = $form_data['form_name'];
                    $form_name = htmlspecialchars( $form_name );
                    echo '  <td>'.$form_name.'</td>'."\n";
                    echo '  <td>'.$short_code.'</td>'."\n";
                    echo '  <td>'.$page_post.'</td>'."\n";
                }
                else {
                    echo '  <td>(削除済み)</td>'."\n";
                    echo '  <td></td>'."\n";
                    echo '  <td>'.$page_post.'</td>'."\n";
                }
                echo '</tr>'."\n";
            }
            echo '</tbody></table>'."\n";
        }
        echo '      <div class="hk_sec1">'."\n";
        if ( $form_exist == 0 )
            echo '        <input type="submit" name="admin_next" value="新規作成" />'."\n";
        else {
            echo '        <input type="submit" name="admin_next" value="編集" />    '."\n";
            echo '        <input type="submit" name="admin_next" value="新規作成" />    '."\n";
            echo '        <input type="submit" name="admin_next" value="コピー" />    '."\n";
            echo '        <input type="submit" name="admin_next" value="削除" />'."\n";
        }
        echo '      </div>'."\n";
        echo '    </div>'."\n";
        echo '  </form>'."\n";
        echo '</div>'."\n";
        return;
    }

    // フォーム削除 確認画面表示
    private function show_del_confirm( $form_no, $form_data ) {
        // フォーム名称
        $form_name = $form_data['form_name'];
        $form_name = htmlspecialchars( $form_name );
        // 確認画面表示
        echo "\n\n".'<div class="hk_plugin_admin">'."\n";
        echo '  <h1>フォームメール 設定</h1>'."\n";
        echo '  <form method="post">'."\n";
        echo '    <div class="hk_chap">'."\n";
        echo '      <h3>フォーム削除</h3>'."\n";
        echo '      <p> 次のフォームを削除します。<br /> 削除する場合は、削除実行をクリックしてください。</p>'."\n";
        echo '      <input type="hidden" name="form_no" value="'.$form_no.'" />'."\n";
        echo '<table><tbody>'."\n";
        echo '<tr>'."\n";
        echo '  <th>NO.</th>'."\n";
        echo '  <th>名称</th>'."\n";
        echo '</tr>'."\n";
        echo '<tr>'."\n";
        echo '  <td>NO.'.$form_no.'</td>'."\n";
        echo '  <td>'.$form_name.'</td>'."\n";
        echo '</tr>'."\n";
        echo '</tbody></table>'."\n";
        echo '      <div class="hk_sec1">'."\n";
        echo '        <input type="submit" name="admin_next" value="削除実行" />    '."\n";
        echo '        <input type="submit" name="admin_next" value="フォーム選択" />'."\n";
        echo '      </div>'."\n";
        echo '    </div>'."\n";
        echo '  </form>'."\n";
        echo '</div>'."\n";
        return;
    }
}

説明

 下記関数に関しては“共通関数”をご覧ください。
  hk_func_post_read

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

関連

更新日:2016/01/24
掲載日:2014/08/10