P2Ajax

The BP Groupblog P2Ajax class.

Defined (1)

The class is defined in the following location(s).

/themes/p2/inc/ajax.php  
  1. class P2Ajax { 
  2. function dispatch() { 
  3. $action = isset( $_REQUEST['action'] )? $_REQUEST['action'] : ''; 
  4. add_action( 'wp_ajax_'.$action, $action ); 
  5. do_action( "p2_ajax", $action ); 
  6. if ( is_callable( array( 'P2Ajax', $action ) ) ) 
  7. call_user_func( array( 'P2Ajax', $action ) ); 
  8. else 
  9. die( '-1' ); 
  10. exit; 
  11.  
  12. function get_post() { 
  13. check_ajax_referer( 'ajaxnonce', '_inline_edit' ); 
  14. if ( !is_user_logged_in() ) { 
  15. die( '<p>'.__( 'Error: not logged in.', 'p2' ).'</p>' ); 
  16. $post_id = $_GET['post_ID']; 
  17. $post_id = substr( $post_id, strpos( $post_id, '-' ) + 1 ); 
  18. if ( !current_user_can( 'edit_post', $post_id ) ) { 
  19. die( '<p>'.__( 'Error: not allowed to edit post.', 'p2' ).'</p>' ); 
  20. $post = get_post( $post_id ); 
  21. echo $post->post_content ; 
  22.  
  23. function tag_search() { 
  24. global $wpdb; 
  25. $term = $_GET['q']; 
  26. if ( false !== strpos( $term, ', ' ) ) { 
  27. $term = explode( ', ', $term ); 
  28. $term = $term[count( $term ) - 1]; 
  29. $term = trim( $term ); 
  30. if ( strlen( $term ) < 2 ) 
  31. die(); // require 2 chars for matching 
  32. $results = $wpdb->get_col( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'post_tag' AND t.name LIKE ( '%". like_escape( $wpdb->escape( $term ) ) . "%' )" ); 
  33. echo join( $results, "\n" ); 
  34.  
  35. function logged_in_out() { 
  36. check_ajax_referer( 'ajaxnonce', '_loggedin' ); 
  37. echo is_user_logged_in()? 'logged_in' : 'not_logged_in'; 
  38.  
  39. function get_comment() { 
  40. check_ajax_referer( 'ajaxnonce', '_inline_edit' ); 
  41. if ( !is_user_logged_in() ) { 
  42. die( '<p>'.__( 'Error: not logged in.', 'p2' ).'</p>' ); 
  43. $comment_id = esc_attr($_GET['comment_ID']); 
  44. $comment_id = substr( $comment_id, strpos( $comment_id, '-' ) + 1); 
  45. $comment = get_comment($comment_id); 
  46. echo $comment->comment_content; 
  47.  
  48. function save_post() { 
  49. check_ajax_referer( 'ajaxnonce', '_inline_edit' ); 
  50. if ( !is_user_logged_in() ) { 
  51. die( '<p>'.__( 'Error: not logged in.', 'p2' ).'</p>' ); 
  52.  
  53. $post_id = $_POST['post_ID']; 
  54. $post_id = substr( $post_id, strpos( $post_id, '-' ) + 1 ); 
  55.  
  56. if ( !current_user_can( 'edit_post', $post_id )) { 
  57. die( '<p>'.__( 'Error: not allowed to edit post.', 'p2' ).'</p>' ); 
  58.  
  59. $new_post_content = $_POST['content']; 
  60.  
  61. // preserve custom "big" titles 
  62. $post = get_post( $post_id ); 
  63.  
  64. if ( !$post ) die( '-1' ); 
  65.  
  66. $clean_title = str_replace( '…', '', $post->post_title ); 
  67.  
  68. if ( strpos($post->post_content, $clean_title ) !== 0 ) { 
  69. $post_title = $post->post_title; 
  70. } else { 
  71. $post_title = prologue_title_from_content( $new_post_content ); 
  72.  
  73. $post = wp_update_post( array( 
  74. 'post_title' => $post_title,  
  75. 'post_content' => $new_post_content,  
  76. 'post_modified' => current_time( 'mysql' ),  
  77. 'post_modified_gmt' => current_time( 'mysql', 1),  
  78. 'ID' => $post_id 
  79. )); 
  80.  
  81. $post = get_post( $post ); 
  82.  
  83. echo apply_filters( 'the_content', $post->post_content ); 
  84.  
  85. function save_comment() { 
  86. check_ajax_referer( 'ajaxnonce', '_inline_edit' ); 
  87. if ( !is_user_logged_in() ) { 
  88. die( '<p>'.__( 'Error: not logged in.', 'p2' ).'</p>' ); 
  89.  
  90. $comment_id = $_POST['comment_ID']; 
  91. $comment_id = substr( $comment_id, strpos( $comment_id, '-' ) + 1); 
  92. $comment = get_comment( $comment_id ); 
  93.  
  94. if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) ) { 
  95. die( '<p>'.__( 'Error: not allowed to edit this comment.', 'p2' ).'</p>' ); 
  96.  
  97. $comment_content = $_POST['comment_content']; 
  98.  
  99. $comment = wp_update_comment( array( 
  100. 'comment_content' => $comment_content,  
  101. 'comment_ID' => $comment_id 
  102. )); 
  103.  
  104. $comment = get_comment( $comment_id ); 
  105. echo apply_filters( 'comment_text', $comment->comment_content ); 
  106.  
  107. function new_post() { 
  108. global $user_ID;  
  109.  
  110. if ( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'new_post' ) { 
  111. die( '-1' ); 
  112. if ( !is_user_logged_in() ) { 
  113. die( '<p>'.__( 'Error: not logged in.', 'p2' ).'</p>' ); 
  114. if ( ! ( current_user_can( 'publish_posts' ) ||  
  115. (get_option( 'p2_allow_users_publish' ) && $user_ID )) ) { 
  116.  
  117. die( '<p>'.__( 'Error: not allowed to post.', 'p2' ).'</p>' ); 
  118. check_ajax_referer( 'ajaxnonce', '_ajax_post' ); 
  119. $user = wp_get_current_user(); 
  120. $user_id = $user->ID; 
  121. $post_content = $_POST['posttext']; 
  122. $tags = trim( $_POST['tags'] ); 
  123. $title = $_POST['post_title']; 
  124.  
  125. // Strip placeholder text for tags 
  126. if ( __( 'Tag it', 'p2' ) == $tags ) 
  127. $tags = ''; 
  128.  
  129. if ( empty( $title ) || __( 'Post Title', 'p2' ) == $title ) 
  130. // For empty or placeholder text, create a nice title based on content 
  131. $post_title = prologue_title_from_content( $post_content ); 
  132. else 
  133. $post_title = $title; 
  134.  
  135. require_once ( ABSPATH . '/wp-admin/includes/taxonomy.php' ); 
  136. require_once ( ABSPATH . WPINC . '/category.php' ); 
  137.  
  138. $accepted_post_cats = apply_filters( 'p2_accepted_post_cats', array( 'post', 'quote', 'status', 'link', 'photo', 'video', 'featured' ) ); 
  139. $post_cat = ( in_array( $_POST['post_cat'], $accepted_post_cats ) ) ? $_POST['post_cat'] : 'post'; 
  140.  
  141. if ( !category_exists( $post_cat ) ) 
  142. wp_insert_category( array( 'cat_name' => $post_cat ) ); 
  143.  
  144. $post_cat = get_category_by_slug( $post_cat ); 
  145.  
  146. /** Add the quote citation to the content if it exists */ 
  147. if ( !empty( $_POST['post_citation'] ) && 'quote' == $post_cat->slug ) { 
  148. $post_content = '<p>' . $post_content . '</p><cite>' . $_POST['post_citation'] . '</cite>'; 
  149.  
  150. $post_id = wp_insert_post( array( 
  151. 'post_author' => $user_id,  
  152. 'post_title' => $post_title,  
  153. 'post_content' => $post_content,  
  154. 'post_type' => $post_type,  
  155. 'post_category' => array( $post_cat->cat_ID ),  
  156. 'tags_input' => $tags,  
  157. 'post_status' => 'publish' 
  158. ) ); 
  159. echo $post_id ? $post_id : '0'; 
  160.  
  161. function get_latest_posts() { 
  162. global $post_request_ajax; 
  163.  
  164. $load_time = $_GET['load_time']; 
  165. $frontpage = $_GET['frontpage']; 
  166. $num_posts = 10; // max amount of posts to load 
  167. $number_of_new_posts = 0; 
  168.  
  169. query_posts( 'showposts=' . $num_posts . '&post_status=publish' ); 
  170. ob_start(); 
  171. while (have_posts()) : the_post(); 
  172. $current_user_id = get_the_author_meta( 'ID' ); 
  173. if ( get_gmt_from_date( get_the_time( 'Y-m-d H:i:s' ) ) <= $load_time ) continue; 
  174. $number_of_new_posts++; 
  175. $post_request_ajax = true; 
  176. require dirname(__FILE__) . '/../entry.php'; 
  177. endwhile; 
  178. $posts_html = ob_get_clean(); 
  179.  
  180. if ( $number_of_new_posts != 0 ) { 
  181. nocache_headers(); 
  182. echo json_encode( array( 
  183. 'numberofnewposts' => $number_of_new_posts,  
  184. 'html' => $posts_html,  
  185. 'lastposttime' => gmdate( 'Y-m-d H:i:s' ) 
  186. ) ); 
  187. } else { 
  188. header("HTTP/1.1 304 Not Modified"); 
  189.  
  190. function new_comment() { 
  191. if ( 'POST' != $_SERVER['REQUEST_METHOD'] || empty( $_POST['action'] ) || $_POST['action'] != 'new_comment' ) 
  192. die(); 
  193.  
  194. check_ajax_referer( 'ajaxnonce', '_ajax_post' ); 
  195.  
  196. $comment_content = isset( $_POST['comment'] ) ? trim( $_POST['comment'] ) : null; 
  197. $comment_post_ID = isset( $_POST['comment_post_ID'] ) ? trim( $_POST['comment_post_ID'] ) : null; 
  198.  
  199. $user = wp_get_current_user(); 
  200.  
  201. if ( is_user_logged_in() ) { 
  202. if ( empty( $user->display_name ) ) 
  203. $user->display_name = $user->user_login; 
  204. $comment_author = $user->display_name; 
  205. $comment_author_email = $user->user_email; 
  206. $comment_author_url = $user->user_url; 
  207. $user_ID = $user->ID; 
  208. } else { 
  209. if ( get_option( 'comment_registration' ) ) { 
  210. die( '<p>'.__( 'Error: you must be logged in to post a comment.', 'p2' ).'</p>' ); 
  211. $comment_author = ( isset($_POST['author']) ) ? trim(strip_tags($_POST['author'])) : null; 
  212. $comment_author_email = ( isset($_POST['email']) ) ? trim($_POST['email']) : null; 
  213. $comment_author_url = ( isset($_POST['url']) ) ? trim($_POST['url']) : null; 
  214.  
  215. $comment_type = ''; 
  216.  
  217. if ( get_option( 'require_name_email' ) && !$user->ID ) 
  218. if ( strlen( $comment_author_email ) < 6 || '' == $comment_author ) { 
  219. die( '<p>'.__( 'Error: please fill the required fields (name, email).', 'p2' ).'</p>' ); 
  220. } elseif ( !is_email( $comment_author_email ) ) { 
  221. die( '<p>'.__( 'Error: please enter a valid email address.', 'p2' ).'</p>' ); 
  222.  
  223. if ( '' == $comment_content ) 
  224. die( '<p>'.__( 'Error: Please type a comment.', 'p2' ).'</p>' ); 
  225.  
  226. $comment_parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : 0; 
  227.  
  228. $commentdata = compact( 'comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID' ); 
  229.  
  230. $comment_id = wp_new_comment( $commentdata ); 
  231. $comment = get_comment( $comment_id ); 
  232. if ( !$user->ID ) { 
  233. setcookie( 'comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 
  234. setcookie( 'comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 
  235. setcookie( 'comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); 
  236. if ($comment) echo $comment_id; 
  237. else echo __("Error: Unknown error occurred. Comment not posted.", 'p2' ); 
  238.  
  239. function get_latest_comments() { 
  240. global $wpdb, $comments, $comment, $max_depth, $depth, $user_login, $user_ID, $user_identity; 
  241.  
  242. $number = 10; //max amount of comments to load 
  243. $load_time = $_GET['load_time']; 
  244. $lc_widget = $_GET['lcwidget']; 
  245. $visible_posts = isset($_GET['vp'])? (array)$_GET['vp'] : array(); 
  246.  
  247. if ( get_option( 'thread_comments' ) ) 
  248. $max_depth = get_option( 'thread_comments_depth' ); 
  249. else 
  250. $max_depth = -1; 
  251.  
  252. // Since we currently cater the same HTML to all widgets,  
  253. // the instances without avatars will have to remove the avatar in javascript 
  254. $avatar_size = 32; 
  255.  
  256. //get new comments 
  257. if ($user_ID) { 
  258. $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' )) AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $user_ID, $load_time)); 
  259. } else if ( empty($comment_author) ) { 
  260. $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $load_time)); 
  261. } else { 
  262. $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE (comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) AND comment_date_gmt > %s ORDER BY comment_date_gmt DESC LIMIT $number", $comment_author, $comment_author_email, $load_time)); 
  263. $number_of_new_comments = count($comments); 
  264.  
  265. $prepare_comments = array(); 
  266. if ($number_of_new_comments > 0) { 
  267. foreach ($comments as $comment) { 
  268. // Setup comment html if post is visible 
  269. $comment_html = ''; 
  270. if ( in_array( $comment->comment_post_ID, $visible_posts ) ) 
  271. $comment_html = p2_comments($comment, array( 'max_depth' => $max_depth, 'before' => ' | ' ), $depth, false); 
  272.  
  273. // Setup widget html if widget is visible 
  274. $comment_widget_html = ''; 
  275. if ( $lc_widget ) 
  276. $comment_widget_html = P2_Recent_Comments::single_comment_html( $comment, $avatar_size ); 
  277.  
  278. $prepare_comments[] = array( "id" => $comment->comment_ID, "postID" => $comment->comment_post_ID, "commentParent" => $comment->comment_parent,  
  279. "html" => $comment_html, "widgetHtml" => $comment_widget_html ); 
  280. $json_data = array("numberofnewcomments" => $number_of_new_comments, "comments" => $prepare_comments, "lastcommenttime" => gmdate( 'Y-m-d H:i:s' ) ); 
  281.  
  282. echo json_encode( $json_data ); 
  283. } else { // No new comments 
  284. header("HTTP/1.1 304 Not Modified");