2019-03-05 21:50:22 +00:00
|
|
|
|
/* global marketplace_suggestions, ajaxurl, Cookies */
|
2019-03-04 21:54:06 +00:00
|
|
|
|
( function( $, marketplace_suggestions, ajaxurl ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
|
$( function() {
|
2019-02-15 01:52:24 +00:00
|
|
|
|
if ( 'undefined' === typeof marketplace_suggestions ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 22:38:55 +00:00
|
|
|
|
// Stand-in wcTracks.recordEvent in case tracks is not available (for any reason).
|
|
|
|
|
window.wcTracks = window.wcTracks || {};
|
|
|
|
|
window.wcTracks.recordEvent = window.wcTracks.recordEvent || function() { };
|
2019-02-20 02:33:57 +00:00
|
|
|
|
|
|
|
|
|
// Tracks events sent in this file:
|
|
|
|
|
// - marketplace_suggestion_displayed
|
|
|
|
|
// - marketplace_suggestion_clicked
|
|
|
|
|
// - marketplace_suggestion_dismissed
|
|
|
|
|
// All are prefixed by {WC_Tracks::PREFIX}.
|
|
|
|
|
// All have one property for `suggestionSlug`, to identify the specific suggestion message.
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Dismiss the specified suggestion from the UI, and save the dismissal in settings.
|
2019-03-22 02:42:06 +00:00
|
|
|
|
function dismissSuggestion( context, product, promoted, url, suggestionSlug ) {
|
2019-02-17 22:48:18 +00:00
|
|
|
|
// hide the suggestion in the UI
|
2019-02-18 02:55:26 +00:00
|
|
|
|
var selector = '[data-suggestion-slug=' + suggestionSlug + ']';
|
2019-02-20 20:52:37 +00:00
|
|
|
|
$( selector ).fadeOut( function() {
|
|
|
|
|
$( this ).remove();
|
2019-03-04 21:38:11 +00:00
|
|
|
|
tidyProductEditMetabox();
|
2019-02-21 23:54:51 +00:00
|
|
|
|
} );
|
2019-02-17 22:48:18 +00:00
|
|
|
|
|
|
|
|
|
// save dismissal in user settings
|
2019-02-15 01:52:24 +00:00
|
|
|
|
jQuery.post(
|
|
|
|
|
ajaxurl,
|
|
|
|
|
{
|
2019-02-21 02:56:26 +00:00
|
|
|
|
'action': 'woocommerce_add_dismissed_marketplace_suggestion',
|
2019-02-15 01:52:24 +00:00
|
|
|
|
'_wpnonce': marketplace_suggestions.dismiss_suggestion_nonce,
|
2019-03-04 21:54:06 +00:00
|
|
|
|
'slug': suggestionSlug
|
2019-02-15 01:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
2019-02-20 02:33:57 +00:00
|
|
|
|
|
2019-03-05 21:50:22 +00:00
|
|
|
|
// if this is a high-use area, delay new suggestion that area for a short while
|
2019-03-06 00:57:18 +00:00
|
|
|
|
var highUseSuggestionContexts = [ 'products-list-inline' ];
|
2019-03-05 21:50:22 +00:00
|
|
|
|
if ( _.contains( highUseSuggestionContexts, context ) ) {
|
2019-03-08 00:11:00 +00:00
|
|
|
|
// snooze suggestions in that area for 2 days
|
|
|
|
|
var contextSnoozeCookie = 'woocommerce_snooze_suggestions__' + context;
|
|
|
|
|
Cookies.set( contextSnoozeCookie, 'true', { expires: 2 } );
|
|
|
|
|
|
|
|
|
|
// keep track of how often this area gets dismissed in a cookie
|
|
|
|
|
var contextDismissalCountCookie = 'woocommerce_dismissed_suggestions__' + context;
|
2019-03-15 12:21:51 +00:00
|
|
|
|
var previousDismissalsInThisContext = parseInt( Cookies.get( contextDismissalCountCookie ), 10 ) || 0;
|
2019-03-08 00:11:00 +00:00
|
|
|
|
Cookies.set( contextDismissalCountCookie, previousDismissalsInThisContext + 1, { expires: 31 } );
|
2019-03-05 21:50:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 22:38:55 +00:00
|
|
|
|
window.wcTracks.recordEvent( 'marketplace_suggestion_dismissed', {
|
2019-03-22 02:42:06 +00:00
|
|
|
|
suggestion_slug: suggestionSlug,
|
|
|
|
|
context: context,
|
2019-03-25 20:59:28 +00:00
|
|
|
|
product: product || '',
|
|
|
|
|
promoted: promoted || '',
|
|
|
|
|
target: url || ''
|
2019-02-20 02:33:57 +00:00
|
|
|
|
} );
|
2019-02-15 01:52:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render DOM element for suggestion dismiss button.
|
2019-03-22 02:42:06 +00:00
|
|
|
|
function renderDismissButton( context, product, promoted, url, suggestionSlug ) {
|
2019-02-15 00:30:53 +00:00
|
|
|
|
var dismissButton = document.createElement( 'a' );
|
|
|
|
|
|
|
|
|
|
dismissButton.classList.add( 'suggestion-dismiss' );
|
2019-03-04 23:20:13 +00:00
|
|
|
|
dismissButton.setAttribute( 'title', marketplace_suggestions.i18n_marketplace_suggestions_dismiss_tooltip );
|
2019-02-15 00:30:53 +00:00
|
|
|
|
dismissButton.setAttribute( 'href', '#' );
|
2019-02-18 02:55:26 +00:00
|
|
|
|
dismissButton.onclick = function( event ) {
|
|
|
|
|
event.preventDefault();
|
2019-03-22 02:42:06 +00:00
|
|
|
|
dismissSuggestion( context, product, promoted, url, suggestionSlug );
|
2019-03-04 21:54:06 +00:00
|
|
|
|
};
|
2019-02-15 00:30:53 +00:00
|
|
|
|
|
|
|
|
|
return dismissButton;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 07:33:38 +00:00
|
|
|
|
function addURLParameters( context, url ) {
|
|
|
|
|
var urlParams = marketplace_suggestions.in_app_purchase_params;
|
|
|
|
|
urlParams.utm_source = 'unknown';
|
|
|
|
|
urlParams.utm_campaign = 'marketplacesuggestions';
|
|
|
|
|
urlParams.utm_medium = 'product';
|
|
|
|
|
|
2019-02-26 02:48:35 +00:00
|
|
|
|
var sourceContextMap = {
|
|
|
|
|
'productstable': [
|
|
|
|
|
'products-list-inline'
|
|
|
|
|
],
|
|
|
|
|
'productsempty': [
|
|
|
|
|
'products-list-empty-header',
|
|
|
|
|
'products-list-empty-footer',
|
|
|
|
|
'products-list-empty-body'
|
|
|
|
|
],
|
|
|
|
|
'ordersempty': [
|
|
|
|
|
'orders-list-empty-header',
|
|
|
|
|
'orders-list-empty-footer',
|
|
|
|
|
'orders-list-empty-body'
|
|
|
|
|
],
|
|
|
|
|
'editproduct': [
|
|
|
|
|
'product-edit-meta-tab-header',
|
|
|
|
|
'product-edit-meta-tab-footer',
|
|
|
|
|
'product-edit-meta-tab-body'
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
var utmSource = _.findKey( sourceContextMap, function( sourceInfo ) {
|
|
|
|
|
return _.contains( sourceInfo, context );
|
|
|
|
|
} );
|
|
|
|
|
if ( utmSource ) {
|
2019-07-04 07:33:38 +00:00
|
|
|
|
urlParams.utm_source = utmSource;
|
2019-02-26 02:48:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-04 07:33:38 +00:00
|
|
|
|
return url + '?' + jQuery.param( urlParams );
|
2019-02-26 02:48:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render DOM element for suggestion linkout, optionally with button style.
|
2019-03-22 02:42:06 +00:00
|
|
|
|
function renderLinkout( context, product, promoted, slug, url, text, isButton ) {
|
2019-02-13 22:16:39 +00:00
|
|
|
|
var linkoutButton = document.createElement( 'a' );
|
|
|
|
|
|
2019-07-04 07:33:38 +00:00
|
|
|
|
var utmUrl = addURLParameters( context, url );
|
2019-02-26 02:48:35 +00:00
|
|
|
|
linkoutButton.setAttribute( 'href', utmUrl );
|
2019-07-10 02:59:24 +00:00
|
|
|
|
|
|
|
|
|
// By default, CTA links should open in same tab (and feel integrated with Woo).
|
|
|
|
|
// Exception: when editing products, use new tab. User may have product edits
|
|
|
|
|
// that need to be saved.
|
2019-07-10 03:05:36 +00:00
|
|
|
|
var newTabContexts = [
|
|
|
|
|
'product-edit-meta-tab-header',
|
|
|
|
|
'product-edit-meta-tab-footer',
|
|
|
|
|
'product-edit-meta-tab-body'
|
|
|
|
|
];
|
|
|
|
|
if ( _.includes( newTabContexts, context ) ) {
|
2019-07-10 02:59:24 +00:00
|
|
|
|
linkoutButton.setAttribute( 'target', 'blank' );
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
|
linkoutButton.textContent = text;
|
|
|
|
|
|
2019-03-04 21:54:06 +00:00
|
|
|
|
linkoutButton.onclick = function() {
|
2019-02-21 22:38:55 +00:00
|
|
|
|
window.wcTracks.recordEvent( 'marketplace_suggestion_clicked', {
|
2019-03-22 02:42:06 +00:00
|
|
|
|
suggestion_slug: slug,
|
|
|
|
|
context: context,
|
2019-03-25 20:59:28 +00:00
|
|
|
|
product: product || '',
|
|
|
|
|
promoted: promoted || '',
|
|
|
|
|
target: url || ''
|
2019-02-20 02:33:57 +00:00
|
|
|
|
} );
|
2019-03-04 21:54:06 +00:00
|
|
|
|
};
|
2019-02-20 02:33:57 +00:00
|
|
|
|
|
2019-02-21 23:54:51 +00:00
|
|
|
|
if ( isButton ) {
|
2019-02-18 23:22:06 +00:00
|
|
|
|
linkoutButton.classList.add( 'button' );
|
2019-03-01 01:22:02 +00:00
|
|
|
|
} else {
|
2019-02-19 00:44:35 +00:00
|
|
|
|
linkoutButton.classList.add( 'linkout' );
|
|
|
|
|
var linkoutIcon = document.createElement( 'span' );
|
|
|
|
|
linkoutIcon.classList.add( 'dashicons', 'dashicons-external' );
|
|
|
|
|
linkoutButton.appendChild( linkoutIcon );
|
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
|
|
|
|
return linkoutButton;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render DOM element for suggestion icon image.
|
2019-02-28 19:40:47 +00:00
|
|
|
|
function renderSuggestionIcon( iconUrl ) {
|
2019-02-21 23:54:51 +00:00
|
|
|
|
if ( ! iconUrl ) {
|
2019-02-19 22:46:06 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var image = document.createElement( 'img' );
|
|
|
|
|
image.src = iconUrl;
|
|
|
|
|
image.classList.add( 'marketplace-suggestion-icon' );
|
|
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render DOM elements for suggestion content.
|
2019-04-07 22:00:25 +00:00
|
|
|
|
function renderSuggestionContent( slug, title, copy ) {
|
2019-03-01 01:22:02 +00:00
|
|
|
|
var container = document.createElement( 'div' );
|
2019-02-19 03:31:14 +00:00
|
|
|
|
|
2019-03-01 01:22:02 +00:00
|
|
|
|
container.classList.add( 'marketplace-suggestion-container-content' );
|
2019-02-18 02:55:26 +00:00
|
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
|
if ( title ) {
|
|
|
|
|
var titleHeading = document.createElement( 'h4' );
|
|
|
|
|
titleHeading.textContent = title;
|
2019-03-01 01:22:02 +00:00
|
|
|
|
container.appendChild( titleHeading );
|
2019-02-18 23:22:06 +00:00
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
2019-02-13 22:21:58 +00:00
|
|
|
|
if ( copy ) {
|
|
|
|
|
var body = document.createElement( 'p' );
|
|
|
|
|
body.textContent = copy;
|
2019-03-01 01:22:02 +00:00
|
|
|
|
container.appendChild( body );
|
2019-02-13 22:21:58 +00:00
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
2019-04-07 22:00:25 +00:00
|
|
|
|
// Conditionally add in a Manage suggestions link to product edit
|
|
|
|
|
// metabox footer (based on suggestion slug).
|
|
|
|
|
var slugsWithManage = [
|
|
|
|
|
'product-edit-empty-footer-browse-all',
|
|
|
|
|
'product-edit-meta-tab-footer-browse-all'
|
|
|
|
|
];
|
|
|
|
|
if ( -1 !== slugsWithManage.indexOf( slug ) ) {
|
|
|
|
|
container.classList.add( 'has-manage-link' );
|
|
|
|
|
|
|
|
|
|
var manageSuggestionsLink = document.createElement( 'a' );
|
|
|
|
|
manageSuggestionsLink.classList.add( 'marketplace-suggestion-manage-link', 'linkout' );
|
|
|
|
|
manageSuggestionsLink.setAttribute(
|
|
|
|
|
'href',
|
2019-04-10 00:16:49 +00:00
|
|
|
|
marketplace_suggestions.manage_suggestions_url
|
2019-04-07 22:00:25 +00:00
|
|
|
|
);
|
|
|
|
|
manageSuggestionsLink.textContent = marketplace_suggestions.i18n_marketplace_suggestions_manage_suggestions;
|
|
|
|
|
|
|
|
|
|
container.appendChild( manageSuggestionsLink );
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 01:22:02 +00:00
|
|
|
|
return container;
|
2019-02-19 03:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render DOM elements for suggestion call-to-action – button or link with dismiss 'x'.
|
2019-03-22 02:42:06 +00:00
|
|
|
|
function renderSuggestionCTA( context, product, promoted, slug, url, linkText, linkIsButton, allowDismiss ) {
|
2019-03-01 01:22:02 +00:00
|
|
|
|
var container = document.createElement( 'div' );
|
2019-02-19 03:31:14 +00:00
|
|
|
|
|
2019-03-01 01:22:02 +00:00
|
|
|
|
if ( ! linkText ) {
|
|
|
|
|
linkText = marketplace_suggestions.i18n_marketplace_suggestions_default_cta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container.classList.add( 'marketplace-suggestion-container-cta' );
|
2019-02-18 23:22:06 +00:00
|
|
|
|
if ( url && linkText ) {
|
2019-03-22 02:42:06 +00:00
|
|
|
|
var linkoutElement = renderLinkout( context, product, promoted, slug, url, linkText, linkIsButton );
|
2019-03-01 01:22:02 +00:00
|
|
|
|
container.appendChild( linkoutElement );
|
2019-02-13 22:21:58 +00:00
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
2019-02-18 22:54:58 +00:00
|
|
|
|
if ( allowDismiss ) {
|
2019-03-22 02:42:06 +00:00
|
|
|
|
container.appendChild( renderDismissButton( context, product, promoted, url, slug ) );
|
2019-02-18 22:54:58 +00:00
|
|
|
|
}
|
2019-02-18 02:55:26 +00:00
|
|
|
|
|
2019-03-01 01:22:02 +00:00
|
|
|
|
return container;
|
2019-02-19 03:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render a "list item" style suggestion.
|
|
|
|
|
// These are used in onboarding style contexts, e.g. products list empty state.
|
2019-03-22 02:42:06 +00:00
|
|
|
|
function renderListItem( context, product, promoted, slug, iconUrl, title, copy, url, linkText, linkIsButton, allowDismiss ) {
|
2019-02-19 03:31:14 +00:00
|
|
|
|
var container = document.createElement( 'div' );
|
|
|
|
|
container.classList.add( 'marketplace-suggestion-container' );
|
|
|
|
|
container.dataset.suggestionSlug = slug;
|
|
|
|
|
|
2019-02-28 19:40:47 +00:00
|
|
|
|
var icon = renderSuggestionIcon( iconUrl );
|
2019-02-19 22:46:06 +00:00
|
|
|
|
if ( icon ) {
|
|
|
|
|
container.appendChild( icon );
|
|
|
|
|
}
|
2019-02-19 03:31:14 +00:00
|
|
|
|
container.appendChild(
|
2019-04-07 22:00:25 +00:00
|
|
|
|
renderSuggestionContent( slug, title, copy )
|
2019-02-19 03:31:14 +00:00
|
|
|
|
);
|
|
|
|
|
container.appendChild(
|
2019-03-22 02:42:06 +00:00
|
|
|
|
renderSuggestionCTA( context, product, promoted, slug, url, linkText, linkIsButton, allowDismiss )
|
2019-02-19 03:31:14 +00:00
|
|
|
|
);
|
2019-02-17 22:48:18 +00:00
|
|
|
|
|
2019-02-13 22:16:39 +00:00
|
|
|
|
return container;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Filter suggestion data to remove less-relevant suggestions.
|
2019-02-14 02:47:32 +00:00
|
|
|
|
function getRelevantPromotions( marketplaceSuggestionsApiData, displayContext ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
|
// select based on display context
|
2019-02-13 00:34:25 +00:00
|
|
|
|
var promos = _.filter( marketplaceSuggestionsApiData, function( promo ) {
|
2019-02-25 02:27:14 +00:00
|
|
|
|
if ( _.isArray( promo.context ) ) {
|
|
|
|
|
return _.contains( promo.context, displayContext );
|
|
|
|
|
}
|
2019-02-13 00:34:25 +00:00
|
|
|
|
return ( displayContext === promo.context );
|
|
|
|
|
} );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-02-15 01:52:24 +00:00
|
|
|
|
// hide promos the user has dismissed
|
|
|
|
|
promos = _.filter( promos, function( promo ) {
|
|
|
|
|
return ! _.contains( marketplace_suggestions.dismissed_suggestions, promo.slug );
|
|
|
|
|
} );
|
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
|
// hide promos for things the user already has installed
|
2019-02-13 00:34:25 +00:00
|
|
|
|
promos = _.filter( promos, function( promo ) {
|
2019-03-27 16:21:54 +00:00
|
|
|
|
return ! _.contains( marketplace_suggestions.active_plugins, promo.product );
|
2019-02-13 00:34:25 +00:00
|
|
|
|
} );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-02-12 22:33:37 +00:00
|
|
|
|
// hide promos that are not applicable based on user's installed extensions
|
2019-02-13 00:34:25 +00:00
|
|
|
|
promos = _.filter( promos, function( promo ) {
|
2019-02-27 03:16:49 +00:00
|
|
|
|
if ( ! promo['show-if-active'] ) {
|
2019-02-12 22:33:37 +00:00
|
|
|
|
// this promotion is relevant to all
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if the user has any of the prerequisites, show the promo
|
2019-02-27 03:16:49 +00:00
|
|
|
|
return ( _.intersection( marketplace_suggestions.active_plugins, promo['show-if-active'] ).length > 0 );
|
2019-02-12 22:33:37 +00:00
|
|
|
|
} );
|
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
|
return promos;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Show and hide page elements dependent on suggestion state.
|
2019-02-25 03:27:49 +00:00
|
|
|
|
function hidePageElementsForSuggestionState( usedSuggestionsContexts ) {
|
2019-02-25 00:46:43 +00:00
|
|
|
|
var showingEmptyStateSuggestions = _.intersection(
|
2019-02-25 03:06:50 +00:00
|
|
|
|
usedSuggestionsContexts,
|
2019-02-25 00:46:43 +00:00
|
|
|
|
[ 'products-list-empty-body', 'orders-list-empty-body' ]
|
|
|
|
|
).length > 0;
|
2019-02-21 02:51:41 +00:00
|
|
|
|
|
|
|
|
|
// Streamline onboarding UI if we're in 'empty state' welcome mode.
|
2019-02-25 00:46:43 +00:00
|
|
|
|
if ( showingEmptyStateSuggestions ) {
|
2019-02-19 00:56:49 +00:00
|
|
|
|
$( '#screen-meta-links' ).hide();
|
|
|
|
|
$( '#wpfooter' ).hide();
|
|
|
|
|
}
|
2019-02-21 02:51:41 +00:00
|
|
|
|
|
|
|
|
|
// Hide the header & footer, they don't make sense without specific promotion content
|
2019-02-25 00:46:43 +00:00
|
|
|
|
if ( ! showingEmptyStateSuggestions ) {
|
2019-02-19 00:56:49 +00:00
|
|
|
|
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="products-list-empty-header"]' ).hide();
|
|
|
|
|
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="products-list-empty-footer"]' ).hide();
|
2019-02-25 00:46:43 +00:00
|
|
|
|
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="orders-list-empty-header"]' ).hide();
|
|
|
|
|
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="orders-list-empty-footer"]' ).hide();
|
2019-02-12 21:38:20 +00:00
|
|
|
|
}
|
2019-03-04 21:38:11 +00:00
|
|
|
|
}
|
2019-02-25 03:27:49 +00:00
|
|
|
|
|
2019-03-04 21:38:11 +00:00
|
|
|
|
// Streamline the product edit suggestions tab dependent on what's visible.
|
|
|
|
|
function tidyProductEditMetabox() {
|
|
|
|
|
var productMetaboxSuggestions = $(
|
|
|
|
|
'.marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-body"]'
|
|
|
|
|
).children();
|
|
|
|
|
if ( 0 >= productMetaboxSuggestions.length ) {
|
2019-03-11 23:56:03 +00:00
|
|
|
|
var metaboxSuggestionsUISelector =
|
|
|
|
|
'.marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-body"]';
|
|
|
|
|
metaboxSuggestionsUISelector +=
|
|
|
|
|
', .marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-header"]';
|
|
|
|
|
metaboxSuggestionsUISelector +=
|
|
|
|
|
', .marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-footer"]';
|
|
|
|
|
$( metaboxSuggestionsUISelector ).fadeOut( {
|
|
|
|
|
complete: function() {
|
|
|
|
|
$( '.marketplace-suggestions-metabox-nosuggestions-placeholder' ).fadeIn();
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
2019-02-25 03:27:49 +00:00
|
|
|
|
}
|
2019-02-14 02:47:32 +00:00
|
|
|
|
}
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-04-08 22:29:51 +00:00
|
|
|
|
function addManageSuggestionsTracksHandler() {
|
|
|
|
|
$( 'a.marketplace-suggestion-manage-link' ).on( 'click', function() {
|
|
|
|
|
window.wcTracks.recordEvent( 'marketplace_suggestions_manage_clicked' );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 21:40:06 +00:00
|
|
|
|
function isContextHiddenOnPageLoad( context ) {
|
|
|
|
|
// Some suggestions are not visible on page load;
|
|
|
|
|
// e.g. the user reveals them by selecting a tab.
|
|
|
|
|
var revealableSuggestionsContexts = [
|
|
|
|
|
'product-edit-meta-tab-header',
|
|
|
|
|
'product-edit-meta-tab-body',
|
|
|
|
|
'product-edit-meta-tab-footer'
|
|
|
|
|
];
|
|
|
|
|
return _.includes( revealableSuggestionsContexts, context );
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 16:23:02 +00:00
|
|
|
|
// track the current product data tab to avoid over-tracking suggestions
|
|
|
|
|
var currentTab = false;
|
|
|
|
|
|
2019-02-21 02:51:41 +00:00
|
|
|
|
// Render suggestion data in appropriate places in UI.
|
2019-02-21 22:23:09 +00:00
|
|
|
|
function displaySuggestions( marketplaceSuggestionsApiData ) {
|
2019-02-25 02:59:11 +00:00
|
|
|
|
var usedSuggestionsContexts = [];
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
|
// iterate over all suggestions containers, rendering promos
|
|
|
|
|
$( '.marketplace-suggestions-container' ).each( function() {
|
|
|
|
|
// determine the context / placement we're populating
|
|
|
|
|
var context = this.dataset.marketplaceSuggestionsContext;
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
|
// find promotions that target this context
|
|
|
|
|
var promos = getRelevantPromotions( marketplaceSuggestionsApiData, context );
|
2019-02-12 21:38:20 +00:00
|
|
|
|
|
2019-02-26 23:29:45 +00:00
|
|
|
|
// shuffle/randomly select five suggestions to display
|
|
|
|
|
var suggestionsToDisplay = _.sample( promos, 5 );
|
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
|
// render the promo content
|
2019-02-26 23:29:45 +00:00
|
|
|
|
for ( var i in suggestionsToDisplay ) {
|
2019-02-25 03:00:47 +00:00
|
|
|
|
|
2019-02-26 23:29:45 +00:00
|
|
|
|
var linkText = suggestionsToDisplay[ i ]['link-text'];
|
2019-03-01 01:22:02 +00:00
|
|
|
|
var linkoutIsButton = true;
|
|
|
|
|
if ( suggestionsToDisplay[ i ]['link-text'] ) {
|
|
|
|
|
linkText = suggestionsToDisplay[ i ]['link-text'];
|
|
|
|
|
linkoutIsButton = false;
|
2019-02-18 23:22:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 00:56:49 +00:00
|
|
|
|
// dismiss is allowed by default
|
|
|
|
|
var allowDismiss = true;
|
2019-02-26 23:29:45 +00:00
|
|
|
|
if ( suggestionsToDisplay[ i ]['allow-dismiss'] === false ) {
|
2019-02-19 00:56:49 +00:00
|
|
|
|
allowDismiss = false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
|
var content = renderListItem(
|
2019-02-26 02:48:35 +00:00
|
|
|
|
context,
|
2019-03-22 02:42:06 +00:00
|
|
|
|
suggestionsToDisplay[ i ].product,
|
|
|
|
|
suggestionsToDisplay[ i ].promoted,
|
2019-02-26 23:29:45 +00:00
|
|
|
|
suggestionsToDisplay[ i ].slug,
|
|
|
|
|
suggestionsToDisplay[ i ].icon,
|
|
|
|
|
suggestionsToDisplay[ i ].title,
|
|
|
|
|
suggestionsToDisplay[ i ].copy,
|
|
|
|
|
suggestionsToDisplay[ i ].url,
|
2019-02-18 23:22:06 +00:00
|
|
|
|
linkText,
|
|
|
|
|
linkoutIsButton,
|
2019-02-19 00:56:49 +00:00
|
|
|
|
allowDismiss
|
2019-02-14 02:47:32 +00:00
|
|
|
|
);
|
2019-02-12 21:38:20 +00:00
|
|
|
|
$( this ).append( content );
|
2019-02-18 22:54:58 +00:00
|
|
|
|
$( this ).addClass( 'showing-suggestion' );
|
2019-02-25 02:59:11 +00:00
|
|
|
|
usedSuggestionsContexts.push( context );
|
2019-04-09 21:40:06 +00:00
|
|
|
|
|
|
|
|
|
if ( ! isContextHiddenOnPageLoad( context ) ) {
|
|
|
|
|
// Fire 'displayed' tracks events for immediately visible suggestions.
|
|
|
|
|
window.wcTracks.recordEvent( 'marketplace_suggestion_displayed', {
|
|
|
|
|
suggestion_slug: suggestionsToDisplay[ i ].slug,
|
|
|
|
|
context: context,
|
|
|
|
|
product: suggestionsToDisplay[ i ].product || '',
|
|
|
|
|
promoted: suggestionsToDisplay[ i ].promoted || '',
|
|
|
|
|
target: suggestionsToDisplay[ i ].url || ''
|
|
|
|
|
} );
|
|
|
|
|
}
|
2019-02-12 21:38:20 +00:00
|
|
|
|
}
|
2019-04-09 16:23:02 +00:00
|
|
|
|
|
2019-04-09 21:40:06 +00:00
|
|
|
|
// Track when suggestions are displayed (and not already visible).
|
2020-11-18 17:56:30 +00:00
|
|
|
|
$( 'ul.product_data_tabs li.marketplace-suggestions_options a' ).on( 'click', function( e ) {
|
2019-04-09 16:23:02 +00:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
if ( '#marketplace_suggestions' === currentTab ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 21:40:06 +00:00
|
|
|
|
if ( ! isContextHiddenOnPageLoad( context ) ) {
|
|
|
|
|
// We've already fired 'displayed' event above.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-09 16:23:02 +00:00
|
|
|
|
for ( var i in suggestionsToDisplay ) {
|
|
|
|
|
window.wcTracks.recordEvent( 'marketplace_suggestion_displayed', {
|
|
|
|
|
suggestion_slug: suggestionsToDisplay[ i ].slug,
|
|
|
|
|
context: context,
|
|
|
|
|
product: suggestionsToDisplay[ i ].product || '',
|
|
|
|
|
promoted: suggestionsToDisplay[ i ].promoted || '',
|
|
|
|
|
target: suggestionsToDisplay[ i ].url || ''
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
} );
|
2019-02-14 02:47:32 +00:00
|
|
|
|
} );
|
|
|
|
|
|
2019-02-25 03:27:49 +00:00
|
|
|
|
hidePageElementsForSuggestionState( usedSuggestionsContexts );
|
2019-03-04 21:38:11 +00:00
|
|
|
|
tidyProductEditMetabox();
|
2019-02-13 01:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 22:23:09 +00:00
|
|
|
|
if ( marketplace_suggestions.suggestions_data ) {
|
|
|
|
|
displaySuggestions( marketplace_suggestions.suggestions_data );
|
2019-04-09 16:23:02 +00:00
|
|
|
|
|
|
|
|
|
// track the current product data tab to avoid over-reporting suggestion views
|
|
|
|
|
$( 'ul.product_data_tabs' ).on( 'click', 'li a', function( e ) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
currentTab = $( this ).attr( 'href' );
|
|
|
|
|
} );
|
2019-02-21 22:23:09 +00:00
|
|
|
|
}
|
2019-04-08 22:29:51 +00:00
|
|
|
|
|
|
|
|
|
addManageSuggestionsTracksHandler();
|
2019-02-12 21:38:20 +00:00
|
|
|
|
});
|
|
|
|
|
|
2019-02-15 01:52:24 +00:00
|
|
|
|
})( jQuery, marketplace_suggestions, ajaxurl );
|