2019-02-15 01:52:24 +00:00
|
|
|
/* global marketplace_suggestions ajaxurl */
|
|
|
|
( function( $, marketplace_suggestions ) {
|
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-17 22:48:18 +00:00
|
|
|
function dismissSuggestion( suggestionSlug ) {
|
|
|
|
// hide the suggestion in the UI
|
2019-02-18 02:55:26 +00:00
|
|
|
var selector = '[data-suggestion-slug=' + suggestionSlug + ']';
|
2019-02-17 22:48:18 +00:00
|
|
|
$( selector ).fadeOut();
|
|
|
|
|
|
|
|
// save dismissal in user settings
|
2019-02-15 01:52:24 +00:00
|
|
|
jQuery.post(
|
|
|
|
ajaxurl,
|
|
|
|
{
|
|
|
|
'action': 'add_dismissed_marketplace_suggestion',
|
|
|
|
'_wpnonce': marketplace_suggestions.dismiss_suggestion_nonce,
|
|
|
|
'slug': suggestionSlug,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-15 00:30:53 +00:00
|
|
|
function renderDismissButton( suggestionSlug ) {
|
|
|
|
var dismissButton = document.createElement( 'a' );
|
|
|
|
|
|
|
|
dismissButton.classList.add( 'suggestion-dismiss' );
|
|
|
|
dismissButton.setAttribute( 'href', '#' );
|
2019-02-18 02:55:26 +00:00
|
|
|
dismissButton.onclick = function( event ) {
|
|
|
|
event.preventDefault();
|
2019-02-17 22:48:18 +00:00
|
|
|
dismissSuggestion( suggestionSlug );
|
2019-02-15 01:52:24 +00:00
|
|
|
}
|
2019-02-15 00:30:53 +00:00
|
|
|
|
|
|
|
return dismissButton;
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
function renderLinkout( url, text, isButton ) {
|
2019-02-13 22:16:39 +00:00
|
|
|
var linkoutButton = document.createElement( 'a' );
|
|
|
|
|
|
|
|
linkoutButton.setAttribute( 'href', url );
|
2019-02-18 23:22:06 +00:00
|
|
|
linkoutButton.setAttribute( 'target', 'blank' );
|
|
|
|
linkoutButton.textContent = text;
|
|
|
|
|
|
|
|
if (isButton) {
|
|
|
|
linkoutButton.classList.add( 'button' );
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
|
|
return linkoutButton;
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:54:58 +00:00
|
|
|
function renderTableBanner( slug, title, url, buttonText, allowDismiss ) {
|
2019-02-13 21:53:25 +00:00
|
|
|
if ( ! title || ! url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
buttonText = 'Go';
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = document.createElement( 'tr' );
|
2019-02-17 22:48:18 +00:00
|
|
|
row.classList.add( 'marketplace-suggestions-container' );
|
2019-02-13 23:16:59 +00:00
|
|
|
row.classList.add( 'marketplace-table-banner' );
|
2019-02-17 22:48:18 +00:00
|
|
|
row.dataset.suggestionSlug = slug;
|
2019-02-13 21:53:25 +00:00
|
|
|
|
|
|
|
var titleColumn = document.createElement( 'td' );
|
|
|
|
titleColumn.setAttribute( 'colspan', 5 );
|
2019-02-13 23:16:59 +00:00
|
|
|
titleColumn.classList.add( 'marketplace-table-title' );
|
2019-02-13 21:53:25 +00:00
|
|
|
var titleHeading = document.createElement( 'h2' );
|
2019-02-15 00:30:53 +00:00
|
|
|
titleColumn.appendChild( titleHeading );
|
2019-02-13 21:53:25 +00:00
|
|
|
titleHeading.textContent = title;
|
|
|
|
|
|
|
|
row.appendChild( titleColumn );
|
|
|
|
|
|
|
|
var linkoutColumn = document.createElement( 'td' );
|
2019-02-15 00:30:53 +00:00
|
|
|
linkoutColumn.setAttribute( 'colspan', 5 );
|
2019-02-13 23:16:59 +00:00
|
|
|
linkoutColumn.classList.add( 'marketplace-table-linkout' );
|
2019-02-18 23:22:06 +00:00
|
|
|
var linkoutButton = renderLinkout( url, buttonText );
|
2019-02-15 00:30:53 +00:00
|
|
|
linkoutColumn.appendChild( linkoutButton );
|
2019-02-18 22:54:58 +00:00
|
|
|
if ( allowDismiss ) {
|
|
|
|
linkoutColumn.appendChild( renderDismissButton( slug ) )
|
|
|
|
}
|
2019-02-13 21:53:25 +00:00
|
|
|
|
|
|
|
row.appendChild( linkoutColumn );
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
function renderListItem( slug, title, url, linkText, linkIsButton, copy, allowDismiss ) {
|
2019-02-13 22:16:39 +00:00
|
|
|
var container = document.createElement( 'div' );
|
2019-02-13 23:16:59 +00:00
|
|
|
container.classList.add( 'marketplace-listitem-container' );
|
2019-02-17 22:48:18 +00:00
|
|
|
container.dataset.suggestionSlug = slug;
|
2019-02-13 22:16:39 +00:00
|
|
|
|
2019-02-18 02:55:26 +00:00
|
|
|
var left = document.createElement( 'div' );
|
|
|
|
left.classList.add( 'marketplace-listitem-container-content' );
|
|
|
|
var right = document.createElement( 'div' );
|
|
|
|
right.classList.add( 'marketplace-listitem-container-cta' );
|
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
if ( title ) {
|
|
|
|
var titleHeading = document.createElement( 'h4' );
|
|
|
|
titleHeading.textContent = title;
|
|
|
|
left.appendChild( titleHeading );
|
|
|
|
}
|
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-02-18 02:55:26 +00:00
|
|
|
left.appendChild( body );
|
2019-02-13 22:21:58 +00:00
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
2019-02-18 23:22:06 +00:00
|
|
|
if ( url && linkText ) {
|
|
|
|
var linkoutElement = renderLinkout( url, linkText, linkIsButton );
|
|
|
|
right.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 ) {
|
|
|
|
right.appendChild( renderDismissButton( slug ) )
|
|
|
|
}
|
2019-02-18 02:55:26 +00:00
|
|
|
|
|
|
|
container.appendChild( left );
|
|
|
|
container.appendChild( right );
|
2019-02-17 22:48:18 +00:00
|
|
|
|
2019-02-13 22:16:39 +00:00
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
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-02-15 01:52:24 +00:00
|
|
|
return ! _.contains( marketplace_suggestions.installed_woo_plugins, promo['hide-if-installed'] );
|
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-12 22:33:37 +00:00
|
|
|
if ( ! promo['show-if-installed'] ) {
|
|
|
|
// this promotion is relevant to all
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the user has any of the prerequisites, show the promo
|
2019-02-15 01:52:24 +00:00
|
|
|
return ( _.intersection( marketplace_suggestions.installed_woo_plugins, promo['show-if-installed'] ).length > 0 );
|
2019-02-12 22:33:37 +00:00
|
|
|
} );
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
return promos;
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:54:58 +00:00
|
|
|
function hidePageElementsForOnboardingStyle( visibleSuggestions ) {
|
2019-02-14 02:47:32 +00:00
|
|
|
if ( _.contains( visibleSuggestions, 'products-list-empty-body' ) ) {
|
|
|
|
$('#screen-meta-links').hide();
|
|
|
|
$('#wpfooter').hide();
|
2019-02-12 21:38:20 +00:00
|
|
|
}
|
2019-02-14 02:47:32 +00:00
|
|
|
}
|
2019-02-12 21:38:20 +00:00
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
function displaySuggestions( marketplaceSuggestionsApiData ) {
|
2019-02-18 22:54:58 +00:00
|
|
|
var visibleSuggestions = [];
|
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-14 02:47:32 +00:00
|
|
|
// render the promo content
|
|
|
|
for ( var i in promos ) {
|
2019-02-18 23:22:06 +00:00
|
|
|
var linkText = promos[ i ]['link-text'];
|
|
|
|
var linkoutIsButton = false;
|
|
|
|
if ( promos[ i ]['button-text'] ) {
|
|
|
|
linkText = promos[ i ]['button-text'];
|
|
|
|
linkoutIsButton = true;
|
|
|
|
}
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
var content = renderListItem(
|
2019-02-17 22:48:18 +00:00
|
|
|
promos[ i ].slug,
|
2019-02-14 02:47:32 +00:00
|
|
|
promos[ i ].title,
|
|
|
|
promos[ i ].url,
|
2019-02-18 23:22:06 +00:00
|
|
|
linkText,
|
|
|
|
linkoutIsButton,
|
2019-02-18 22:54:58 +00:00
|
|
|
promos[ i ].copy,
|
|
|
|
promos[ 0 ]['allow-dismiss']
|
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-14 02:47:32 +00:00
|
|
|
visibleSuggestions.push( promos[i].context );
|
2019-02-12 21:38:20 +00:00
|
|
|
}
|
2019-02-14 02:47:32 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// render inline promos in products list
|
|
|
|
$( '.wp-admin.admin-bar.edit-php.post-type-product table.wp-list-table.posts tbody').first().each( function() {
|
|
|
|
var context = 'products-list-inline';
|
|
|
|
|
|
|
|
// find promotions that target this context
|
|
|
|
var promos = getRelevantPromotions( marketplaceSuggestionsApiData, context );
|
|
|
|
if ( ! promos || ! promos.length ) {
|
|
|
|
return;
|
2019-02-12 21:38:20 +00:00
|
|
|
}
|
2019-02-13 21:53:25 +00:00
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
// render first promo
|
|
|
|
var content = renderTableBanner(
|
2019-02-15 00:30:53 +00:00
|
|
|
promos[ 0 ].slug,
|
2019-02-14 02:47:32 +00:00
|
|
|
promos[ 0 ].title,
|
|
|
|
promos[ 0 ].url,
|
2019-02-18 22:54:58 +00:00
|
|
|
promos[ 0 ]['button-text'],
|
|
|
|
promos[ 0 ]['allow-dismiss']
|
2019-02-14 02:47:32 +00:00
|
|
|
);
|
2019-02-12 21:38:20 +00:00
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
if ( content ) {
|
|
|
|
// where should we put it in the list?
|
|
|
|
var rows = $( this ).children();
|
|
|
|
var minRow = 3;
|
|
|
|
|
|
|
|
if ( rows.length <= minRow ) {
|
|
|
|
// if small number of rows, append at end
|
|
|
|
$( this ).append( content );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// for more rows, append
|
|
|
|
$( rows[ minRow - 1 ] ).after( content );
|
|
|
|
}
|
|
|
|
|
|
|
|
visibleSuggestions.push( context );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2019-02-18 22:54:58 +00:00
|
|
|
hidePageElementsForOnboardingStyle( visibleSuggestions );
|
2019-02-13 01:35:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
var data =
|
|
|
|
jQuery.getJSON(
|
|
|
|
ajaxurl,
|
|
|
|
{
|
|
|
|
'action': 'marketplace_suggestions',
|
|
|
|
},
|
|
|
|
displaySuggestions
|
|
|
|
);
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
});
|
|
|
|
|
2019-02-15 01:52:24 +00:00
|
|
|
})( jQuery, marketplace_suggestions, ajaxurl );
|