query_vars['md'] ) ) { return; } $post_id = self::get_current_post_id(); if ( ! $post_id ) { status_header( 404 ); header( 'Content-Type: text/plain; charset=UTF-8' ); echo "# 404 Not Found\n\nNo content found for this URL."; exit; } $settings = ajwa_get_settings(); $enabled_types = $settings['enabled_post_types'] ?? array(); $post = get_post( $post_id ); if ( ! $post || ! in_array( $post->post_type, $enabled_types, true ) ) { status_header( 403 ); header( 'Content-Type: text/plain; charset=UTF-8' ); echo "# Access Denied\n\nThis page is not available as Markdown."; exit; } $md = AJWA_Markdown::get_or_generate( $post_id ); if ( ! $md ) { status_header( 500 ); header( 'Content-Type: text/plain; charset=UTF-8' ); echo "# Error\n\nCould not generate Markdown for this page."; exit; } status_header( 200 ); header( 'Content-Type: text/plain; charset=UTF-8' ); header( 'X-Robots-Tag: noindex, nofollow' ); nocache_headers(); // Output is plain text Markdown — not HTML, no escaping needed here. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $md; exit; } /** * Get the post ID for the current request. * * @return int Post ID or 0. */ private static function get_current_post_id() { $post_id = get_queried_object_id(); if ( $post_id ) { return $post_id; } global $post; if ( $post && $post->ID ) { return $post->ID; } return 0; } /** * AJAX: get Markdown content for a post (used by copy button on frontend). */ public static function ajax_get_md() { // Verify nonce passed from frontend JS. if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ajwa_frontend_nonce' ) ) { wp_send_json_error( 'Invalid nonce.' ); } $post_id = absint( wp_unslash( $_POST['post_id'] ?? 0 ) ); if ( ! $post_id ) { wp_send_json_error( 'No post ID.' ); } $settings = ajwa_get_settings(); $enabled_types = $settings['enabled_post_types'] ?? array(); $post = get_post( $post_id ); if ( ! $post || ! in_array( $post->post_type, $enabled_types, true ) ) { wp_send_json_error( 'Not enabled for this post type.' ); } $md = AJWA_Markdown::get_or_generate( $post_id ); if ( $md ) { wp_send_json_success( array( 'md' => $md ) ); } else { wp_send_json_error( 'Failed to generate.' ); } } }