|
| 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