<?php

class RewriteBtn {
	static function show($post) {
		?>
        <h3>Действия</h3>
		<button id="rewriteBtn" data-post_id="<?php echo esc_attr($post->ID); ?>">
			Рерайт поста
		</button>
		<?php
	}

    static function get_rewrited_text() {
        $status = "error";
        $post_id = $_POST["post_id"];

	    $post = get_post($post_id);
        $raw_text = $post->post_content;

        $rewrited_text = Ai2MediaService::rewrite_text($raw_text);

        if ($rewrited_text != null) {
	        $post_data = array(
		        'ID'           => $post_id,
		        'post_content' => $rewrited_text
	        );
	        wp_update_post($post_data);
            $status = "success";
        }

	    wp_send_json(array("status"=>$status));
    }
}

add_action('wp_ajax_rewrite_action', [RewriteBtn::class, 'get_rewrited_text']);