Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions src/js/_enqueues/wp/dashboard.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ jQuery( function( $ ) {
.removeClass( 'hide-if-js' );

$container.on( 'click', '.community-events-toggle-location, .community-events-cancel', app.toggleLocationForm );
$container.on( 'click', '.community-events-clear-location', app.clearLocationForm);

/**
* Filters events based on entered location.
Expand Down Expand Up @@ -354,6 +355,36 @@ jQuery( function( $ ) {
app.initialized = true;
},

getRequestParams: function () {
var requestParams = {},initiatedBy;
requestParams._wpnonce = communityEventsData.nonce;
requestParams.timezone = window.Intl ? window.Intl.DateTimeFormat().resolvedOptions().timeZone : '';
initiatedBy = requestParams.location ? 'user' : 'app';
return {requestParams: requestParams,initiatedBy: initiatedBy};
},

/**
* Resets the location
* @return {void}
*/
clearLocationForm: function() {
var params = app.getRequestParams(),
requestParams= params.requestParams,
initiatedBy = params.initiatedBy;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be the 3rd place the above code is duplicated. It'd be nice to create a getRequestParams() function to keep it DRY.

wp.ajax.post( 'clear-community-events', requestParams )
.done( function() {
app.getEvents();
})
.fail( function() {
app.renderEventsTemplate({
'location' : false,
'events' : [],
'error' : true
}, initiatedBy );
});
},

/**
* Toggles the visibility of the Edit Location form.
*
Expand All @@ -366,6 +397,7 @@ jQuery( function( $ ) {
*/
toggleLocationForm: function( action ) {
var $toggleButton = $( '.community-events-toggle-location' ),
$clearButton = $( '.community-events-clear-location' ),
$cancelButton = $( '.community-events-cancel' ),
$form = $( '.community-events-form' ),
$target = $();
Expand All @@ -383,6 +415,7 @@ jQuery( function( $ ) {

if ( 'hide' === action ) {
$toggleButton.attr( 'aria-expanded', 'false' );
$clearButton.attr( 'aria-expanded', 'false' );
$cancelButton.attr( 'aria-expanded', 'false' );
$form.attr( 'aria-hidden', 'true' );
/*
Expand All @@ -395,6 +428,7 @@ jQuery( function( $ ) {
}
} else {
$toggleButton.attr( 'aria-expanded', 'true' );
$clearButton.attr( 'aria-expanded', 'true' );
$cancelButton.attr( 'aria-expanded', 'true' );
$form.attr( 'aria-hidden', 'false' );
}
Expand All @@ -410,15 +444,14 @@ jQuery( function( $ ) {
* @return {void}
*/
getEvents: function( requestParams ) {
var initiatedBy,
app = this,
$spinner = $( '.community-events-form' ).children( '.spinner' );
var app = this,
params = app.getRequestParams(),
initiatedBy = params.initiatedBy,
$spinner = $('.community-events-form').children('.spinner');

requestParams = requestParams || {};
requestParams._wpnonce = communityEventsData.nonce;
requestParams.timezone = window.Intl ? window.Intl.DateTimeFormat().resolvedOptions().timeZone : '';

initiatedBy = requestParams.location ? 'user' : 'app';
requestParams = requestParams || params.requestParams;
requestParams._wpnonce = params.requestParams._wpnonce;
requestParams.timezone= params.requestParams.timezone;

$spinner.addClass( 'is-active' );

Expand Down Expand Up @@ -468,6 +501,7 @@ jQuery( function( $ ) {
var template,
elementVisibility,
$toggleButton = $( '.community-events-toggle-location' ),
$clearButton = $( '.community-events-clear-location' ),
$locationMessage = $( '#community-events-location-message' ),
$results = $( '.community-events-results' );

Expand All @@ -493,6 +527,7 @@ jQuery( function( $ ) {
'.community-events-could-not-locate' : false,
'#community-events-location-message' : false,
'.community-events-toggle-location' : false,
'.community-events-clear-location' : false,
'.community-events-results' : false
};

Expand All @@ -517,6 +552,7 @@ jQuery( function( $ ) {

elementVisibility['#community-events-location-message'] = true;
elementVisibility['.community-events-toggle-location'] = true;
elementVisibility['.community-events-clear-location'] = true;
elementVisibility['.community-events-results'] = true;

} else if ( templateParams.location.description ) {
Expand Down Expand Up @@ -544,6 +580,7 @@ jQuery( function( $ ) {

elementVisibility['#community-events-location-message'] = true;
elementVisibility['.community-events-toggle-location'] = true;
elementVisibility['.community-events-clear-location'] = true;
elementVisibility['.community-events-results'] = true;

} else if ( templateParams.unknownCity ) {
Expand Down Expand Up @@ -591,6 +628,7 @@ jQuery( function( $ ) {

elementVisibility['#community-events-location-message'] = true;
elementVisibility['.community-events-toggle-location'] = true;
elementVisibility['.community-events-clear-location'] = true;
}

// Set the visibility of toggleable elements.
Expand All @@ -599,6 +637,7 @@ jQuery( function( $ ) {
});

$toggleButton.attr( 'aria-expanded', elementVisibility['.community-events-toggle-location'] );
$clearButton.attr( 'aria-expanded', elementVisibility['.community-events-clear-location'] );

if ( templateParams.location && ( templateParams.location.ip || templateParams.location.latitude ) ) {
// Hide the form when there's a valid location.
Expand Down
1 change: 1 addition & 0 deletions src/wp-admin/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
'oembed-cache',
'image-editor',
'delete-comment',
'clear-community-events',
'delete-tag',
'delete-link',
'delete-meta',
Expand Down
11 changes: 11 additions & 0 deletions src/wp-admin/includes/ajax-actions.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ function wp_ajax_autocomplete_user() {
wp_die( wp_json_encode( $return ) );
}


/**
* Handles Ajax requests for clearing community events
*/
function wp_ajax_clear_community_events() {
check_ajax_referer( 'community_events' );
$user_id = get_current_user_id();
delete_user_option( $user_id, 'community-events-location', true );
wp_send_json_success();
}

/**
* Handles Ajax requests for community events
*
Expand Down
4 changes: 4 additions & 0 deletions src/wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,10 @@ function wp_print_community_events_markup() {
<button class="button-link community-events-toggle-location" aria-label="<?php esc_attr_e( 'Edit city' ); ?>" aria-expanded="false">
<span class="dashicons dashicons-edit"></span>
</button>

<button class="button-link community-events-clear-location" aria-label="<?php esc_attr_e( 'Clear city' ); ?>" aria-expanded="false">
<span class="dashicons dashicons-no"></span>
</button>
</p>

<form class="community-events-form" aria-hidden="true" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post">
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/testcase-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase {
protected static $_core_actions_post = array(
'oembed_cache',
'image-editor',
'clear-community-events',
'delete-comment',
'delete-tag',
'delete-link',
Expand Down
110 changes: 110 additions & 0 deletions tests/phpunit/tests/ajax/CommunityEventLocation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Admin Ajax functions to be tested.
*/
require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';

/**
* Testing Ajax Community_Event_location functionality.
*
* @package WordPress
* @subpackage UnitTests
* @since 5.7.0
* @group ajax
*/
class Tests_Community_Event_location extends WP_Ajax_UnitTestCase {
private $user_id;

/**
* Runs before each Test
*/
public function setUp() {
parent::setUp();
$wp_user = get_user_by( 'email', WP_TESTS_EMAIL );
update_user_meta( $wp_user->ID, 'community-events-location', 'DummyData' );

wp_set_current_user( $wp_user->ID );
$this->user_id = $wp_user->ID;
}

/**
* Tests the new Clear Community Events Location Ajax endpoint without nonce
*/
public function test_clear_community_events_location_without_nonce() {
// Before should be set to 'DummyData'
$content = get_user_meta( $this->user_id, 'community-events-location', true );
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
$this->handleAjaxCall();

// After Failed Ajax Call should be still 'DummyData';
$content = $this->getCommunityEventsContent();
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
}

/**
* Tests the new Clear Community Events Location Ajax endpoint with wrong nonce
*/
public function test_clear_community_events_location_with_wrong_nonce() {
// Before should be set to 'DummyData'
$content = get_user_meta( $this->user_id, 'community-events-location', true );
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
$_POST['_wpnonce'] = wp_create_nonce( 'community_events_but_wrong_nonce' );
$this->handleAjaxCall();

// After Failed Ajax Call should be still 'DummyData';
$content = $this->getCommunityEventsContent();
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
}

/**
* Tests the new Clear Community Events Location Ajax endpoint without being logged in
*/
public function test_clear_community_events_location_logged_out() {
$this->logout();

// Before should be set to 'DummyData'
$content = get_user_meta( $this->user_id, 'community-events-location', true );
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
$_POST['_wpnonce'] = wp_create_nonce( 'community_events' );
$this->handleAjaxCall();

// After Failed Ajax Call should be still 'DummyData';
$content = $this->getCommunityEventsContent();
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
}

/**
* Tests the new Clear Community Events Location Ajax endpoint in the correct way
*/
public function test_clear_community_events_location() {
// Before should be set to 'DummyData'
$content = $this->getCommunityEventsContent();
$this->assertNotTrue( $this->isNullOrEmptyString( $content ) );
$_POST['_wpnonce'] = wp_create_nonce( 'community_events' );
$this->handleAjaxCall();

// After Ajax Call should be set to null
$content = $this->getCommunityEventsContent();
$this->assertTrue( $this->isNullOrEmptyString( $content ) );
}

/**
* @return mixed
*/
private function getCommunityEventsContent() {
return get_user_meta( $this->user_id, 'community-events-location', true );
}

private function handleAjaxCall() {
try {
$this->_handleAjax( 'clear-community-events' );
} catch ( WPDieException $e ) {
unset( $e );
}
}

private function isNullOrEmptyString( $string ) {
return ( ! isset( $string ) || trim( $string ) === '' );
}
}