Skip to content

Commit 6e498bb

Browse files
authored
Merge pull request #650 from ProgressPlanner/filip/v19/improve-pdf-handling
New task provider: Improve PDF handling
2 parents a1b98d4 + b9f56fa commit 6e498bb

5 files changed

Lines changed: 416 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ customElements.define(
1818
pluginName,
1919
action,
2020
providerId,
21-
className = 'prpl-button-link'
21+
className = 'prpl-button-link',
22+
completeTask = 'true' // String on purpose, since element attributes are always strings.
2223
) {
2324
// Get parent class properties
2425
super();
@@ -29,6 +30,8 @@ customElements.define(
2930
pluginName ?? this.getAttribute( 'data-plugin-name' );
3031
this.pluginName = this.pluginName ?? this.pluginSlug;
3132
this.action = action ?? this.getAttribute( 'data-action' );
33+
this.completeTask =
34+
completeTask ?? this.getAttribute( 'data-complete-task' );
3235
this.providerId =
3336
providerId ?? this.getAttribute( 'data-provider-id' );
3437
this.className = className ?? this.getAttribute( 'class' );
@@ -37,6 +40,9 @@ customElements.define(
3740
return;
3841
}
3942

43+
// Convert the string to a boolean.
44+
this.completeTask = 'true' === this.completeTask;
45+
4046
// Set the inner HTML.
4147
this.innerHTML = `
4248
<button type="button" class="${ this.className }">
@@ -112,7 +118,11 @@ customElements.define(
112118
} )
113119
.then( () => {
114120
button.innerHTML = prplL10n( 'activated' );
115-
thisObj.completeTask();
121+
122+
// Complete the task if the completeTask attribute is set to true.
123+
if ( true === thisObj.completeTask ) {
124+
thisObj.completeTask();
125+
}
116126
} )
117127
.catch( ( error ) => console.error( error ) );
118128
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* global customElements, PrplInteractiveTask */
2+
/*
3+
* Web Component: prpl-email-test-popup
4+
*
5+
* A web component that displays a gauge.
6+
*
7+
* Dependencies: progress-planner/web-components/prpl-interactive-task, progress-planner/web-components/prpl-install-plugin
8+
*/
9+
/**
10+
* Register the custom web component.
11+
*/
12+
customElements.define(
13+
'prpl-improve-pdf-handling-popup',
14+
class extends PrplInteractiveTask {
15+
// eslint-disable-next-line no-useless-constructor
16+
constructor() {
17+
// Get parent class properties
18+
super();
19+
20+
// First step.
21+
this.firstStep = this.querySelector(
22+
'#prpl-improve-pdf-handling-first-step'
23+
);
24+
}
25+
26+
/**
27+
* Runs when the popover is added to the DOM.
28+
*/
29+
popoverAddedToDOM() {
30+
super.popoverAddedToDOM();
31+
}
32+
33+
/**
34+
* Hide all steps.
35+
*/
36+
hideAllSteps() {
37+
this.querySelectorAll( '.prpl-task-step' ).forEach( ( step ) => {
38+
step.style.display = 'none';
39+
} );
40+
}
41+
42+
/**
43+
* Show the form (first step).
44+
*/
45+
showFirstStep() {
46+
this.hideAllSteps();
47+
48+
this.firstStep.style.display = 'flex';
49+
}
50+
51+
/**
52+
* Show the PDF XML Sitemap step.
53+
*/
54+
showPdfXmlSitemapStep() {
55+
this.hideAllSteps();
56+
57+
this.querySelector(
58+
'#prpl-improve-pdf-handling-pdf-xml-sitemap-step'
59+
).style.display = 'flex';
60+
}
61+
62+
/**
63+
* Show final success message.
64+
*/
65+
showSuccess() {
66+
this.hideAllSteps();
67+
68+
this.querySelector(
69+
'#prpl-improve-pdf-handling-success-step'
70+
).style.display = 'flex';
71+
}
72+
73+
/**
74+
* Popover closing, reset the layout, values, etc.
75+
*/
76+
popoverClosing() {
77+
// Hide all steps and show the first step.
78+
this.showFirstStep();
79+
}
80+
}
81+
);

classes/suggested-tasks/class-tasks-manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Progress_Planner\Suggested_Tasks\Providers\Select_Timezone;
3939
use Progress_Planner\Suggested_Tasks\Providers\Set_Date_Format;
4040
use Progress_Planner\Suggested_Tasks\Providers\SEO_Plugin;
41+
use Progress_Planner\Suggested_Tasks\Providers\Improve_Pdf_Handling;
4142

4243
/**
4344
* Tasks_Manager class.
@@ -85,6 +86,7 @@ public function __construct() {
8586
new Select_Timezone(),
8687
new Set_Date_Format(),
8788
new SEO_Plugin(),
89+
new Improve_Pdf_Handling(),
8890
];
8991

9092
// Add the plugin integration.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* Add task for Email sending.
4+
*
5+
* @package Progress_Planner
6+
*/
7+
8+
namespace Progress_Planner\Suggested_Tasks\Providers;
9+
10+
/**
11+
* Add task for Email sending.
12+
*/
13+
class Improve_Pdf_Handling extends Tasks_Interactive {
14+
15+
/**
16+
* Whether the task is an onboarding task.
17+
*
18+
* @var bool
19+
*/
20+
protected const IS_ONBOARDING_TASK = false;
21+
22+
23+
/**
24+
* The minimum number of PDF files.
25+
*
26+
* @var int
27+
*/
28+
protected const MIN_PDF_FILES = 10;
29+
30+
/**
31+
* The provider ID.
32+
*
33+
* @var string
34+
*/
35+
const PROVIDER_ID = 'improve-pdf-handling';
36+
37+
/**
38+
* The provider type.
39+
*
40+
* @var string
41+
*/
42+
const CATEGORY = 'configuration';
43+
44+
/**
45+
* The popover ID.
46+
*
47+
* @var string
48+
*/
49+
const POPOVER_ID = 'improve-pdf-handling';
50+
51+
/**
52+
* The external link URL.
53+
*
54+
* @var string
55+
*/
56+
protected const EXTERNAL_LINK_URL = 'https://prpl.fyi/improve-pdf-handling';
57+
58+
/**
59+
* Whether the task is dismissable.
60+
*
61+
* @var bool
62+
*/
63+
protected $is_dismissable = true;
64+
65+
/**
66+
* The task priority.
67+
*
68+
* @var int
69+
*/
70+
protected $priority = 1;
71+
72+
/**
73+
* Initialize the task provider.
74+
*
75+
* @return void
76+
*/
77+
public function init() {
78+
// Enqueue the scripts.
79+
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
80+
}
81+
82+
/**
83+
* We want task to be added always.
84+
*
85+
* @return bool
86+
*/
87+
public function should_add_task() {
88+
$pdf_files_count = \progress_planner()->get_utils__cache()->get( 'pdf_files_count' );
89+
if ( false === $pdf_files_count ) {
90+
// Detect if there are more than 10 PDF files.
91+
$query = new \WP_Query(
92+
[
93+
'post_type' => 'attachment',
94+
'post_mime_type' => 'application/pdf',
95+
'post_status' => 'publish',
96+
'posts_per_page' => static::MIN_PDF_FILES + 1, // We want to get at least 11 PDF files to be sure we have enough.
97+
'fields' => 'ids',
98+
]
99+
);
100+
$pdf_files_count = $query->found_posts;
101+
\progress_planner()->get_utils__cache()->set( 'pdf_files_count', $pdf_files_count, DAY_IN_SECONDS );
102+
}
103+
104+
return static::MIN_PDF_FILES < $pdf_files_count;
105+
}
106+
107+
/**
108+
* Task should be completed only manually by the user.
109+
*
110+
* @param string $task_id The task ID.
111+
*
112+
* @return bool
113+
*/
114+
public function is_task_completed( $task_id = '' ) {
115+
return false;
116+
}
117+
118+
/**
119+
* Task should be completed only manually by the user.
120+
*
121+
* @param string $task_id The task ID.
122+
*
123+
* @return bool|string
124+
*/
125+
public function evaluate_task( $task_id ) {
126+
return false;
127+
}
128+
129+
/**
130+
* Get the title.
131+
*
132+
* @return string
133+
*/
134+
protected function get_title() {
135+
return \esc_html__( 'Improve PDF handling', 'progress-planner' );
136+
}
137+
138+
/**
139+
* Get the description.
140+
*
141+
* @param array $task_data Optional data to include in the task.
142+
* @return string
143+
*/
144+
protected function get_description( $task_data = [] ) {
145+
return \esc_html__( 'Your site seems to have quite a few PDF files, we can improve the way your site handles them.', 'progress-planner' );
146+
}
147+
148+
/**
149+
* Enqueue the scripts.
150+
*
151+
* @param string $hook The current admin page.
152+
*
153+
* @return void
154+
*/
155+
public function enqueue_scripts( $hook ) {
156+
// Enqueue the script only on Progress Planner and WP dashboard pages.
157+
if ( 'toplevel_page_progress-planner' !== $hook && 'index.php' !== $hook ) {
158+
return;
159+
}
160+
161+
// Don't enqueue the script if the task is already completed.
162+
if ( true === \progress_planner()->get_suggested_tasks()->was_task_completed( $this->get_task_id() ) ) {
163+
return;
164+
}
165+
166+
// Enqueue the web component.
167+
\progress_planner()->get_admin__enqueue()->enqueue_script(
168+
'progress-planner/web-components/prpl-task-' . $this->get_provider_id(),
169+
);
170+
}
171+
172+
/**
173+
* The popover content.
174+
*
175+
* @return void
176+
*/
177+
public function the_popover_content() {
178+
\progress_planner()->the_view(
179+
'popovers/improve-pdf-handling.php',
180+
[
181+
'prpl_popover_id' => static::POPOVER_ID,
182+
'prpl_provider_id' => $this->get_provider_id(),
183+
]
184+
);
185+
}
186+
187+
/**
188+
* Print the popover form contents.
189+
*
190+
* @return void
191+
*/
192+
public function print_popover_form_contents() {
193+
// The form is handled in the popovers/email-sending view.
194+
}
195+
196+
/**
197+
* Add task actions specific to this task.
198+
*
199+
* @param array $data The task data.
200+
* @param array $actions The existing actions.
201+
*
202+
* @return array
203+
*/
204+
public function add_task_actions( $data = [], $actions = [] ) {
205+
$actions[] = [
206+
'priority' => 10,
207+
'html' => '<a href="#" class="prpl-tooltip-action-text" role="button" onclick="document.getElementById(\'prpl-popover-' . \esc_attr( static::POPOVER_ID ) . '\')?.showPopover()">' . \esc_html__( 'Improve PDF handling', 'progress-planner' ) . '</a>',
208+
];
209+
210+
return $actions;
211+
}
212+
}

0 commit comments

Comments
 (0)