Skip to content

Commit

Permalink
JITM: Show ToS update notice (#14541)
Browse files Browse the repository at this point in the history
- Allows JITMs to trigger AJAX actions when the CTA button is clicked.
- Adds an AJAX action for the ToS JITM to mark the ToS as accepted.
  • Loading branch information
mmtr authored Feb 17, 2020
1 parent 04e1709 commit cb1e169
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
26 changes: 24 additions & 2 deletions _inc/jetpack-jitm.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ jQuery( document ).ready( function( $ ) {
ctaClasses += ' is-primary';
}

var ajaxAction = envelope.CTA.ajax_action;

html += '<div class="jitm-banner__action">';
html +=
'<a href="' +
envelope.url +
'" target="' +
( envelope.CTA.newWindow === false ? '_self' : '_blank' ) +
( envelope.CTA.newWindow === false || ajaxAction ? '_self' : '_blank' ) +
'" rel="noopener noreferrer" title="' +
envelope.CTA.message +
'" data-module="' +
Expand All @@ -76,7 +78,9 @@ jQuery( document ).ready( function( $ ) {
ctaClasses +
'" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-' +
envelope.id +
'">' +
'" ' +
( ajaxAction ? 'data-ajax-action="' + ajaxAction + '"' : '' ) +
'>' +
envelope.CTA.message +
'</a>';
html += '</div>';
Expand Down Expand Up @@ -176,6 +180,24 @@ jQuery( document ).ready( function( $ ) {
}, 2000 );
} );
} );

// Handle CTA ajax actions.
$template.find( '.jitm-button[data-ajax-action]' ).click( function( e ) {
e.preventDefault();
var button = $( this );
button.attr( 'disabled', true );
$.post( window.ajaxurl, {
action: button.data( 'ajax-action' ),
_nonce: $el.data( 'ajax-nonce' ),
} )
.done( function() {
$template.fadeOut( 'slow' );
} )
.fail( function() {
button.attr( 'disabled', false );
} );
return false;
} );
};

var reFetch = function() {
Expand Down
1 change: 1 addition & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = [
'modules/widgets/contact-info.php',
'modules/widgets/social-icons.php',
'modules/wpcom-block-editor/class-jetpack-wpcom-block-editor.php',
'modules/wpcom-tos/wpcom-tos.php',
'packages',
'tests/e2e/plugins/e2e-plan-data-interceptor.php',
];
1 change: 1 addition & 0 deletions modules/module-extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'simple-payments/simple-payments.php',
'woocommerce-analytics/wp-woocommerce-analytics.php',
'wpcom-block-editor/class-jetpack-wpcom-block-editor.php',
'wpcom-tos/wpcom-tos.php',
);

// Add connected features to our existing list if the site is currently connected.
Expand Down
41 changes: 41 additions & 0 deletions modules/wpcom-tos/wpcom-tos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Handles acceptance of WordPress.com Terms of Service for sites connected to WP.com.
*
* This is auto-loaded as of Jetpack v8.3 for WP.com connected-sites only.
*
* @package Jetpack
*/

namespace Automattic\Jetpack\TOS;

use Automattic\Jetpack\Connection\Client;

/**
* Makes a request to the WP.com legal endpoint to mark the Terms of Service as accepted.
*/
function accept_tos() {
check_ajax_referer( 'wp_ajax_action', '_nonce' );

$response = Client::wpcom_json_api_request_as_user(
'/legal',
'2',
array(
'method' => 'POST',
),
array(
'action' => 'accept_tos',
)
);

if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => __( 'Could not accept the Terms of Service. Please try again later.', 'jetpack' ) ) );
wp_die();
}

wp_send_json_success( $response );

wp_die();
}

add_action( 'wp_ajax_jetpack_accept_tos', __NAMESPACE__ . '\accept_tos' );
1 change: 1 addition & 0 deletions packages/jitm/src/class-jitm.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public function ajax_message() {
?>
<div class="jetpack-jitm-message"
data-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_rest' ) ); ?>"
data-ajax-nonce="<?php echo esc_attr( wp_create_nonce( 'wp_ajax_action' ) ); ?>"
data-message-path="<?php echo esc_attr( $message_path ); ?>"
data-query="<?php echo urlencode_deep( $query_string ); ?>"
data-redirect="<?php echo urlencode_deep( $current_screen ); ?>"
Expand Down

0 comments on commit cb1e169

Please sign in to comment.