2019-02-14 02:47:32 +00:00
|
|
|
/* global installed_woo_plugins ajaxurl */
|
2019-02-12 23:19:42 +00:00
|
|
|
( function( $, installed_woo_plugins ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
$( function() {
|
2019-02-12 23:19:42 +00:00
|
|
|
if ( 'undefined' === typeof installed_woo_plugins ) {
|
2019-02-12 21:38:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:16:39 +00:00
|
|
|
function renderLinkoutButton( url, buttonText ) {
|
|
|
|
var linkoutButton = document.createElement( 'a' );
|
|
|
|
|
|
|
|
linkoutButton.classList.add( 'button' );
|
|
|
|
linkoutButton.setAttribute( 'href', url );
|
|
|
|
linkoutButton.textContent = buttonText;
|
|
|
|
|
|
|
|
return linkoutButton;
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:53:25 +00:00
|
|
|
function renderTableBanner( title, url, buttonText ) {
|
|
|
|
if ( ! title || ! url ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
buttonText = 'Go';
|
|
|
|
}
|
|
|
|
|
|
|
|
var row = document.createElement( 'tr' );
|
2019-02-13 23:16:59 +00:00
|
|
|
row.classList.add( 'marketplace-table-banner' );
|
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' );
|
|
|
|
titleColumn.append( titleHeading );
|
|
|
|
titleHeading.textContent = title;
|
|
|
|
|
|
|
|
row.appendChild( titleColumn );
|
|
|
|
|
|
|
|
var linkoutColumn = document.createElement( 'td' );
|
|
|
|
linkoutColumn.setAttribute( 'colspan', 4 );
|
2019-02-13 23:16:59 +00:00
|
|
|
linkoutColumn.classList.add( 'marketplace-table-linkout' );
|
2019-02-13 22:16:39 +00:00
|
|
|
var linkoutButton = renderLinkoutButton( url, buttonText );
|
2019-02-13 21:53:25 +00:00
|
|
|
linkoutColumn.append( linkoutButton );
|
|
|
|
|
|
|
|
row.appendChild( linkoutColumn );
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
2019-02-13 22:21:58 +00:00
|
|
|
function renderListItem( title, url, buttonText, copy ) {
|
|
|
|
if ( ! title ) {
|
2019-02-13 22:16:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! buttonText ) {
|
|
|
|
buttonText = 'Go';
|
|
|
|
}
|
|
|
|
|
|
|
|
var container = document.createElement( 'div' );
|
2019-02-13 23:16:59 +00:00
|
|
|
container.classList.add( 'marketplace-listitem-container' );
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
|
|
var titleHeading = document.createElement( 'h4' );
|
|
|
|
titleHeading.textContent = title;
|
|
|
|
container.appendChild( titleHeading );
|
|
|
|
|
2019-02-13 22:21:58 +00:00
|
|
|
if ( copy ) {
|
|
|
|
var body = document.createElement( 'p' );
|
|
|
|
body.textContent = copy;
|
|
|
|
container.appendChild( body );
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
2019-02-13 22:21:58 +00:00
|
|
|
if ( url ) {
|
|
|
|
var linkoutButton = renderLinkoutButton( url, buttonText );
|
|
|
|
container.appendChild( linkoutButton );
|
|
|
|
}
|
2019-02-13 22:16:39 +00:00
|
|
|
|
|
|
|
return container;
|
|
|
|
}
|
|
|
|
|
2019-02-13 01:35:56 +00:00
|
|
|
var visibleSuggestions = [];
|
|
|
|
|
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
|
|
|
|
|
|
|
// hide promos for things the user already has installed
|
2019-02-13 00:34:25 +00:00
|
|
|
promos = _.filter( promos, function( promo ) {
|
|
|
|
return ! _.contains( installed_woo_plugins, promo['hide-if-installed'] );
|
|
|
|
} );
|
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
|
|
|
|
return ( _.intersection( installed_woo_plugins, promo['show-if-installed'] ).length > 0 );
|
|
|
|
} );
|
|
|
|
|
2019-02-12 21:38:20 +00:00
|
|
|
return promos;
|
|
|
|
}
|
|
|
|
|
2019-02-14 02:47:32 +00:00
|
|
|
function hidePageElementsForOnboardingStyle() {
|
|
|
|
if ( _.contains( visibleSuggestions, 'products-list-empty-body' ) ) {
|
|
|
|
$('h1.wp-heading-inline').hide();
|
|
|
|
$('#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-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 ) {
|
|
|
|
var content = renderListItem(
|
|
|
|
promos[ i ].title,
|
|
|
|
promos[ i ].url,
|
|
|
|
promos[ i ]['button-text'],
|
|
|
|
promos[ i ].copy
|
|
|
|
);
|
2019-02-12 21:38:20 +00:00
|
|
|
$( this ).append( content );
|
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(
|
|
|
|
promos[ 0 ].title,
|
|
|
|
promos[ 0 ].url,
|
|
|
|
promos[ 0 ]['button-text']
|
|
|
|
);
|
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 );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
hidePageElementsForOnboardingStyle();
|
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-14 02:47:32 +00:00
|
|
|
})( jQuery, installed_woo_plugins, ajaxurl );
|