Skip to content

Commit 486ee5a

Browse files
authored
Merge pull request #690 from ProgressPlanner/filip/v19/fix-interactive-tasks-on-wp-dashboard
Fix missing JS global variable on WP Dashboard page (again)
2 parents 696d80c + 789efb4 commit 486ee5a

2 files changed

Lines changed: 22 additions & 30 deletions

File tree

assets/js/web-components/prpl-install-plugin.js

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global customElements, HTMLElement, prplL10n, progressPlannerAjaxRequest, prplSuggestedTask */
1+
/* global customElements, HTMLElement, prplL10n, progressPlanner, progressPlannerAjaxRequest, prplSuggestedTask */
22
/*
33
* Install Plugin
44
*
@@ -19,7 +19,7 @@ customElements.define(
1919
action,
2020
providerId,
2121
className = 'prpl-button-link',
22-
completeTask = 'true' // String on purpose, since element attributes are always strings.
22+
completeTaskAttr = 'true' // String on purpose, since element attributes are always strings.
2323
) {
2424
// Get parent class properties
2525
super();
@@ -30,8 +30,8 @@ customElements.define(
3030
pluginName ?? this.getAttribute( 'data-plugin-name' );
3131
this.pluginName = this.pluginName ?? this.pluginSlug;
3232
this.action = action ?? this.getAttribute( 'data-action' );
33-
this.completeTask =
34-
completeTask ?? this.getAttribute( 'data-complete-task' );
33+
this.completeTaskAttr =
34+
completeTaskAttr ?? this.getAttribute( 'data-complete-task' );
3535
this.providerId =
3636
providerId ?? this.getAttribute( 'data-provider-id' );
3737
this.className = className ?? this.getAttribute( 'class' );
@@ -41,7 +41,7 @@ customElements.define(
4141
}
4242

4343
// Convert the string to a boolean.
44-
this.completeTask = 'true' === this.completeTask;
44+
this.completeTaskAttr = 'true' === this.completeTaskAttr;
4545

4646
// Set the inner HTML.
4747
this.innerHTML = `
@@ -87,12 +87,12 @@ customElements.define(
8787
`;
8888

8989
progressPlannerAjaxRequest( {
90-
url: this.getAjaxUrl(),
90+
url: progressPlanner.ajaxUrl,
9191
data: {
9292
action: 'progress_planner_install_plugin',
9393
plugin_slug: this.pluginSlug,
9494
plugin_name: this.pluginName,
95-
nonce: this.getNonce(),
95+
nonce: progressPlanner.nonce,
9696
},
9797
} )
9898
.then( () => thisObj.activatePlugin() )
@@ -108,19 +108,19 @@ customElements.define(
108108
`;
109109

110110
progressPlannerAjaxRequest( {
111-
url: this.getAjaxUrl(),
111+
url: progressPlanner.ajaxUrl,
112112
data: {
113113
action: 'progress_planner_activate_plugin',
114114
plugin_slug: thisObj.pluginSlug,
115115
plugin_name: thisObj.pluginName,
116-
nonce: this.getNonce(),
116+
nonce: progressPlanner.nonce,
117117
},
118118
} )
119119
.then( () => {
120120
button.innerHTML = prplL10n( 'activated' );
121121

122122
// Complete the task if the completeTask attribute is set to true.
123-
if ( true === thisObj.completeTask ) {
123+
if ( true === thisObj.completeTaskAttr ) {
124124
thisObj.completeTask();
125125
}
126126
} )
@@ -151,25 +151,5 @@ customElements.define(
151151
}
152152
} );
153153
}
154-
155-
/**
156-
* Get the AJAX URL.
157-
*
158-
* @return {string} The AJAX URL.
159-
*/
160-
getAjaxUrl() {
161-
return window.progressPlanner?.ajaxUrl ?? window.ajaxurl;
162-
}
163-
164-
/**
165-
* Get the nonce.
166-
*
167-
* @return {string} The nonce.
168-
*/
169-
getNonce() {
170-
return (
171-
window.progressPlanner?.nonce ?? window.prplSuggestedTask?.nonce
172-
);
173-
}
174154
}
175155
);

classes/admin/class-dashboard-widget-score.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ public function render_widget() {
5050

5151
\progress_planner()->get_admin__enqueue()->enqueue_script( 'external-link-accessibility-helper' );
5252

53+
// Majoriry of the tasks are now interactive, we need a global object to handle the AJAX requests.
54+
\progress_planner()->get_admin__enqueue()->enqueue_script(
55+
'recommendations/interactive-task',
56+
[
57+
'name' => 'progressPlanner',
58+
'data' => [
59+
'ajaxUrl' => \admin_url( 'admin-ajax.php' ),
60+
'nonce' => \wp_create_nonce( 'progress_planner' ),
61+
],
62+
]
63+
);
64+
5365
\progress_planner()->the_view( "dashboard-widgets/{$this->id}.php" );
5466
}
5567

0 commit comments

Comments
 (0)