<?php
/**
 * Register Plugin Blocks
 *
 * @package Post Slider and Carousel
 * @since 3.5.5
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Registers the block using the metadata loaded from the `block.json` file.
 * Behind the scenes, it registers also all assets so they can be enqueued through the block editor in the corresponding context.
 */
function psac_block_init() {

	// Registers block.json
	$layout_block = register_block_type( __DIR__ . '/build/psacp-layout/' );

	// Localize the auto-registered editor script handle
	if ( $layout_block && ! empty( $layout_block->editor_script_handles ) ) {
		wp_localize_script(
			$layout_block->editor_script_handles[0], // main editor script handle
			'PsacpLayoutBlock',
			[
				'layout_page_url' => add_query_arg( array('page' => 'psacp-layouts'), admin_url('admin.php') )
			]
		);
	}
}
add_action( 'init', 'psac_block_init' );

/**
 * Register REST API endpoint for post search
 */
function psac_layout_register_rest_route() {
	register_rest_route(
		'psacp-layout-selector/v1',
		'/search-layouts',
		array(
			'methods'				=> 'GET',
			'callback'				=> 'psac_layout_search_posts',
			'permission_callback'	=> function() {
				return current_user_can( 'edit_posts' );
			},
			'args' => array(
				'search' => array(
					'required'			=> false,
					'type'				=> 'string',
					'sanitize_callback'	=> 'sanitize_text_field',
				),
			),
		)
	);
}
add_action( 'rest_api_init', 'psac_layout_register_rest_route' );

/**
 * Search posts by ID or title
 *
 * @param WP_REST_Request $request The REST request object.
 * @return array The search results.
 */
function psac_layout_search_posts( $request ) {

	global $post;

	$search_term = $request->get_param( 'search' );

	$args = array(
		'post_type'			=> PSAC_LAYOUT_POST_TYPE,
		'post_status'		=> array('publish', 'pending'),
		'posts_per_page'	=> 20,
		'orderby'			=> 'date',
		'order'				=> 'ASC',
		'no_found_rows'		=> true,
	);

	// Check if search term is numeric (Post ID search)
	if ( ! empty( $search_term ) && is_numeric( $search_term ) ) {
		$args['p'] = intval( $search_term );
	} elseif ( ! empty( $search_term ) ) {
		$args['s'] = $search_term;
	}

	$results	= array();
	$query		= new WP_Query( $args );

	if ( $query->have_posts() ) {
		while ( $query->have_posts() ) {
			$query->the_post();

			$layout_status	= get_post_status( $post );
			$layout_title 	= get_the_title();
			$layout_title	= ( 'pending' == $layout_status ) ? $layout_title ." &mdash; ". ucfirst( $layout_status ) : $layout_title;

			$results[] = array(
				'id'    => get_the_ID(),
				'title' => html_entity_decode( $layout_title, ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
			);
		}
		wp_reset_postdata();
	}

	return $results;
}