updated plugin Jetpack Protect
version 1.3.0
This commit is contained in:
@ -20,7 +20,7 @@ use Automattic\Jetpack\Status;
|
||||
*/
|
||||
class JITM {
|
||||
|
||||
const PACKAGE_VERSION = '2.2.35';
|
||||
const PACKAGE_VERSION = '2.3.1';
|
||||
|
||||
/**
|
||||
* The configuration method that is called from the jetpack-config package.
|
||||
|
@ -40,31 +40,6 @@ class Post_Connection_JITM extends JITM {
|
||||
$this->tracking = new Tracking();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare actions according to screen and post type.
|
||||
*
|
||||
* @since 1.1.0
|
||||
* @since-jetpack 3.8.2
|
||||
*
|
||||
* @uses Jetpack_Autoupdate::get_possible_failures()
|
||||
*
|
||||
* @param \WP_Screen $screen WP Core's screen object.
|
||||
*/
|
||||
public function prepare_jitms( $screen ) {
|
||||
parent::prepare_jitms( $screen );
|
||||
if ( ! in_array(
|
||||
$screen->id,
|
||||
array(
|
||||
'jetpack_page_akismet-key-config',
|
||||
'admin_page_jetpack_modules',
|
||||
),
|
||||
true
|
||||
) ) {
|
||||
// Not really a JITM. Don't know where else to put this :) .
|
||||
add_action( 'admin_notices', array( $this, 'delete_user_update_connection_owner_notice' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A special filter for WooCommerce, to set a message based on local state.
|
||||
*
|
||||
@ -229,172 +204,6 @@ class Post_Connection_JITM extends JITM {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is an entire admin notice dedicated to messaging and handling of the case where a user is trying to delete
|
||||
* the connection owner.
|
||||
*/
|
||||
public function delete_user_update_connection_owner_notice() {
|
||||
global $current_screen;
|
||||
|
||||
/*
|
||||
* phpcs:disable WordPress.Security.NonceVerification.Recommended
|
||||
*
|
||||
* This function is firing within wp-admin and checks (below) if it is in the midst of a deletion on the users
|
||||
* page. Nonce will be already checked by WordPress, so we do not need to check ourselves.
|
||||
*/
|
||||
|
||||
if ( ! isset( $current_screen->base ) || 'users' !== $current_screen->base ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! isset( $_REQUEST['action'] ) || 'delete' !== $_REQUEST['action'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get connection owner or bail.
|
||||
$connection_manager = new Manager();
|
||||
$connection_owner_id = $connection_manager->get_connection_owner_id();
|
||||
if ( ! $connection_owner_id ) {
|
||||
return;
|
||||
}
|
||||
$connection_owner_userdata = get_userdata( $connection_owner_id );
|
||||
|
||||
// Bail if we're not trying to delete connection owner.
|
||||
$user_ids_to_delete = array();
|
||||
if ( isset( $_REQUEST['users'] ) ) {
|
||||
$user_ids_to_delete = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['users'] ) );
|
||||
} elseif ( isset( $_REQUEST['user'] ) ) {
|
||||
$user_ids_to_delete[] = sanitize_text_field( wp_unslash( $_REQUEST['user'] ) );
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
$user_ids_to_delete = array_map( 'absint', $user_ids_to_delete );
|
||||
$deleting_connection_owner = in_array( $connection_owner_id, (array) $user_ids_to_delete, true );
|
||||
if ( ! $deleting_connection_owner ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bail if they're trying to delete themselves to avoid confusion.
|
||||
if ( get_current_user_id() === $connection_owner_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Track it!
|
||||
if ( method_exists( $this->tracking, 'record_user_event' ) ) {
|
||||
$this->tracking->record_user_event( 'delete_connection_owner_notice_view' );
|
||||
}
|
||||
|
||||
$connected_admins = $connection_manager->get_connected_users( 'jetpack_disconnect' );
|
||||
$user = is_a( $connection_owner_userdata, 'WP_User' ) ? esc_html( $connection_owner_userdata->data->user_login ) : '';
|
||||
|
||||
echo "<div class='notice notice-warning' id='jetpack-notice-switch-connection-owner'>";
|
||||
echo '<h2>' . esc_html__( 'Important notice about your Jetpack connection:', 'jetpack-jitm' ) . '</h2>';
|
||||
echo '<p>' . sprintf(
|
||||
/* translators: WordPress User, if available. */
|
||||
esc_html__( 'Warning! You are about to delete the Jetpack connection owner (%s) for this site, which may cause some of your Jetpack features to stop working.', 'jetpack-jitm' ),
|
||||
esc_html( $user )
|
||||
) . '</p>';
|
||||
|
||||
if ( ! empty( $connected_admins ) && count( $connected_admins ) > 1 ) {
|
||||
echo '<form id="jp-switch-connection-owner" action="" method="post">';
|
||||
echo "<label for='owner'>" . esc_html__( 'You can choose to transfer connection ownership to one of these already-connected admins:', 'jetpack-jitm' ) . ' </label>';
|
||||
|
||||
$connected_admin_ids = array_map(
|
||||
function ( $connected_admin ) {
|
||||
return $connected_admin->ID;
|
||||
},
|
||||
$connected_admins
|
||||
);
|
||||
|
||||
wp_dropdown_users(
|
||||
array(
|
||||
'name' => 'owner',
|
||||
'include' => array_diff( $connected_admin_ids, array( $connection_owner_id ) ),
|
||||
'show' => 'display_name_with_login',
|
||||
)
|
||||
);
|
||||
|
||||
echo '<p>';
|
||||
submit_button( esc_html__( 'Set new connection owner', 'jetpack-jitm' ), 'primary', 'jp-switch-connection-owner-submit', false );
|
||||
echo '</p>';
|
||||
|
||||
echo "<div id='jp-switch-user-results'></div>";
|
||||
echo '</form>';
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery( document ).ready( function( $ ) {
|
||||
$( '#jp-switch-connection-owner' ).on( 'submit', function( e ) {
|
||||
var formData = $( this ).serialize();
|
||||
var submitBtn = document.getElementById( 'jp-switch-connection-owner-submit' );
|
||||
var results = document.getElementById( 'jp-switch-user-results' );
|
||||
|
||||
submitBtn.disabled = true;
|
||||
|
||||
$.ajax( {
|
||||
type : "POST",
|
||||
url : "<?php echo esc_url( get_rest_url() . 'jetpack/v4/connection/owner' ); ?>",
|
||||
data : formData,
|
||||
headers : {
|
||||
'X-WP-Nonce': "<?php echo esc_js( wp_create_nonce( 'wp_rest' ) ); ?>",
|
||||
},
|
||||
success: function() {
|
||||
results.innerHTML = "<?php esc_html_e( 'Success!', 'jetpack-jitm' ); ?>";
|
||||
setTimeout( function() {
|
||||
$( '#jetpack-notice-switch-connection-owner' ).hide( 'slow' );
|
||||
}, 1000 );
|
||||
}
|
||||
} ).done( function() {
|
||||
submitBtn.disabled = false;
|
||||
} );
|
||||
|
||||
e.preventDefault();
|
||||
return false;
|
||||
} );
|
||||
} );
|
||||
</script>
|
||||
<?php
|
||||
} else {
|
||||
echo '<p>' . esc_html__( 'Every Jetpack site needs at least one connected admin for the features to work properly. Please connect to your WordPress.com account via the button below. Once you connect, you may refresh this page to see an option to change the connection owner.', 'jetpack-jitm' ) . '</p>';
|
||||
$connect_url = $connection_manager->get_authorization_url();
|
||||
$connect_url = add_query_arg( 'from', 'delete_connection_owner_notice', $connect_url );
|
||||
echo "<a href='" . esc_url( $connect_url ) . "' target='_blank' rel='noopener noreferrer' class='button-primary'>" . esc_html__( 'Connect to WordPress.com', 'jetpack-jitm' ) . '</a>';
|
||||
}
|
||||
|
||||
echo '<p>';
|
||||
printf(
|
||||
wp_kses(
|
||||
/* translators: URL to Jetpack support doc regarding the primary user. */
|
||||
__( "<a href='%s' target='_blank' rel='noopener noreferrer'>Learn more</a> about the connection owner and what will break if you do not have one.", 'jetpack-jitm' ),
|
||||
array(
|
||||
'a' => array(
|
||||
'href' => true,
|
||||
'target' => true,
|
||||
'rel' => true,
|
||||
),
|
||||
)
|
||||
),
|
||||
esc_url( Redirect::get_url( 'jetpack-support-primary-user' ) )
|
||||
);
|
||||
echo '</p>';
|
||||
echo '<p>';
|
||||
printf(
|
||||
wp_kses(
|
||||
/* translators: URL to contact Jetpack support. */
|
||||
__( 'As always, feel free to <a href="%s" target="_blank" rel="noopener noreferrer">contact our support team</a> if you have any questions.', 'jetpack-jitm' ),
|
||||
array(
|
||||
'a' => array(
|
||||
'href' => true,
|
||||
'target' => true,
|
||||
'rel' => true,
|
||||
),
|
||||
)
|
||||
),
|
||||
esc_url( Redirect::get_url( 'jetpack-contact-support' ) )
|
||||
);
|
||||
echo '</p>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Dismisses a JITM feature class so that it will no longer be shown.
|
||||
*
|
||||
|
@ -39,7 +39,6 @@ class Rest_Api_Endpoints {
|
||||
'permission_callback' => __CLASS__ . '::delete_jitm_message_permission_callback',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,31 +17,32 @@
|
||||
}
|
||||
|
||||
@mixin jitm-banner-color( $color ) {
|
||||
border-left-color: $color;
|
||||
&::before {
|
||||
background-color: $color;
|
||||
}
|
||||
|
||||
.jitm-banner__icon {
|
||||
color: $color;
|
||||
}
|
||||
|
||||
.jitm-banner__icon-circle {
|
||||
background-color: $color;
|
||||
}
|
||||
}
|
||||
|
||||
// New JITMS - modified calypso banner styles
|
||||
$blue-grey-light: #f6f7f7;
|
||||
$blue-medium-dark: #2271b1;
|
||||
$jp-gray: #dcdcde;
|
||||
$jp-gray-20: #a7aaad;
|
||||
|
||||
.jitm-button {
|
||||
background: $blue-grey-light;
|
||||
border-color: $blue-medium-dark;
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
color: $blue-medium-dark;
|
||||
cursor: pointer;
|
||||
display: inline-block;
|
||||
margin: 0;
|
||||
outline: 0;
|
||||
overflow: hidden;
|
||||
font-size: 14px;
|
||||
text-overflow: ellipsis;
|
||||
text-decoration: none;
|
||||
vertical-align: top;
|
||||
@ -53,34 +54,42 @@ $blue-medium-dark: #2271b1;
|
||||
appearance: none;
|
||||
text-align: center;
|
||||
min-width: 90px;
|
||||
font-weight: 600;
|
||||
border-color: black;
|
||||
background: $white;
|
||||
color: black;
|
||||
|
||||
&:hover {
|
||||
background: #f0f0f1;
|
||||
border-color: $blue-grey-dark;
|
||||
color: $blue-grey-dark;
|
||||
// Primary buttons
|
||||
&.is-primary {
|
||||
background: black;
|
||||
color: $white;
|
||||
}
|
||||
&[disabled],
|
||||
&:disabled {
|
||||
color: lighten( $gray, 30% );
|
||||
background: $white;
|
||||
border-color: lighten( $gray, 30% );
|
||||
cursor: default;
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
border-color: $gray-dark;
|
||||
background: $gray-dark;
|
||||
color: $white;
|
||||
}
|
||||
&:focus {
|
||||
background: $white;
|
||||
border-color: $blue-medium-dark;
|
||||
box-shadow: 0 0 0 1px $blue-medium-dark;
|
||||
box-shadow:
|
||||
0 0 0 1px $white,
|
||||
0 0 0 3px $blue-medium-dark;
|
||||
}
|
||||
|
||||
&[disabled],
|
||||
&:disabled {
|
||||
color: $jp-gray-20;
|
||||
background: $jp-gray;
|
||||
border-color: $jp-gray;
|
||||
cursor: default;
|
||||
}
|
||||
&.is-compact {
|
||||
padding: 7px;
|
||||
font-size: 11px;
|
||||
padding: 12px;
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
text-transform: uppercase;
|
||||
white-space: nowrap;
|
||||
|
||||
&:disabled {
|
||||
color: lighten( $gray, 30% );
|
||||
}
|
||||
.gridicon {
|
||||
top: 4px;
|
||||
margin-top: -8px;
|
||||
@ -103,48 +112,20 @@ $blue-medium-dark: #2271b1;
|
||||
}
|
||||
}
|
||||
|
||||
// Primary buttons
|
||||
.jitm-button.is-primary {
|
||||
background: $blue-medium;
|
||||
border-color: $blue-medium;
|
||||
color: $white;
|
||||
|
||||
&:hover,
|
||||
&:focus {
|
||||
border-color: $blue-medium-dark;
|
||||
background: $blue-medium-dark;
|
||||
color: $white;
|
||||
}
|
||||
&:focus {
|
||||
box-shadow:
|
||||
0 0 0 1px $white,
|
||||
0 0 0 3px $blue-medium-dark;
|
||||
}
|
||||
&[disabled],
|
||||
&:disabled {
|
||||
background: tint( $blue-light, 50% );
|
||||
border-color: tint( $blue-wordpress, 55% );
|
||||
color: $white;
|
||||
}
|
||||
&.is-compact {
|
||||
color: $white;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.jitm-card {
|
||||
display: block;
|
||||
clear: both;
|
||||
position: relative;
|
||||
margin: rem( 48px ) rem( 20px ) 0 auto;
|
||||
padding: rem( 16px );
|
||||
padding: 16px ;
|
||||
box-sizing: border-box;
|
||||
background: $white;
|
||||
box-shadow:
|
||||
0 0 0 1px $light-gray-700,
|
||||
0 1px 1px 1px rgba(0,0,0,.04);
|
||||
|
||||
background-color: $white;
|
||||
background-image: url( "../images/background.png" );
|
||||
background-repeat: no-repeat;
|
||||
background-position: right center;
|
||||
border: 1px solid $white;
|
||||
border-radius: 2px;
|
||||
box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.16);
|
||||
|
||||
@include clear-fix;
|
||||
|
||||
@ -197,15 +178,23 @@ $blue-medium-dark: #2271b1;
|
||||
}
|
||||
|
||||
.jitm-banner.jitm-card {
|
||||
border-left: 4px solid;
|
||||
display: flex;
|
||||
padding: rem( 12px ) rem( 6px ) rem( 12px ) rem( 12px );
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
|
||||
&::before {
|
||||
content: '';
|
||||
width: 4px;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 100%;
|
||||
border-top-left-radius: 1px;
|
||||
border-bottom-left-radius: 1px;
|
||||
}
|
||||
|
||||
@include breakpoint( "<480px" ) {
|
||||
display: flex;
|
||||
padding: rem( 12px );
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
@ -222,7 +211,7 @@ $blue-medium-dark: #2271b1;
|
||||
@include jitm-banner-color( $alert-yellow );
|
||||
}
|
||||
&.is-upgrade-premium {
|
||||
@include jitm-banner-color( $alert-green );
|
||||
@include jitm-banner-color( #069e08 );
|
||||
}
|
||||
&.is-upgrade-business,
|
||||
&.woo-jitm {
|
||||
@ -255,14 +244,9 @@ $blue-medium-dark: #2271b1;
|
||||
}
|
||||
|
||||
.jitm-banner__buttons_container {
|
||||
display: grid;
|
||||
display: flex;
|
||||
height: 50%;
|
||||
margin-top: auto;
|
||||
margin-bottom: auto;
|
||||
|
||||
@include breakpoint( ">480px" ) {
|
||||
display: flex;
|
||||
}
|
||||
margin: auto 16px auto;
|
||||
}
|
||||
|
||||
.jitm-banner__icons {
|
||||
@ -329,7 +313,7 @@ $blue-medium-dark: #2271b1;
|
||||
|
||||
.jitm-jp-logo {
|
||||
height: inherit;
|
||||
width: 6rem;
|
||||
width: rem( 42px );
|
||||
fill: inherit;
|
||||
}
|
||||
}
|
||||
@ -358,7 +342,6 @@ $blue-medium-dark: #2271b1;
|
||||
}
|
||||
|
||||
@include breakpoint( "<960px" ) {
|
||||
display: grid;
|
||||
margin-right: 5px;
|
||||
}
|
||||
}
|
||||
@ -376,18 +359,19 @@ $blue-medium-dark: #2271b1;
|
||||
}
|
||||
|
||||
.jitm-banner__title,
|
||||
.jitm-banner__description {
|
||||
color: $gray-dark;
|
||||
.jitm-banner__description,
|
||||
.jitm-banner__description a {
|
||||
color: black;
|
||||
}
|
||||
|
||||
.jitm-banner__title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.jitm-banner__description {
|
||||
font-size: rem( 12px );
|
||||
line-height: 1.5;
|
||||
font-size: rem( 14px );
|
||||
line-height: 1.4;
|
||||
margin-top: rem( 6px );
|
||||
}
|
||||
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
@ -1,269 +0,0 @@
|
||||
import '../css/jetpack-admin-jitm.scss';
|
||||
|
||||
jQuery( document ).ready( function ( $ ) {
|
||||
var templates = {
|
||||
default: function ( envelope ) {
|
||||
var html =
|
||||
'<div class="jitm-card jitm-banner ' +
|
||||
( envelope.CTA.message ? 'has-call-to-action' : '' ) +
|
||||
' is-upgrade-premium ' +
|
||||
envelope.content.classes +
|
||||
'" data-stats_url="' +
|
||||
envelope.jitm_stats_url +
|
||||
'">';
|
||||
html += '<div class="jitm-banner__content">';
|
||||
html += '<div class="jitm-banner__icon-plan">' + envelope.content.icon + '</div>';
|
||||
html += '<div class="jitm-banner__info">';
|
||||
html += '<div class="jitm-banner__title">' + envelope.content.message + '</div>';
|
||||
if ( envelope.content.description && envelope.content.description !== '' ) {
|
||||
html += '<div class="jitm-banner__description">' + envelope.content.description;
|
||||
if ( envelope.content.list.length > 0 ) {
|
||||
html += '<ul class="banner__list">';
|
||||
for ( var i = 0; i < envelope.content.list.length; i++ ) {
|
||||
var text = envelope.content.list[ i ].item;
|
||||
|
||||
if ( envelope.content.list[ i ].url ) {
|
||||
text =
|
||||
'<a href="' +
|
||||
envelope.content.list[ i ].url +
|
||||
'" target="_blank" rel="noopener noreferrer" data-module="' +
|
||||
envelope.feature_class +
|
||||
'" data-jptracks-name="nudge_item_click" data-jptracks-prop="jitm-' +
|
||||
envelope.id +
|
||||
'">' +
|
||||
text +
|
||||
'</a>';
|
||||
}
|
||||
|
||||
html +=
|
||||
'<li>' +
|
||||
'<svg class="gridicon gridicons-checkmark" height="16" width="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g>' +
|
||||
'<path d="M9 19.414l-6.707-6.707 1.414-1.414L9 16.586 20.293 5.293l1.414 1.414" /></g></svg>' +
|
||||
text +
|
||||
'</li>';
|
||||
}
|
||||
}
|
||||
html += '</div>';
|
||||
}
|
||||
html += '</div>';
|
||||
html += '</div>';
|
||||
|
||||
html += '<div class="jitm-banner__buttons_container">';
|
||||
|
||||
if ( envelope.activate_module ) {
|
||||
html += '<div class="jitm-banner__action" id="jitm-banner__activate">';
|
||||
html +=
|
||||
'<a href="#" data-module="' +
|
||||
envelope.activate_module +
|
||||
'" type="button" class="jitm-button is-compact is-primary jptracks" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-' +
|
||||
envelope.id +
|
||||
'-activate_module">' +
|
||||
window.jitm_config.activate_module_text +
|
||||
'</a>';
|
||||
html += '</div>';
|
||||
}
|
||||
if ( envelope.CTA.message ) {
|
||||
var ctaClasses = 'jitm-button is-compact jptracks';
|
||||
if ( envelope.CTA.primary && null === envelope.activate_module ) {
|
||||
ctaClasses += ' is-primary';
|
||||
}
|
||||
|
||||
var ajaxAction = envelope.CTA.ajax_action;
|
||||
|
||||
html += '<div class="jitm-banner__action">';
|
||||
html +=
|
||||
'<a href="' +
|
||||
( envelope.CTA.hasOwnProperty( 'link' ) && envelope.CTA.link.length
|
||||
? envelope.CTA.link
|
||||
: envelope.url ) +
|
||||
'" target="' +
|
||||
( envelope.CTA.newWindow === false || ajaxAction ? '_self' : '_blank' ) +
|
||||
'" rel="noopener noreferrer" title="' +
|
||||
envelope.CTA.message +
|
||||
'" data-module="' +
|
||||
envelope.feature_class +
|
||||
'" type="button" class="' +
|
||||
ctaClasses +
|
||||
'" data-jptracks-name="nudge_click" data-jptracks-prop="jitm-' +
|
||||
envelope.id +
|
||||
'" ' +
|
||||
( ajaxAction ? 'data-ajax-action="' + ajaxAction + '"' : '' ) +
|
||||
'>' +
|
||||
envelope.CTA.message +
|
||||
'</a>';
|
||||
html += '</div>';
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
|
||||
if ( envelope.is_dismissible ) {
|
||||
html +=
|
||||
'<a href="#" data-module="' +
|
||||
envelope.feature_class +
|
||||
'" class="jitm-banner__dismiss"></a>';
|
||||
}
|
||||
html += '</div>';
|
||||
|
||||
return $( html );
|
||||
},
|
||||
};
|
||||
|
||||
var setJITMContent = function ( $el, response, redirect ) {
|
||||
var template;
|
||||
|
||||
var render = function ( $my_template ) {
|
||||
return function ( e ) {
|
||||
e.preventDefault();
|
||||
|
||||
$my_template.hide();
|
||||
|
||||
$.ajax( {
|
||||
url: window.jitm_config.api_root + 'jetpack/v4/jitm',
|
||||
method: 'POST', // using DELETE without permalinks is broken in default nginx configuration
|
||||
beforeSend: function ( xhr ) {
|
||||
xhr.setRequestHeader( 'X-WP-Nonce', window.jitm_config.nonce );
|
||||
},
|
||||
data: {
|
||||
id: response.id,
|
||||
feature_class: response.feature_class,
|
||||
},
|
||||
} );
|
||||
};
|
||||
};
|
||||
|
||||
template = response.template;
|
||||
|
||||
// if we don't have a template for this version, just use the default template
|
||||
if ( ! template || ! templates[ template ] ) {
|
||||
template = 'default';
|
||||
}
|
||||
|
||||
response.url = response.url + '&redirect=' + redirect;
|
||||
|
||||
var $template = templates[ template ]( response );
|
||||
$template.find( '.jitm-banner__dismiss' ).on( 'click', render( $template ) );
|
||||
|
||||
if ( $( '#jp-admin-notices' ).length > 0 ) {
|
||||
// Add to Jetpack notices within the Jetpack settings app.
|
||||
$el.innerHTML = $template;
|
||||
|
||||
// If we already have a message, replace it.
|
||||
if ( $( '#jp-admin-notices' ).find( '.jitm-card' ) ) {
|
||||
$( '.jitm-card' ).replaceWith( $template );
|
||||
}
|
||||
|
||||
// No existing JITM? Add ours to the top of the Jetpack admin notices.
|
||||
$template.prependTo( $( '#jp-admin-notices' ) );
|
||||
} else {
|
||||
// Replace placeholder div on other pages.
|
||||
$el.replaceWith( $template );
|
||||
}
|
||||
|
||||
// Handle Module activation button if it exists.
|
||||
$template.find( '#jitm-banner__activate a' ).on( 'click', function () {
|
||||
var $activate_button = $( this );
|
||||
|
||||
// Do not allow any requests if the button is disabled.
|
||||
if ( $activate_button.attr( 'disabled' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Make request to activate module.
|
||||
$.ajax( {
|
||||
url:
|
||||
window.jitm_config.api_root +
|
||||
'jetpack/v4/module/' +
|
||||
$activate_button.data( 'module' ) +
|
||||
'/active',
|
||||
method: 'POST',
|
||||
beforeSend: function ( xhr ) {
|
||||
xhr.setRequestHeader( 'X-WP-Nonce', $el.data( 'nonce' ) );
|
||||
|
||||
// Change the button status to disabled as the change is in progress.
|
||||
$( '#jitm-banner__activate a' ).text( window.jitm_config.activating_module_text );
|
||||
$( '#jitm-banner__activate a' ).attr( 'disabled', true );
|
||||
},
|
||||
} ).done( function () {
|
||||
$( '#jitm-banner__activate a' ).text( window.jitm_config.activated_module_text );
|
||||
$( '#jitm-banner__activate a' ).attr( 'disabled', true );
|
||||
|
||||
// Hide the JITM after 2 seconds.
|
||||
setTimeout( function () {
|
||||
$template.fadeOut( 'slow' );
|
||||
}, 2000 );
|
||||
} );
|
||||
} );
|
||||
|
||||
// Handle CTA ajax actions.
|
||||
$template.find( '.jitm-button[data-ajax-action]' ).on( '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 () {
|
||||
$( '.jetpack-jitm-message' ).each( function () {
|
||||
var $el = $( this );
|
||||
|
||||
var message_path = $el.data( 'message-path' );
|
||||
var query = $el.data( 'query' );
|
||||
var redirect = $el.data( 'redirect' );
|
||||
var hash = location.hash;
|
||||
|
||||
hash = hash.replace( /#\//, '_' );
|
||||
if ( '_dashboard' !== hash ) {
|
||||
message_path = message_path.replace(
|
||||
'toplevel_page_jetpack',
|
||||
'toplevel_page_jetpack' + hash
|
||||
);
|
||||
}
|
||||
|
||||
var full_jp_logo_exists = $( '.jetpack-logo__masthead' ).length ? true : false;
|
||||
|
||||
$.get( window.jitm_config.api_root + 'jetpack/v4/jitm', {
|
||||
message_path: message_path,
|
||||
query: query,
|
||||
full_jp_logo_exists: full_jp_logo_exists,
|
||||
_wpnonce: $el.data( 'nonce' ),
|
||||
} ).then( function ( response ) {
|
||||
if ( 'object' === typeof response && response[ '1' ] ) {
|
||||
response = [ response[ '1' ] ];
|
||||
}
|
||||
|
||||
// properly handle the case of an empty array or no content set
|
||||
if ( 0 === response.length || ! response[ 0 ].content ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// for now, always take the first response
|
||||
setJITMContent( $el, response[ 0 ], redirect );
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
reFetch();
|
||||
|
||||
$( window ).on( 'hashchange', function ( e ) {
|
||||
var newURL = e.originalEvent.newURL;
|
||||
|
||||
if ( newURL.indexOf( 'jetpack#/' ) >= 0 ) {
|
||||
var jitm_card = document.querySelector( '.jitm-card' );
|
||||
if ( jitm_card ) {
|
||||
jitm_card.remove();
|
||||
}
|
||||
reFetch();
|
||||
}
|
||||
} );
|
||||
} );
|
Reference in New Issue
Block a user