fix: addHeading accepts title|text param; add elementor_delete_items tool and deleteItems method

This commit is contained in:
2026-06-02 12:07:57 +00:00
parent 56d779c70b
commit 5151183718
@@ -30,7 +30,7 @@ class ElementorTools {
$this->def('elementor_move_element','Move an element to a different parent or position within the page.',['post_id'=>['type'=>'integer'],'element_id'=>['type'=>'string'],'target_parent_id'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','element_id'],'write','moveElement'), $this->def('elementor_move_element','Move an element to a different parent or position within the page.',['post_id'=>['type'=>'integer'],'element_id'=>['type'=>'string'],'target_parent_id'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','element_id'],'write','moveElement'),
$this->def('elementor_add_widget','Add any Elementor widget to a container. widget_type: heading, text-editor, image, button, video, icon, divider, spacer, google_maps, html, shortcode, accordion, tabs, toggle, icon-list, counter, progress, testimonial, alert, social-icons, star-rating, image-box, icon-box, basic-gallery, image-carousel, reviews, posts, nav-menu, search-form, woocommerce-product-images, etc.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'widget_type'=>['type'=>'string'],'settings'=>['type'=>'object'],'position'=>['type'=>'integer']],['post_id','container_id','widget_type'],'write','addWidget'), $this->def('elementor_add_widget','Add any Elementor widget to a container. widget_type: heading, text-editor, image, button, video, icon, divider, spacer, google_maps, html, shortcode, accordion, tabs, toggle, icon-list, counter, progress, testimonial, alert, social-icons, star-rating, image-box, icon-box, basic-gallery, image-carousel, reviews, posts, nav-menu, search-form, woocommerce-product-images, etc.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'widget_type'=>['type'=>'string'],'settings'=>['type'=>'object'],'position'=>['type'=>'integer']],['post_id','container_id','widget_type'],'write','addWidget'),
$this->def('elementor_update_widget','Update an existing widget\'s settings.',['post_id'=>['type'=>'integer'],'element_id'=>['type'=>'string'],'settings'=>['type'=>'object'],'merge'=>['type'=>'boolean','default'=>true]],['post_id','element_id','settings'],'write','updateWidget'), $this->def('elementor_update_widget','Update an existing widget\'s settings.',['post_id'=>['type'=>'integer'],'element_id'=>['type'=>'string'],'settings'=>['type'=>'object'],'merge'=>['type'=>'boolean','default'=>true]],['post_id','element_id','settings'],'write','updateWidget'),
$this->def('elementor_add_heading','Add a heading widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'text'=>['type'=>'string'],'tag'=>['type'=>'string','default'=>'h2'],'align'=>['type'=>'string'],'color'=>['type'=>'string'],'size'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','text'],'write','addHeading'), $this->def('elementor_add_heading','Add a heading widget. Accepts title or text param.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'title'=>['type'=>'string'],'text'=>['type'=>'string'],'tag'=>['type'=>'string','default'=>'h2'],'align'=>['type'=>'string'],'color'=>['type'=>'string'],'size'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id'],'write','addHeading'),
$this->def('elementor_add_text','Add a text editor widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'content'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','content'],'write','addText'), $this->def('elementor_add_text','Add a text editor widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'content'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','content'],'write','addText'),
$this->def('elementor_add_image','Add an image widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'url'=>['type'=>'string'],'alt'=>['type'=>'string'],'link'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','url'],'write','addImage'), $this->def('elementor_add_image','Add an image widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'url'=>['type'=>'string'],'alt'=>['type'=>'string'],'link'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','url'],'write','addImage'),
$this->def('elementor_add_button','Add a button widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'text'=>['type'=>'string'],'url'=>['type'=>'string'],'type'=>['type'=>'string','default'=>'default'],'align'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','text','url'],'write','addButton'), $this->def('elementor_add_button','Add a button widget.',['post_id'=>['type'=>'integer'],'container_id'=>['type'=>'string'],'text'=>['type'=>'string'],'url'=>['type'=>'string'],'type'=>['type'=>'string','default'=>'default'],'align'=>['type'=>'string'],'position'=>['type'=>'integer']],['post_id','container_id','text','url'],'write','addButton'),
@@ -47,6 +47,7 @@ class ElementorTools {
$this->def('elementor_clone_page','Duplicate an existing Elementor page as a new draft.',['post_id'=>['type'=>'integer'],'title'=>['type'=>'string']],['post_id'],'write','clonePage'), $this->def('elementor_clone_page','Duplicate an existing Elementor page as a new draft.',['post_id'=>['type'=>'integer'],'title'=>['type'=>'string']],['post_id'],'write','clonePage'),
$this->def('elementor_replace_text','Replace all occurrences of a text string across all widgets in a page.',['post_id'=>['type'=>'integer'],'search'=>['type'=>'string'],'replace'=>['type'=>'string'],'case_sensitive'=>['type'=>'boolean','default'=>false]],['post_id','search','replace'],'write','replaceText'), $this->def('elementor_replace_text','Replace all occurrences of a text string across all widgets in a page.',['post_id'=>['type'=>'integer'],'search'=>['type'=>'string'],'replace'=>['type'=>'string'],'case_sensitive'=>['type'=>'boolean','default'=>false]],['post_id','search','replace'],'write','replaceText'),
$this->def('elementor_get_page_outline','Get a simplified structural outline of an Elementor page (containers + widget types).',['post_id'=>['type'=>'integer']],['post_id'],'read','getPageOutline'), $this->def('elementor_get_page_outline','Get a simplified structural outline of an Elementor page (containers + widget types).',['post_id'=>['type'=>'integer']],['post_id'],'read','getPageOutline'),
$this->def('elementor_delete_items','Delete one or more elements (containers or widgets) from a page by element_id. Use element_ids array.',['post_id'=>['type'=>'integer'],'element_ids'=>['type'=>'array','items'=>['type'=>'string']],'element_id'=>['type'=>'string']],['post_id'],'write','deleteItems'),
]; ];
} }
@@ -219,7 +220,9 @@ class ElementorTools {
public function updateWidget(array $args): array { return $this->updateElement($args); } public function updateWidget(array $args): array { return $this->updateElement($args); }
public function addHeading(array $args): array { public function addHeading(array $args): array {
$settings = ['title'=>$args['text']??'']; // Accept both 'title' and 'text' for compatibility
$text = $args['title'] ?? $args['text'] ?? '';
$settings = ['title'=>$text];
if (!empty($args['tag'])) $settings['header_size'] = $args['tag']; if (!empty($args['tag'])) $settings['header_size'] = $args['tag'];
if (!empty($args['align'])) $settings['align'] = $args['align']; if (!empty($args['align'])) $settings['align'] = $args['align'];
if (!empty($args['color'])) $settings['title_color'] = $args['color']; if (!empty($args['color'])) $settings['title_color'] = $args['color'];
@@ -415,6 +418,17 @@ class ElementorTools {
return $elements; return $elements;
} }
public function deleteItems(array $args): array {
$ids = $args['element_ids'] ?? (isset($args['element_id']) ? [$args['element_id']] : []);
if (empty($ids)) return ['error'=>'element_ids or element_id required'];
$deleted = [];
foreach ($ids as $eid) {
$result = $this->removeElement(['post_id'=>$args['post_id'],'element_id'=>$eid]);
$deleted[] = ['element_id'=>$eid,'result'=>$result];
}
return ['post_id'=>$args['post_id'],'deleted'=>$deleted,'count'=>count($deleted)];
}
private function getActiveKit(): ?\WP_Post { private function getActiveKit(): ?\WP_Post {
if (!class_exists('\Elementor\Plugin')) return null; if (!class_exists('\Elementor\Plugin')) return null;
$kit_id = get_option('elementor_active_kit'); $kit_id = get_option('elementor_active_kit');