Skip to content

Commit 8b8336c

Browse files
Merge pull request #1078 from Codeinwp/feat/live-preview-playground
Add Live Preview button for plugin page
2 parents 661f2a2 + 198d229 commit 8b8336c

File tree

4 files changed

+202
-122
lines changed

4 files changed

+202
-122
lines changed

assets/blueprints/blueprint.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "https://playground.wordpress.net/blueprint-schema.json",
3+
"_comment1": "Set the Wizard Setup page as landing page with preview environment (which disable distractions)",
4+
"landingPage": "/wp-admin/admin.php?page=visualizer-setup-wizard&env=preview",
5+
"_comment2": "Preferred WordPress instance specifications",
6+
"preferredVersions": {
7+
"php": "8.0",
8+
"wp": "latest"
9+
},
10+
"features": {
11+
"_comment3.1": "Allow networking for internet access",
12+
"networking": true
13+
},
14+
"steps": [
15+
{
16+
"_comment4.1": "Login to the WordPress instance",
17+
"step": "login",
18+
"username": "admin",
19+
"password": "password"
20+
},
21+
{
22+
"_comment4.2": "Install the Visualizer plugin",
23+
"step": "installPlugin",
24+
"pluginZipFile": {
25+
"resource": "wordpress.org/plugins",
26+
"slug": "visualizer"
27+
},
28+
"options": {
29+
"activate": true
30+
}
31+
}
32+
]
33+
}

classes/Visualizer/Module/Admin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,14 +671,14 @@ public function registerAdminMenu() {
671671
__( 'Chart Library', 'visualizer' ),
672672
__( 'Chart Library', 'visualizer' ),
673673
'edit_posts',
674-
admin_url( 'admin.php?page=' . Visualizer_Plugin::NAME )
674+
'admin.php?page=' . Visualizer_Plugin::NAME
675675
);
676676
add_submenu_page(
677677
Visualizer_Plugin::NAME,
678678
__( 'Add New Chart', 'visualizer' ),
679679
__( 'Add New Chart', 'visualizer' ),
680680
'edit_posts',
681-
admin_url( 'admin.php?page=' . Visualizer_Plugin::NAME . '&vaction=addnew' )
681+
'admin.php?page=' . Visualizer_Plugin::NAME . '&vaction=addnew'
682682
);
683683
add_submenu_page(
684684
Visualizer_Plugin::NAME,

js/setup-wizard.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,48 @@
77
*/
88
/* jshint unused:false */
99
jQuery(function ($) {
10+
11+
/**
12+
* Check if the user is in live preview mode.
13+
* Live Preview is used in WordPress Playground to test the plugin.
14+
* Use this check to deactivate any process that does not showcase the plugin features (e.g. subscription to the newsletter, etc.)
15+
*
16+
* @type {boolean}
17+
*/
18+
const isLivePreview = window.location.search.includes('env=preview');
19+
20+
/**
21+
* Redirect to draft page after subscribe (optional).
22+
*
23+
* @param {Object} postData - Post data.
24+
*/
25+
const goToDraftPage = function ( postData = {} ) {
26+
postData ??= {};
27+
postData.action ??= 'visualizer_wizard_step_process';
28+
postData.security ??= visualizerSetupWizardData.ajax.security;
29+
postData.step ??= 'step_subscribe';
30+
31+
// Subscribe the user using the email if provided. Redirect to the draft page.
32+
$.post(visualizerSetupWizardData.ajax.url, postData, function (res) {
33+
34+
// Toggle the redirect popup.
35+
$('.redirect-popup').find('h3.popup-title').html(res.message);
36+
$('.redirect-popup').show();
37+
38+
if (1 === res.status) {
39+
setTimeout(function () {
40+
window.location.href = res.redirect_to;
41+
}, 5000);
42+
} else {
43+
$('.redirect-popup').hide();
44+
}
45+
currentStep.find('.spinner').removeClass('is-active');
46+
}).fail(function () {
47+
$('.redirect-popup').hide();
48+
currentStep.find('.spinner').removeClass('is-active');
49+
});
50+
}
51+
1052
var provideContent = function(id, stepDirection, stepPosition, selStep, callback) {
1153
// Import chart data.
1254
if ( 1 == id ) {
@@ -119,17 +161,22 @@ jQuery(function ($) {
119161
".btn-primary:not(.next-btn,.vz-create-page,.vz-subscribe)",
120162
function (e) {
121163
var stepNumber = $(this).data("step_number");
164+
122165
switch (stepNumber) {
123166
case 1:
124167
if ($(".vz-radio-btn").is(":checked")) {
125168
$('#smartwizard').smartWizard('next');
126169
}
127170
break;
128171
case 3:
129-
var urlParams = new URLSearchParams(window.location.search);
130-
urlParams.set('preview_chart', Date.now());
131-
window.location.hash = "#step-3";
132-
window.location.search = urlParams;
172+
if ( isLivePreview ) {
173+
$('#smartwizard').smartWizard('next');
174+
} else {
175+
var urlParams = new URLSearchParams(window.location.search);
176+
urlParams.set('preview_chart', Date.now());
177+
window.location.hash = "#step-3";
178+
window.location.search = urlParams;
179+
}
133180
break;
134181
case 4:
135182
$('#step-4').find('.spinner').addClass('is-active');
@@ -181,7 +228,11 @@ jQuery(function ($) {
181228
},
182229
function (res) {
183230
if (res.status > 0) {
184-
$("#smartwizard").smartWizard("next");
231+
if ( isLivePreview ) {
232+
goToDraftPage();
233+
} else {
234+
$("#smartwizard").smartWizard("next");
235+
}
185236
}
186237
_this.next(".spinner").removeClass("is-active");
187238
}
@@ -235,21 +286,7 @@ jQuery(function ($) {
235286
var currentStep = $( '.vz-wizard-wrap .tab-pane:last-child' );
236287
currentStep.find(".spinner").addClass("is-active");
237288

238-
$.post(visualizerSetupWizardData.ajax.url, postData, function (res) {
239-
$('.redirect-popup').find('h3.popup-title').html(res.message);
240-
$('.redirect-popup').show();
241-
if (1 === res.status) {
242-
setTimeout(function () {
243-
window.location.href = res.redirect_to;
244-
}, 5000);
245-
} else {
246-
$('.redirect-popup').hide();
247-
}
248-
currentStep.find('.spinner').removeClass('is-active');
249-
}).fail(function () {
250-
$('.redirect-popup').hide();
251-
currentStep.find('.spinner').removeClass('is-active');
252-
});
289+
goToDraftPage( postData );
253290
e.preventDefault();
254291
});
255292

0 commit comments

Comments
 (0)