register( 'elementor_pages_list', array( 'description' => 'List Elementor-built pages.', 'domain' => 'elementor', 'capability' => 'edit_pages', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'per_page' => array( 'type' => 'integer', 'default' => 20 ) ), ), ), function ( $args ) { $query = new WP_Query( array( 'post_type' => array( 'page', 'elementor_library' ), 'posts_per_page' => isset( $args['per_page'] ) ? min( 100, absint( $args['per_page'] ) ) : 20, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Identifying Elementor documents requires this meta key; bounded by posts_per_page (max 100). 'meta_query' => array( array( 'key' => '_elementor_edit_mode', 'value' => 'builder', 'compare' => '=', ), ), ) ); $items = array(); foreach ( $query->posts as $post ) { $items[] = array( 'id' => $post->ID, 'title' => get_the_title( $post ), 'type' => $post->post_type, 'link' => get_permalink( $post ), ); } return array( 'pages' => $items ); } ); $registry->register( 'elementor_data_get', array( 'description' => 'Get Elementor document JSON data for a post.', 'domain' => 'elementor', 'capability' => 'edit_pages', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer' ) ), 'required' => array( 'post_id' ), ), ), function ( $args ) { $post_id = absint( $args['post_id'] ); $allowed = Agentdesk_Security::can_read_post( $post_id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $data = get_post_meta( $post_id, '_elementor_data', true ); if ( empty( $data ) ) { return new WP_Error( 'agentdesk_no_elementor_data', 'No Elementor data for this post' ); } $decoded = is_string( $data ) ? json_decode( $data, true ) : $data; return array( 'post_id' => $post_id, 'data' => $decoded, 'version' => get_post_meta( $post_id, '_elementor_version', true ), ); } ); $registry->register( 'elementor_data_set', array( 'description' => 'Set Elementor document JSON data for a post.', 'domain' => 'elementor', 'capability' => 'edit_pages', 'power_mode' => true, 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer' ), 'data' => array( 'type' => 'array' ), ), 'required' => array( 'post_id', 'data' ), ), ), function ( $args ) { $post_id = absint( $args['post_id'] ); $allowed = Agentdesk_Security::can_edit_post( $post_id ); if ( is_wp_error( $allowed ) ) { return $allowed; } $data = Agentdesk_Security::sanitize_elementor_tree( $args['data'] ); if ( is_wp_error( $data ) ) { return $data; } update_post_meta( $post_id, '_elementor_data', wp_slash( wp_json_encode( $data ) ) ); update_post_meta( $post_id, '_elementor_edit_mode', 'builder' ); if ( defined( 'ELEMENTOR_VERSION' ) ) { update_post_meta( $post_id, '_elementor_version', ELEMENTOR_VERSION ); } agentdesk_elementor_clear_cache( $post_id ); return array( 'updated' => true, 'post_id' => $post_id ); } ); $registry->register( 'elementor_templates_list', array( 'description' => 'List Elementor library templates.', 'domain' => 'elementor', 'capability' => 'edit_pages', 'inputSchema' => array( 'type' => 'object', 'properties' => array() ), ), function () { $query = new WP_Query( array( 'post_type' => 'elementor_library', 'posts_per_page' => 100, 'post_status' => 'publish', ) ); $items = array(); foreach ( $query->posts as $post ) { $items[] = array( 'id' => $post->ID, 'title' => get_the_title( $post ), 'type' => get_post_meta( $post->ID, '_elementor_template_type', true ), ); } return array( 'templates' => $items ); } ); $registry->register( 'elementor_globals_get', array( 'description' => 'Get Elementor global colors, fonts, and kit settings snapshot.', 'domain' => 'elementor', 'capability' => 'edit_pages', 'inputSchema' => array( 'type' => 'object', 'properties' => array() ), ), function () { $kit_id = get_option( 'elementor_active_kit' ); $meta = $kit_id ? get_post_meta( $kit_id, '_elementor_page_settings', true ) : array(); return array( 'kit_id' => $kit_id, 'settings' => $meta, ); } ); $registry->register( 'elementor_cache_clear', array( 'description' => 'Clear Elementor CSS cache for a post or site-wide.', 'domain' => 'elementor', 'capability' => 'manage_options', 'inputSchema' => array( 'type' => 'object', 'properties' => array( 'post_id' => array( 'type' => 'integer' ) ), ), ), function ( $args ) { $post_id = isset( $args['post_id'] ) ? absint( $args['post_id'] ) : 0; agentdesk_elementor_clear_cache( $post_id ); return array( 'cleared' => true, 'post_id' => $post_id ); } ); } ); /** * Clear Elementor cache. * * @param int $post_id Post ID or 0 for all. */ function agentdesk_elementor_clear_cache( $post_id = 0 ) { if ( class_exists( '\Elementor\Plugin' ) ) { $files_manager = \Elementor\Plugin::$instance->files_manager; if ( $post_id ) { $files_manager->clear_cache(); } else { $files_manager->clear_cache(); } } delete_post_meta( $post_id, '_elementor_css' ); }