PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/code/cake/app/webroot/cp/wp-content/plugins/commentpress-core/commentpress-ajax/cp-ajax-comments.php

https://github.com/DigitalPaulScholtenProject/DPSP-Platform
PHP | 586 lines | 190 code | 226 blank | 170 comment | 26 complexity | 821279a46af329c977b90e933c037d6a MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php /*
  2. ================================================================================
  3. CommentPress AJAX Comments
  4. ================================================================================
  5. AUTHOR: Christian Wach <needle@haystack.co.uk>
  6. --------------------------------------------------------------------------------
  7. NOTES
  8. --------------------------------------------------------------------------------
  9. */
  10. // enable the plugin at the appropriate point
  11. add_action( 'wp', 'cpajax_enable_plugin' );
  12. // always add AJAX functionality
  13. add_action( 'wp_ajax_cpajax_get_new_comments', 'cpajax_get_new_comments' );
  14. add_action( 'wp_ajax_nopriv_cpajax_get_new_comments', 'cpajax_get_new_comments' );
  15. // remove comment flood filter if you want more 'chat-like' functionality
  16. //remove_filter('comment_flood_filter', 'wp_throttle_comment_flood', 10, 3);
  17. // add AJAX reassign functionality
  18. add_action( 'wp_ajax_cpajax_reassign_comment', 'cpajax_reassign_comment' );
  19. add_action( 'wp_ajax_nopriv_cpajax_reassign_comment', 'cpajax_reassign_comment' );
  20. /**
  21. * @description: get context in which to enable this plugin
  22. * @todo:
  23. *
  24. */
  25. function cpajax_enable_plugin() {
  26. // access globals
  27. global $commentpress_core;
  28. // kick out if...
  29. // cp is not enabled
  30. if ( is_null( $commentpress_core ) OR !is_object( $commentpress_core ) ) { return; }
  31. // we're in the WP back end
  32. if ( is_admin() ) { return; }
  33. // add localised text
  34. add_action( 'wp_head', 'cpajax_localise' );
  35. // add our javascripts
  36. add_action( 'wp_enqueue_scripts', 'cpajax_add_javascripts', 120 );
  37. // add a button to the comment meta
  38. add_filter( 'cp_comment_edit_link', 'cpajax_add_reassign_button', 20, 2 );
  39. }
  40. /**
  41. * @description: get new comments in response to an ajax request
  42. * @todo:
  43. *
  44. */
  45. function cpajax_get_new_comments() {
  46. // init return
  47. $data = array();
  48. // get incoming data
  49. $last_comment_count = isset( $_POST[ 'last_count' ] ) ? $_POST[ 'last_count' ] : NULL;
  50. // store incoming unless updated later
  51. $data['cpajax_comment_count'] = $last_comment_count;
  52. // get post ID
  53. $post_id = isset( $_POST[ 'post_id' ] ) ? $_POST[ 'post_id' ] : NULL;
  54. // make it an integer, just to be sure
  55. $post_id = (int) $post_id;
  56. // enable Wordpress API on post
  57. $GLOBALS['post'] = get_post( $post_id );
  58. // get any comments posted since last update time
  59. $data['cpajax_new_comments'] = array();
  60. // get current array
  61. $current_comment_count_array = get_comment_count( $post_id );
  62. // get approved -> do we want others?
  63. $current_comment_count = $current_comment_count_array['approved'];
  64. // get number of new comments to fetch
  65. $num_to_get = (int) $current_comment_count - (int) $last_comment_count;
  66. // are there any?
  67. if ( $num_to_get > 0 ) {
  68. // update comment count since last request
  69. $data['cpajax_comment_count'] = (string) $current_comment_count;
  70. // set get_comments defaults
  71. $defaults = array(
  72. 'number' => $num_to_get,
  73. 'orderby' => 'comment_date',
  74. 'order' => 'DESC',
  75. 'post_id' => $post_id,
  76. 'status' => 'approve',
  77. 'type' => 'comment'
  78. );
  79. // get them
  80. $comments = get_comments( $defaults );
  81. // if we get some - again, just to be sure
  82. if ( count( $comments ) > 0 ) {
  83. // init identifier
  84. $identifier = 1;
  85. // set args
  86. $args = array();
  87. $args['max_depth'] = get_option( 'thread_comments_depth' );
  88. // loop
  89. foreach( $comments AS $_comment ) {
  90. // assume top level
  91. $depth = 1;
  92. // if no parent
  93. if ( $_comment->comment_parent != '0' ) {
  94. // override depth
  95. $depth = cpajax_get_comment_depth( $_comment, $depth );
  96. }
  97. // get comment markup
  98. $html = commentpress_get_comment_markup( $_comment, $args, $depth );
  99. // close li (walker would normally do this)
  100. $html .= '</li>'."\n\n\n\n";
  101. // add comment to array
  102. $data['cpajax_new_comment_'.$identifier] = array(
  103. 'parent' => $_comment->comment_parent,
  104. 'id' => $_comment->comment_ID,
  105. 'text_sig' => $_comment->comment_signature,
  106. 'markup' => $html
  107. );
  108. // increment
  109. $identifier++;
  110. }
  111. }
  112. }
  113. // set reasonable headers
  114. header('Content-type: text/plain');
  115. header("Cache-Control: no-cache");
  116. header("Expires: -1");
  117. // echo
  118. echo json_encode( $data );
  119. //print_r( $last_comment_count );
  120. // die!
  121. exit();
  122. }
  123. /**
  124. * @description: get comment depth
  125. * @todo:
  126. *
  127. */
  128. function cpajax_get_comment_depth( $comment, $depth ) {
  129. // is parent top level?
  130. if ( $comment->comment_parent == '0' ) {
  131. // --<
  132. return $depth;
  133. }
  134. // get parent comment
  135. $parent = get_comment( $comment->comment_parent );
  136. // increase depth
  137. $depth++;
  138. // recurse
  139. return cpajax_get_comment_depth( $parent, $depth );
  140. }
  141. /**
  142. * @description: add our plugin javascripts
  143. * @todo:
  144. *
  145. */
  146. function cpajax_add_javascripts() {
  147. // access globals
  148. global $post, $commentpress_core;
  149. // can only now see $post
  150. if ( !cpajax_plugin_can_activate() ) { return; }
  151. // init vars
  152. $vars = array();
  153. // is "live" comment refreshing enabled?
  154. $vars['cpajax_live'] = ( $commentpress_core->db->option_get( 'cp_para_comments_live' ) == '1' ) ? 1 : 0;
  155. // we need to know the url of the Ajax handler
  156. $vars['cpajax_ajax_url'] = admin_url( 'admin-ajax.php' );
  157. // add the url of the animated loading bar gif
  158. $vars['cpajax_spinner_url'] = plugins_url( 'commentpress-ajax/assets/images/loading.gif', COMMENTPRESS_PLUGIN_FILE );
  159. // time formatted thus: 2009-08-09 14:46:14
  160. $vars['cpajax_current_time'] = date('Y-m-d H:i:s');
  161. // get comment count at the time the page is served
  162. $_count = get_comment_count( $post->ID );
  163. // adding moderation queue as well, since we do show these
  164. $vars['cpajax_comment_count'] = $_count['approved']; // + $_count['awaiting_moderation'];
  165. // add post ID
  166. $vars['cpajax_post_id'] = $post->ID;
  167. // default to minified scripts
  168. $debug_state = '';
  169. // target different scripts when debugging
  170. if ( defined( 'SCRIPT_DEBUG' ) AND SCRIPT_DEBUG === true ) {
  171. // use uncompressed scripts
  172. $debug_state = '.dev';
  173. }
  174. // are we asking for in-page comments?
  175. if ( $commentpress_core->db->is_special_page() ) {
  176. // add comments in page script
  177. wp_enqueue_script(
  178. 'cpajax',
  179. plugins_url( 'commentpress-ajax/cp-ajax-comments-page'.$debug_state.'.js', COMMENTPRESS_PLUGIN_FILE )
  180. );
  181. } else {
  182. // add comments in sidebar script
  183. wp_enqueue_script(
  184. 'cpajax',
  185. plugins_url( 'commentpress-ajax/cp-ajax-comments'.$debug_state.'.js', COMMENTPRESS_PLUGIN_FILE ),
  186. // load in droppable
  187. array( 'jquery-ui-droppable', 'jquery-ui-dialog' )
  188. );
  189. // add WordPress dialog CSS
  190. wp_enqueue_style( 'wp-jquery-ui-dialog' );
  191. }
  192. // use wp function to localise
  193. wp_localize_script( 'cpajax', 'CommentpressAjaxSettings', $vars );
  194. }
  195. /**
  196. * @description: translation
  197. * @todo:
  198. *
  199. */
  200. function cpajax_localise() {
  201. // can only now see $post
  202. if ( !cpajax_plugin_can_activate() ) { return; }
  203. // init array
  204. $text = array();
  205. // add translations for comment form
  206. $text[] = __( 'Loading...', 'commentpress-core' );
  207. $text[] = __( 'Please enter your name.', 'commentpress-core' );
  208. $text[] = __( 'Please enter your email address.', 'commentpress-core' );
  209. $text[] = __( 'Please enter a valid email address.', 'commentpress-core' );
  210. $text[] = __( 'Please enter your comment.', 'commentpress-core' );
  211. $text[] = __( 'Your comment has been added.', 'commentpress-core' );
  212. $text[] = __( 'AJAX error!', 'commentpress-core' );
  213. // add translations for comment reassignment
  214. $text[] = __( 'Are you sure?', 'commentpress-core' );
  215. $text[] = __( 'Are you sure you want to assign the comment and its replies to the textblock? This action cannot be undone.', 'commentpress-core' );
  216. $text[] = __( 'Submitting...', 'commentpress-core' );
  217. $text[] = __( 'Please wait while the comments are reassigned. The page will refresh when this has been done.', 'commentpress-core' );
  218. // wrap each item in single quotes
  219. array_walk( $text, create_function( '&$val', '$val = "\'".$val."\'";' ) );
  220. // construct array
  221. $array = implode( ', ', $text );
  222. // add to head
  223. echo '<script type="text/javascript">
  224. var cpajax_lang = ['.$array.'];
  225. </script>';
  226. }
  227. /**
  228. * @description: validate that the plugin can be activated
  229. * @todo:
  230. *
  231. */
  232. function cpajax_plugin_can_activate() {
  233. // access globals
  234. global $post, $commentpress_core;
  235. // disallow if no post ID (such as 404)
  236. if ( !is_object( $post ) ) { return false; }
  237. // it's the Theme My Login page
  238. if ( $commentpress_core->is_theme_my_login_page() ) { return false; }
  239. // init
  240. $allowed = true;
  241. // disallow generally if page doesn't allow commenting
  242. if ( !$commentpress_core->is_commentable() ) { $allowed = false; }
  243. // but, allow general comments page
  244. if ( $commentpress_core->db->option_get( 'cp_general_comments_page' ) == $post->ID ) { $allowed = true; }
  245. // --<
  246. return $allowed;
  247. }
  248. /**
  249. * @description: get comment depth
  250. * @todo:
  251. *
  252. */
  253. function cpajax_add_reassign_button( $edit_button, $comment ) {
  254. //print_r( $comment ); die();
  255. // pass if not top level
  256. if ( $comment->comment_parent != '0' ) { return $edit_button; }
  257. // pass if pingback or trackback
  258. if ( $comment->comment_type == 'trackback' OR $comment->comment_type == 'pingback' ) { return $edit_button; }
  259. // pass if not orphan
  260. //if ( !isset( $comment->orphan ) ) { return $edit_button; }
  261. // set default edit link title text
  262. $_title_text = apply_filters(
  263. 'cpajax_comment_assign_link_title_text',
  264. __( 'Drop on to a text-block to reassign this comment (and any replies) to it', 'commentpress-core' )
  265. );
  266. // set default edit link text
  267. $_text = apply_filters(
  268. 'cp_comment_assign_link_text',
  269. __( 'Move', 'commentpress-core' )
  270. );
  271. // construct assign button
  272. $assign_button = '<span class="alignright comment-assign" title="'.$_title_text.'" id="cpajax_assign-'.$comment->comment_ID.'">'.
  273. $_text.
  274. '</span>';
  275. // add our assign button
  276. $edit_button .= $assign_button;
  277. // --<
  278. return $edit_button;
  279. }
  280. /**
  281. * @description: change a comment's text-signature
  282. * @todo:
  283. *
  284. */
  285. function cpajax_reassign_comment() {
  286. global $data;
  287. // init return
  288. $data = array();
  289. $data['msg'] = '';
  290. // init checker
  291. $comment_ids = array();
  292. // get incoming data
  293. $text_sig = isset( $_POST[ 'text_signature' ] ) ? $_POST[ 'text_signature' ] : '';
  294. $comment_id = isset( $_POST[ 'comment_id' ] ) ? $_POST[ 'comment_id' ] : '';
  295. // sanity check
  296. if ( $text_sig !== '' AND $comment_id !== '' ) {
  297. // access globals
  298. global $commentpress_core;
  299. // store text signature
  300. $commentpress_core->db->save_comment_signature( $comment_id );
  301. // trace
  302. $comment_ids[] = $comment_id;
  303. // recurse for any comment children
  304. cpajax_reassign_comment_children( $comment_id, $text_sig, $comment_ids );
  305. }
  306. // add message
  307. $data['msg'] .= 'comments '.implode( ', ', $comment_ids ).' updated'."\n";
  308. // set reasonable headers
  309. header('Content-type: text/plain');
  310. header("Cache-Control: no-cache");
  311. header("Expires: -1");
  312. // echo
  313. echo json_encode( $data );
  314. // die!
  315. exit();
  316. }
  317. /**
  318. * @description: store text signature for all children of a comment
  319. * @todo:
  320. *
  321. */
  322. function cpajax_reassign_comment_children( $comment_id, $text_sig, &$comment_ids ) {
  323. // get the children of the comment
  324. $children = cpajax_get_children( $comment_id );
  325. // did we get any
  326. if ( count( $children ) > 0 ) {
  327. // loop
  328. foreach( $children AS $child ) {
  329. // access globals
  330. global $commentpress_core;
  331. // store text signature
  332. $commentpress_core->db->save_comment_signature( $child->comment_ID );
  333. // trace
  334. $comment_ids[] = $child->comment_ID;
  335. // recurse for any comment children
  336. cpajax_reassign_comment_children( $child->comment_ID, $text_sig, $comment_ids );
  337. }
  338. }
  339. }
  340. /**
  341. * @description: retrieve comment children
  342. * @todo:
  343. *
  344. */
  345. function cpajax_get_children(
  346. $comment_id
  347. ) { //-->
  348. // declare access to globals
  349. global $wpdb;
  350. // construct query for comment children
  351. $query = "
  352. SELECT *
  353. FROM $wpdb->comments
  354. WHERE comment_parent = '$comment_id'
  355. ORDER BY comment_date ASC
  356. ";
  357. // --<
  358. return $wpdb->get_results( $query );
  359. }