網站 / LINE Notify 推播 版本2

原來的版本是將插圖寫在外掛的php檔案中

如果要修改的話,可以透過兩種方式

第一種是直接修改php檔案後,再重新上傳外掛程式

另一種是直接在外掛編輯器裡修改

但是這兩種方式都是直接動到全部程式

所以想說能不能從外掛設定頁面中改需要修改的部分就好

外掛的設定頁面參考了《WordPress站長練功秘笈:網站客製化、佈景主題與外掛開發的16堂課》中的範例

但由於我想要修改的設定會有兩個參數:stickerPackageId跟stickerId

如果只想在wp_option增加一個紀錄,那麼外掛程式的參數都必須用陣列的方式儲存到資料庫

整體外掛程式可以分成A:主要功能、B:外掛頁面的設定

A的部分就是套用前一版的兩種add_action

程式碼在#23~#103

而B是這次練習的核心部分

可以再分成

B1-設定頁面內容,程式碼在#105~#163

#105~#110:透過add_action的admin_enqueue_scripts鈎掛trico_line_load_bootstrap,來引用外部bootstrap的CSS、JavaScript

#110~#163 :trico_line_mainpage是外掛設定頁面的內容

B2-wp資料庫處理

B2-1 #9~#21:設定外掛功能選項在wp_options的初始值;以及在其他函式都會用到的全域變數

B2-2 #164~#177:透過add_action的admin_menu鈎掛外掛設定選單-trico_line_setting_menu,呼叫外掛設定頁面的函式-trico_line_mainpage,再透過add_action的admin_init鈎掛外掛初始設定-trico_line_register_settings

B2-3 #178~#186:trico_line_sanitize_options-變數預處理,這個則是鈎掛於外掛初始設定-trico_line_register_settings

B2-4  #188~#193:trico_line_register_settings-外掛初始設定,在外掛設定選單-trico_line_setting_menu中被呼叫

可以發現每個函式都是相互鈎掛與呼叫,而其中trico_line_register_settings-外掛初始設定register_setting的變數群組,又必須跟表單內的settings_fields的變數群組一致

  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
<?php
/*
Plugin Name: @ tricohobby LINE Notify 文章推播
Plugin URI: 
Description: LINE Notify 
Version: 0.1a V5
Author: Hsing-Yen Chuang
Author URI: 
*/
//trico_line_options的預設值,如果陣列索引值為數字時要省略
$sr=array("stickerPackageId"=>array(1,1),"stickerId"=>array(114,106));

////判斷是否有trico_line_options	 
if(!get_option( 'trico_line_options' )){
	add_option( 'trico_line_options' ,$value=$sr);
}

//全域變數 抓取 在wp_option的設定
$trico_line_options = get_option( 'trico_line_options' );
// count() 取得物件的數量
$cc=count($trico_line_options);

//發佈文章的通知
//
function publishOne($new_status, $old_status, $post){
	global $trico_line_options;
	$line_options_sPId_p = esc_attr( $trico_line_options['stickerPackageId'][0]);
	$line_options_sId_p = esc_attr( $trico_line_options['stickerId'][0]);
	$postOne = get_post($post); 
	$posrUrl = get_permalink($post);
	$titleOne = $postOne->post_title;
	$author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'nickname', $author );
	//判斷文章狀態
	if (  $new_status == "publish" &&  $old_status !== "publish" && $post->post_type == "post") 
	{         
		$initData['message'] = $name."剛剛發佈".$titleOne."→".$posrUrl;
		//$initData['imageThumbnail'] = 'https://i2.kknews.cc/SIG=1hsq8os/ctp-vzntr/r3sn77r39o364q46nps4n57pop734q35.jpg';
		//$initData['imageFullsize'] = 'https://i2.kknews.cc/SIG=1hsq8os/ctp-vzntr/r3sn77r39o364q46nps4n57pop734q35.jpg';
		$initData['stickerPackageId'] = $line_options_sPId_p;
		$initData['stickerId'] = $line_options_sId_p;  
		$token = '*******************************************';
		$url = 'https://notify-api.line.me/api/notify';

		$ch = curl_init();
		$header[] = 'Authorization: Bearer';
		$header[] = $token;
		curl_setopt_array($ch, array(
			CURLOPT_URL => $url,
			CURLOPT_POST => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYHOST => 0,
			CURLOPT_SSL_VERIFYPEER => 0,
			CURLOPT_HTTPHEADER => array(implode(' ',$header)),
			CURLOPT_POSTFIELDS => http_build_query($initData),
		));

		$result = curl_exec($ch);
		curl_close($ch); 
	}
}
	add_action('transition_post_status','publishOne',10,3);

//更新文章的通知
//
function updated($new_status, $old_status, $post){
	global $trico_line_options;
	$line_options_sPId_p = esc_attr( $trico_line_options['stickerPackageId'][1]);
	$line_options_sId_p = esc_attr( $trico_line_options['stickerId'][1]);
	$postOne = get_post($post); 
	$posrUrl = get_permalink($post);
	$titleOne = $postOne->post_title;
	$author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'nickname', $author );
	//判斷文章狀態
	if ( $old_status == "publish"  &&  $new_status == "publish" &&  $post->ID != 16) // ID是佈景主題自訂的CSS
	{         
		$initData['message'] = $name."剛剛更新".$titleOne."→".$posrUrl;
		//$initData['imageThumbnail'] = 'https://i2.kknews.cc/SIG=1hsq8os/ctp-vzntr/r3sn77r39o364q46nps4n57pop734q35.jpg';
		//$initData['imageFullsize'] = 'https://i2.kknews.cc/SIG=1hsq8os/ctp-vzntr/r3sn77r39o364q46nps4n57pop734q35.jpg';
		$initData['stickerPackageId'] = $line_options_sPId_p;
		$initData['stickerId'] = $line_options_sId_p;  
		$token = 'vHeBOa11eMnx7kNZlBKJLqiKJWb28uiL5fzcXBVKUUi';
		$url = 'https://notify-api.line.me/api/notify';

		$ch = curl_init();
		$header[] = 'Authorization: Bearer';
		$header[] = $token;
		curl_setopt_array($ch, array(
			CURLOPT_URL => $url,
			CURLOPT_POST => TRUE,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYHOST => 0,
			CURLOPT_SSL_VERIFYPEER => 0,
			CURLOPT_HTTPHEADER => array(implode(' ',$header)),
			CURLOPT_POSTFIELDS => http_build_query($initData),
		));

		$result = curl_exec($ch);
		curl_close($ch); 
	}
}
	add_action('transition_post_status','updated',10,3);

//抓取外部資源
function trico_line_load_bootstrap() {
    wp_enqueue_style( 'bootstrap_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
    wp_enqueue_script( 'bootstrap_js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' );
}
add_action('admin_enqueue_scripts', 'trico_line_load_bootstrap');

//設定網頁設定頁面內容
function trico_line_mainpage() {
    ?>
    <br><br><br>
    <div class='row'>
        <div class='col-sm-10 col-sm-offset-1'>
            <div class='panel panel-primary'>
				<div class='panel-heading'>
                    <h2>@ tricohobby LINE Notify 文章插圖推播設定</h2>
					<?php
							global $trico_line_options; //引入全域變數$trico_line_options
							global $cc;
							//$trico_line_options = get_option( 'trico_line_options' );
							//for loop 抓出物件內容
							for ($i=0; $i < $cc ; $i++){
								//資料過濾處理
								$line_options_sPId_p = esc_attr( $trico_line_options['stickerPackageId'][$i]);
								$line_options_sId_p = esc_attr( $trico_line_options['stickerId'][$i]);
								$j="";
								if($i<1){
									$j="發佈文章";
								}else{
									$j="更新文章";
								}
					?>
					<!--輸出到html-->
					 <h3>目前設定:<?= $j; ?>--@stickerPackageId:<?= $line_options_sPId_p; ?>  @--stickerId:<?= $line_options_sId_p; ?></h3>
							<?php } ?>
                </div>
                <div class='panel-body'>
					<!--表單-->
                    <form method='post' action='options.php'>
						<?php
							settings_fields('trico_line-settings-group'); //設定變數群組,配合register_setting
							for ($i=0; $i <$cc ; $i++){
								$line_options_sPId_i = esc_attr( $trico_line_options['stickerPackageId'][$i]);
								$line_options_sId_i = esc_attr( $trico_line_options['stickerId'][$i]);
						?>
						stickerPackageId:<input type='text' name=<?php echo '"trico_line_options[stickerPackageId]['.$i.']"' ; ?> value='<?=$line_options_sPId_i ?>'><br>
						stickerId:<input type='text' name=<?php echo '"trico_line_options[stickerId]['.$i.']"' ; ?> value='<?=$line_options_sId_i ?>' size=50><br><br>
												<?php } ?>
                        <input type='submit' value='儲存設定'>
                    </form>  
                </div>
                <div class='panel-footer'>
                    <p>tricohobby LINE Notify推播插圖設定</p>
                </div>
            </div>
        </div>
    </div>
    <?php
}
//增加外掛設定選單
function trico_line_setting_menu() {
    add_menu_page(
					'@ tricohobby LINE Notify',
					'LINE Notify文章推播插圖設定',
					'manage_options',
					__FILE__,//外掛網址,檔案所在
					'trico_line_mainpage',//外掛頁面內容
					null,
					67);
	add_action( 'admin_init', 'trico_line_register_settings');//啟動外掛初始設定
}
add_action( 'admin_menu', 'trico_line_setting_menu' );

//變數預處理
function trico_line_sanitize_options( $sanitize_t ) {
	global $cc;
	for ($i=0; $i < $cc ; $i++){
		$sanitize_t['stickerPackageId'][$i] = sanitize_text_field($sanitize_t['stickerPackageId'][$i]);
		$sanitize_t['stickerId'][$i] = sanitize_text_field($sanitize_t['stickerId'][$i]);
	}		
	return $sanitize_t;	
}

//外掛初始設定
function trico_line_register_settings() {
	register_setting('trico_line-settings-group',//設定變數群組,配合settings_fields
					 'trico_line_options',//變數名稱
					 array('sanitize_callback' => 'trico_line_sanitize_options') );//變數預處理
}

?>