| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Task_List\Application\Tasks; |
| 5 |
|
| 6 |
use WP_Comment; |
| 7 |
use WP_Post; |
| 8 |
use Yoast\WP\SEO\Task_List\Domain\Components\Call_To_Action_Entry; |
| 9 |
use Yoast\WP\SEO\Task_List\Domain\Components\Copy_Set; |
| 10 |
use Yoast\WP\SEO\Task_List\Domain\Exceptions\Complete_Hello_World_Task_Exception; |
| 11 |
use Yoast\WP\SEO\Task_List\Domain\Tasks\Abstract_Task; |
| 12 |
use Yoast\WP\SEO\Task_List\Domain\Tasks\Completeable_Task_Interface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents the task for deleting the Hello World post. |
| 16 |
*/ |
| 17 |
class Delete_Hello_World extends Abstract_Task implements Completeable_Task_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* Holds the id. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $id = 'delete-hello-world'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the priority. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $priority = 'medium'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Holds the duration. |
| 35 |
* |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
protected $duration = 1; |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns whether this task is completed. |
| 42 |
* |
| 43 |
* @return bool Whether this task is completed. |
| 44 |
*/ |
| 45 |
public function get_is_completed(): bool { |
| 46 |
$post = \get_post( 1 ); |
| 47 |
if ( $post instanceof WP_Post === false || $post->post_status !== 'publish' ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
// Check if this is the actual Hello World post by checking the first comment. |
| 52 |
$comments = \get_comments( |
| 53 |
[ |
| 54 |
'post_id' => 1, |
| 55 |
'number' => 1, |
| 56 |
'order' => 'ASC', |
| 57 |
], |
| 58 |
); |
| 59 |
|
| 60 |
if ( empty( $comments ) || \is_a( $comments[0], WP_Comment::class ) === false || $comments[0]->comment_author_email !== 'wapuu@wordpress.example' ) { |
| 61 |
// Not the Hello World post, so consider task completed. |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
return $post->post_date !== $post->post_modified; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Returns the task's link. |
| 70 |
* |
| 71 |
* @return string|null |
| 72 |
*/ |
| 73 |
public function get_link(): ?string { |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Completes a task. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
* |
| 82 |
* @throws Complete_Hello_World_Task_Exception If the Hello World post could not be deleted. |
| 83 |
*/ |
| 84 |
public function complete_task(): void { |
| 85 |
$post = \get_post( 1 ); |
| 86 |
|
| 87 |
if ( $post instanceof WP_Post ) { |
| 88 |
$result = \wp_delete_post( $post->ID ); |
| 89 |
|
| 90 |
if ( ! $result ) { |
| 91 |
throw new Complete_Hello_World_Task_Exception(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Returns the task's call to action entry. |
| 98 |
* |
| 99 |
* @return Call_To_Action_Entry|null |
| 100 |
*/ |
| 101 |
public function get_call_to_action(): ?Call_To_Action_Entry { |
| 102 |
return new Call_To_Action_Entry( |
| 103 |
\__( 'Delete for me', 'wordpress-seo' ), |
| 104 |
'delete', |
| 105 |
$this->get_link(), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the task's copy set. |
| 111 |
* |
| 112 |
* @return string|null |
| 113 |
*/ |
| 114 |
public function get_copy_set(): Copy_Set { |
| 115 |
return new Copy_Set( |
| 116 |
\__( 'Remove the “Hello World” post', 'wordpress-seo' ), |
| 117 |
'<p>' . \__( 'Leaving placeholder content makes your site look unfinished and untrustworthy. Removing it keeps your site clean and professional for visitors and search engines.', 'wordpress-seo' ) . '</p>', |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|