Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 11 additions & 34 deletions src/Integration/YoastLlmsTxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@ class YoastLlmsTxt {
public function register(): void {
// Wrap each trigger that causes Yoast to (re)generate the llms.txt file.
// Priority 9 = before Yoast's priority 10, priority 11 = after.

// 1. Feature toggle (enable/disable llms.txt in Yoast settings).
add_action( 'update_option_wpseo', [ $this, 'start' ], 9 );
add_action( 'update_option_wpseo', [ $this, 'stop' ], 11 );

// 2. llms.txt page selection settings change.
add_action( 'update_option_wpseo_llmstxt', [ $this, 'start' ], 9 );
add_action( 'update_option_wpseo_llmstxt', [ $this, 'stop' ], 11 );

// 3. Weekly cron regeneration.
add_action( 'wpseo_llms_txt_population', [ $this, 'start' ], 9 );
add_action( 'wpseo_llms_txt_population', [ $this, 'stop' ], 11 );
foreach ( [ 'update_option_wpseo', 'update_option_wpseo_llmstxt', 'wpseo_llms_txt_population' ] as $action ) {
add_action( $action, [ $this, 'start' ], 9, 0 );
add_action( $action, [ $this, 'stop' ], 11, 0 );
}
}

/**
Expand All @@ -65,7 +57,7 @@ public function start(): void {

// from_post() fallback path: uses get_permalink() directly.
add_filter( 'post_link', [ $this, 'rewrite_post_link' ], 10, 2 );
add_filter( 'page_link', [ $this, 'rewrite_page_link' ], 10, 2 );
add_filter( 'page_link', [ $this, 'rewrite_post_link' ], 10, 2 );
add_filter( 'post_type_link', [ $this, 'rewrite_post_link' ], 10, 2 );
}

Expand All @@ -82,7 +74,7 @@ public function stop(): void {

remove_filter( 'wpseo_canonical', [ $this, 'rewrite_canonical' ], 10 );
remove_filter( 'post_link', [ $this, 'rewrite_post_link' ], 10 );
remove_filter( 'page_link', [ $this, 'rewrite_page_link' ], 10 );
remove_filter( 'page_link', [ $this, 'rewrite_post_link' ], 10 );
remove_filter( 'post_type_link', [ $this, 'rewrite_post_link' ], 10 );
}

Expand Down Expand Up @@ -110,12 +102,13 @@ public function rewrite_canonical( $canonical, $presentation ) {
}

/**
* Rewrite post/CPT permalink to .md.
* Rewrite post/page/CPT permalink to .md.
*
* Works for both post_link and post_type_link filters.
* Handles post_link, page_link, and post_type_link filters.
* get_post_type() accepts both a WP_Post object and an integer post ID.
*
* @param string $url The permalink.
* @param \WP_Post $post The post object.
* @param string $url The permalink.
* @param \WP_Post|int $post The post object or post ID.
* @return string
*/
public function rewrite_post_link( $url, $post ): string {
Expand All @@ -126,20 +119,4 @@ public function rewrite_post_link( $url, $post ): string {

return rtrim( $url, '/' ) . '.md';
}

/**
* Rewrite page permalink to .md.
*
* @param string $url The page URL.
* @param int $post_id The page ID.
* @return string
*/
public function rewrite_page_link( $url, $post_id ): string {
$post_type = get_post_type( $post_id );
if ( ! $post_type || ! PostTypeSupport::is_supported( $post_type ) ) {
return $url;
}

return rtrim( $url, '/' ) . '.md';
}
}