ID)) { return $actions; } $url = wp_nonce_url( admin_url('admin-post.php?action=at_seo_download_markdown&post_id=' . $post->ID), 'at_seo_download_markdown', 'at_seo_download_nonce' ); $actions['at_seo_download'] = '' . esc_html__('Download Markdown', 'active-twist-seo-json-ld') . ''; return $actions; } public function inject_alternate_link() { if (!is_singular()) { return; } global $post; if (empty($post)) { return; } $url = add_query_arg('at_seo_format', 'markdown', get_permalink($post)); echo '' . "\n"; } public function handle_markdown_request() { $format = get_query_var('at_seo_format'); if (!$format) { $format = filter_input(INPUT_GET, 'at_seo_format', FILTER_SANITIZE_FULL_SPECIAL_CHARS); } if ($format === 'markdown') { if (!is_singular()) { return; } global $post; $markdown = $this->generate_markdown($post); if (empty($markdown)) { return; // Fallback to normal rendering if empty? Or 404? Let's just 404 or return. // If we return, it loads the page normally. // Let's force text/plain header and exit. } header('Content-Type: text/markdown; charset=utf-8'); header('X-Robots-Tag: noindex'); // Don't index the markdown source itself as a duplicate page // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Markdown is served as plain text. echo $markdown; exit; } } public function handle_download() { if (!current_user_can('edit_posts')) { wp_die('Unauthorized'); } check_admin_referer('at_seo_download_markdown', 'at_seo_download_nonce'); $post_id = filter_input(INPUT_GET, 'post_id', FILTER_VALIDATE_INT); $post_id = $post_id ? (int) $post_id : 0; if (!$post_id) { wp_die('Invalid Post ID'); } $post = get_post($post_id); if (!$post) { wp_die('Post not found'); } $markdown = $this->generate_markdown($post); $filename = sanitize_file_name($post->post_name) . '.md'; header('Content-Description: File Transfer'); header('Content-Type: text/markdown; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . strlen($markdown)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Markdown is served as a file download. echo $markdown; exit; } private function generate_markdown($post) { // Get the content with filters applied (shortcodes expanded, etc) // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress filter. $content = apply_filters('the_content', $post->post_content); $title = get_the_title($post); $md = "# " . $title . "\n\n"; $md .= Markdown::convert($content); return $md; } }