wp_create_nonce( 'ajwa_meta_nonce' ), 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'post_id' => get_the_ID(), 'strings' => array( 'generating' => esc_html__( 'Generating…', 'ajw-ai-markdown-page-widget' ), 'regenerate' => esc_html__( 'Regenerate', 'ajw-ai-markdown-page-widget' ), 'generate' => esc_html__( 'Generate Markdown', 'ajw-ai-markdown-page-widget' ), 'copy' => esc_html__( 'Copy', 'ajw-ai-markdown-page-widget' ), 'copied' => esc_html__( 'Copied!', 'ajw-ai-markdown-page-widget' ), 'error' => esc_html__( 'Error generating content. Please try again.', 'ajw-ai-markdown-page-widget' ), ), ) ); } /** * Render the meta box HTML. * * @param WP_Post $post Current post object. */ public static function render_meta_box( $post ) { wp_nonce_field( 'ajwa_meta_box_nonce', 'ajwa_meta_box_nonce_field' ); $md_content = get_post_meta( $post->ID, '_ajwa_md_content', true ); $auto_updated = get_post_meta( $post->ID, '_ajwa_md_auto_updated', true ); $md_url = self::get_md_url( $post->ID ); ?>
post_content ) ); } // Clear auto-updated flag. delete_post_meta( $post_id, '_ajwa_md_auto_updated' ); // Auto-regenerate if content changed. AJWA_Markdown::maybe_regenerate_on_save( $post_id ); } /** * AJAX: generate Markdown for a post and save it. */ public static function ajax_generate_md() { check_ajax_referer( 'ajwa_meta_nonce', 'nonce' ); $post_id = absint( wp_unslash( $_POST['post_id'] ?? 0 ) ); if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { wp_send_json_error( esc_html__( 'Permission denied.', 'ajw-ai-markdown-page-widget' ) ); } $md = AJWA_Markdown::post_to_markdown( $post_id ); if ( ! $md ) { wp_send_json_error( esc_html__( 'Failed to generate Markdown.', 'ajw-ai-markdown-page-widget' ) ); } update_post_meta( $post_id, '_ajwa_md_content', $md ); $post = get_post( $post_id ); update_post_meta( $post_id, '_ajwa_content_hash', md5( $post->post_content ) ); wp_send_json_success( array( 'md' => $md ) ); } /** * AJAX: force-regenerate Markdown for a post, optionally using live editor content. */ public static function ajax_regenerate_md() { check_ajax_referer( 'ajwa_meta_nonce', 'nonce' ); $post_id = absint( wp_unslash( $_POST['post_id'] ?? 0 ) ); if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) { wp_send_json_error( esc_html__( 'Permission denied.', 'ajw-ai-markdown-page-widget' ) ); } // Use live editor content if passed (unsaved content in editor). if ( ! empty( $_POST['post_content'] ) ) { $live_content = wp_kses_post( wp_unslash( $_POST['post_content'] ) ); $post = get_post( $post_id ); if ( $post ) { $post->post_content = $live_content; $md = AJWA_Markdown::post_to_markdown_from_post( $post ); } else { $md = false; } } else { $md = AJWA_Markdown::post_to_markdown( $post_id ); } if ( ! $md ) { wp_send_json_error( esc_html__( 'Failed to generate Markdown.', 'ajw-ai-markdown-page-widget' ) ); } update_post_meta( $post_id, '_ajwa_md_content', $md ); $post = get_post( $post_id ); update_post_meta( $post_id, '_ajwa_content_hash', md5( $post->post_content ) ); wp_send_json_success( array( 'md' => $md ) ); } /** * Get the .md/ URL for a post. * * @param int $post_id Post ID. * @return string */ public static function get_md_url( $post_id ) { $permalink = get_permalink( $post_id ); if ( ! $permalink ) { return ''; } return trailingslashit( $permalink ) . 'md/'; } }