WordPress是使用PHP語言開發的博客平臺,也可以把 WordPress當作一個內容管理系統(CMS)來使用。它是使用PHP語言和MySQL數據庫開發的。WordPress有許多第三方開發的免費模板,安裝方式簡單易用。今天就為大家介紹其中的一款插件,短信寶短信插件。
當我們的網站需要注冊的時候就必不可免的用到短信功能,一個穩定快速的短信平臺是我們所需要的,我們短信寶就是一個既穩定,又快速的短信群發平臺。
插件的目錄結構如下:
├─SMSBAO插件目錄
│ ├─captcha 字體文件目錄
│ │ ├─resources
│ │ │ ├─fonts
│ │ │ ├─tools
│ │ │ ├─words
│ │ ├─ captcha.php
│ ├─includes 核心配置目錄
│ │ ├─article_audit.php 文章發布
│ │ ├─get_code.php 驗證類
│ │ ├─menu_ui.php 配置頁
│ │ ├─new_register.php 注冊
│ │ ├─send_sms.php 核心發送類
│ │ ├─sms_log.txt 發送日志
│ └─smsbao.php 插件安裝類
下面具體給大家說一下每個文件的作用及代碼,smsbao.php是插件的安裝文件,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/20 * Time: 11:23 */ /* Plugin Name: 短信寶短信插件 Plugin URI: http://www.gjrencai.com/plugin/ Description: 專注提供最好用的短信服務。穩定,快速是我們不變的追求。該插件提供用戶注冊時的短信驗證功能,以及注冊用戶提交文章審核時,通知博主功能。 Author: smsbao Version: 1.1 Author URI: http://www.gjrencai.com */ if (!isset( $_SESSION )) { session_start(); session_regenerate_id(TRUE); } global $wpdb ; $tabelName = $wpdb ->prefix . 'user_active' ; $sql = "CREATE TABLE IF NOT EXISTS ` $tabelName ` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `phone` varchar(20) NOT NULL DEFAULT '' , `active_num` varchar(20) NOT NULL DEFAULT '' , `active_time` INT NOT NULL DEFAULT 0, `is_active` INT NOT NULL DEFAULT 0 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; $wpdb ->query( $sql ); define( 'SMSBAO_PLUGIN_URL' , plugin_dir_url( __FILE__ )); wp_enqueue_script( "jquery" ); require (dirname( __FILE__ ) . '/includes/menu_ui.php' ); require (dirname( __FILE__ ) . '/includes/new_register.php' ); require (dirname( __FILE__ ) . '/includes/article_audit.php' ); |
includes/article_audit.php文件是發布文章通知博主,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/30 * Time: 17:00 */ function artical_submit( $new , $old , $post ) { global $wpdb ; if ( 'pending' === $new ) { $smsName = get_option( 'smsbao_name' ); $password = get_option( 'smsbao_password' ); $tmp = get_option( 'smsbao_audit_tmp' ); $phone = get_option( 'smsbao_phone' ); $title = $post ->post_title; $userId = $post ->post_author; $sql = "select user_nicename from {$wpdb->users} where ID='{$userId}'" ; $name = $wpdb ->get_var( $sql ); $sign = get_option( 'smsbao_sign' ); if ( empty ( $tmp ) || empty ( $phone ) || empty ( $title ) || empty ( $name ) || empty ( $smsName ) || empty ( $password )) { return ; } $title = mb_substr( $title , 0, 20); $password = md5( $password ); $content = str_replace ( '{user}' , $name , $tmp ); $content = '【' . $sign . '】' . str_replace ( '{title}' , $title , $content ); include 'send_sms.php' ; $res = send_sms( $phone , $content , $smsName , $password ); if (true !== $res ) { file_put_contents ( 'sms_log.txt' , $res ); } } } add_action( 'transition_post_status' , 'artical_submit' , 10, 3); |
includes/get_code.php文件是驗證類,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/24 * Time: 15:14 */ if (isset( $_SERVER [ "HTTP_X_REQUESTED_WITH" ]) && strtolower ( $_SERVER [ "HTTP_X_REQUESTED_WITH" ])== "xmlhttprequest" ){ global $wpdb ; if (!isset( $wpdb )) { include_once $_POST [ 'dburl' ]; require_wp_db(); } if (!isset( $_SESSION )) { session_start(); session_regenerate_id(TRUE); } $ret = array (); $err = '' ; $phone = $_POST [ 'phone' ]; $captcha = $_POST [ 'captcha' ]; $tmp = $_POST [ 'tmp' ]; $username = $_POST [ 'username' ]; $passowrd = $_POST [ 'password' ]; $sign = $_POST [ 'sign' ]; $is_phone = preg_match( '/^1[34578]{1}\d{9}$/' , $phone ); if ( empty ( $phone )) { $err = '手機號碼沒有填寫!' ; } else if (false == $is_phone ) { $err = '手機格式不正確!' ; } else { if ( empty ( $captcha ) || empty ( $_SESSION [ 'sms_code' ])) { $err = '驗證碼必須填寫!' ; } else if ((trim( strtolower ( $captcha )) != $_SESSION [ 'sms_code' ])) { $err = '驗證碼填寫不正確!' ; } else { unset( $_SESSION [ 'sms_code' ]); include 'send_sms.php' ; $setActiveCode = rand(100000, 999999); $tmp = $sign . str_replace ( '{code}' , $setActiveCode , $tmp ); $smsRet = send_sms( $phone , $tmp , $username , $passowrd ); if (true === $smsRet ) { $table = $wpdb ->prefix . 'user_active' ; $ret [ 'flg' ] = true; $sql = "select id from {$table} where phone='{$phone}'" ; $id = $wpdb ->get_var( $sql ) + 0; $currentTime = time(); if ( $id > 0) { $res = $wpdb ->update( $table , array ( 'active_num' => $setActiveCode , 'active_time' => $currentTime , 'is_active' =>0), array ( 'id' => $id )); } else { $res = $wpdb ->insert( $table , array ( 'phone' => $phone , 'active_num' => $setActiveCode , 'active_time' => $currentTime )); } if (! $res ) { $err = '服務器內部錯誤!' ; } } else { file_put_contents ( 'sms_log.txt' , '短信發送失敗,原因:' . $smsRet ); $err = '短信發送失敗,請聯系管理員。' ; } } } if (! empty ( $err )) { $ret [ 'flg' ] = false; $ret [ 'err' ] = $err ; } echo json_encode( $ret ); } exit ; |
includes/menu_ui.php短信寶配置,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/20 * Time: 16:38 */ add_action( 'admin_menu' , 'create_admin_page' ); function create_admin_page() { add_options_page( 'SmsBao' , '短信寶' , 'manage_options' , 'smsbao' , 'output_menu_page' ); } function output_menu_page() { if (isset( $_POST [ 'submit' ])) { update_option( 'smsbao_name' , $_POST [ 'smsbao_name' ]); update_option( 'smsbao_password' , $_POST [ 'smsbao_password' ]); update_option( 'smsbao_sign' , $_POST [ 'smsbao_sign' ]); update_option( 'smsbao_register_tmp' , $_POST [ 'smsbao_register_tmp' ]); update_option( 'smsbao_audit_tmp' , $_POST [ 'smsbao_audit_tmp' ]); update_option( 'smsbao_phone' , $_POST [ 'smsbao_phone' ]); } $name = get_option( 'smsbao_name' ); $password = get_option( 'smsbao_password' ); $sign = get_option( 'smsbao_sign' ); $register = get_option( 'smsbao_register_tmp' ); $audit = get_option( 'smsbao_audit_tmp' ); $phone = get_option( 'smsbao_phone' ); if ( empty ( $name )) { $name = 'smsbaouser' ; add_option( 'smsbao_name' , $name ); } if ( empty ( $password )) { $password = '******' ; add_option( 'smsbao_password' , $password ); } if ( empty ( $sign )) { $sign = '我的博客' ; add_option( 'smsbao_sign' , $sign ); } if ( empty ( $register )) { $register = '用戶您好,您的注冊驗證碼為:{code}。' ; add_option( 'smsbao_register_tmp' , $register ); } if ( empty ( $audit )) { $audit = '用戶{user}發布了標題為:{title}的文章,請審核。' ; add_option( 'smsbao_audit_tmp' , $audit ); } if ( empty ( $phone )) { $phone = '' ; add_option( 'smsbao_phone' , $phone ); } print <<< STR <h1>短信寶短信設置</h1> <form method= "post" > 短信寶用戶名:<input type= "text" name= "smsbao_name" value= "$name" /> 沒有賬號?<a href= "http://www.gjrencai.com/reg" >立即注冊</a><br /> <div style= "height:10px;" ></div> 短信寶密碼: <input type= "password" name= "smsbao_password" value= "$password" /> <br /> <div style= "height:10px;" ></div> 短信簽名: <input type= "text" name= "smsbao_sign" value= "$sign" /> <br /> <div style= "height:10px;" ></div> 博主手機號: <input type= "text" name= "smsbao_phone" value= "$phone" /> <br /> <div style= "height:10px;" ></div> 注冊驗證模板:<textarea name= "smsbao_register_tmp" > $register </textarea><br /> <div style= "height:10px;" ></div> 文章審核模板:<textarea name= "smsbao_audit_tmp" > $audit </textarea><br /> <div style= "height:10px;" ></div> <input type= "submit" value= "保 存" name= "submit" /> </form> STR; } |
includes/new_register.php注冊文件,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/23 * Time: 14:51 */ if ( !function_exists( 'wp_new_user_notification' ) ) { /** * Notify the blog admin of a new user, normally via email. * * @since 2.0 * * @param int $user_id User ID * @param string $plaintext_pass Optional. The user's plaintext password */ function wp_new_user_notification( $user_id , $plaintext_pass = '' , $flag = '' ) { if (func_num_args() > 1 && $flag !== 1) return ; $user = new WP_User( $user_id ); $user_login = stripslashes ( $user ->user_login); $user_email = stripslashes ( $user ->user_email); // The blogname option is escaped with esc_html on the way into the database in sanitize_option // we want to reverse this for the plain text arena of emails. $blogname = wp_specialchars_decode(get_option( 'blogname' ), ENT_QUOTES); $message = sprintf(__( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n" ; $message .= sprintf(__( 'Username: %s' ), $user_login ) . "\r\n\r\n" ; $message .= sprintf(__( 'E-mail: %s' ), $user_email ) . "\r\n" ; @wp_mail(get_option( 'admin_email' ), sprintf(__( '[%s] New User Registration' ), $blogname ), $message ); if ( empty ( $plaintext_pass )) return ; $message = sprintf(__( 'Username: %s' ), $user_login ) . "\r\n" ; $message .= sprintf(__( 'Password: %s' ), $plaintext_pass ) . "\r\n" ; $message .= '登陸網址: ' . wp_login_url() . "\r\n" ; wp_mail( $user_email , sprintf(__( '[%s] Your username and password' ), $blogname ), $message ); } } function new_register_form() { $code_url = constant( 'SMSBAO_PLUGIN_URL' ) . '/captcha/captcha.php' ; ?> <script> jQuery(document).ready( function ($) { if ($( "#get_code" ).length > 0) { var flg = false; var node = $( "#get_code" ); var text = node.text(); var setT = 60; var clear = null; function okSet() { --setT; node.text(setT + "秒后提交" ); if (0 == setT) { node.attr( "disabled" , false); node.text(text); setT = 60; clearTimeout(clear); } else { node.attr( "disabled" , "disabled" ); clearTimeout(clear); clear = setTimeout( function (){ okSet(); }, 1000); } } $( "#get_code" ).on( "click" , function () { var phone = $( "#user_phone" ).val(); var captcha = $( "#CAPTCHA" ).val(); var tmp = "<?php echo get_option('smsbao_register_tmp', null)?>" ; var username = "<?php echo get_option('smsbao_name', null)?>" ; var password = "<?php echo md5(get_option('smsbao_password', null)) ?>" ; var sign = "<?php echo get_option('smsbao_sign', null) ?>" ; var dburl = "<?php echo str_replace('\\', '/', ABSPATH . 'wp-load.php')?>" ; if (null != sign) { sign = '【' + sign + '】' ; } var data = { "phone" : phone, "captcha" : captcha, "tmp" : tmp, "username" : username, "password" : password, "sign" : sign, "dburl" : dburl}; if (false == flg) { flg = true; $.ajax({ "url" : "<?php echo constant('SMSBAO_PLUGIN_URL') . '/includes/get_code.php'?>" , "type" : "post" , "data" : data, "dataType" : "json" , "success" : function (msg) { var errorMsg = null; if (true == msg.flg) { $( "#captcha_img" ).click(); alert( '發送成功' ); okSet(); } else { alert(msg.err); } flg = false; } }); } }); } }); function setCode() { } </script> <style> #reg_passmail {display: none;} </style> <p> <label for = "user_pwd1" >密碼(至少6位)<br/> <input id= "user_pwd1" class = "input" type= "password" size= "25" value= "" name= "user_pass" /> </label> </p> <p> <label for = "user_pwd2" >重復密碼<br/> <input id= "user_pwd2" class = "input" type= "password" size= "25" value= "" name= "user_pass2" /> </label> </p> <p> <label for = "user_phone" >手機號碼<br/> <input id= "user_phone" class = "input" type= "text" size= "25" value= "<?php echo empty($_POST['user_phone']) ? '':$_POST['user_phone']; ?>" name= "user_phone" /> </label> </p> <p> <label for = "CAPTCHA" >驗證碼:<br /> <input id= "CAPTCHA" style= "width:50%;*float:left;" class = "input" type= "text" size= "10" value= "" name= "captcha_code" /> 看不清?<a href= "javascript:void(0)" onclick= "document.getElementById('captcha_img').src='<?php echo constant(" SMSBAO_PLUGIN_URL "); ?>/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;" >點擊更換</a> </label> </p> <p> <label> <img id= "captcha_img" src= "<?php echo constant(" SMSBAO_PLUGIN_URL "); ?>/captcha/captcha.php" title= "看不清?點擊更換" alt= "看不清?點擊更換" onclick= "document.getElementById('captcha_img').src='<?php echo constant(" SMSBAO_PLUGIN_URL "); ?>/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;" /> </label> </p> <p> <label for = "user_activation_key" >短信驗證碼:<br /> <input id= "user_active" style= "width:50%;*float:left;" class = "input" type= "text" size= "10" value= "" name= "user_active" /> <button type= "button" class = "button button-large" id= "get_code" style= "display:inline;" >獲取驗證碼</button> </label> </p> <input type= "hidden" name= "user_role" value= "contributor" /> <?php } function check_fields( $login , $email , $errors ) { global $wpdb ; if ( strlen ( $_POST [ 'user_pass' ]) < 6) $errors ->add( 'password_length' , "<strong>錯誤</strong>:密碼長度至少6位" ); elseif ( $_POST [ 'user_pass' ] != $_POST [ 'user_pass2' ]) $errors ->add( 'password_error' , "<strong>錯誤</strong>:兩次輸入的密碼必須一致" ); if ( $_POST [ 'user_role' ] != 'contributor' ) $errors ->add( 'role_error' , "<strong>錯誤</strong>:不存在的用戶身份" ); $table = $wpdb ->prefix . 'user_active' ; $key = 0; $is_phone = preg_match( '/^1[34578]{1}\d{9}$/' , $_POST [ 'user_phone' ]); if ( empty ( $_POST [ 'user_phone' ])) { $errors ->add( 'phone_error' , "<strong>錯誤</strong>:手機號碼必填" ); } else if (false == $is_phone ) { $errors ->add( 'phone_error' , "<strong>錯誤</strong>:手機號碼格式不正確" ); } else { $sql = "select id, active_num, active_time, is_active from {$table} where phone='{$_POST['user_phone']}'" ; $obj = $wpdb ->get_row( $sql ); if ( empty ( $obj )) { $errors ->add( 'phone_error' , "<strong>錯誤</strong>:請先手機獲取激活碼!" ); } else { if ( empty ( $_POST [ 'user_active' ])) { $errors ->add( 'user_active_error' , "<strong>錯誤</strong>:請填寫短信驗證碼!" ); } else if ( $obj ->active_num != $_POST [ 'user_active' ]) { $errors ->add( 'user_active_error' , "<strong>錯誤</strong>:短信驗證碼不匹配!" ); } else { $currentTime = time(); $getTime = $obj ->active_time; $expireTime = $getTime + (3600 * 24); if ( $currentTime >= $expireTime ) { $errors ->add( 'user_active_error' , "<strong>錯誤</strong>:短信驗證碼已過期,請重新獲取!" ); } if (! empty ( $obj ->is_active)) { $errors ->add( 'user_active_error' , "<strong>錯誤</strong>:該短信驗證碼已經被用于注冊,請重新獲取!" ); } } } } if ( empty ( $errors ->errors)) { $wpdb ->update( $table , array ( 'is_active' =>1), array ( 'id' => $obj ->id)); } } function save_register_data( $user_id , $password = "" , $meta = array ()) { $userdata = array (); $userdata [ 'ID' ] = $user_id ; $userdata [ 'user_pass' ] = $_POST [ 'user_pass' ]; $userdata [ 'role' ] = $_POST [ 'user_role' ]; wp_new_user_notification( $user_id , $_POST [ 'user_pass' ], 1); wp_update_user( $userdata ); } add_action( 'register_form' , 'new_register_form' ); add_action( 'admin_header' , 'setJquery' ); add_action( 'register_post' , 'check_fields' , 10, 3); add_action( 'user_register' , 'save_register_data' ); ?> |
includes/send_sms.php接口文件,代碼如下:
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
|
<?php /** * Created by PhpStorm. * User: smsbao * Date: 2016/6/24 * Time: 16:08 */ function send_sms( $phone_num , $msg , $username , $password ) { $statusStr = array ( "0" => "短信發送成功" , "-1" => "短信參數不全" , "-2" => "短信寶服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!" , "30" => "短信設置密碼錯誤" , "40" => "短信設置賬號不存在" , "41" => "短信寶余額不足" , "42" => "短信寶帳戶已過期" , "43" => "短信寶IP地址限制" , "50" => "發送模板內容含有敏感詞" ); $user = $username ; $pass = $password ; $content = $msg ; $phone = $phone_num ; $sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" .urlencode( $content ); $result = file_get_contents ( $sendurl ) ; if ( "0" == $result ) { return true; } return $statusStr [ $result ]; } |
includes/sms_log.txt日志文件。存放發送記錄。
captcha文件下載鏈接 http://www.gjrencai.com/download/captcha.zip
好了短信寶短信插件開發完成,是不是很簡單,進行測試發送。
給大家一個小提示,報備短信寶的VIP模版,這樣就可以走短信寶的優質通道了,并且免審核了,短信內容3~5秒就可送達。
最新更新
電商類
CMS類
微信類