<?php
/**
 * WordPress core SEO metadata adapter.
 *
 * @package SmartyPress_AI_Engine
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Writes neutral metadata to WordPress core fields.
 */
class SmartyPress_AI_Content_Generator_WordPress_Slug_SEO_Metadata_Adapter implements SmartyPress_AI_Content_Generator_SEO_Metadata_Adapter_Interface {

	/**
	 * Adapter identifier.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'wordpress';
	}

	/**
	 * Whether this adapter can write metadata.
	 *
	 * @return bool
	 */
	public function is_available() {
		return true;
	}

	/**
	 * Apply metadata to a post.
	 *
	 * @param int   $post_id Post ID.
	 * @param array $metadata Neutral metadata package.
	 * @return array Updated fields.
	 */
	public function apply( $post_id, array $metadata ) {
		if ( empty( $metadata['slug'] ) ) {
			return array();
		}

		wp_update_post(
			array(
				'ID'        => absint( $post_id ),
				'post_name' => sanitize_title( $metadata['slug'] ),
			)
		);

		return array( 'slug' );
	}
}
