1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13:
14: class WxRobot_Wp{
15:
16:
17: public $opt_big_show = array();
18: public $opt_small_show = array();
19:
20:
21: 22: 23:
24: public function __construct(){
25:
26: $this->options = get_option(WEIXIN_ROBOT_OPTIONS);
27: $this->obj = WxRobot_SDK::instance();
28:
29: if($this->options['opt_pic_show']){
30: $this->opt_pic_sign = true;
31: $this->option_pic_to_array();
32:
33: }else{
34: $this->opt_pic_sign = false;
35: }
36: }
37:
38: 39: 40:
41: public static $_instance = null;
42:
43: 44: 45: 46: 47:
48: public static function instance(){
49: if( is_null (self::$_instance)){
50: self::$_instance = new self();
51: }
52: return self::$_instance;
53: }
54:
55: 56: 57: 58: 59:
60: public function option_pic_to_array(){
61:
62: $small = $this->options['opt_small_show'];
63: if(!empty($small)){
64: $this->opt_small_show = false;
65: }
66: $s_arr = explode("\r\n", $small);
67: $tmp = array();
68: foreach($s_arr as $k=>$v){
69: $tmp[] = trim($v);
70: }
71: $this->opt_small_show = $tmp;
72:
73: $big = $this->options['opt_big_show'];
74: if(!empty($big)){
75: $this->opt_big_show = false;
76: }
77: $s_arr = explode("\r\n", $big);
78: $tmp = array();
79: foreach($s_arr as $k=>$v){
80: $tmp[] = trim($v);
81: }
82: $this->opt_big_show = $tmp;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function path_url_encode($thumb){
92: $pos = strrpos($thumb,'/');
93: return substr($thumb, 0,$pos+1).urlencode(substr($thumb, $pos+1));
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function get_opt_pic($c, $type){
104: $u2 = '/(<img[^>]+src\s*=\s*\"?([^>\"\s]+)\"?[^>]*>)/im';
105:
106: $p_sign = preg_match($u2 ,$c, $match);
107: if($p_sign){
108: return $this->path_url_encode($match[2]);
109: }
110:
111:
112: if('small' == $type){
113: $num = count($this->opt_small_show);
114: $t = $num - 1;
115: $mt = mt_rand(0, $t);
116: if($num){
117: return $this->opt_small_show[$mt];
118: }
119: }else if('big' == $type){
120: $num = count($this->opt_big_show);
121: $t = $num - 1;
122: $mt = mt_rand(0, $t);
123: if($num){
124: return $this->opt_big_show[$mt];
125: }
126: }
127: return false;
128: }
129:
130:
131: 132: 133: 134: 135: 136:
137: public function get_opt_pic_small($content = ''){
138: if($this->opt_pic_sign){
139: $pic = $this->get_opt_pic($content, 'small');
140: if(!empty($pic)){
141: return $pic;
142: }
143: }
144: return wx_random_small_pic();
145: }
146:
147: 148: 149: 150: 151: 152:
153: public function get_opt_pic_big($content = ''){
154: if($this->opt_pic_sign){
155: $pic = $this->get_opt_pic($content, 'big');
156: if(!empty($pic)){
157: return $pic;
158: }
159: }
160: return wx_random_big_pic();
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function head_one_line($c){
170: $c = html_entity_decode($c, ENT_NOQUOTES, 'utf-8');
171: $c = strip_tags($c);
172: $c = mb_substr($c, 0, 50, 'utf-8').'...';
173: return $c;
174: }
175:
176:
177: 178: 179: 180: 181: 182:
183: public function Qid($id){
184: query_posts('p='.$id);
185: $info = array();
186: $i = 0;
187: while(have_posts()){the_post();
188: ++$i;
189: if($i==1){
190: $a['title'] = get_the_title();
191: $a['desc'] = $this->head_one_line(get_the_content());
192: $a['pic'] = $this->get_opt_pic_big(get_the_content());
193: $a['link'] = get_permalink();
194: }else{
195: $a['title'] = get_the_title();
196: $a['desc'] = get_the_title();
197: $a['pic'] = $this->get_opt_pic_small(get_the_content());
198: $a['link'] = get_permalink();
199: }
200: $info[] = $a;
201: }
202: if(!empty($info)){
203: return $this->obj->toMsgTextPic($info);
204: }else{
205: return false;
206: }
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function QidResult($id){
216: $wp = new WP_query('p='.$id);
217: $info = array();
218:
219: while($wp->have_posts()){$wp->the_post();
220: $a['title'] = get_the_title();
221: $a['desc'] = get_the_content();
222: $a['pic'] = get_the_content();
223: $a['link'] = get_permalink();
224: $info = $a;
225: }
226: return $info;
227: }
228:
229:
230: 231: 232: 233: 234: 235:
236: public function Qids($id){
237: $string = array();
238: $i = 0;
239: foreach($id as $k){
240: $res = $this->QidResult($k);
241: if($res){
242: ++$i;
243: if(1 == $i){
244: $a['title'] = $res['title'];
245: $a['desc'] = $this->head_one_line($res['desc']);
246: $a['pic'] = $this->get_opt_pic_big($res['desc']);
247: $a['link'] = $res['link'];
248: }else{
249: $a['title'] = $res['title'];
250: $a['desc'] = $res['desc'];
251: $a['pic'] = $this->get_opt_pic_small($res['desc']);
252: $a['link'] = $res['link'];
253: }
254: }
255: $string[] = $a;
256: }
257: return $this->obj->toMsgTextPic($string);
258: }
259:
260: 261: 262: 263: 264:
265: public function today(){
266: $sql = 'showposts=10'.'&year='.date('Y').'&monthnum='.date('m').'&day='.date('d');
267: $wp = new WP_query($sql);
268: $info = array();
269: $i = 0;
270: while($wp->have_posts()){$wp->the_post();
271: ++$i;
272: if($i==1){
273: $a['title'] = get_the_title();
274: $a['desc'] = $this->head_one_line(get_the_content());
275: $a['pic'] = $this->get_opt_pic_big(get_the_content());
276: $a['link'] = get_permalink();
277: }else{
278: $a['title'] = get_the_title();
279: $a['desc'] = get_the_title();
280: $a['pic'] = $this->get_opt_pic_small(get_the_content());
281: $a['link'] = get_permalink();
282: }
283: $info[] = $a;
284: }
285: if(empty($info)){
286: return $this->obj->toMsgText('今日暂未发表文章!!!');
287: }
288: return $this->obj->toMsgTextPic($info);
289: }
290:
291: 292: 293: 294: 295: 296:
297: public function hot($int){
298: $wp = new WP_query(array(
299: 'post_status' => 'publish',
300: 'post_not_in' => array(),
301: 'ignore_sticky_posts'=> 1,
302: 'orderby' => 'comment_count',
303: 'showposts' => $int,
304: ));
305: $info = array();
306: $i = 0;
307: while($wp->have_posts()){$wp->the_post();
308: ++$i;
309: if($i==1){
310: $a['title'] = get_the_title();
311: $a['desc'] = $this->head_one_line(get_the_content());
312: $a['pic'] = $this->get_opt_pic_big(get_the_content());
313: $a['link'] = get_permalink();
314: }else{
315: $a['title'] = get_the_title();
316: $a['desc'] = get_the_title();
317: $a['pic'] = $this->get_opt_pic_small(get_the_content());
318: $a['link'] = get_permalink();
319: }
320: $info[] = $a;
321: }
322: return $this->obj->toMsgTextPic($info);
323: }
324:
325:
326: 327: 328: 329: 330: 331:
332: public function news($int){
333: $wp = new WP_query('showposts='.$int);
334: $info = array();
335: $i = 0;
336: while($wp->have_posts()){$wp->the_post();
337: ++$i;
338: if($i==1){
339: $a['title'] = get_the_title();
340: $a['desc'] = $this->head_one_line(get_the_content());
341: $a['pic'] = $this->get_opt_pic_big(get_the_content());
342: $a['link'] = get_permalink();
343: }else{
344: $a['title'] = get_the_title();
345: $a['desc'] = get_the_title();
346: $a['pic'] = $this->get_opt_pic_small(get_the_content());
347: $a['link'] = get_permalink();
348: }
349: $info[] = $a;
350: }
351: return $this->obj->toMsgTextPic($info);
352: }
353:
354: 355: 356: 357: 358: 359:
360: public function rand($int){
361: $wp = new WP_query("showposts={$int}&orderby=rand");
362: $info = array();
363: $i = 0;
364: while($wp->have_posts()){$wp->the_post();
365: ++$i;
366: if($i==1){
367: $a['title'] = get_the_title();
368: $a['desc'] = $this->head_one_line(get_the_content());
369: $a['pic'] = $this->get_opt_pic_big(get_the_content());
370: $a['link'] = get_permalink();
371: }else{
372: $a['title'] = get_the_title();
373: $a['desc'] = get_the_title();
374: $a['pic'] = $this->get_opt_pic_small(get_the_content());
375: $a['link'] = get_permalink();
376: }
377: $info[] = $a;
378: }
379: return $this->obj->toMsgTextPic($info);
380: }
381:
382:
383: }
384: ?>
385: