• File: markdown-search-ajax.php
  • Full Path: /home/slvgwrnz/public_html/wp-content/plugins/elementor/modules/agents/classes/markdown-search-ajax.php
  • Date Modified: 11/05/2026 9:17 AM
  • File size: 1.3 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace Elementor\Modules\Agents\Classes;

use Elementor\Core\Utils\Exceptions;
use Elementor\Modules\Agents\Agent_Ready_Settings;

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

class Markdown_Search_Ajax {

	const ACTION = 'agents_ready_search_markdown';

	private Agent_Ready_Settings $settings;

	private Markdown_Content_Catalog $catalog;

	public function __construct( Agent_Ready_Settings $settings, Markdown_Content_Catalog $catalog ) {
		$this->settings = $settings;
		$this->catalog  = $catalog;
	}

	/**
	 * @param array $data Ajax request data: { term?: string }.
	 * @return array{items: array}
	 * @throws \Exception When the user can't manage options.
	 */
	public function handle( array $data ): array {
		if ( ! current_user_can( 'manage_options' ) ) {
			throw new \Exception( esc_html__( 'Permission denied.', 'elementor' ), Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		}

		if ( ! $this->settings->is_markdown_enabled() ) {
			return [ 'items' => [] ];
		}

		$term = isset( $data['term'] ) && is_string( $data['term'] ) ? trim( sanitize_text_field( $data['term'] ) ) : '';

		return [
			'items' => $this->catalog->search(
				$this->settings->get_markdown_post_types(),
				$term,
				$this->settings->get_markdown_excluded_post_ids()
			),
		];
	}
}