1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: 18: 19:
20: class WxRobot_Admin_Menu_Menu{
21:
22: 23: 24: 25: 26:
27: public function init(){
28: add_action('init', array($this, 'menu_ajax'), 10);
29: add_action('admin_head', array(&$this, 'weixin_robot_menu_js'), 10);
30:
31: }
32:
33: 34: 35: 36: 37:
38: public function menu_ajax(){
39: if(!empty($_POST['page']) && 'weixin-robot-menu-setting' == $_POST['page']){
40: if($_POST['method'] == 'update'){
41: echo($this->setting_ajax_update());
42: }
43: }
44: }
45:
46: 47: 48: 49: 50:
51: public function setting_ajax_update(){
52: $id = $_POST['id'];
53: $type = $_POST['type'];
54: $name = $_POST['name'];
55: $value = strip_tags($_POST['value']);
56: $sort = $_POST['sort'];
57: $res = WxRobot_Table_Menu::instance()->update_menu($id, $name, $type, $value, $sort);
58: if($res){
59: $this->weixin_robot_ab_menu();
60: return 'ok';
61: }else{
62: return 'fail';
63: }
64: }
65:
66: 67: 68: 69: 70:
71: public function weixin_robot_menu_js(){
72: $url = WEIXIN_ROBOT_URL;
73: if(!empty($_GET['page']) && 'weixin-robot-menu-setting' == $_GET['page']){
74: echo '<link type="text/css" rel="stylesheet" href="'.$url.'/assets/css/hover.css" />';
75: echo '<link type="text/css" rel="stylesheet" href="'.$url.'/assets/css/weixin_robot_menu_setting.css" />';
76: echo '<script type="text/javascript" src="'.$url.'/assets/js/weixin_robot_menu_setting.js"></script>';
77: }
78: }
79:
80: 81: 82: 83: 84:
85: public function menu(){
86: add_submenu_page('weixin-robot',
87: 'weixin-robot',
88: '微信机器人菜单',
89: 'manage_options',
90: 'weixin-robot-menu-setting',
91: array($this, 'menu_setting'));
92: }
93:
94:
95: 96: 97: 98: 99:
100: public function weixin_robot_ab_menu(){
101: if($data = WxRobot_Table_Menu::instance()->weixin_get_menu_p_data()){
102: $menu = array();
103: foreach($data as $k=>$v){
104: if($data2 = WxRobot_Table_Menu::instance()->weixin_get_menu_p_data_id($v['id'])){
105: $list['name'] = $v['menu_name'];
106: foreach($data2 as $k1=>$v2){
107: $list2['type'] = $v2['menu_type'];
108: $list2['name'] = $v2['menu_name'];
109: if('view' == $v2['menu_type']){
110: $list2['url'] = $v2['menu_callback'];
111: }else{
112: $list2['key'] = $v2['menu_key'];
113: }
114: $list['sub_button'][] = $list2;
115: $list2 = array();
116: }
117:
118: $menu[] = $list;
119: $list = array();
120: }else{
121: $list['type'] = $v['menu_type'];
122: $list['name'] = $v['menu_name'];
123:
124: if('view' == $v['menu_type']){
125: $list['url'] = $v['menu_callback'];
126: }else{
127: $list['key'] = $v['menu_key'];
128: }
129: $menu[] = $list;
130: $list = array();
131: }
132: }
133: $M['button'] = $menu;
134: $menu = WxRobot_SDK::instance()->to_json($M);
135: $data = WxRobot_SDK::instance()->menuSet($menu);
136: $_data = json_decode($data, true);
137: if($_data['errmsg'] == 'ok'){
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: 145: 146: 147: 148:
149: public function weixin_robot_rand_menu(){
150: return 'MENU_'.time();
151: }
152:
153: 154: 155: 156: 157:
158: public function check_right_server(){
159: $data = WxRobot_SDK::instance()->getToken();
160: if(empty($data)){
161: return false;
162: }
163:
164: $data = WxRobot_SDK::instance()->menuGet();
165:
166: if(!$data){
167: return false;
168: }
169:
170:
171: return true;
172: }
173:
174: 175: 176: 177: 178:
179: public function menu_setting_post(){
180:
181: $opts = get_option(WEIXIN_ROBOT_OPTIONS);
182:
183:
184: if(!$this->check_right_server()){
185: wx_notice_msg('你填写服务号信息,有问题。请重新填写');exit;
186: }
187:
188:
189: if(isset($_POST['submit_menu'])){
190: switch($_POST['submit_menu']){
191: case '提交菜单':
192: $data = $_POST['weixin_robot_menu'];
193: if(empty($data['name']) || empty($data['value'])){
194: if($this->weixin_robot_ab_menu()){
195: wx_notice_msg('菜单成功更新微信服务器上了!!!');
196: }else{
197: wx_notice_msg('菜单更新失败!!!');
198: }
199: wx_notice_msg('请填写号内容!!!');
200: }else{
201:
202: if(isset($data['child']) && 'true' == $data['child'] && $data['parent'] != 'false'){
203: if(WxRobot_Table_Menu::instance()->weixin_get_menu_c_count($data['parent']) < 5){
204: $data = WxRobot_Table_Menu::instance()
205: ->insert_menu($data['name'], $data['type'], $this->weixin_robot_rand_menu(), $data['value'], $data['parent']);
206:
207: if($this->weixin_robot_ab_menu()){
208: wx_notice_msg('菜单成功更新微信服务器上了!!!');
209: }else{
210: wx_notice_msg('菜单更新失败!!!');
211: }
212: }else{
213: wx_notice_msg('二级菜单不能再添加了!!!');
214: }
215: }else{
216: if(WxRobot_Table_Menu::instance()->weixin_get_menu_p_count() < 3){
217: $data = WxRobot_Table_Menu::instance()
218: ->insert_menu($data['name'], $data['type'], $this->weixin_robot_rand_menu(), $data['value'], 0);
219: if($this->weixin_robot_ab_menu()){
220: wx_notice_msg('菜单成功更新微信服务器上了!!!');
221: }else{
222: wx_notice_msg('菜单更新失败!!!');
223: }
224: }else{
225: wx_notice_msg('一级菜单不能再添加了!!!');
226: }
227: }
228: }
229: break;
230: case '清空菜单':
231: WxRobot_Table_Menu::instance()->clear_menu();
232: $data = WxRobot_SDK::instance()->menuDel();
233: $_data = json_decode($data, true);
234: if('ok' == $_data['errmsg']){
235: wx_notice_msg('清空菜单成功!!!');
236: }else{
237: wx_notice_msg('清空菜单失败!!!');
238: }
239: break;
240: case '删除':
241: if(isset($_POST['id'])){
242: if($data = WxRobot_Table_Menu::instance()->delete_menu_id($_POST['id'])){
243: wx_notice_msg('ok!!!');
244: if($this->weixin_robot_ab_menu()){
245: wx_notice_msg('菜单成功更新微信服务器上了!!!');
246: }else{
247: wx_notice_msg('菜单更新失败!!!');
248: }
249: }else{
250: wx_notice_msg('fail!!!');
251: }
252: }
253: break;
254: }
255: }
256: }
257:
258: 259: 260:
261: public function menu_setting(){
262:
263: $this->menu_setting_post();
264:
265: echo '<div class="wrap">';
266: echo '<h2>微信菜单设置</h2>';
267:
268: echo '<div class="metabox-holder">';
269: echo '<div class="postbox">';
270:
271: echo '<table class="form-table" style="width:700px;border:2px;border-color:#21759b;">';
272:
273: $trTpl = "<tr class='wp_weixin_robot_table_head_tr'>
274: <td class='wp_weixin_robot_table_head_td' style='text-align:center;color:#21759b;' scope='col'>%s</td>
275: <td class='wp_weixin_robot_table_head_td' style='text-align:center;color:#21759b;' scope='col'>%s</td>
276: <td class='wp_weixin_robot_table_head_td' style='text-align:center;color:#21759b;' scope='col'>%s</td>
277: <td class='wp_weixin_robot_table_head_td' style='text-align:center;color:#21759b;' scope='col'>%s</td>
278: <td class='wp_weixin_robot_table_head_td' style='text-align:center;color:#21759b;' scope='col'>%s</td>
279: <td class='wp_weixin_robot_table_head_td' style='width:180px;text-align:center;color:#21759b;' scope='col'>%s</td></tr>";
280: $tableHeadTpl = sprintf($trTpl, '序号ID', '菜单名', '菜单类型', 'key/url', '排序','操作');
281: echo $tableHeadTpl;
282:
283:
284:
285: $data = WxRobot_Table_Menu::instance()->weixin_get_menu_p_data();
286:
287: if($data){
288: foreach($data as $k=>$v){
289: $trTpl = "<tr><td style='text-align:center;' scope='col'>{$v['id']}</td>
290: <td style='text-align:left;' scope='col'>─{$v['menu_name']}</td>
291: <td style='text-align:center;' scope='col'>{$v['menu_type']}</td>
292: <td style='text-align:center;' scope='col'>{$v['menu_callback']}</td>
293: <td style='text-align:center;' scope='col'>{$v['menu_sort']}</td>
294: <td style='text-align:center;' scope='col'>";
295: $trTpl .= '<input type="hidden" name="id" value="'.$v['id'].'" />';
296: $trTpl .= '<input class="button" name="submit_menu" type="submit" value="删除" />';
297: $trTpl .= '|<span class="weixin_robot_mv button wobble-to-top-right">修改</span>';
298: $trTpl .= "</td></tr>";
299: echo '<form method="POST">';
300: echo $trTpl;
301: echo '</form>';
302:
303: if($data2 = WxRobot_Table_Menu::instance()->weixin_get_menu_p_data_id($v['id'])){
304: foreach($data2 as $k=>$v){
305: $trTpl = "<tr><td style='width:40px;text-align:center;' scope='col'>{$v['id']}</td>
306: <td style='width:100px;text-align:left;' scope='col'>└─{$v['menu_name']}</td>
307: <td style='text-align:center;' scope='col'>{$v['menu_type']}</td>
308: <td style='text-align:center;' scope='col'>{$v['menu_callback']}</td>
309: <td style='text-align:center;' scope='col'>{$v['menu_sort']}</td>
310: <td style='width:100px;text-align:center;' scope='col'>";
311: $trTpl .= '<input type="hidden" name="id" value="'.$v['id'].'" />';
312: $trTpl .= '<input class="button" name="submit_menu" type="submit" value="删除" />';
313: $trTpl .= '|<span class="weixin_robot_mv button wobble-to-top-right">修改</span>';
314: $trTpl .= "</td></tr>";
315:
316: echo '<form method="POST">';
317: echo $trTpl;
318: echo '</form>';
319: }
320: }
321: }
322: }else{
323: echo '<tr>';
324: echo "<td class='wp_weixin_robot_table_head_td' style='color:#21759b;width:100px;text-align:center;' scope='col' colspan='6'>没有设置相应菜单</td>";
325: echo '</tr>';
326: }
327: echo '</table></div></div>';
328:
329: echo '<div><div>';
330: echo '<table class="form-table">';
331: echo '<form action="" method="POST">';
332:
333: echo '<tr valign="top"><th scope="row">菜单名称</th>';
334: echo '<td><input type="text" name="weixin_robot_menu[name]" value="" size="35"></input></td></tr>';
335:
336:
337: echo '<tr valign="top"><th scope="row">事件类型选择</th><td>'
338: ,'<select name="weixin_robot_menu[type]" id="method">'
339: ,'<option value="click" selected="selected">点击</option>'
340: ,'<option value="view" >URL</option>'
341: ,'</select><p></p><td></tr>';
342:
343:
344: echo '<tr valign="top"><th scope="row">key/url</th>';
345: echo '<td><input type="text" name="weixin_robot_menu[value]" value="" size="35"></input><br />';
346: echo '<p>如果选择"URL"选项,应该填写网址: http://midoks.cachecha.com/</p>';
347: echo '<p>如果选择"点击"选项,可以是使用today(今日发布),n(1-10)最新信息, h(1-10)热门信息, r(1-10)随机信息, ?(帮助信息)等内置命令!!</p>';
348: echo '<p style="color:red">如果回复内容在关键字设置了, 就会返回关键字的回复信息!</p>';
349: echo '<p>不满足上面的话,则会返回文本信息</p>';
350: echo '</td></tr>';
351:
352:
353: echo '<tr valign="top"><th scope="row">是否为子菜单</th>';
354: echo '<td><input type="checkbox" name="weixin_robot_menu[child]" value="true"/>';
355: echo '<br />为子菜单时,请一定选择</td></tr>';
356:
357:
358: echo '<tr valign="top"><th scope="row">父级菜单选择</th><td>';
359: echo '<select name="weixin_robot_menu[parent]" id="method" />';
360: $data = WxRobot_Table_Menu::instance()->weixin_get_menu_p_data();
361: if($data){
362: foreach($data as $k=>$v){
363: echo "<option value='{$v['id']}' selected='selected'>{$v['menu_name']}</option>";
364: }
365: }else{
366: echo '<option value="false" selected="selected">无顶级菜单,请先创建</option>';
367: }
368: echo '</select><td></tr></table>'
369: ,'<p class="submit">'
370: ,'<input name="submit_menu" type="submit" class="button-primary" value="提交菜单" title="提交本地的数据库中..."/>'
371: ,'<input style="margin-left:10px" name="submit_menu" type="submit" class="button-primary" value="清空菜单" title="删除本地数据菜单相关数据" />'
372: ,'</p></form></div></div>';
373:
374: do_action('weixin_midoks_push');
375: }
376:
377: }
378: ?>
379: