widgets.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. class Duoshuo_Widget_Recent_Comments extends WP_Widget {
  3. protected $duoshuoPlugin;
  4. function __construct() {
  5. $widget_ops = array('classname' => 'ds-widget-recent-comments', 'description' => '最新评论(由多说提供)' );
  6. parent::__construct('ds-recent-comments', '最新评论(多说)', $widget_ops);
  7. $this->alt_option_name = 'duoshuo_widget_recent_comments';
  8. if ( is_active_widget(false, false, $this->id_base) )
  9. add_action( 'wp_head', array(&$this, 'recent_comments_style') );
  10. //add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
  11. //add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
  12. $this->duoshuoPlugin = Duoshuo_WordPress::getInstance();
  13. }
  14. function recent_comments_style() {
  15. if ( ! current_theme_supports( 'widgets' ) )// Temp hack #14876
  16. return;
  17. }
  18. function widget( $args, $instance ) {
  19. global $comments, $comment;
  20. if ( ! isset( $args['widget_id'] ) )
  21. $args['widget_id'] = $this->id;
  22. extract($args, EXTR_SKIP);
  23. $output = '';
  24. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );
  25. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  26. $number = 10;
  27. $output .= $before_widget;
  28. if ( $title )
  29. $output .= $before_title . $title . $after_title;
  30. $data = array(
  31. 'num_items' => $number,
  32. 'show_avatars'=>isset($instance['show_avatars']) ? $instance['show_avatars'] : 1,
  33. 'show_time'=> isset($instance['show_time']) ? $instance['show_time'] : 1,
  34. 'show_title'=> isset($instance['show_title']) ? $instance['show_title'] : 1,
  35. 'show_admin'=> isset($instance['show_admin']) ? $instance['show_admin'] : 1,
  36. 'avatar_size'=> 30,
  37. 'excerpt_length'=> isset($instance['excerpt_length']) ? $instance['excerpt_length'] : 70,
  38. );
  39. $attribs = '';
  40. foreach ($data as $key => $value)
  41. $attribs .= ' data-' . str_replace('_','-',$key) . '="' . esc_attr($value) . '"';
  42. $output .= '<ul class="ds-recent-comments"' . $attribs . '></ul>'
  43. . $after_widget;
  44. echo $output;?>
  45. <script>
  46. if (typeof DUOSHUO !== 'undefined')
  47. DUOSHUO.RecentComments && DUOSHUO.RecentComments('.ds-recent-comments');
  48. </script><?php
  49. $this->duoshuoPlugin->printScripts();
  50. }
  51. function update( $new_instance, $old_instance ) {
  52. $instance = $old_instance;
  53. $instance['title'] = strip_tags($new_instance['title']);
  54. $instance['number'] = absint( $new_instance['number'] );
  55. $instance['excerpt_length'] = absint( $new_instance['excerpt_length'] );
  56. $instance['show_avatars'] = absint( $new_instance['show_avatars'] );
  57. $instance['show_time'] = absint( $new_instance['show_time'] );
  58. $instance['show_title'] = absint( $new_instance['show_title'] );
  59. $instance['show_admin'] = absint( $new_instance['show_admin'] );
  60. $alloptions = wp_cache_get( 'alloptions', 'options' );
  61. if ( isset($alloptions['duoshuo_widget_recent_comments']) )
  62. delete_option('duoshuo_widget_recent_comments');
  63. return $instance;
  64. }
  65. function form( $instance ) {
  66. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  67. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  68. $show_avatars = isset($instance['show_avatars']) ? absint( $instance['show_avatars']) : 1;
  69. $show_title = isset($instance['show_title']) ? absint($instance['show_title']) : 1;
  70. $show_time = isset($instance['show_time']) ? absint($instance['show_time']) : 1;
  71. $show_admin = isset($instance['show_admin']) ? absint($instance['show_admin']) : 1;
  72. $excerpt_length = isset($instance['excerpt_length']) ? absint($instance['excerpt_length']) : 70;
  73. ?>
  74. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  75. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  76. <p>
  77. <input name="<?php echo $this->get_field_name('show_avatars'); ?>" type="hidden" value="0" />
  78. <input id="<?php echo $this->get_field_id('show_avatars'); ?>" name="<?php echo $this->get_field_name('show_avatars'); ?>" type="checkbox" value="1" <?php if ($show_avatars) echo 'checked="checked" '?>/>
  79. <label for="<?php echo $this->get_field_id('show_avatars'); ?>">显示头像</label>
  80. </p>
  81. <p>
  82. <input name="<?php echo $this->get_field_name('show_time'); ?>" type="hidden" value="0" />
  83. <input id="<?php echo $this->get_field_id('show_time'); ?>" name="<?php echo $this->get_field_name('show_time'); ?>" type="checkbox" value="1" <?php if ($show_time) echo 'checked="checked" '?>/>
  84. <label for="<?php echo $this->get_field_id('show_time'); ?>">显示评论时间</label>
  85. </p>
  86. <p>
  87. <input name="<?php echo $this->get_field_name('show_title'); ?>" type="hidden" value="0" />
  88. <input id="<?php echo $this->get_field_id('show_title'); ?>" name="<?php echo $this->get_field_name('show_title'); ?>" type="checkbox" value="1" <?php if ($show_title) echo 'checked="checked" '?>/>
  89. <label for="<?php echo $this->get_field_id('show_title'); ?>">显示文章标题</label>
  90. </p>
  91. <p>
  92. <input name="<?php echo $this->get_field_name('show_admin'); ?>" type="hidden" value="0" />
  93. <input id="<?php echo $this->get_field_id('show_admin'); ?>" name="<?php echo $this->get_field_name('show_admin'); ?>" type="checkbox" value="1" <?php if ($show_admin) echo 'checked="checked" '?>/>
  94. <label for="<?php echo $this->get_field_id('show_admin'); ?>">显示管理员评论</label>
  95. </p>
  96. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  97. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  98. <p><label for="<?php echo $this->get_field_id('excerpt_length'); ?>">引文字数(中文):</label>
  99. <input id="<?php echo $this->get_field_id('excerpt_length'); ?>" name="<?php echo $this->get_field_name('excerpt_length'); ?>" type="text" value="<?php echo $excerpt_length; ?>" size="5" /></p>
  100. <?php
  101. }
  102. }
  103. class Duoshuo_Widget_Top_Threads extends WP_Widget {
  104. protected $duoshuoPlugin;
  105. function __construct() {
  106. $widget_ops = array('classname' => 'ds-widget-top-threads', 'description' => '热评文章(由多说提供)');
  107. parent::__construct('ds-top-threads', '热评文章(多说)', $widget_ops);
  108. $this->alt_option_name = 'duoshuo_widget_top_threads';
  109. $this->duoshuoPlugin = Duoshuo_WordPress::getInstance();
  110. }
  111. function widget( $args, $instance ) {
  112. global $comments, $comment;
  113. if ( ! isset( $args['widget_id'] ) )
  114. $args['widget_id'] = $this->id;
  115. extract($args, EXTR_SKIP);
  116. $output = '';
  117. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '热评文章' : $instance['title'], $instance, $this->id_base );
  118. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  119. $number = 5;
  120. $output .= $before_widget;
  121. if ( $title )
  122. $output .= $before_title . $title . $after_title;
  123. $data = array(
  124. 'num_items' => $number,
  125. 'range' => isset($instance['range']) ? $instance['range'] : 'weekly',
  126. //'show_avatars'=>isset($instance['show_avatars']) ? $instance['show_avatars'] : 1,
  127. //'avatar_size'=> 30,
  128. );
  129. $attribs = '';
  130. foreach ($data as $key => $value)
  131. $attribs .= ' data-' . str_replace('_','-',$key) . '="' . esc_attr($value) . '"';
  132. $output .= '<ul class="ds-top-threads"' . $attribs . '></ul>'
  133. . $after_widget;
  134. echo $output;?>
  135. <script>
  136. if (typeof DUOSHUO !== 'undefined')
  137. DUOSHUO.TopThreads && DUOSHUO.TopThreads('.ds-top-threads');
  138. </script><?php
  139. $this->duoshuoPlugin->printScripts();
  140. }
  141. function update( $new_instance, $old_instance ) {
  142. $instance = $old_instance;
  143. $instance['range'] = $new_instance['range'];
  144. $instance['title'] = strip_tags($new_instance['title']);
  145. $instance['number'] = absint( $new_instance['number'] );
  146. //$instance['show_avatars'] = absint( $new_instance['show_avatars'] );
  147. $alloptions = wp_cache_get( 'alloptions', 'options' );
  148. if ( isset($alloptions['duoshuo_widget_top_threads']) )
  149. delete_option('duoshuo_widget_top_threads');
  150. return $instance;
  151. }
  152. function form( $instance ) {
  153. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  154. $range = isset($instance['range']) ? esc_attr($instance['range']) : 'weekly';
  155. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  156. //$show_avatars = isset($instance['show_avatars']) ? absint( $instance['show_avatars']) : 1;
  157. ?>
  158. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  159. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  160. <p>
  161. <label><input name="<?php echo $this->get_field_name('range'); ?>" type="radio" value="daily" <?php if ($range == 'daily') echo 'checked="checked" '?>/>24小时内</label>
  162. <label><input name="<?php echo $this->get_field_name('range'); ?>" type="radio" value="weekly" <?php if ($range == 'weekly') echo 'checked="checked" '?>/>7天内</label>
  163. <label><input name="<?php echo $this->get_field_name('range'); ?>" type="radio" value="monthly" <?php if ($range == 'monthly') echo 'checked="checked" '?>/>30天内</label>
  164. </p>
  165. <!--
  166. <p>
  167. <input name="<?php echo $this->get_field_name('show_avatars'); ?>" type="hidden" value="0" />
  168. <input id="<?php echo $this->get_field_id('show_avatars'); ?>" name="<?php echo $this->get_field_name('show_avatars'); ?>" type="checkbox" value="1" <?php if ($show_avatars) echo 'checked="checked" '?>/>
  169. <label for="<?php echo $this->get_field_id('show_avatars'); ?>">显示头像</label>
  170. </p>
  171. -->
  172. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
  173. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  174. <?php
  175. }
  176. }
  177. class Duoshuo_Widget_Recent_Visitors extends WP_Widget {
  178. function __construct() {
  179. $widget_ops = array('classname' => 'ds-widget-recent-visitors', 'description' => '最近访客(由多说提供)' );
  180. parent::__construct('ds-recent-visitors', '最近访客(多说)', $widget_ops);
  181. $this->alt_option_name = 'duoshuo_widget_recent_visitors';
  182. if ( is_active_widget(false, false, $this->id_base) )
  183. add_action( 'wp_head', array(&$this, 'printScripts') );
  184. //add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
  185. //add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
  186. $this->duoshuoPlugin = Duoshuo_WordPress::getInstance();
  187. }
  188. function printScripts() {
  189. if ( ! current_theme_supports( 'widgets' ) )// Temp hack #14876
  190. return;
  191. }
  192. function widget( $args, $instance ) {
  193. global $comments, $comment;
  194. if ( ! isset( $args['widget_id'] ) )
  195. $args['widget_id'] = $this->id;
  196. extract($args, EXTR_SKIP);
  197. $output = '';
  198. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '最近访客' : $instance['title'], $instance, $this->id_base );
  199. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  200. $number = 12;
  201. $output .= $before_widget;
  202. if ( $title )
  203. $output .= $before_title . $title . $after_title;
  204. $data = array(
  205. 'num_items' => $number,
  206. 'show_time'=> isset($instance['show_time']) ? $instance['show_time'] : 1,
  207. 'avatar_size'=> isset($instance['avatar_size']) ? $instance['avatar_size'] : 50,
  208. );
  209. $attribs = '';
  210. foreach ($data as $key => $value)
  211. $attribs .= ' data-' . str_replace('_','-',$key) . '="' . esc_attr($value) . '"';
  212. $output .= '<ul class="ds-recent-visitors"' . $attribs . '></ul>'
  213. . $after_widget;
  214. echo $output;?>
  215. <script>
  216. if (typeof DUOSHUO !== 'undefined')
  217. DUOSHUO.RecentVisitors('.ds-recent-visitors');
  218. </script><?php
  219. $this->duoshuoPlugin->printScripts();
  220. }
  221. function update( $new_instance, $old_instance ) {
  222. $instance = $old_instance;
  223. $instance['title'] = strip_tags($new_instance['title']);
  224. $instance['number'] = absint( $new_instance['number'] );
  225. $instance['show_time'] = absint( $new_instance['show_time'] );
  226. $instance['avatar_size'] = absint( $new_instance['avatar_size'] );
  227. $alloptions = wp_cache_get( 'alloptions', 'options' );
  228. if ( isset($alloptions['duoshuo_widget_recent_visitors']) )
  229. delete_option('duoshuo_widget_recent_visitors');
  230. return $instance;
  231. }
  232. function form( $instance ) {
  233. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  234. $number = isset($instance['number']) ? absint($instance['number']) : 15;
  235. $show_time = isset($instance['show_time']) ? absint($instance['show_time']) : 1;
  236. $avatar_size = isset($instance['avatar_size']) ? absint($instance['avatar_size']) : 50;
  237. ?>
  238. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  239. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  240. <!-- p>
  241. <input name="<?php echo $this->get_field_name('show_time'); ?>" type="hidden" value="0" />
  242. <input id="<?php echo $this->get_field_id('show_time'); ?>" name="<?php echo $this->get_field_name('show_time'); ?>" type="checkbox" value="1" <?php if ($show_time) echo 'checked="checked" '?>/>
  243. <label for="<?php echo $this->get_field_id('show_time'); ?>">显示访问时间</label>
  244. </p -->
  245. <p><label for="<?php echo $this->get_field_id('number'); ?>">显示访客的数量:</label>
  246. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  247. <p><label for="<?php echo $this->get_field_id('avatar_size'); ?>">头像尺寸:</label>
  248. <input id="<?php echo $this->get_field_id('avatar_size'); ?>" name="<?php echo $this->get_field_name('avatar_size'); ?>" type="text" value="<?php echo $avatar_size; ?>" size="3" />px</p>
  249. <?php
  250. }
  251. }
  252. class Duoshuo_Widget_Qqt_Follow extends WP_Widget {
  253. function __construct() {
  254. $widget_ops = array('classname' => 'ds-widget-qqt-follow', 'description' => '腾讯微博-收听组件(由多说提供)' );
  255. parent::__construct('ds-qqt-follow', '腾讯微博-收听(多说)', $widget_ops);
  256. $this->alt_option_name = 'duoshuo_widget_qqt_follow';
  257. }
  258. function widget( $args, $instance ) {
  259. if ( ! isset( $args['widget_id'] ) )
  260. $args['widget_id'] = $this->id;
  261. extract($args, EXTR_SKIP);
  262. $output = $before_widget;
  263. $title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
  264. if ( $title )
  265. $output .= $before_title . $title . $after_title;
  266. $params = array(
  267. 'c' => 'follow',
  268. 'a' => 'quick',
  269. 'name'=>isset($instance['qqt_name']) ? $instance['qqt_name'] : 'duo-shuo',
  270. 'style'=>isset($instance['qqt_style']) ? $instance['qqt_style'] : 1,
  271. 't' => time() . sprintf("%03d", microtime() * 1000),
  272. 'f' => isset($instance['qqt_followers']) ? $instance['qqt_followers'] : 1,
  273. );
  274. switch($params['style']){
  275. case 1:
  276. $width = $params['f'] ? 227 : 167;
  277. $height = 75;
  278. break;
  279. case 2:
  280. $width = $params['f'] ? 191 : 136;
  281. $height = 38;
  282. break;
  283. case 3:
  284. $width = $params['f'] ? 168 : 125;
  285. $height = 20;
  286. break;
  287. case 4:
  288. $width = $params['f'] ? 182 : 125;
  289. $height = 27;
  290. break;
  291. case 5:
  292. $width = $params['f'] ? 178 : 125;
  293. $height = 24;
  294. break;
  295. default:
  296. }
  297. $attribs = array(
  298. 'scrolling' => 'no',
  299. 'width' => $width,
  300. 'height' => $height,
  301. 'frameborder'=> 0,
  302. 'allowtransparency'=>'true',
  303. 'marginheight'=>0,
  304. 'marginwidth'=> 0,
  305. 'src' => (is_ssl()?'https':'http').'://follow.v.t.qq.com/index.php?' . http_build_query($params, null, '&'),
  306. );
  307. $output .= '<iframe ';
  308. foreach ($attribs as $key => $value)
  309. $output .= ' ' . $key . '="' . $value . '"';
  310. $output .= '></iframe>' . $after_widget;
  311. echo $output;
  312. }
  313. function update( $new_instance, $old_instance ) {
  314. $instance = $old_instance;
  315. $instance['title'] = strip_tags($new_instance['title']);
  316. $instance['qqt_name'] = strip_tags($new_instance['qqt_name']);
  317. $instance['qqt_style'] = absint( $new_instance['qqt_style'] );
  318. $instance['qqt_followers'] = absint( $new_instance['qqt_followers'] );
  319. $alloptions = wp_cache_get( 'alloptions', 'options' );
  320. if ( isset($alloptions['duoshuo_widget_qqt_follow']) )
  321. delete_option('duoshuo_widget_qqt_follow');
  322. return $instance;
  323. }
  324. function form( $instance ) {
  325. $title = isset($instance['title']) ? $instance['title'] : '';
  326. $qqt_name = isset($instance['qqt_name']) ? $instance['qqt_name'] : '';
  327. $qqt_style = isset($instance['qqt_style']) ? absint( $instance['qqt_style']) : 1;
  328. $qqt_followers = isset($instance['qqt_followers']) ? absint( $instance['qqt_followers']) : 1;
  329. ?>
  330. <p><label for="<?php echo $this->get_field_id('title'); ?>">标题:</label>
  331. <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($title);?>"></p>
  332. <p><label for="<?php echo $this->get_field_id('qqt_name'); ?>">腾讯微博帐号 (不含@,如:duo-shuo):</label>
  333. <input type="text" class="widefat" id="<?php echo $this->get_field_id('qqt_name'); ?>" name="<?php echo $this->get_field_name('qqt_name'); ?>" value="<?php echo esc_attr($qqt_name);?>"></p>
  334. <p><input name="<?php echo $this->get_field_name('qqt_followers'); ?>" type="hidden" value="0" />
  335. <input id="<?php echo $this->get_field_id('qqt_followers'); ?>" name="<?php echo $this->get_field_name('qqt_followers'); ?>" type="checkbox" value="1" <?php if ($qqt_followers) echo 'checked="checked" '?>/>
  336. <label for="<?php echo $this->get_field_id('qqt_followers'); ?>">显示已收听人数</label></p>
  337. <ul>
  338. <li><label><input name="<?php echo $this->get_field_name('qqt_style'); ?>" type="radio" value="1" <?php if ($qqt_style == 1) echo 'checked="checked" '?>/> 头像+收听按钮(样式丰富,更有效吸引听众)</label></li>
  339. <li><label><input name="<?php echo $this->get_field_name('qqt_style'); ?>" type="radio" value="2" <?php if ($qqt_style == 2) echo 'checked="checked" '?>/> 收听按钮(适合有限的展现空间)</label></li>
  340. <li><label><input name="<?php echo $this->get_field_name('qqt_style'); ?>" type="radio" value="3" <?php if ($qqt_style == 3) echo 'checked="checked" '?>/> 收听文字链(最简洁的状态)</label></li>
  341. <li><label><input name="<?php echo $this->get_field_name('qqt_style'); ?>" type="radio" value="4" <?php if ($qqt_style == 4) echo 'checked="checked" '?>/> 收听按钮(清新蓝色)</label></li>
  342. <li><label><input name="<?php echo $this->get_field_name('qqt_style'); ?>" type="radio" value="5" <?php if ($qqt_style == 5) echo 'checked="checked" '?>/> 收听按钮(小巧白色)</label></li>
  343. </ul>
  344. <?php
  345. }
  346. }