| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Infrastructure\Content; |
| 5 |
|
| 6 |
use WP_Post; |
| 7 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 8 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 9 |
use Yoast\WP\SEO\Llms_Txt\Domain\Content\Post_Collection_Interface; |
| 10 |
use Yoast\WP\SEO\Llms_Txt\Domain\Content_Types\Content_Type_Entry; |
| 11 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 12 |
use Yoast\WP\SEO\Surfaces\Meta_Surface; |
| 13 |
|
| 14 |
/** |
| 15 |
* The class that handles the manual post collection. Based on either indexables or WP_Query. |
| 16 |
*/ |
| 17 |
class Manual_Post_Collection implements Post_Collection_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* The options helper. |
| 21 |
* |
| 22 |
* @var Options_Helper |
| 23 |
*/ |
| 24 |
private $options_helper; |
| 25 |
|
| 26 |
/** |
| 27 |
* The indexable helper. |
| 28 |
* |
| 29 |
* @var Indexable_Helper |
| 30 |
*/ |
| 31 |
private $indexable_helper; |
| 32 |
|
| 33 |
/** |
| 34 |
* The indexable repository. |
| 35 |
* |
| 36 |
* @var Indexable_Repository |
| 37 |
*/ |
| 38 |
private $indexable_repository; |
| 39 |
|
| 40 |
/** |
| 41 |
* The meta surface. |
| 42 |
* |
| 43 |
* @var Meta_Surface |
| 44 |
*/ |
| 45 |
private $meta; |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor. |
| 49 |
* |
| 50 |
* @param Options_Helper $options_helper The options helper. |
| 51 |
* @param Indexable_Helper $indexable_helper The indexable helper. |
| 52 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 53 |
* @param Meta_Surface $meta The meta surface. |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
Options_Helper $options_helper, |
| 57 |
Indexable_Helper $indexable_helper, |
| 58 |
Indexable_Repository $indexable_repository, |
| 59 |
Meta_Surface $meta |
| 60 |
) { |
| 61 |
$this->options_helper = $options_helper; |
| 62 |
$this->indexable_helper = $indexable_helper; |
| 63 |
$this->indexable_repository = $indexable_repository; |
| 64 |
$this->meta = $meta; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* The post method to get all relevant content type entries |
| 69 |
* |
| 70 |
* @return array<int, array<Content_Type_Entry>> The posts that are relevant for the LLMs.txt. |
| 71 |
*/ |
| 72 |
public function get_posts(): array { |
| 73 |
$posts = []; |
| 74 |
$pages = [ |
| 75 |
'about_us_page', |
| 76 |
'contact_page', |
| 77 |
'terms_page', |
| 78 |
'privacy_policy_page', |
| 79 |
'shop_page', |
| 80 |
]; |
| 81 |
|
| 82 |
foreach ( $pages as $page ) { |
| 83 |
$page_id = $this->options_helper->get( $page ); |
| 84 |
if ( ! empty( $page_id ) ) { |
| 85 |
$post = $this->get_content_type_entry( $page_id ); |
| 86 |
|
| 87 |
if ( $post !== null ) { |
| 88 |
$posts[] = $post; |
| 89 |
} |
| 90 |
else { |
| 91 |
$this->options_helper->set( $page, 0 ); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
$other_pages = $this->options_helper->get( 'other_included_pages' ); |
| 96 |
$filtered_pages = []; |
| 97 |
if ( ! empty( $other_pages ) ) { |
| 98 |
foreach ( $other_pages as $page_id ) { |
| 99 |
$post = $this->get_content_type_entry( $page_id ); |
| 100 |
|
| 101 |
if ( $post !== null ) { |
| 102 |
$posts[] = $post; |
| 103 |
$filtered_pages[] = $page_id; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ( \count( $filtered_pages ) !== \count( $other_pages ) ) { |
| 108 |
$this->options_helper->set( 'other_included_pages', $filtered_pages ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return $posts; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Gets the content entries. |
| 117 |
* |
| 118 |
* @param int $page_id The id of the page. |
| 119 |
* |
| 120 |
* @return Content_Type_Entry The content type entry. |
| 121 |
*/ |
| 122 |
public function get_content_type_entry( int $page_id ): ?Content_Type_Entry { |
| 123 |
if ( $this->indexable_helper->should_index_indexables() ) { |
| 124 |
$post = $this->get_content_type_entry_for_indexable( $page_id ); |
| 125 |
} |
| 126 |
else { |
| 127 |
$post = $this->get_content_type_entry_wp_query( $page_id ); |
| 128 |
} |
| 129 |
|
| 130 |
return $post; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Gets the content entries with WP query. |
| 135 |
* |
| 136 |
* @param int $page_id The id of the page. |
| 137 |
* |
| 138 |
* @return Content_Type_Entry The content type entry. |
| 139 |
*/ |
| 140 |
public function get_content_type_entry_wp_query( int $page_id ): ?Content_Type_Entry { |
| 141 |
$page = \get_post( $page_id ); |
| 142 |
|
| 143 |
if ( $page !== null && $page->post_password === '' && $page->post_status === 'publish' ) { |
| 144 |
|
| 145 |
return Content_Type_Entry::from_post( $page, \get_permalink( $page->ID ) ); |
| 146 |
} |
| 147 |
|
| 148 |
return null; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Gets the content entries with indexables. |
| 153 |
* |
| 154 |
* @param int $page_id The id of the page. |
| 155 |
* |
| 156 |
* @return Content_Type_Entry The content type entry. |
| 157 |
*/ |
| 158 |
public function get_content_type_entry_for_indexable( int $page_id ): ?Content_Type_Entry { |
| 159 |
$indexable = $this->indexable_repository->find_by_id_and_type( $page_id, 'post' ); |
| 160 |
if ( $indexable && ( $indexable->is_public === null || $indexable->is_public ) ) { |
| 161 |
$indexable_meta = $this->meta->for_indexable( $indexable ); |
| 162 |
if ( $indexable_meta->post instanceof WP_Post ) { |
| 163 |
return Content_Type_Entry::from_meta( $indexable_meta ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return null; |
| 168 |
} |
| 169 |
} |
| 170 |
|