Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions assets/js/web-components/prpl-install-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ customElements.define(
pluginName,
action,
providerId,
className = 'prpl-button-link'
className = 'prpl-button-link',
completeTask = 'true' // String on purpose, since element attributes are always strings.
) {
// Get parent class properties
super();
Expand All @@ -29,6 +30,8 @@ customElements.define(
pluginName ?? this.getAttribute( 'data-plugin-name' );
this.pluginName = this.pluginName ?? this.pluginSlug;
this.action = action ?? this.getAttribute( 'data-action' );
this.completeTask =
completeTask ?? this.getAttribute( 'data-complete-task' );
this.providerId =
providerId ?? this.getAttribute( 'data-provider-id' );
this.className = className ?? this.getAttribute( 'class' );
Expand All @@ -37,6 +40,9 @@ customElements.define(
return;
}

// Convert the string to a boolean.
this.completeTask = 'true' === this.completeTask;

// Set the inner HTML.
this.innerHTML = `
<button type="button" class="${ this.className }">
Expand Down Expand Up @@ -112,7 +118,11 @@ customElements.define(
} )
.then( () => {
button.innerHTML = prplL10n( 'activated' );
thisObj.completeTask();

// Complete the task if the completeTask attribute is set to true.
if ( true === thisObj.completeTask ) {
thisObj.completeTask();
}
} )
.catch( ( error ) => console.error( error ) );
}
Expand Down
81 changes: 81 additions & 0 deletions assets/js/web-components/prpl-task-improve-pdf-handling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* global customElements, PrplInteractiveTask */
/*
* Web Component: prpl-email-test-popup
*
* A web component that displays a gauge.
*
* Dependencies: progress-planner/web-components/prpl-interactive-task, progress-planner/web-components/prpl-install-plugin
*/
/**
* Register the custom web component.
*/
customElements.define(
'prpl-improve-pdf-handling-popup',
class extends PrplInteractiveTask {
// eslint-disable-next-line no-useless-constructor
constructor() {
// Get parent class properties
super();

// First step.
this.firstStep = this.querySelector(
'#prpl-improve-pdf-handling-first-step'
);
}

/**
* Runs when the popover is added to the DOM.
*/
popoverAddedToDOM() {
super.popoverAddedToDOM();
}

/**
* Hide all steps.
*/
hideAllSteps() {
this.querySelectorAll( '.prpl-task-step' ).forEach( ( step ) => {
step.style.display = 'none';
} );
}

/**
* Show the form (first step).
*/
showFirstStep() {
this.hideAllSteps();

this.firstStep.style.display = 'flex';
}

/**
* Show the PDF XML Sitemap step.
*/
showPdfXmlSitemapStep() {
this.hideAllSteps();

this.querySelector(
'#prpl-improve-pdf-handling-pdf-xml-sitemap-step'
).style.display = 'flex';
}

/**
* Show final success message.
*/
showSuccess() {
this.hideAllSteps();

this.querySelector(
'#prpl-improve-pdf-handling-success-step'
).style.display = 'flex';
}

/**
* Popover closing, reset the layout, values, etc.
*/
popoverClosing() {
// Hide all steps and show the first step.
this.showFirstStep();
}
}
);
2 changes: 2 additions & 0 deletions classes/suggested-tasks/class-tasks-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Progress_Planner\Suggested_Tasks\Providers\Select_Timezone;
use Progress_Planner\Suggested_Tasks\Providers\Set_Date_Format;
use Progress_Planner\Suggested_Tasks\Providers\SEO_Plugin;
use Progress_Planner\Suggested_Tasks\Providers\Improve_Pdf_Handling;

/**
* Tasks_Manager class.
Expand Down Expand Up @@ -85,6 +86,7 @@ public function __construct() {
new Select_Timezone(),
new Set_Date_Format(),
new SEO_Plugin(),
new Improve_Pdf_Handling(),
];

// Add the plugin integration.
Expand Down
212 changes: 212 additions & 0 deletions classes/suggested-tasks/providers/class-improve-pdf-handling.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php
/**
* Add task for Email sending.
*
* @package Progress_Planner
*/

namespace Progress_Planner\Suggested_Tasks\Providers;

/**
* Add task for Email sending.
*/
class Improve_Pdf_Handling extends Tasks_Interactive {

/**
* Whether the task is an onboarding task.
*
* @var bool
*/
protected const IS_ONBOARDING_TASK = false;


/**
* The minimum number of PDF files.
*
* @var int
*/
protected const MIN_PDF_FILES = 10;

/**
* The provider ID.
*
* @var string
*/
const PROVIDER_ID = 'improve-pdf-handling';

/**
* The provider type.
*
* @var string
*/
const CATEGORY = 'configuration';

/**
* The popover ID.
*
* @var string
*/
const POPOVER_ID = 'improve-pdf-handling';

/**
* The external link URL.
*
* @var string
*/
protected const EXTERNAL_LINK_URL = 'https://prpl.fyi/improve-pdf-handling';

/**
* Whether the task is dismissable.
*
* @var bool
*/
protected $is_dismissable = true;

/**
* The task priority.
*
* @var int
*/
protected $priority = 1;

/**
* Initialize the task provider.
*
* @return void
*/
public function init() {
// Enqueue the scripts.
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}

/**
* We want task to be added always.
*
* @return bool
*/
public function should_add_task() {
$pdf_files_count = \progress_planner()->get_utils__cache()->get( 'pdf_files_count' );
if ( false === $pdf_files_count ) {
// Detect if there are more than 10 PDF files.
$query = new \WP_Query(
[
'post_type' => 'attachment',
'post_mime_type' => 'application/pdf',
'post_status' => 'publish',
'posts_per_page' => static::MIN_PDF_FILES + 1, // We want to get at least 11 PDF files to be sure we have enough.
'fields' => 'ids',
]
);
$pdf_files_count = $query->found_posts;
\progress_planner()->get_utils__cache()->set( 'pdf_files_count', $pdf_files_count, DAY_IN_SECONDS );
}

return static::MIN_PDF_FILES < $pdf_files_count;
}

/**
* Task should be completed only manually by the user.
*
* @param string $task_id The task ID.
*
* @return bool
*/
public function is_task_completed( $task_id = '' ) {
return false;
}

/**
* Task should be completed only manually by the user.
*
* @param string $task_id The task ID.
*
* @return bool|string
*/
public function evaluate_task( $task_id ) {
return false;
}

/**
* Get the title.
*
* @return string
*/
protected function get_title() {
return \esc_html__( 'Improve PDF handling', 'progress-planner' );
}

/**
* Get the description.
*
* @param array $task_data Optional data to include in the task.
* @return string
*/
protected function get_description( $task_data = [] ) {
return \esc_html__( 'Your site seems to have quite a few PDF files, we can improve the way your site handles them.', 'progress-planner' );
}

/**
* Enqueue the scripts.
*
* @param string $hook The current admin page.
*
* @return void
*/
public function enqueue_scripts( $hook ) {
// Enqueue the script only on Progress Planner and WP dashboard pages.
if ( 'toplevel_page_progress-planner' !== $hook && 'index.php' !== $hook ) {
return;
}

// Don't enqueue the script if the task is already completed.
if ( true === \progress_planner()->get_suggested_tasks()->was_task_completed( $this->get_task_id() ) ) {
return;
}

// Enqueue the web component.
\progress_planner()->get_admin__enqueue()->enqueue_script(
'progress-planner/web-components/prpl-task-' . $this->get_provider_id(),
);
}

/**
* The popover content.
*
* @return void
*/
public function the_popover_content() {
\progress_planner()->the_view(
'popovers/improve-pdf-handling.php',
[
'prpl_popover_id' => static::POPOVER_ID,
'prpl_provider_id' => $this->get_provider_id(),
]
);
}

/**
* Print the popover form contents.
*
* @return void
*/
public function print_popover_form_contents() {
// The form is handled in the popovers/email-sending view.
}

/**
* Add task actions specific to this task.
*
* @param array $data The task data.
* @param array $actions The existing actions.
*
* @return array
*/
public function add_task_actions( $data = [], $actions = [] ) {
$actions[] = [
'priority' => 10,
'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>',
];

return $actions;
}
}
Loading
Loading