/** * AJAX receiver for Activity replies via the admin screen. Adds a new activity * comment, and returns HTML for a new table row. * * @since 1.6 */ function bp_activity_admin_reply() { // Check nonce check_ajax_referer('bp-activity-admin-reply', '_ajax_nonce-bp-activity-admin-reply'); $parent_id = !empty($_REQUEST['parent_id']) ? (int) $_REQUEST['parent_id'] : 0; $root_id = !empty($_REQUEST['root_id']) ? (int) $_REQUEST['root_id'] : 0; // $parent_id is required if (empty($parent_id)) { die('-1'); } // If $root_id not set (e.g. for root items), use $parent_id if (empty($root_id)) { $root_id = $parent_id; } // Check that a reply has been entered if (empty($_REQUEST['content'])) { die(__('ERROR: Please type a reply.', 'buddypress')); } // Check parent activity exists $parent_activity = new BP_Activity_Activity($parent_id); if (empty($parent_activity->component)) { die(__('ERROR: The item you are trying to reply to cannot be found, or it has been deleted.', 'buddypress')); } // @todo: Check if user is allowed to create new activity items // if ( ! current_user_can( 'bp_new_activity' ) ) if (!is_super_admin()) { die('-1'); } // Add new activity comment $new_activity_id = bp_activity_new_comment(array('activity_id' => $root_id, 'content' => $_REQUEST['content'], 'parent_id' => $parent_id)); // Fetch the new activity item, as we need it to create table markup to return $new_activity = new BP_Activity_Activity($new_activity_id); // This needs to be set for the BP_Activity_List_Table constructor to work set_current_screen('toplevel_page_bp-activity'); // Set up an output buffer ob_start(); $list_table = new BP_Activity_List_Table(); $list_table->single_row((array) $new_activity); // Get table markup $response = array('data' => ob_get_contents(), 'id' => $new_activity_id, 'position' => -1, 'what' => 'bp_activity'); ob_end_clean(); // Send response $r = new WP_Ajax_Response(); $r->add($response); $r->send(); exit; }
/** * @group get_activity_comments * * Verify the format of the activity comments array, for internal * refactoring */ public function test_get_activity_comments_format() { $now = time(); $u1 = $this->factory->user->create(); $u2 = $this->factory->user->create(); $a1 = $this->factory->activity->create(array('content' => 'Life Rules', 'recorded_time' => date('Y-m-d H:i:s', $now), 'user_id' => $u1)); $a2 = bp_activity_new_comment(array('activity_id' => $a1, 'content' => 'Candy is good', 'recorded_time' => date('Y-m-d H:i:s', $now - 50), 'user_id' => $u1)); $a3 = bp_activity_new_comment(array('activity_id' => $a1, 'content' => 'Bread is good', 'recorded_time' => date('Y-m-d H:i:s', $now - 25), 'user_id' => $u2)); $keys = array('id', 'item_id', 'secondary_item_id', 'user_id', 'primary_link', 'component', 'type', 'action', 'content', 'date_recorded', 'hide_sitewide', 'mptt_left', 'mptt_right', 'is_spam'); $a2_obj = new BP_Activity_Activity($a2); $e2 = new stdClass(); foreach ($keys as $key) { $e2->{$key} = $a2_obj->{$key}; } $e2_user = new WP_User($a2_obj->user_id); $e2->user_email = $e2_user->user_email; $e2->user_nicename = $e2_user->user_nicename; $e2->user_login = $e2_user->user_login; $e2->display_name = $e2_user->display_name; $e2->user_fullname = bp_core_get_user_displayname($e2->user_id); $e2->children = array(); $e2->depth = 1; $a3_obj = new BP_Activity_Activity($a3); $e3 = new stdClass(); foreach ($keys as $key) { $e3->{$key} = $a3_obj->{$key}; } $e3_user = new WP_User($e3->user_id); $e3->user_email = $e3_user->user_email; $e3->user_nicename = $e3_user->user_nicename; $e3->user_login = $e3_user->user_login; $e3->display_name = $e3_user->display_name; $e3->user_fullname = bp_core_get_user_displayname($e3->user_id); $e3->children = array(); $e3->depth = 1; $expected = array($a2 => $e2, $a3 => $e3); $a1_obj = new BP_Activity_Activity($a1); $comments = BP_Activity_Activity::get_activity_comments($a1, $a1_obj->mptt_left, $a1_obj->mptt_right, 'ham_only', $a1); $this->assertEquals($expected, $comments); }
/** * @group bp_has_activities */ public function test_bp_has_activities_with_type_new_blog_comments() { add_filter('bp_disable_blogforum_comments', '__return_false'); $u = $this->factory->user->create(); $now = time(); $a1 = $this->factory->activity->create(array('content' => 'Life Rules', 'component' => 'blogs', 'type' => 'new_blog_post', 'recorded_time' => date('Y-m-d H:i:s', $now), 'user_id' => $u)); $a2 = $this->factory->activity->create(array('content' => 'Life Drools', 'component' => 'blogs', 'type' => 'new_blog_comment', 'recorded_time' => date('Y-m-d H:i:s', $now - 100), 'user_id' => $u)); // This one will show up in the stream because it's a comment // on a blog post $a3 = bp_activity_new_comment(array('activity_id' => $a1, 'content' => 'Candy is good', 'recorded_time' => date('Y-m-d H:i:s', $now - 200), 'user_id' => $u)); $a4 = $this->factory->activity->create(array('content' => 'Life Rulez', 'component' => 'activity', 'type' => 'activity_update', 'recorded_time' => date('Y-m-d H:i:s', $now - 300), 'user_id' => $u)); // This one should not show up in the stream because it's a // comment on an activity item $a5 = bp_activity_new_comment(array('activity_id' => $a4, 'content' => 'Candy is great', 'recorded_time' => date('Y-m-d H:i:s', $now - 400), 'user_id' => $u)); global $activities_template; // prime bp_has_activities(array('component' => 'blogs', 'action' => 'new_blog_comment')); $this->assertEquals(array($a3, $a2), wp_parse_id_list(wp_list_pluck($activities_template->activities, 'id'))); // Clean up $activities_template = null; remove_filter('bp_disable_blogforum_comments', '__return_false'); }
/** * Posts new Activity comments received via a POST request. * * @global BP_Activity_Template $activities_template * @return string HTML * @since BuddyPress (1.2) */ function bp_legacy_theme_new_activity_comment() { global $activities_template; // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Check the nonce check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); if (!is_user_logged_in()) { exit('-1'); } if (empty($_POST['content'])) { exit('-1<div id="message" class="error"><p>' . __('Please do not leave the comment area blank.', 'buddypress') . '</p></div>'); } if (empty($_POST['form_id']) || empty($_POST['comment_id']) || !is_numeric($_POST['form_id']) || !is_numeric($_POST['comment_id'])) { exit('-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'); } $comment_id = bp_activity_new_comment(array('activity_id' => $_POST['form_id'], 'content' => $_POST['content'], 'parent_id' => $_POST['comment_id'])); if (!$comment_id) { exit('-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'); } // Load the new activity item into the $activities_template global bp_has_activities('display_comments=stream&hide_spam=false&include=' . $comment_id); // Swap the current comment with the activity item we just loaded if (isset($activities_template->activities[0])) { $activities_template->activity = new stdClass(); $activities_template->activity->id = $activities_template->activities[0]->item_id; $activities_template->activity->current_comment = $activities_template->activities[0]; } // get activity comment template part bp_get_template_part('activity/comment'); unset($activities_template); exit; }
/** * Post new activity comment. * * @since 1.2.0 * * @uses is_user_logged_in() * @uses bp_is_activity_component() * @uses bp_is_current_action() * @uses check_admin_referer() * @uses apply_filters() To call 'bp_activity_post_comment_activity_id' hook. * @uses apply_filters() To call 'bp_activity_post_comment_content' hook. * @uses bp_core_add_message() * @uses bp_core_redirect() * @uses bp_activity_new_comment() * @uses wp_get_referer() * * @return bool False on failure. */ function bp_activity_action_post_comment() { if (!is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action('reply')) { return false; } // Check the nonce. check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); /** * Filters the activity ID a comment will be in reply to. * * @since 1.2.0 * * @param string $value ID of the activity being replied to. */ $activity_id = apply_filters('bp_activity_post_comment_activity_id', $_POST['comment_form_id']); /** * Filters the comment content for a comment reply. * * @since 1.2.0 * * @param string $value Comment content being posted. */ $content = apply_filters('bp_activity_post_comment_content', $_POST['ac_input_' . $activity_id]); if (empty($content)) { bp_core_add_message(__('Please do not leave the comment area blank.', 'buddypress'), 'error'); bp_core_redirect(wp_get_referer() . '#ac-form-' . $activity_id); } $comment_id = bp_activity_new_comment(array('content' => $content, 'activity_id' => $activity_id, 'parent_id' => false)); if (!empty($comment_id)) { bp_core_add_message(__('Reply Posted!', 'buddypress')); } else { bp_core_add_message(__('There was an error posting that reply. Please try again.', 'buddypress'), 'error'); } bp_core_redirect(wp_get_referer() . '#ac-form-' . $activity_id); }
/** * @group bp_blogs_sync_activity_edit_to_post_comment * @group post_type_comment_activities * @group imath */ public function test_bp_blogs_sync_activity_edit_to_post_comment_trash_comment_ham_activity() { $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); $userdata = get_userdata($u); // let's use activity comments instead of single "new_blog_comment" activity items add_filter('bp_disable_blogforum_comments', '__return_false'); // create the blog post $post_id = $this->factory->post->create(array('post_status' => 'publish', 'post_type' => 'post', 'post_title' => 'Test activity comment to post comment')); // grab the activity ID for the activity comment $a1 = bp_activity_get_activity_id(array('type' => 'new_blog_post', 'component' => buddypress()->blogs->id, 'filter' => array('item_id' => get_current_blog_id(), 'secondary_item_id' => $post_id))); $a2 = bp_activity_new_comment(array('content' => 'the generated comment should be spamed/unspamed once the activity comment is spamed/unspamed', 'user_id' => $u, 'activity_id' => $a1)); $c = bp_activity_get_meta($a2, 'bp_blogs_post_comment_id'); wp_trash_comment($c); $activity = new BP_Activity_Activity($a2); bp_activity_mark_as_ham($activity); $activity->save(); $post_comments = get_comments(array('post_id' => $post_id, 'status' => 'approve')); $comment = reset($post_comments); $this->assertTrue((int) $comment->comment_ID === (int) bp_activity_get_meta($a2, 'bp_blogs_post_comment_id'), 'The comment ID should be in the activity meta'); $this->assertTrue((int) $a2 === (int) get_comment_meta($comment->comment_ID, 'bp_activity_comment_id', true), 'The activity ID should be in the comment meta'); // reset remove_filter('bp_disable_blogforum_comments', '__return_false'); $this->set_current_user($old_user); }
function bp_dtheme_new_activity_comment() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Check the nonce check_admin_referer('new_activity_comment', '_nxtnonce_new_activity_comment'); if (!is_user_logged_in()) { echo '-1'; return false; } if (empty($_POST['content'])) { echo '-1<div id="message" class="error"><p>' . __('Please do not leave the comment area blank.', 'buddypress') . '</p></div>'; return false; } if (empty($_POST['form_id']) || empty($_POST['comment_id']) || !is_numeric($_POST['form_id']) || !is_numeric($_POST['comment_id'])) { echo '-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'; return false; } $comment_id = bp_activity_new_comment(array('activity_id' => $_POST['form_id'], 'content' => $_POST['content'], 'parent_id' => $_POST['comment_id'])); if (!$comment_id) { echo '-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'; return false; } global $activities_template; // Load the new activity item into the $activities_template global bp_has_activities('display_comments=stream&include=' . $comment_id); // Swap the current comment with the activity item we just loaded $activities_template->activity->id = $activities_template->activities[0]->item_id; $activities_template->activity->current_comment = $activities_template->activities[0]; $template = locate_template('activity/comment.php', false, false); // Backward compatibility. In older versions of BP, the markup was // generated in the PHP instead of a template. This ensures that // older themes (which are not children of bp-default and won't // have the new template) will still work. if (empty($template)) { $template = BP_PLUGIN_DIR . '/bp-themes/bp-default/activity/comment.php'; } load_template($template, false); unset($activities_template); }
function check_return_comments() { global $rtmedia_query; if ($rtmedia_query->action_query->action != 'comment') { return; } if (isset($rtmedia_query->action_query->id) && count($_POST)) { /** * /media/comments [POST] * Post a comment to the album by post id */ $nonce = $_REQUEST['rtmedia_comment_nonce']; if (wp_verify_nonce($nonce, 'rtmedia_comment_nonce')) { if (empty($_POST['comment_content'])) { return false; } $comment = new RTMediaComment(); $attr = $_POST; $mediaModel = new RTMediaModel(); $result = $mediaModel->get(array('id' => $rtmedia_query->action_query->id)); if (!isset($attr['comment_post_ID'])) { $attr['comment_post_ID'] = $result[0]->media_id; } $id = $comment->add($attr); if ($result[0]->activity_id != null) { global $rtmedia_buddypress_activity; remove_action("bp_activity_comment_posted", array($rtmedia_buddypress_activity, "comment_sync"), 10, 2); if (function_exists('bp_activity_new_comment')) { $comment_activity_id = bp_activity_new_comment(array('content' => $_POST['comment_content'], 'activity_id' => $result[0]->activity_id)); } } if (!empty($comment_activity_id)) { update_comment_meta($id, 'activity_id', $comment_activity_id); } if (isset($_POST["rtajax"])) { global $wpdb; $comments = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id), ARRAY_A); echo rmedia_single_comment($comments); exit; } } else { _e('Ooops !!! Invalid access. No nonce was found !!', 'buddypress-media'); } } }
function bp_dtheme_new_activity_comment() { global $bp; /* Check the nonce */ check_admin_referer( 'new_activity_comment', '_wpnonce_new_activity_comment' ); if ( !is_user_logged_in() ) { echo '-1'; return false; } if ( empty( $_POST['content'] ) ) { echo '-1<div id="message" class="error"><p>' . __( 'Please do not leave the comment area blank.', 'buddypress' ) . '</p></div>'; return false; } if ( empty( $_POST['form_id'] ) || empty( $_POST['comment_id'] ) || !is_numeric( $_POST['form_id'] ) || !is_numeric( $_POST['comment_id'] ) ) { echo '-1<div id="message" class="error"><p>' . __( 'There was an error posting that reply, please try again.', 'buddypress' ) . '</p></div>'; return false; } $comment_id = bp_activity_new_comment( array( 'content' => $_POST['content'], 'activity_id' => $_POST['form_id'], 'parent_id' => $_POST['comment_id'] )); if ( !$comment_id ) { echo '-1<div id="message" class="error"><p>' . __( 'There was an error posting that reply, please try again.', 'buddypress' ) . '</p></div>'; return false; } if ( bp_has_activities ( 'include=' . $comment_id ) ) : ?> <?php while ( bp_activities() ) : bp_the_activity(); ?> <li id="acomment-<?php bp_activity_id() ?>"> <div class="acomment-avatar"> <?php bp_activity_avatar() ?> </div> <div class="acomment-meta"> <?php echo bp_core_get_userlink( bp_get_activity_user_id() ) ?> · <?php printf( __( '%s ago', 'buddypress' ), bp_core_time_since( bp_core_current_time() ) ) ?> · <a class="acomment-reply" href="#acomment-<?php bp_activity_id() ?>" id="acomment-reply-<?php echo esc_attr( $_POST['form_id'] ) ?>"><?php _e( 'Reply', 'buddypress' ) ?></a> · <a href="<?php echo wp_nonce_url( $bp->root_domain . '/' . $bp->activity->slug . '/delete/' . bp_get_activity_id() . '?cid=' . $comment_id, 'bp_activity_delete_link' ) ?>" class="delete acomment-delete confirm"><?php _e( 'Delete', 'buddypress' ) ?></a> </div> <div class="acomment-content"> <?php bp_activity_content_body() ?> </div> </li> <?php endwhile; ?> <?php endif; }
/** * @group bp_blogs_comment_sync_activity_comment * @group post_type_comment_activities */ public function test_bp_blogs_comment_sync_activity_comment_for_custom_post_type() { if (is_multisite()) { $b = $this->factory->blog->create(); switch_to_blog($b); add_filter('comment_flood_filter', '__return_false'); } else { $b = get_current_blog_id(); } $u = $this->factory->user->create(); $userdata = get_userdata($u); $labels = array('name' => 'bars', 'singular_name' => 'bar'); register_post_type('foo', array('labels' => $labels, 'public' => true, 'supports' => array('comments'))); add_post_type_support('foo', 'buddypress-activity'); bp_activity_set_post_type_tracking_args('foo', array('comment_action_id' => 'new_foo_comment')); add_filter('bp_disable_blogforum_comments', '__return_false'); $p = $this->factory->post->create(array('post_author' => $u, 'post_type' => 'foo')); $a1 = bp_activity_get_activity_id(array('type' => 'new_foo', 'filter' => array('item_id' => $b, 'secondary_item_id' => $p))); $c = wp_new_comment(array('comment_post_ID' => $p, 'comment_author' => $userdata->user_nicename, 'comment_author_url' => 'http://buddypress.org', 'comment_author_email' => $userdata->user_email, 'comment_content' => 'this is a foo comment', 'comment_type' => '', 'comment_parent' => 0, 'user_id' => $u)); $a2 = bp_activity_new_comment(array('content' => 'this should generate a new foo comment', 'user_id' => $u, 'activity_id' => $a1)); $activity_args = array('type' => 'activity_comment', 'display_comments' => 'stream', 'meta_query' => array(array('key' => 'bp_blogs_foo_comment_id', 'compare' => 'exists'))); $a = bp_activity_get($activity_args); $aids = wp_list_pluck($a['activities'], 'id'); $cids = wp_list_pluck(get_approved_comments($p), 'comment_ID'); foreach ($aids as $aid) { $this->assertTrue(in_array(bp_activity_get_meta($aid, 'bp_blogs_foo_comment_id'), $cids), 'The comment ID should be in the activity meta'); } foreach ($cids as $cid) { $this->assertTrue(in_array(get_comment_meta($cid, 'bp_activity_comment_id', true), $aids), 'The activity ID should be in the comment meta'); } _unregister_post_type('foo'); if (is_multisite()) { restore_current_blog(); remove_filter('comment_flood_filter', '__return_false'); } remove_filter('bp_disable_blogforum_comments', '__return_false'); }
/** * Posts new Activity comments received via a POST request. * * @global BP_Activity_Template $activities_template * @return string HTML * @since BuddyPress (1.2) */ function bp_dtheme_new_activity_comment() { global $activities_template; // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Check the nonce check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); if (!is_user_logged_in()) { exit('-1'); } if (empty($_POST['content'])) { exit('-1<div id="message" class="error"><p>' . __('Please do not leave the comment area blank.', 'buddypress') . '</p></div>'); } if (empty($_POST['form_id']) || empty($_POST['comment_id']) || !is_numeric($_POST['form_id']) || !is_numeric($_POST['comment_id'])) { exit('-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'); } $comment_id = bp_activity_new_comment(array('activity_id' => $_POST['form_id'], 'content' => $_POST['content'], 'parent_id' => $_POST['comment_id'], 'error_type' => 'wp_error')); if (false === $comment_id) { exit('-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'); } elseif (is_wp_error($comment_id)) { exit('-1<div id="message" class="error bp-ajax-message"><p>' . esc_html($comment_id->get_error_message()) . '</p></div>'); } // Load the new activity item into the $activities_template global bp_has_activities('display_comments=stream&hide_spam=false&show_hidden=true&include=' . $comment_id); // Swap the current comment with the activity item we just loaded $activities_template->activity = new stdClass(); $activities_template->activity->id = $activities_template->activities[0]->item_id; $activities_template->activity->current_comment = $activities_template->activities[0]; $template = locate_template('activity/comment.php', false, false); /** * Backward compatibility. In older versions of BP, the markup was * generated in the PHP instead of a template. This ensures that * older themes (which are not children of bp-default and won't * have the new template) will still work. */ if (empty($template)) { $template = buddypress()->plugin_dir . '/bp-themes/bp-default/activity/comment.php'; } load_template($template, false); unset($activities_template); exit; }
/** * @group bp_activity_new_comment * @group BP5907 */ public function test_bp_activity_comment_on_deleted_activity() { $u = $this->factory->user->create(); $a = $this->factory->activity->create(); bp_activity_delete_by_activity_id($a); $c = bp_activity_new_comment(array('activity_id' => $a, 'parent_id' => $a, 'content' => 'foo', 'user_id' => $u)); $this->assertEmpty($c); }
/** * Post comment on activity_id or media_id * @global type $this ->msg_server_error * @global int $this ->ec_server_error * @global int $this ->ec_invalid_media_id * @global type $this ->msg_invalid_media_id */ function rtmedia_api_process_add_rtmedia_comment_request() { $this->rtmediajsonapifunction->rtmedia_api_verfiy_token(); $this->rtmediajsonapifunction->rtmedia_api_media_activity_id_missing(); //Post comment errors $ec_comment_content_missing = 800001; $msg_comment_content_missing = esc_html__('comment content missing', 'buddypress-media'); $ec_comment_posted = 800002; $msg_comment_posted = esc_html__('comment posted', 'buddypress-media'); //Fetch user id from token $user_data = get_userdata($this->user_id); $content = filter_input(INPUT_POST, 'content', FILTER_SANITIZE_STRING); if (empty($content)) { wp_send_json($this->rtmedia_api_response_object('FALSE', $ec_comment_content_missing, $msg_comment_content_missing)); } if (empty($activity_id) && !empty($media_id)) { $activity_id = $this->rtmediajsonapifunction->rtmedia_api_activityid_from_mediaid($media_id); } if (empty($activity_id)) { wp_send_json($this->rtmedia_api_response_object('FALSE', $this->ec_invalid_media_id, $this->msg_invalid_media_id)); } $args = array('content' => $content, 'activity_id' => intval($activity_id), 'user_id' => intval($this->user_id), 'parent_id' => false); if (function_exists('bp_activity_new_comment')) { $comment_id = bp_activity_new_comment($args); } if (isset($comment_id)) { wp_send_json($this->rtmedia_api_response_object('TRUE', $ec_comment_posted, $msg_comment_posted)); } else { wp_send_json($this->rtmedia_api_response_object('FALSE', $this->msg_server_error, $this->ec_server_error)); } }
/** * Update Activity and blogs meta and eventually sync comment with activity comment * * @since 2.5.0 * * @param int|bool $activity_id ID of recorded activity, or false if sync is active. * @param WP_Comment|null $comment The comment object. * @param array $activity_args Array of activity arguments. * @param object|null $activity_post_object The post type tracking args object. * @return int|bool Returns false if no activity, the activity id otherwise. */ function bp_blogs_comment_sync_activity_comment(&$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null) { if (empty($activity_args) || empty($comment->post->ID) || empty($activity_post_object->comment_action_id)) { return false; } // Set the current blog id. $blog_id = get_current_blog_id(); // These activity metadatas are used to build the new_blog_comment action string if (!empty($activity_id) && !empty($activity_args['item_id']) && 'new_blog_comment' === $activity_post_object->comment_action_id) { // add some post info in activity meta bp_activity_update_meta($activity_id, 'post_title', $comment->post->post_title); bp_activity_update_meta($activity_id, 'post_url', esc_url_raw(add_query_arg('p', $comment->post->ID, home_url('/')))); } // Sync comment - activity comment if (!bp_disable_blogforum_comments()) { if (!empty($_REQUEST['action'])) { $existing_activity_id = get_comment_meta($comment->comment_ID, 'bp_activity_comment_id', true); if (!empty($existing_activity_id)) { $activity_args['id'] = $existing_activity_id; } } if (empty($activity_post_object)) { $activity_post_object = bp_activity_get_post_type_tracking_args($comment->post->post_type); } if (isset($activity_post_object->action_id) && isset($activity_post_object->component_id)) { // find the parent 'new_post_type' activity entry $parent_activity_id = bp_activity_get_activity_id(array('component' => $activity_post_object->component_id, 'type' => $activity_post_object->action_id, 'item_id' => $blog_id, 'secondary_item_id' => $comment->comment_post_ID)); // Try to create a new activity item for the parent blog post. if (empty($parent_activity_id)) { $parent_activity_id = bp_activity_post_type_publish($comment->post->ID, $comment->post); } } // we found the parent activity entry // so let's go ahead and reconfigure some activity args if (!empty($parent_activity_id)) { // set the parent activity entry ID $activity_args['activity_id'] = $parent_activity_id; // now see if the WP parent comment has a BP activity ID $comment_parent = 0; if (!empty($comment->comment_parent)) { $comment_parent = get_comment_meta($comment->comment_parent, 'bp_activity_comment_id', true); } // WP parent comment does not have a BP activity ID // so set to 'new_' . post_type activity ID if (empty($comment_parent)) { $comment_parent = $parent_activity_id; } $activity_args['parent_id'] = $comment_parent; $activity_args['skip_notification'] = true; // could not find corresponding parent activity entry // so wipe out $args array } else { $activity_args = array(); } // Record in activity streams if (!empty($activity_args)) { $activity_id = bp_activity_new_comment($activity_args); if (empty($activity_args['id'])) { // The activity metadata to inform about the corresponding comment ID bp_activity_update_meta($activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID); // The comment metadata to inform about the corresponding activity ID add_comment_meta($comment->comment_ID, 'bp_activity_comment_id', $activity_id); // These activity metadatas are used to build the new_blog_comment action string if ('new_blog_comment' === $activity_post_object->comment_action_id) { bp_activity_update_meta($activity_id, 'post_title', $comment->post->post_title); bp_activity_update_meta($activity_id, 'post_url', esc_url_raw(add_query_arg('p', $comment->post->ID, home_url('/')))); } } /** * Fires after an activity comment is added from a WP post comment. * * @since 2.6.0 * * @param int $activity_id The activity comment ID. * @param WP_Comment $post_type_comment WP Comment object. * @param array $activity_args Activity comment arguments. * @param object $activity_post_object The post type tracking args object. */ do_action('bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object); } } // Update the blogs last active date bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time()); if ('new_blog_comment' === $activity_post_object->comment_action_id) { /** * Fires after BuddyPress has recorded metadata about a published blog post comment. * * @since 2.5.0 * * @param int $value Comment ID of the blog post comment being recorded. * @param WP_Post $post WP_Comment object for the current blog post. * @param string $value ID of the user associated with the current blog post comment. */ do_action('bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id()); } return $activity_id; }
/** * @group bp_blogs_sync_activity_edit_to_post_comment * @group post_type_comment_activities */ public function test_spammed_activity_comment_should_not_create_post_comment() { $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); $userdata = get_userdata($u); // let's use activity comments instead of single "new_blog_comment" activity items. add_filter('bp_disable_blogforum_comments', '__return_false'); // create the blog post. $post_id = $this->factory->post->create(array('post_status' => 'publish', 'post_type' => 'post', 'post_title' => 'Test activity comment to post comment')); // Grab the activity ID for the activity comment. $a1 = bp_activity_get_activity_id(array('type' => 'new_blog_post', 'component' => buddypress()->blogs->id, 'filter' => array('item_id' => get_current_blog_id(), 'secondary_item_id' => $post_id))); // Set activity item to spam. add_action('bp_activity_before_save', array($this, 'set_activity_to_spam')); // Create spammed activity comment. $a2 = bp_activity_new_comment(array('content' => 'this activity comment shoud not be created as a new post comment. yolo.', 'user_id' => $u, 'activity_id' => $a1)); // Grab post comments. $approved_comments = get_approved_comments($post_id); $comment = reset($approved_comments); // Assert that post comment wasn't created. $this->assertEmpty($comment); // Reset. remove_filter('bp_disable_blogforum_comments', '__return_false'); remove_action('bp_activity_before_save', array($this, 'set_activity_to_spam')); $this->set_current_user($old_user); }
function check_return_comments() { global $rtmedia_query; if ('comment' !== $rtmedia_query->action_query->action) { return; } if (isset($rtmedia_query->action_query->id) && count($_POST)) { // @codingStandardsIgnoreLine /** * /media/comments [POST] * Post a comment to the album by post id */ $nonce = isset($_REQUEST['rtmedia_comment_nonce']) ? wp_unslash($_REQUEST['rtmedia_comment_nonce']) : ''; $comment_content = isset($_REQUEST['comment_content']) ? sanitize_text_field(wp_unslash($_REQUEST['comment_content'])) : ''; if (wp_verify_nonce($nonce, 'rtmedia_comment_nonce')) { if (empty($comment_content)) { return false; } $comment = new RTMediaComment(); $attr = $_POST; $media_model = new RTMediaModel(); $result = $media_model->get(array('id' => $rtmedia_query->action_query->id)); if (!isset($attr['comment_post_ID'])) { $attr['comment_post_ID'] = $result[0]->media_id; } $id = $comment->add($attr); if (!is_null($result[0]->activity_id)) { global $rtmedia_buddypress_activity; remove_action('bp_activity_comment_posted', array($rtmedia_buddypress_activity, 'comment_sync'), 10, 2); if (function_exists('bp_activity_new_comment')) { $comment_activity_id = bp_activity_new_comment(array('content' => $comment_content, 'activity_id' => $result[0]->activity_id)); do_action('rtm_bp_activity_comment_posted', $comment_activity_id, $result[0]); } } if (!empty($comment_activity_id)) { $rtmedia_activity_comment = rtmedia_activity_comment($comment_activity_id); if ($rtmedia_activity_comment['content']) { update_comment_meta($id, 'activity_comment_content', $rtmedia_activity_comment['content']); } update_comment_meta($id, 'activity_id', $comment_activity_id); } $_rt_ajax = filter_input(INPUT_POST, 'rtajax', FILTER_SANITIZE_STRING); if (!empty($_rt_ajax)) { global $wpdb; $comments = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d limit 100", $id), ARRAY_A); echo rmedia_single_comment($comments); // @codingStandardsIgnoreLine exit; } } else { esc_html_e('Ooops !!! Invalid access. No nonce was found !!', 'buddypress-media'); } } }
/** * Posts new Activity comments received via a POST request. * * @global BP_Activity_Template $activities_template * @return string HTML * @since BuddyPress (1.2) */ function bp_legacy_theme_new_activity_comment() { global $activities_template; $bp = buddypress(); // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Check the nonce check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); if (!is_user_logged_in()) { exit('-1'); } $feedback = __('There was an error posting your reply. Please try again.', 'buddypress'); if (empty($_POST['content'])) { exit('-1<div id="message" class="error bp-ajax-message"><p>' . esc_html__('Please do not leave the comment area blank.', 'buddypress') . '</p></div>'); } if (empty($_POST['form_id']) || empty($_POST['comment_id']) || !is_numeric($_POST['form_id']) || !is_numeric($_POST['comment_id'])) { exit('-1<div id="message" class="error bp-ajax-message"><p>' . esc_html($feedback) . '</p></div>'); } $comment_id = bp_activity_new_comment(array('activity_id' => $_POST['form_id'], 'content' => $_POST['content'], 'parent_id' => $_POST['comment_id'])); if (!$comment_id) { if (!empty($bp->activity->errors['new_comment']) && is_wp_error($bp->activity->errors['new_comment'])) { $feedback = $bp->activity->errors['new_comment']->get_error_message(); unset($bp->activity->errors['new_comment']); } exit('-1<div id="message" class="error bp-ajax-message"><p>' . esc_html($feedback) . '</p></div>'); } // Load the new activity item into the $activities_template global bp_has_activities('display_comments=stream&hide_spam=false&show_hidden=true&include=' . $comment_id); // Swap the current comment with the activity item we just loaded if (isset($activities_template->activities[0])) { $activities_template->activity = new stdClass(); $activities_template->activity->id = $activities_template->activities[0]->item_id; $activities_template->activity->current_comment = $activities_template->activities[0]; // Because the whole tree has not been loaded, we manually // determine depth $depth = 1; $parent_id = (int) $activities_template->activities[0]->secondary_item_id; while ($parent_id !== (int) $activities_template->activities[0]->item_id) { $depth++; $p_obj = new BP_Activity_Activity($parent_id); $parent_id = (int) $p_obj->secondary_item_id; } $activities_template->activity->current_comment->depth = $depth; } // get activity comment template part bp_get_template_part('activity/comment'); unset($activities_template); exit; }
/** * @group bp_blogs_record_comment * @group unique */ public function test_bp_blogs_record_comment_no_duplicate_activity_comments() { // save the current user and override logged-in user $old_user = get_current_user_id(); $u = $this->factory->user->create(); $this->set_current_user($u); $userdata = get_userdata($u); $this->activity_saved_comment_count = 0; $this->comment_saved_count = 0; // let's use activity comments instead of single "new_blog_comment" activity items add_filter('bp_disable_blogforum_comments', '__return_false'); add_action('bp_activity_add', array($this, 'count_activity_comment_saved')); add_action('wp_insert_comment', array($this, 'count_post_comment_saved')); add_action('edit_comment', array($this, 'count_post_comment_saved')); // create the blog post $post_id = $this->factory->post->create(array('post_status' => 'publish', 'post_type' => 'post', 'post_title' => 'Test Duplicate activity comments')); // grab the activity ID for the activity comment $a1 = bp_activity_get_activity_id(array('type' => 'new_blog_post', 'component' => buddypress()->blogs->id, 'filter' => array('item_id' => get_current_blog_id(), 'secondary_item_id' => $post_id))); $a2 = bp_activity_new_comment(array('content' => 'activity comment should be unique', 'user_id' => $u, 'activity_id' => $a1)); $activities = bp_activity_get(array('type' => 'activity_comment', 'display_comments' => 'stream', 'search_terms' => 'activity comment should be unique')); $this->assertTrue(count($activities['activities']) === 1, 'An activity comment should be unique'); $this->assertTrue(2 === $this->activity_saved_comment_count, 'An activity comment should be saved only twice'); $this->assertTrue(1 === $this->comment_saved_count, 'A comment should be saved only once'); // reset remove_filter('bp_disable_blogforum_comments', '__return_false'); remove_action('bp_activity_add', array($this, 'count_activity_comment_saved')); remove_action('wp_insert_comment', array($this, 'count_post_comment_saved')); remove_action('edit_comment', array($this, 'count_post_comment_saved')); $this->activity_saved_comment_count = 0; $this->comment_saved_count = 0; $this->set_current_user($old_user); }
/** * Post comment on activity_id or media_id * @global type $this->msg_server_error * @global int $this->ec_server_error * @global int $this->ec_invalid_media_id * @global type $this->msg_invalid_media_id */ function rtmedia_api_process_add_rtmedia_comment_request() { $this->rtmediajsonapifunction->rtmedia_api_verfiy_token(); $this->rtmediajsonapifunction->rtmedia_api_media_activity_id_missing(); //Post comment errors $ec_comment_content_missing = 800001; $msg_comment_content_missing = __('comment content missing', 'rtmedia'); $ec_comment_posted = 800002; $msg_comment_posted = __('comment posted', 'rtmedia'); //Fetch user id from token $user_data = get_userdata($this->user_id); if (empty($_POST['content'])) { echo $this->rtmedia_api_response_object('FALSE', $ec_comment_content_missing, $msg_comment_content_missing); exit; } extract($_POST); if (empty($activity_id) && !empty($media_id)) { $activity_id = $this->rtmediajsonapifunction->rtmedia_api_activityid_from_mediaid($media_id); } if (empty($activity_id)) { echo $this->rtmedia_api_response_object('FALSE', $this->ec_invalid_media_id, $this->msg_invalid_media_id); exit; } $args = array('content' => $content, 'activity_id' => $activity_id, 'user_id' => $this->user_id, 'parent_id' => false); if (function_exists('bp_activity_new_comment')) { $comment_id = bp_activity_new_comment($args); } if ($comment_id) { echo $this->rtmedia_api_response_object('TRUE', $ec_comment_posted, $msg_comment_posted); exit; } else { echo $this->rtmedia_api_response_object('FALSE', $this->msg_server_error, $this->ec_server_error); exit; } }
/** * Post new activity comment. * * @since BuddyPress (1.2) * * @uses is_user_logged_in() * @uses bp_is_activity_component() * @uses bp_is_current_action() * @uses check_admin_referer() * @uses apply_filters() To call 'bp_activity_post_comment_activity_id' hook. * @uses apply_filters() To call 'bp_activity_post_comment_content' hook. * @uses bp_core_add_message() * @uses bp_core_redirect() * @uses bp_activity_new_comment() * @uses wp_get_referer() * * @return bool False on failure. */ function bp_activity_action_post_comment() { if (!is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action('reply')) { return false; } // Check the nonce check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); $activity_id = apply_filters('bp_activity_post_comment_activity_id', $_POST['comment_form_id']); $content = apply_filters('bp_activity_post_comment_content', $_POST['ac_input_' . $activity_id]); if (empty($content)) { bp_core_add_message(__('Please do not leave the comment area blank.', 'buddypress'), 'error'); bp_core_redirect(wp_get_referer() . '#ac-form-' . $activity_id); } $comment_id = bp_activity_new_comment(array('content' => $content, 'activity_id' => $activity_id, 'parent_id' => false)); if (!empty($comment_id)) { bp_core_add_message(__('Reply Posted!', 'buddypress')); } else { bp_core_add_message(__('There was an error posting that reply, please try again.', 'buddypress'), 'error'); } bp_core_redirect(wp_get_referer() . '#ac-form-' . $activity_id); }
function bp_dtheme_new_activity_comment() { global $bp; // Check the nonce check_admin_referer('new_activity_comment', '_wpnonce_new_activity_comment'); if (!is_user_logged_in()) { echo '-1'; return false; } if (empty($_POST['content'])) { echo '-1<div id="message" class="error"><p>' . __('Please do not leave the comment area blank.', 'buddypress') . '</p></div>'; return false; } if (empty($_POST['form_id']) || empty($_POST['comment_id']) || !is_numeric($_POST['form_id']) || !is_numeric($_POST['comment_id'])) { echo '-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'; return false; } $comment_id = bp_activity_new_comment(array('activity_id' => $_POST['form_id'], 'content' => $_POST['content'], 'parent_id' => $_POST['comment_id'])); if (!$comment_id) { echo '-1<div id="message" class="error"><p>' . __('There was an error posting that reply, please try again.', 'buddypress') . '</p></div>'; return false; } global $activities_template; // Load the new activity item into the $activities_template global bp_has_activities('display_comments=stream&include=' . $comment_id); // Swap the current comment with the activity item we just loaded $activities_template->activity->id = $activities_template->activities[0]->item_id; $activities_template->activity->current_comment = $activities_template->activities[0]; gconnect_locate_template(array('activity/comment.php'), true); unset($activities_template); }
/** * Post by email routine. * * Validates the parsed data and posts the various BuddyPress content. * * @since 1.0-RC3 * * @param bool $retval True by default. * @param array $data { * An array of arguments. * * @type array $headers Email headers. * @type string $content The email body content. * @type string $subject The email subject line. * @type int $user_id The user ID who sent the email. * @type bool $is_html Whether the email content is HTML or not. * @type int $i The email message number. * } * @param array $params Parsed paramaters from the email address querystring. * See {@link BP_Reply_By_Email_Parser::get_parameters()}. * @return array|object Array of the parsed item on success. WP_Error object * on failure. */ public function post($retval, $data, $params) { global $bp, $wpdb; // Activity reply if (!empty($params['a'])) { bp_rbe_log('Message #' . $data['i'] . ': this is an activity reply, checking if parent activities still exist'); // Check to see if the root activity ID and the parent activity ID exist before posting $activity_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$bp->activity->table_name} WHERE id IN ( %d, %d )", $params['a'], $params['p'])); // If $a = $p, this means that we're replying to a top-level activity update // So check if activity count is 1 if ($params['a'] == $params['p'] && $activity_count != 1) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'root_activity_deleted' ); return new WP_Error('root_activity_deleted'); // If we're here, this means we're replying to an activity comment // If count != 2, this means either the super admin or activity author has deleted one of the update(s) } elseif ($params['a'] != $params['p'] && $activity_count != 2) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'root_or_parent_activity_deleted' ); return new WP_Error('root_or_parent_activity_deleted'); } /* Let's start posting! */ // Add our filter to override the activity action in bp_activity_new_comment() bp_rbe_activity_comment_action_filter($data['user_id']); $comment_id = bp_activity_new_comment(array('content' => $data['content'], 'user_id' => $data['user_id'], 'activity_id' => $params['a'], 'parent_id' => $params['p'])); if (!$comment_id) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'activity_comment_fail' ); return new WP_Error('activity_comment_fail'); } // special hook for RBE activity items // might want to do something like add some activity meta do_action('bp_rbe_new_activity', array('activity_id' => $comment_id, 'type' => 'activity_comment', 'user_id' => $data['user_id'], 'item_id' => $params['a'], 'secondary_item_id' => $params['p'], 'content' => $data['content'])); bp_rbe_log('Message #' . $data['i'] . ': activity comment successfully posted!'); // remove the filter after posting remove_filter('bp_activity_comment_action', 'bp_rbe_activity_comment_action'); // return array of item on success return array('activity_comment_id' => $comment_id); // Private message reply } elseif (!empty($params['m'])) { if (bp_is_active($bp->messages->id)) { bp_rbe_log('Message #' . $data['i'] . ': this is a private message reply'); // see if the PM thread still exists if (messages_is_valid_thread($params['m'])) { // see if the user is in the PM conversation $has_access = messages_check_thread_access($params['m'], $data['user_id']) || is_super_admin($data['user_id']); if (!$has_access) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'private_message_not_in_thread' ); return new WP_Error('private_message_not_in_thread'); } // post the PM! $message_id = messages_new_message(array('thread_id' => $params['m'], 'sender_id' => $data['user_id'], 'content' => $data['content'])); if (!$message_id) { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'private_message_fail' ); return new WP_Error('private_message_fail'); } // special hook for RBE parsed PMs do_action('bp_rbe_new_pm_reply', array('thread_id' => $params['m'], 'sender_id' => $data['user_id'], 'content' => $data['content'])); bp_rbe_log('Message #' . $data['i'] . ': PM reply successfully posted!'); // return array of item on success return array('message_id' => $message_id); // the PM thread doesn't exist anymore } else { //do_action( 'bp_rbe_imap_no_match', $this->connection, $i, $headers, 'private_message_thread_deleted' ); return new WP_Error('private_message_thread_deleted'); } } } }