| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded |
| 5 |
namespace Yoast\WP\SEO\Task_List\Application\Tasks\Child_Tasks; |
| 6 |
|
| 7 |
use Yoast\WP\SEO\Task_List\Domain\Components\Copy_Set; |
| 8 |
use Yoast\WP\SEO\Task_List\Domain\Data\Content_Item_Data_Interface; |
| 9 |
use Yoast\WP\SEO\Task_List\Domain\Exceptions\Incorrect_Child_Trait_Usage_Exception; |
| 10 |
use Yoast\WP\SEO\Task_List\Domain\Tasks\Abstract_Child_Task; |
| 11 |
use Yoast\WP\SEO\Task_List\Domain\Tasks\Parent_Task_Interface; |
| 12 |
|
| 13 |
/** |
| 14 |
* Trait for shared child task functionality. |
| 15 |
* Provides implementations common to all child tasks that use a Content_Item_Data_Interface. |
| 16 |
*/ |
| 17 |
trait Child_Task_Trait { |
| 18 |
|
| 19 |
/** |
| 20 |
* The content item data. |
| 21 |
* |
| 22 |
* @var Content_Item_Data_Interface |
| 23 |
*/ |
| 24 |
protected $content_item_data; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructs the task. |
| 28 |
* |
| 29 |
* @param Parent_Task_Interface $parent_task The parent task. |
| 30 |
* @param Content_Item_Data_Interface $content_item_data The content item data. |
| 31 |
* |
| 32 |
* @throws Incorrect_Child_Trait_Usage_Exception If the class using this trait is not an Abstract_Child_Task. |
| 33 |
*/ |
| 34 |
public function __construct( |
| 35 |
Parent_Task_Interface $parent_task, |
| 36 |
Content_Item_Data_Interface $content_item_data |
| 37 |
) { |
| 38 |
if ( ! $this instanceof Abstract_Child_Task ) { |
| 39 |
throw new Incorrect_Child_Trait_Usage_Exception(); |
| 40 |
} |
| 41 |
|
| 42 |
$this->parent_task = $parent_task; |
| 43 |
$this->content_item_data = $content_item_data; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns the task ID. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_id(): string { |
| 52 |
return $this->parent_task->get_id() . '-' . $this->content_item_data->get_content_id(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the task's link. |
| 57 |
* |
| 58 |
* @return string|null |
| 59 |
*/ |
| 60 |
public function get_link(): ?string { |
| 61 |
return \get_edit_post_link( $this->content_item_data->get_content_id(), '&' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the task's copy set. |
| 66 |
* |
| 67 |
* @return Copy_Set |
| 68 |
*/ |
| 69 |
public function get_copy_set(): Copy_Set { |
| 70 |
$title = \html_entity_decode( $this->content_item_data->get_title(), \ENT_QUOTES, 'UTF-8' ); |
| 71 |
if ( $title === '' ) { |
| 72 |
// Mirror WordPress' own untitled-post convention so screen readers have text content. |
| 73 |
$title = \__( '(no title)', 'wordpress-seo' ); |
| 74 |
} |
| 75 |
|
| 76 |
return new Copy_Set( |
| 77 |
$title, |
| 78 |
$this->parent_task->get_copy_set()->get_about(), |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|