Skip to content

Commit 8e59894

Browse files
committed
tweaks & coding improvements
1 parent cd97d65 commit 8e59894

5 files changed

Lines changed: 65 additions & 109 deletions

File tree

classes/suggested-tasks/providers/class-content-review.php

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,23 @@ class Content_Review extends Tasks {
7575
/**
7676
* The snoozed post IDs.
7777
*
78-
* @var array|null
78+
* @var array
7979
*/
80-
protected $snoozed_post_ids = null;
80+
protected $snoozed_post_ids = [];
8181

8282
/**
8383
* The dismissed post IDs.
8484
*
85-
* @var array|null
85+
* @var array
8686
*/
87-
protected $dismissed_post_ids = null;
87+
protected $dismissed_post_ids = [];
8888

8989
/**
9090
* The post to update IDs.
9191
*
92-
* @var array|null
92+
* @var array
9393
*/
94-
protected $task_post_mappings = null;
94+
protected $task_post_mappings = [];
9595

9696
/**
9797
* The include post types.
@@ -209,14 +209,11 @@ protected function get_url_with_data( $task_data = [] ) {
209209
* @return bool
210210
*/
211211
public function should_add_task() {
212-
if ( null !== $this->task_post_mappings ) {
213-
return 0 < \count( $this->task_post_mappings );
212+
if ( ! empty( $this->task_post_mappings ) ) {
213+
return true;
214214
}
215215

216-
$this->task_post_mappings = [];
217-
218-
$number_of_posts_to_inject = static::ITEMS_TO_INJECT;
219-
$last_updated_posts = [];
216+
$last_updated_posts = [];
220217

221218
// Check if there are any important pages to update.
222219
$important_page_ids = [];
@@ -255,9 +252,7 @@ public function should_add_task() {
255252
}
256253

257254
// Lets check for other posts to update.
258-
$number_of_posts_to_inject = $number_of_posts_to_inject - \count( $last_updated_posts );
259-
260-
if ( 0 < $number_of_posts_to_inject ) {
255+
if ( 0 < static::ITEMS_TO_INJECT - \count( $last_updated_posts ) ) {
261256
// Get the post that was updated last.
262257
$last_updated_posts = \array_merge(
263258
$last_updated_posts,
@@ -299,7 +294,7 @@ public function should_add_task() {
299294
];
300295
}
301296

302-
return 0 < \count( $this->task_post_mappings );
297+
return ! empty( $this->task_post_mappings );
303298
}
304299

305300
/**
@@ -313,28 +308,26 @@ public function get_tasks_to_inject() {
313308
}
314309

315310
$task_to_inject = [];
316-
if ( ! empty( $this->task_post_mappings ) ) {
317-
foreach ( $this->task_post_mappings as $task_data ) {
318-
if ( true === \progress_planner()->get_suggested_tasks()->was_task_completed( $task_data['task_id'] ) ) {
319-
continue;
320-
}
321-
322-
$task_to_inject[] = [
323-
'task_id' => $this->get_task_id( [ 'target_post_id' => $task_data['target_post_id'] ] ),
324-
'provider_id' => $this->get_provider_id(),
325-
'category' => $this->get_provider_category(),
326-
'target_post_id' => $task_data['target_post_id'],
327-
'target_post_type' => $task_data['target_post_type'],
328-
'date' => \gmdate( 'YW' ),
329-
'post_title' => $this->get_title_with_data( $task_data ),
330-
'description' => $this->get_description_with_data( $task_data ),
331-
'url' => $this->get_url_with_data( $task_data ),
332-
'url_target' => $this->get_url_target(),
333-
'dismissable' => $this->is_dismissable(),
334-
'snoozable' => $this->is_snoozable,
335-
'points' => $this->get_points(),
336-
];
311+
foreach ( $this->task_post_mappings as $task_data ) {
312+
if ( true === \progress_planner()->get_suggested_tasks()->was_task_completed( $task_data['task_id'] ) ) {
313+
continue;
337314
}
315+
316+
$task_to_inject[] = [
317+
'task_id' => $this->get_task_id( [ 'target_post_id' => $task_data['target_post_id'] ] ),
318+
'provider_id' => $this->get_provider_id(),
319+
'category' => $this->get_provider_category(),
320+
'target_post_id' => $task_data['target_post_id'],
321+
'target_post_type' => $task_data['target_post_type'],
322+
'date' => \gmdate( 'YW' ),
323+
'post_title' => $this->get_title_with_data( $task_data ),
324+
'description' => $this->get_description_with_data( $task_data ),
325+
'url' => $this->get_url_with_data( $task_data ),
326+
'url_target' => $this->get_url_target(),
327+
'dismissable' => $this->is_dismissable(),
328+
'snoozable' => $this->is_snoozable,
329+
'points' => $this->get_points(),
330+
];
338331
}
339332

340333
$added_tasks = [];
@@ -473,23 +466,20 @@ public function filter_update_posts_args( $args ) {
473466
* @return array
474467
*/
475468
protected function get_snoozed_post_ids() {
476-
if ( null !== $this->snoozed_post_ids ) {
469+
if ( ! empty( $this->snoozed_post_ids ) ) {
477470
return $this->snoozed_post_ids;
478471
}
479472

480-
$this->snoozed_post_ids = [];
481-
$snoozed = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'future' ] );
482-
483-
if ( ! empty( $snoozed ) ) {
484-
foreach ( $snoozed as $task ) {
485-
/**
486-
* The task object.
487-
*
488-
* @var \Progress_Planner\Suggested_Tasks\Task $task
489-
*/
490-
if ( isset( $task->provider->slug ) && 'review-post' === $task->provider->slug ) {
491-
$this->snoozed_post_ids[] = $task->target_post_id;
492-
}
473+
$snoozed = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'future' ] );
474+
475+
foreach ( $snoozed as $task ) {
476+
/**
477+
* The task object.
478+
*
479+
* @var \Progress_Planner\Suggested_Tasks\Task $task
480+
*/
481+
if ( isset( $task->provider->slug ) && 'review-post' === $task->provider->slug ) {
482+
$this->snoozed_post_ids[] = $task->target_post_id;
493483
}
494484
}
495485

@@ -502,12 +492,11 @@ protected function get_snoozed_post_ids() {
502492
* @return array
503493
*/
504494
protected function get_dismissed_post_ids() {
505-
if ( null !== $this->dismissed_post_ids ) {
495+
if ( ! empty( $this->dismissed_post_ids ) ) {
506496
return $this->dismissed_post_ids;
507497
}
508498

509-
$this->dismissed_post_ids = [];
510-
$dismissed = $this->get_dismissed_tasks();
499+
$dismissed = $this->get_dismissed_tasks();
511500

512501
if ( ! empty( $dismissed ) ) {
513502
$this->dismissed_post_ids = \array_values( \wp_list_pluck( $dismissed, 'post_id' ) );

classes/suggested-tasks/providers/class-core-update.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ protected function get_description() {
9797
* @return array
9898
*/
9999
public function add_core_update_link( $update_actions ) {
100-
$pending_tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'publish' ] );
101-
102100
// All updates are completed and there is a 'update-core' task in the published tasks.
103-
if ( $pending_tasks && $this->is_task_completed() ) {
104-
foreach ( $pending_tasks as $task ) {
101+
if ( $this->is_task_completed() ) {
102+
foreach ( \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'publish' ] ) as $task ) {
105103
if ( $this->get_task_id() === $task->task_id ) {
106104
$update_actions['prpl_core_update'] =
107105
'<img src="' . \esc_attr( \constant( 'PROGRESS_PLANNER_URL' ) . '/assets/images/icon_progress_planner.svg' ) . '" style="width:1rem;padding-left:0.25rem;padding-right:0.25rem;vertical-align:middle;" alt="Progress Planner" />' .

classes/suggested-tasks/providers/class-remove-terms-without-posts.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,7 @@ public function init() {
112112
* @return void
113113
*/
114114
public function maybe_remove_irrelevant_tasks( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
115-
$pending_tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] );
116-
117-
if ( ! $pending_tasks ) {
118-
return;
119-
}
120-
121-
foreach ( $pending_tasks as $task ) {
115+
foreach ( \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] ) as $task ) {
122116
/**
123117
* The task post object.
124118
*
@@ -298,13 +292,10 @@ protected function get_completed_term_ids() {
298292
}
299293

300294
$this->completed_term_ids = [];
301-
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] );
302295

303-
if ( ! empty( $tasks ) ) {
304-
foreach ( $tasks as $task ) {
305-
if ( 'trash' === $task->post_status ) {
306-
$this->completed_term_ids[] = $task->target_term_id;
307-
}
296+
foreach ( \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] ) as $task ) {
297+
if ( 'trash' === $task->post_status ) {
298+
$this->completed_term_ids[] = $task->target_term_id;
308299
}
309300
}
310301

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,14 @@ public function get_task_id( $task_data = [] ) {
255255
$parts = [ $this->get_provider_id() ];
256256

257257
// Order is important here, new parameters should be added at the end.
258-
if ( isset( $task_data['target_post_id'] ) ) {
259-
$parts[] = $task_data['target_post_id'];
260-
}
261-
262-
if ( isset( $task_data['target_term_id'] ) ) {
263-
$parts[] = $task_data['target_term_id'];
264-
}
265-
266-
if ( isset( $task_data['target_taxonomy'] ) ) {
267-
$parts[] = $task_data['target_taxonomy'];
268-
}
269-
258+
$parts[] = $task_data['target_post_id'] ?? false;
259+
$parts[] = $task_data['target_term_id'] ?? false;
260+
$parts[] = $task_data['target_taxonomy'] ?? false;
270261
// If the task is repetitive, add the date as the last part.
271-
if ( $this->is_repetitive() ) {
272-
$parts[] = \gmdate( 'YW' );
273-
}
262+
$parts[] = $this->is_repetitive() ? \gmdate( 'YW' ) : false;
263+
264+
// Remove empty parts.
265+
$parts = \array_filter( $parts );
274266

275267
return \implode( '-', $parts );
276268
}
@@ -354,12 +346,7 @@ public function is_onboarding_task() {
354346
* @return bool
355347
*/
356348
public function is_task_snoozed() {
357-
$snoozed = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'future' ] );
358-
if ( empty( $snoozed ) ) {
359-
return false;
360-
}
361-
362-
foreach ( $snoozed as $task ) {
349+
foreach ( \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'post_status' => 'future' ] ) as $task ) {
363350
$task = \progress_planner()->get_suggested_tasks_db()->get_post( $task->task_id );
364351
$provider_id = $task ? $task->get_provider_id() : '';
365352

@@ -472,23 +459,20 @@ protected function check_task_condition() {
472459
* @return array
473460
*/
474461
public function get_tasks_to_inject() {
475-
$task_id = $this->get_task_id();
476-
477462
if (
478463
true === $this->is_task_snoozed() ||
479464
! $this->should_add_task() || // No need to add the task.
480-
true === \progress_planner()->get_suggested_tasks()->was_task_completed( $task_id )
465+
true === \progress_planner()->get_suggested_tasks()->was_task_completed( $this->get_task_id() )
481466
) {
482467
return [];
483468
}
484469

485470
$task_data = $this->modify_injection_task_data( $this->get_task_details() );
486471

487-
// Get the task post.
488-
$task_post = \progress_planner()->get_suggested_tasks_db()->get_post( $task_data['task_id'] );
489-
490472
// Skip the task if it was already injected.
491-
return $task_post ? [] : [ \progress_planner()->get_suggested_tasks_db()->add( $task_data ) ];
473+
return \progress_planner()->get_suggested_tasks_db()->get_post( $task_data['task_id'] )
474+
? []
475+
: [ \progress_planner()->get_suggested_tasks_db()->add( $task_data ) ];
492476
}
493477

494478
/**

classes/suggested-tasks/providers/class-update-term-description.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,8 @@ public function get_tasks_to_inject() {
218218
)
219219
);
220220

221-
// Get the task post.
222-
$task_post = \progress_planner()->get_suggested_tasks_db()->get_post( $task_data['task_id'] );
223-
224221
// Skip the task if it was already injected.
225-
if ( $task_post ) {
222+
if ( \progress_planner()->get_suggested_tasks_db()->get_post( $task_data['task_id'] ) ) {
226223
return [];
227224
}
228225

@@ -282,13 +279,10 @@ protected function get_completed_term_ids() {
282279
}
283280

284281
$this->completed_term_ids = [];
285-
$tasks = \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] );
286282

287-
if ( ! empty( $tasks ) ) {
288-
foreach ( $tasks as $task ) {
289-
if ( 'trash' === $task->post_status ) {
290-
$this->completed_term_ids[] = $task->target_term_id;
291-
}
283+
foreach ( \progress_planner()->get_suggested_tasks_db()->get_tasks_by( [ 'provider_id' => $this->get_provider_id() ] ) as $task ) {
284+
if ( 'trash' === $task->post_status ) {
285+
$this->completed_term_ids[] = $task->target_term_id;
292286
}
293287
}
294288

0 commit comments

Comments
 (0)