From 1814d3da5d798b4db0dc1ed9d518c94471611433 Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Thu, 26 Feb 2026 15:09:00 +0100 Subject: [PATCH 1/3] refactor: simplify YoastLlmsTxt by merging duplicate rewrite methods Removes `rewrite_page_link` which was functionally identical to `rewrite_post_link`. Since `get_post_type()` accepts both a WP_Post object and an integer post ID, a single method handles all three permalink filters (post_link, page_link, post_type_link). Co-Authored-By: Claude Sonnet 4.6 --- src/Integration/YoastLlmsTxt.php | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/Integration/YoastLlmsTxt.php b/src/Integration/YoastLlmsTxt.php index 4116fd7..a2cf8a3 100644 --- a/src/Integration/YoastLlmsTxt.php +++ b/src/Integration/YoastLlmsTxt.php @@ -65,7 +65,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 ); } @@ -82,7 +82,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 ); } @@ -110,12 +110,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 { @@ -126,20 +127,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'; - } } From 8414dc771fdde4c2d8573260859185608b27160a Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Thu, 26 Feb 2026 16:13:38 +0100 Subject: [PATCH 2/3] refactor: collapse register() add_action pairs into a loop Co-Authored-By: Claude Sonnet 4.6 --- src/Integration/YoastLlmsTxt.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Integration/YoastLlmsTxt.php b/src/Integration/YoastLlmsTxt.php index a2cf8a3..67a26c9 100644 --- a/src/Integration/YoastLlmsTxt.php +++ b/src/Integration/YoastLlmsTxt.php @@ -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 ); + add_action( $action, [ $this, 'stop' ], 11 ); + } } /** From b2361742220223bd5355371ee29ea0ed6a96029f Mon Sep 17 00:00:00 2001 From: Joost de Valk Date: Thu, 26 Feb 2026 17:14:36 +0100 Subject: [PATCH 3/3] fix: address Copilot review feedback on PR #29 Set accepted_args to 0 for start()/stop() add_action calls so WordPress does not pass arguments into these parameter-less methods. The update_option_{$option} hook fires with 3 args (old, new, option name), which would cause an ArgumentCountError on PHP 8+ with the default accepted_args of 1. Co-Authored-By: Claude Sonnet 4.6 --- src/Integration/YoastLlmsTxt.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Integration/YoastLlmsTxt.php b/src/Integration/YoastLlmsTxt.php index 67a26c9..c80ea71 100644 --- a/src/Integration/YoastLlmsTxt.php +++ b/src/Integration/YoastLlmsTxt.php @@ -36,8 +36,8 @@ 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. foreach ( [ 'update_option_wpseo', 'update_option_wpseo_llmstxt', 'wpseo_llms_txt_population' ] as $action ) { - add_action( $action, [ $this, 'start' ], 9 ); - add_action( $action, [ $this, 'stop' ], 11 ); + add_action( $action, [ $this, 'start' ], 9, 0 ); + add_action( $action, [ $this, 'stop' ], 11, 0 ); } }