<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php

namespace Elementor\Modules\Mcp\Abilities;

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

class List_Pages_Ability extends Abstract_Ability {

	protected function get_ability_id(): string {
		return 'elementor/list-pages';
	}

	protected function get_definition(): Ability_Definition {
		return new Ability_Definition(
			__( 'List Elementor Pages', 'elementor' ),
			__( 'Returns pages and posts built with Elementor on this WordPress site. Each item includes ID, title, status (publish/draft), URL, and post type. Use this first to discover which pages exist before fetching their structure or modifying settings.', 'elementor' ),
			'elementor',
			[
				'type' => 'array',
				'items' => [
					'type' => 'object',
					'properties' => [
						'id' => [ 'type' => 'integer' ],
						'title' => [ 'type' => 'string' ],
						'status' => [ 'type' => 'string' ],
						'url' => [ 'type' => 'string' ],
						'type' => [ 'type' => 'string' ],
					],
				],
			],
			[
				'annotations' => [
					'readonly' => true,
					'idempotent' => true,
					'destructive' => false,
				],
			],
			function () {
				return current_user_can( 'edit_posts' );
			},
			[
				'type' => 'object',
				'properties' => [
					'status' => [
						'type' => 'string',
						'enum' => [ 'publish', 'draft', 'any' ],
						'default' => 'any',
					],
					'post_type' => [
						'type' => 'string',
						'description' => 'Filter by post type. Omit for all Elementor-supported types.',
					],
				],
			]
		);
	}

	public function execute( $input = [] ) {
		$input = is_array( $input ) ? $input : [];

		$args = [
			'post_type' => get_post_types_by_support( 'elementor' ),
			'meta_key' => '_elementor_edit_mode',
			'meta_value' => 'builder',
			'post_status' => isset( $input['status'] ) ? $input['status'] : 'any',
			'fields' => 'ids',
			'posts_per_page' => -1,
			'orderby' => 'date',
			'order' => 'DESC',
			'suppress_filters' => false,
		];

		if ( ! empty( $input['post_type'] ) ) {
			$args['post_type'] = sanitize_key( $input['post_type'] );
		}

		$ids = get_posts( $args );

		$ids = array_values(
			array_filter(
				array_map( 'absint', $ids ),
				function ( $id ) {
					return $id && current_user_can( 'edit_post', $id );
				}
			)
		);

		return array_map(
			function ( $id ) {
				return [
					'id' => $id,
					'title' => get_the_title( $id ),
					'status' => get_post_status( $id ),
					'url' => (string) get_permalink( $id ),
					'type' => get_post_type( $id ),
				];
			},
			$ids
		);
	}
}
