Skip to content

Commit

Permalink
Fixed: user is not able to edit their own question.
Browse files Browse the repository at this point in the history
  • Loading branch information
rahularyan committed Jan 21, 2018
1 parent 9a4a05f commit 1555ff2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
15 changes: 7 additions & 8 deletions addons/email/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ public function get_admin_emails( $opt ) {

if ( ap_opt( $opt ) ) {
$admin_emails = explode( ',', preg_replace( '/\s+/', '', ap_opt( 'email_admin_emails' ) ) );

// Don't bother if current user is admin.
if ( empty( $admin_emails ) || in_array( $current_user->user_email, $admin_emails, true ) ) {
return false;
Expand All @@ -303,7 +302,7 @@ public function get_admin_emails( $opt ) {
public function ap_after_new_question( $question_id ) {
$args = [];

$admin_emails = self::get_admin_emails( 'email_admin_new_question' );
$admin_emails = $this->get_admin_emails( 'email_admin_new_question' );

if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
Expand Down Expand Up @@ -339,7 +338,7 @@ public function ap_after_new_answer( $answer_id ) {
'users' => [],
);

$admin_emails = self::get_admin_emails( 'email_admin_new_answer' );
$admin_emails = $this->get_admin_emails( 'email_admin_new_answer' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down Expand Up @@ -406,7 +405,7 @@ public function select_answer( $_post ) {
public function new_comment( $comment ) {
$args = [];

$admin_emails = self::get_admin_emails( 'email_admin_new_comment' );
$admin_emails = $this->get_admin_emails( 'email_admin_new_comment' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down Expand Up @@ -456,7 +455,7 @@ public function ap_after_update_question( $post_id, $question ) {

$args = [];

$admin_emails = self::get_admin_emails( 'email_admin_edit_question' );
$admin_emails = $this->get_admin_emails( 'email_admin_edit_question' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down Expand Up @@ -510,7 +509,7 @@ public function ap_after_update_answer( $answer_id, $answer ) {

$args = [];

$admin_emails = self::get_admin_emails( 'email_admin_edit_answer' );
$admin_emails = $this->get_admin_emails( 'email_admin_edit_answer' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down Expand Up @@ -562,7 +561,7 @@ public function ap_trash_question( $post_id, $_post ) {

$args = [];
$current_user = wp_get_current_user();
$admin_emails = self::get_admin_emails( 'email_admin_trash_question' );
$admin_emails = $this->get_admin_emails( 'email_admin_trash_question' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down Expand Up @@ -590,7 +589,7 @@ public function ap_trash_answer( $post_id, $_post ) {

$args = [];
$current_user = wp_get_current_user();
$admin_emails = self::get_admin_emails( 'email_admin_trash_question' );
$admin_emails = $this->get_admin_emails( 'email_admin_trash_question' );
if ( ! empty( $admin_emails ) ) {
$args['users'] = $admin_emails;
}
Expand Down
3 changes: 2 additions & 1 deletion includes/class/roles-cap.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ function ap_user_can_edit_answer( $post_id, $user_id = false ) {
* @since 2.4.7 Added new argument `$user_id`.
* @since 2.4.7 Added new filter `ap_user_can_edit_question`.
* @since 4.1.5 Check if valid post type.
* @since 4.1.8 Fixed: user is not able to edit their own question.
*/
function ap_user_can_edit_question( $post_id = false, $user_id = false ) {
if ( false === $user_id ) {
Expand All @@ -434,7 +435,7 @@ function ap_user_can_edit_question( $post_id = false, $user_id = false ) {
}

// Check post_type.
if ( ! $question || 'question' === $question->post_type ) {
if ( ! $question || 'question' !== $question->post_type ) {
return false;
}

Expand Down
20 changes: 9 additions & 11 deletions tests/wpunit/EmailAddonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ public function tearDown()
*/
public function testAPAfterNewQuestion()
{

// Check hook exists.
$this->assertEquals( 10, has_action( 'ap_after_new_question', [ AnsPress\Addons\Email::init(), 'ap_after_new_question' ] ) );
ap_opt( 'email_admin_emails', 'admin@local.local, admin2@aptext.local, admin@site.com' );
ap_opt( 'email_admin_emails', 'admin@xsdsdsd.local, admin2@aptext.local, admin@site.com' );
codecept_debug(ap_opt( 'email_admin_emails' ));

$this->setRole('subscriber');

reset_phpmailer_instance();
// Check if question created without author set current user as subscriber.
$id = $this->insert_question('', '', get_current_user_id());

// Run action so that ap_after_new_question hook can trigger.
do_action( 'ap_processed_new_question', $id, get_post( $id ) );
//do_action( 'ap_processed_new_question', $id, get_post( $id ) );

$mailer = tests_retrieve_phpmailer_instance();

$this->assertEquals( 'admin@local.local', $mailer->get_recipient( 'to' )->address );
$this->assertEquals( 'admin@xsdsdsd.local', $mailer->get_recipient( 'to' )->address );
$this->assertEquals( 'admin2@aptext.local', $mailer->get_recipient( 'bcc' )->address );
$this->assertEquals( 'admin@site.com', $mailer->get_recipient( 'bcc', 0, 1 )->address );
}
Expand All @@ -51,7 +52,7 @@ private function testApAfterNewAnswer(){
$this->setRole('subscriber');
$q_user = get_current_user_id();

ap_opt( 'email_admin_emails', 'admin@local.local' );
ap_opt( 'email_admin_emails', 'admin@xsdsdsd.local' );

// Check if question created without author set current user as subscriber.
$question_id = $this->insert_question('', '', $q_user);
Expand All @@ -61,16 +62,13 @@ private function testApAfterNewAnswer(){
ap_new_subscriber( $subs_user, 'question', $question_id);

$this->setRole('subscriber');
reset_phpmailer_instance();
$answer_id = $this->factory->post->create( array( 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => $question_id, 'post_author' => get_current_user_id() ) );

// Run action so that ap_after_new_question hook can trigger.
do_action( 'ap_processed_new_answer', $answer_id, get_post( $answer_id ) );

$mailer = tests_retrieve_phpmailer_instance();
codecept_debug([$mailer->get_recipient( 'bcc', 0, 1 ), $mailer->get_recipient( 'bcc' )]);
$this->assertEquals( get_user_by('id', $subs_user)->user_email, $mailer->get_recipient( 'bcc', 0, 1 )->address );
$this->assertEquals( get_user_by('id', $subs_user)->user_email, $mailer->get_recipient( 'bcc' )->address );
$this->assertEquals( 'admin@local.local', $mailer->get_recipient( 'to' )->address );
$this->assertEquals( 'admin@xsdsdsd.local', $mailer->get_recipient( 'to' )->address );
}

}

0 comments on commit 1555ff2

Please sign in to comment.