duoshuo.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /*
  3. Plugin Name: 多说
  4. Plugin URI: http://wordpress.org/extend/plugins/duoshuo/
  5. Description: 追求最佳用户体验的社会化评论框,为中小网站提供“新浪微博、QQ、人人、豆瓣等多帐号登录并评论”功能。“多说”帮你搭建更活跃,互动性更强的评论平台,功能强大且永久免费。
  6. Author: 多说网
  7. Version: 1.2
  8. Author URI: http://duoshuo.com/
  9. */
  10. define('DUOSHUO_PLUGIN_PATH', dirname(__FILE__));
  11. if (version_compare(PHP_VERSION, '5.0.0', '<')){
  12. if(is_admin()){
  13. function duoshuo_php_version_warning(){
  14. echo '<div class="updated"><p><strong>您的php版本低于5.0,请升级php到最新版,多说就能为您服务了。</strong></p></div>';
  15. }
  16. add_action('admin_notices', 'duoshuo_php_version_warning');
  17. }
  18. return;
  19. }
  20. if (version_compare( $wp_version, '2.8', '<' )){
  21. if(is_admin()){
  22. function duoshuo_wp_version_warning(){
  23. echo '<div class="updated"><p><strong>您的WordPress版本低于2.8,请升级WordPress到最新版,多说就能为您服务了。</strong></p></div>';
  24. }
  25. add_action('admin_notices', 'duoshuo_wp_version_warning');
  26. }
  27. return;
  28. }
  29. function duoshuo_get_available_transport(){
  30. if (extension_loaded('curl') && function_exists('curl_init') && function_exists('curl_exec'))
  31. return 'curl';
  32. if (function_exists('fopen') && function_exists('ini_get') && ini_get('allow_url_fopen'))
  33. return 'streams';
  34. if (function_exists('fsockopen') && (false === ($option = get_option( 'disable_fsockopen' )) || time() - $option >= 43200))
  35. return 'fsockopen';
  36. return false;
  37. }
  38. $transport = duoshuo_get_available_transport();
  39. if ($transport === false){
  40. if(is_admin()){
  41. function duoshuo_transport_warning(){
  42. echo '<div class="updated"><p><strong>没有可用的 HTTP 传输器</strong>,请联系你的主机商,安装或开启curl</p></div>';
  43. }
  44. add_action('admin_notices', 'duoshuo_transport_warning');
  45. }
  46. return;
  47. }
  48. if (!extension_loaded('json'))
  49. include DUOSHUO_PLUGIN_PATH . '/compat-json.php';
  50. require DUOSHUO_PLUGIN_PATH . '/Exception.php';
  51. require DUOSHUO_PLUGIN_PATH . '/Client.php';
  52. require DUOSHUO_PLUGIN_PATH . '/Abstract.php';
  53. require DUOSHUO_PLUGIN_PATH . '/WordPress.php';
  54. function duoshuo_admin_initialize(){
  55. global $wp_version, $duoshuoPlugin, $plugin_page;
  56. //在admin界面内执行的action
  57. // wordpress2.8 以后都支持这个过滤器
  58. add_filter('plugin_action_links_duoshuo/duoshuo.php', array($duoshuoPlugin, 'pluginActionLinks'), 10, 2);
  59. if (empty($duoshuoPlugin->shortName) || empty($duoshuoPlugin->secret)){//你尚未安装这个插件。
  60. function duoshuo_config_warning(){
  61. echo '<div class="updated"><p><strong>只要再<a href="' . admin_url('admin.php?page=duoshuo') . '">配置一下</a>多说帐号,多说就能开始为您服务了。</strong></p></div>';
  62. }
  63. if ($plugin_page !== 'duoshuo')
  64. add_action('admin_notices', 'duoshuo_config_warning');
  65. return ;
  66. }
  67. add_action('admin_notices', array($duoshuoPlugin, 'notices'));
  68. add_action('switch_theme', array($duoshuoPlugin, 'updateSite'));
  69. // support from WP 2.9
  70. //add_action('updated_option', array($duoshuoPlugin, 'updatedOption'));
  71. add_filter('post_row_actions', array($duoshuoPlugin, 'actionsFilter'));
  72. if (function_exists('get_post_types')){// support from WP 2.9
  73. $post_types = get_post_types( array('public' => true, 'show_in_nav_menus' => true), 'objects');
  74. foreach($post_types as $type => $object)
  75. add_meta_box('duoshuo-sidebox', '同时发布到', array($duoshuoPlugin,'syncOptions'), $type, 'side', 'high');
  76. }
  77. else{
  78. add_meta_box('duoshuo-sidebox', '同时发布到', array($duoshuoPlugin,'syncOptions'), 'post', 'side', 'high');
  79. add_meta_box('duoshuo-sidebox', '同时发布到', array($duoshuoPlugin,'syncOptions'), 'page', 'side', 'high');
  80. }
  81. //wp 3.0以下不支持此项功能
  82. /**
  83. * TODO
  84. if ($post !== null && 'publish' == $post->post_status || 'private' == $post->post_status)
  85. add_meta_box('duoshuo-comments', '来自社交网站的评论(多说)', array($duoshuoPlugin,'managePostComments'), 'post', 'normal', 'low');
  86. */
  87. add_action('post_comment_status_meta_box-options', array($duoshuoPlugin, 'commentStatusMetaBoxOptions'));
  88. add_action('wp_dashboard_setup', 'duoshuo_add_dashboard_widget');
  89. //// backwards compatible (before WP 3.0)
  90. if (version_compare( $wp_version, '3.0', '<' ) && current_user_can('administrator')){
  91. function duoshuo_wp_version_notice(){
  92. echo '<div class="updated"><p>您的WordPress版本低于3.0,如果您能升级WordPress,多说就能更好地为您服务。</p></div>';
  93. }
  94. add_action(get_plugin_page_hook('duoshuo', 'duoshuo'), 'duoshuo_wp_version_notice');
  95. add_action(get_plugin_page_hook('duoshuo-preferences', 'duoshuo'), 'duoshuo_wp_version_notice');
  96. add_action(get_plugin_page_hook('duoshuo-settings', 'duoshuo'), 'duoshuo_wp_version_notice');
  97. }
  98. if (!is_numeric($duoshuoPlugin->getOption('synchronized')) && current_user_can('administrator')){
  99. function duoshuo_unsynchronized_notice(){
  100. echo '<div class="updated"><p>上一次同步没有完成,<a href="' . admin_url('admin.php?page=duoshuo-settings') . '">点此继续同步</a></p></div>';
  101. }
  102. add_action(get_plugin_page_hook('duoshuo', 'duoshuo'), 'duoshuo_unsynchronized_notice');
  103. add_action(get_plugin_page_hook('duoshuo-preferences', 'duoshuo'), 'duoshuo_unsynchronized_notice');
  104. add_action(get_plugin_page_hook('duoshuo-settings', 'duoshuo'), 'duoshuo_unsynchronized_notice');
  105. }
  106. add_action('admin_head-edit-comments.php', array($duoshuoPlugin, 'originalCommentsNotice'));
  107. if (defined('DOING_AJAX')){
  108. add_action('wp_ajax_duoshuo_export', array($duoshuoPlugin, 'export'));
  109. add_action('wp_ajax_duoshuo_sync_log', array($duoshuoPlugin, 'syncLogAction'));
  110. }
  111. duoshuo_common_initialize();
  112. }
  113. function duoshuo_initialize(){
  114. global $duoshuoPlugin;
  115. if (empty($duoshuoPlugin->shortName) || empty($duoshuoPlugin->secret)){
  116. return;
  117. }
  118. if ($duoshuoPlugin->getOption('social_login_enabled')){
  119. add_action('login_form', array($duoshuoPlugin, 'loginForm'));
  120. add_action('register_form', array($duoshuoPlugin, 'loginForm'));
  121. }
  122. // wp2.8 以后支持这个事件
  123. add_action(get_option('duoshuo_postpone_print_scripts') ? 'wp_print_footer_scripts' : 'wp_print_scripts', array($duoshuoPlugin, 'appendScripts'));
  124. //以下应该根据是否设置,选择是否启用
  125. add_filter('comments_template', array($duoshuoPlugin,'commentsTemplate'));
  126. if (get_option('duoshuo_cc_fix')){ //直接输出HTML评论
  127. add_filter('comments_popup_link_attributes', array($duoshuoPlugin, 'commentsPopupLinkAttributes'));
  128. add_filter('comments_number', array($duoshuoPlugin, 'commentsText'));
  129. }
  130. if (get_option('duoshuo_sync_pingback_and_trackback')){
  131. add_action('trackback_post', array($duoshuoPlugin, 'exportOneComment'));
  132. add_action('pingback_post', array($duoshuoPlugin, 'exportOneComment'));
  133. }
  134. duoshuo_common_initialize();
  135. }
  136. function duoshuo_common_initialize(){
  137. global $duoshuoPlugin;
  138. // 没有用cookie方式保持身份,所以不需要重定向
  139. //add_action('wp_logout', array($duoshuoPlugin, 'logout'));
  140. add_filter('comments_open', array($duoshuoPlugin, 'commentsOpen'), 10, 2);
  141. add_action('set_auth_cookie', array($duoshuoPlugin, 'setJwtCookie'), 10, 5);
  142. add_action('clear_auth_cookie', array($duoshuoPlugin, 'clearJwtCookie'));
  143. add_action('profile_update', array($duoshuoPlugin, 'syncUserToRemote'));
  144. add_action('user_register', array($duoshuoPlugin, 'userRegisterHook'));
  145. add_action('wp_login', array($duoshuoPlugin, 'bindUser'), 10, 2);
  146. if ($duoshuoPlugin->getOption('cron_sync_enabled')){
  147. add_action('duoshuo_sync_log_cron', array($duoshuoPlugin, 'syncLog'));
  148. if (!wp_next_scheduled('duoshuo_sync_log_cron')){
  149. wp_schedule_event(time(), 'hourly', 'duoshuo_sync_log_cron');
  150. }
  151. }
  152. }
  153. // Register widgets.
  154. function duoshuo_register_widgets(){
  155. require_once dirname(__FILE__) . '/widgets.php';
  156. register_widget('Duoshuo_Widget_Recent_Visitors');
  157. //register_widget('Duoshuo_Widget_Top_Commenters');
  158. register_widget('Duoshuo_Widget_Recent_Comments');
  159. register_widget('Duoshuo_Widget_Top_Threads');
  160. register_widget('Duoshuo_Widget_Qqt_Follow');
  161. }
  162. function duoshuo_add_pages() {
  163. global $duoshuoPlugin;
  164. if (empty($duoshuoPlugin->shortName) || empty($duoshuoPlugin->secret)){ // 尚未安装
  165. add_object_page(
  166. '安装',
  167. '多说评论',
  168. 'moderate_comments', // 权限
  169. 'duoshuo',
  170. array($duoshuoPlugin, 'config'),
  171. $duoshuoPlugin->pluginDirUrl . 'images/menu-icon.png'
  172. );
  173. }
  174. else{ // 已经安装成功
  175. if (current_user_can('moderate_comments')){
  176. if(get_option('duoshuo_synchronized') === false){
  177. add_object_page(
  178. '数据同步',
  179. '多说评论',
  180. 'moderate_comments',
  181. 'duoshuo',
  182. array($duoshuoPlugin, 'sync'),
  183. $duoshuoPlugin->pluginDirUrl . 'images/menu-icon.png'
  184. );
  185. add_submenu_page(
  186. 'duoshuo',
  187. '多说评论管理',
  188. '评论管理',
  189. 'moderate_comments',
  190. 'duoshuo-manage',
  191. array($duoshuoPlugin,'manage')
  192. );
  193. }
  194. else{
  195. add_object_page(
  196. '多说评论管理',
  197. '多说评论',
  198. 'moderate_comments',
  199. 'duoshuo',
  200. array($duoshuoPlugin,'manage'),
  201. $duoshuoPlugin->pluginDirUrl . 'images/menu-icon.png'
  202. );
  203. }
  204. add_submenu_page(
  205. 'duoshuo',//$parent_slug
  206. '个性化设置',//page_title
  207. '个性化设置',//menu_title
  208. 'manage_options',//权限
  209. 'duoshuo-preferences',//menu_slug
  210. array($duoshuoPlugin, 'preferences')//function
  211. );
  212. add_submenu_page(
  213. 'duoshuo',//$parent_slug
  214. '主题设置',//page_title
  215. '主题设置',//menu_title
  216. 'manage_options',//权限
  217. 'duoshuo-themes',//menu_slug
  218. array($duoshuoPlugin, 'themes')//function
  219. );
  220. add_submenu_page(
  221. 'duoshuo',//$parent_slug
  222. '高级选项',//page_title
  223. '高级选项',//menu_title
  224. 'manage_options',//权限
  225. 'duoshuo-settings',//menu_slug
  226. array($duoshuoPlugin, 'settings')//function
  227. );
  228. add_submenu_page(
  229. 'duoshuo',//$parent_slug
  230. '数据统计',//page_title
  231. '数据统计',//menu_title
  232. 'manage_options',//权限
  233. 'duoshuo-statistics',//menu_slug
  234. array($duoshuoPlugin, 'statistics')//function
  235. );
  236. add_submenu_page(
  237. 'duoshuo',//$parent_slug
  238. '我的多说帐号',//page_title
  239. '我的多说帐号',//menu_title
  240. 'level_0',//权限
  241. 'duoshuo-profile',//menu_slug
  242. array($duoshuoPlugin, 'profile')//function
  243. );
  244. }
  245. elseif(current_user_can('level_0')){
  246. add_submenu_page(
  247. 'profile.php',//$parent_slug
  248. '我的多说帐号',//page_title
  249. '我的多说帐号',//menu_title
  250. 'level_0',//权限
  251. 'duoshuo-profile',//menu_slug
  252. array($duoshuoPlugin, 'profile')//function
  253. );
  254. }
  255. }
  256. }
  257. function duoshuo_add_dashboard_widget(){
  258. global $duoshuoPlugin;
  259. wp_add_dashboard_widget('dashboard_duoshuo', '多说最新评论', array($duoshuoPlugin, 'dashboardWidget'), array($duoshuoPlugin, 'dashboardWidgetControl'));
  260. }
  261. function duoshuo_request_handler(){
  262. global $duoshuoPlugin, $parent_file;
  263. if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  264. switch ($parent_file){
  265. case 'duoshuo':
  266. if (isset($_POST['duoshuo_reset']))
  267. $duoshuoPlugin->reset();
  268. if (isset($_POST['duoshuo_local_options']))
  269. $duoshuoPlugin->updateLocalOptions();
  270. break;
  271. default:
  272. }
  273. }
  274. elseif ($_SERVER['REQUEST_METHOD'] == 'GET'){
  275. switch ($parent_file){
  276. case 'options-general.php':
  277. if (isset($_GET['settings-updated']))
  278. $duoshuoPlugin->updateSite();
  279. break;
  280. case 'duoshuo':
  281. if (isset($_GET['duoshuo_connect_site']))
  282. $duoshuoPlugin->connectSite();
  283. if (isset($_GET['duoshuo_theme'])){
  284. update_option('duoshuo_theme', $_GET['duoshuo_theme']);
  285. }
  286. break;
  287. default:
  288. }
  289. }
  290. }
  291. function duoshuo_deactivate($network_wide = false){
  292. // 升级插件的时候也会停用插件
  293. //delete_option('duoshuo_synchronized');
  294. }
  295. $duoshuoPlugin = Duoshuo_WordPress::getInstance();
  296. if(is_admin()){//在admin界面内执行的action
  297. register_deactivation_hook(__FILE__, 'duoshuo_deactivate');
  298. add_action('admin_menu', 'duoshuo_add_pages', 10);
  299. add_action('admin_init', 'duoshuo_request_handler');
  300. add_action('admin_init', array($duoshuoPlugin, 'registerSettings'));
  301. add_action('admin_init', 'duoshuo_admin_initialize');
  302. }
  303. else{
  304. add_action('init', 'duoshuo_initialize');
  305. add_action('login_form_duoshuo_login', array($duoshuoPlugin, 'oauthConnect'));
  306. //add_action('login_form_duoshuo_logout', array($duoshuoPlugin,'oauthDisconnect'));
  307. }
  308. add_action('widgets_init', 'duoshuo_register_widgets');
  309. add_action('save_post', array($duoshuoPlugin, 'savePostDuoshuoStatus'));
  310. add_action('save_post', array($duoshuoPlugin, 'syncPostToRemote'), 10, 2);
  311. /*
  312. if (function_exists('get_post_types')){ // cron jobs runs in common mode, sometimes
  313. foreach(get_post_types() as $type)
  314. if ($type !== 'nav_menu_item' && $type !== 'revision')
  315. add_action('publish_' . $type, array($duoshuoPlugin,'syncPostToRemote'));
  316. }
  317. else{
  318. add_action('publish_post', array($duoshuoPlugin,'syncPostToRemote'));
  319. add_action('publish_page', array($duoshuoPlugin,'syncPostToRemote'));
  320. }
  321. */