<?php
/**
 * SEO helpers – basic (all tiers) and advanced (bundle).
 *
 * @package EasySite_Pro
 */

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

/**
 * Register SEO meta box on pages and posts.
 */
function easysite_seo_meta_box() {
	$post_types = array( 'page', 'post' );
	if ( easysite_is_bundle() ) {
		$post_types[] = 'product';
	}
	add_meta_box(
		'easysite_seo',
		__( 'EasySite SEO', 'easysite-pro' ),
		'easysite_render_seo_meta_box',
		$post_types,
		'normal',
		'high'
	);
}
add_action( 'add_meta_boxes', 'easysite_seo_meta_box' );

/**
 * Render SEO meta box.
 *
 * @param WP_Post $post Post object.
 */
function easysite_render_seo_meta_box( $post ) {
	wp_nonce_field( 'easysite_seo_save', 'easysite_seo_nonce' );
	$title = get_post_meta( $post->ID, '_easysite_seo_title', true );
	$desc  = get_post_meta( $post->ID, '_easysite_seo_description', true );
	?>
	<p>
		<label for="easysite_seo_title"><strong><?php esc_html_e( 'SEO title', 'easysite-pro' ); ?></strong></label><br />
		<input type="text" id="easysite_seo_title" name="easysite_seo_title" class="widefat" value="<?php echo esc_attr( $title ); ?>" />
		<span class="description"><?php esc_html_e( 'Optional custom title for search engines. Leave blank to use the page title.', 'easysite-pro' ); ?></span>
	</p>
	<p>
		<label for="easysite_seo_description"><strong><?php esc_html_e( 'Meta description', 'easysite-pro' ); ?></strong></label><br />
		<textarea id="easysite_seo_description" name="easysite_seo_description" class="widefat" rows="3"><?php echo esc_textarea( $desc ); ?></textarea>
		<span class="description"><?php esc_html_e( 'A short summary that may appear in search results.', 'easysite-pro' ); ?></span>
		<?php if ( easysite_is_bundle() ) : ?>
			<br /><button type="button" class="button easysite-ai-suggest" data-target="easysite_seo_description" data-type="meta"><?php esc_html_e( 'Suggest meta description', 'easysite-pro' ); ?></button>
		<?php endif; ?>
	</p>
	<?php
}

/**
 * Save SEO meta.
 *
 * @param int $post_id Post ID.
 */
function easysite_save_seo_meta( $post_id ) {
	if ( ! isset( $_POST['easysite_seo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['easysite_seo_nonce'] ) ), 'easysite_seo_save' ) ) {
		return;
	}
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}

	if ( isset( $_POST['easysite_seo_title'] ) ) {
		update_post_meta( $post_id, '_easysite_seo_title', sanitize_text_field( wp_unslash( $_POST['easysite_seo_title'] ) ) );
	}
	if ( isset( $_POST['easysite_seo_description'] ) ) {
		update_post_meta( $post_id, '_easysite_seo_description', sanitize_textarea_field( wp_unslash( $_POST['easysite_seo_description'] ) ) );
	}
}
add_action( 'save_post', 'easysite_save_seo_meta' );

/**
 * Output meta description in head.
 */
function easysite_output_meta_description() {
	$description = '';

	if ( is_singular() ) {
		$description = get_post_meta( get_queried_object_id(), '_easysite_seo_description', true );
	}

	if ( ! $description ) {
		$description = easysite_get_mod( 'easysite_seo_description' );
	}

	if ( $description ) {
		echo '<meta name="description" content="' . esc_attr( wp_strip_all_tags( $description ) ) . '" />' . "\n";
	}

	$og_image = easysite_get_mod( 'easysite_seo_social_image' );
	if ( $og_image ) {
		echo '<meta property="og:image" content="' . esc_url( $og_image ) . '" />' . "\n";
	}
}
add_action( 'wp_head', 'easysite_output_meta_description', 1 );

/**
 * Filter document title for custom SEO title.
 *
 * @param array $title Title parts.
 * @return array
 */
function easysite_document_title( $title ) {
	if ( ! is_singular() ) {
		return $title;
	}
	$custom = get_post_meta( get_queried_object_id(), '_easysite_seo_title', true );
	if ( $custom ) {
		$title['title'] = $custom;
	}
	return $title;
}
add_filter( 'document_title_parts', 'easysite_document_title' );

/**
 * AJAX: save per-page SEO from visual editor.
 */
function easysite_ajax_ve_save_seo() {
	check_ajax_referer( 'easysite_ve_nonce', 'nonce' );
	$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0;
	if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) {
		wp_send_json_error();
	}
	if ( isset( $_POST['seo_title'] ) ) {
		update_post_meta( $post_id, '_easysite_seo_title', sanitize_text_field( wp_unslash( $_POST['seo_title'] ) ) );
	}
	if ( isset( $_POST['seo_description'] ) ) {
		update_post_meta( $post_id, '_easysite_seo_description', sanitize_textarea_field( wp_unslash( $_POST['seo_description'] ) ) );
	}
	wp_send_json_success();
}
add_action( 'wp_ajax_easysite_ve_save_seo', 'easysite_ajax_ve_save_seo' );
