v2.2.0: includes/Tools/ThemeDiscoveryTools.php
This commit is contained in:
@@ -0,0 +1,213 @@
|
|||||||
|
<?php
|
||||||
|
namespace AskTamMCP\Tools;
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ThemeDiscoveryTools — exposes what the active theme contributes to Elementor
|
||||||
|
* and the broader WP environment: custom widget areas, CPTs, taxonomies,
|
||||||
|
* Elementor theme-kit tokens, WooMart/Woodmart-specific elements, etc.
|
||||||
|
*/
|
||||||
|
class ThemeDiscoveryTools {
|
||||||
|
|
||||||
|
public static function getThemeElementorSupport(array $args): array {
|
||||||
|
$theme = wp_get_theme();
|
||||||
|
$support = [];
|
||||||
|
|
||||||
|
// Check Elementor-specific theme support declarations
|
||||||
|
$elementor_features = [
|
||||||
|
'elementor-full-width-blocks',
|
||||||
|
'elementor-page-builder',
|
||||||
|
'elementor-canvas',
|
||||||
|
'align-wide',
|
||||||
|
];
|
||||||
|
foreach ($elementor_features as $f) {
|
||||||
|
$support['theme_support'][$f] = current_theme_supports($f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Elementor Pro Theme Builder support
|
||||||
|
$support['elementor_pro'] = [
|
||||||
|
'header_footer' => defined('ELEMENTOR_PRO_VERSION'),
|
||||||
|
'theme_builder' => defined('ELEMENTOR_PRO_VERSION'),
|
||||||
|
'woo_builder' => defined('ELEMENTOR_PRO_VERSION') && class_exists('WooCommerce'),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Check if theme has Elementor-specific template parts
|
||||||
|
$theme_dir = get_template_directory();
|
||||||
|
$support['template_files'] = [
|
||||||
|
'elementor-canvas' => file_exists("$theme_dir/elementor/canvas.php") || file_exists("$theme_dir/templates/canvas.php"),
|
||||||
|
'elementor-full-width'=> file_exists("$theme_dir/elementor/full-width.php") || file_exists("$theme_dir/templates/full-width.php"),
|
||||||
|
];
|
||||||
|
|
||||||
|
// Get Elementor page templates registered by theme
|
||||||
|
$templates = [];
|
||||||
|
foreach (wp_get_theme()->get_page_templates() as $file => $name) {
|
||||||
|
if (stripos($file, 'elementor') !== false || stripos($name, 'elementor') !== false
|
||||||
|
|| in_array($name, ['Canvas','Full Width','Elementor Canvas','Elementor Full Width'])) {
|
||||||
|
$templates[$file] = $name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$support['elementor_templates'] = $templates;
|
||||||
|
|
||||||
|
return array_merge($support, [
|
||||||
|
'theme_name' => $theme->get('Name'),
|
||||||
|
'theme_version' => $theme->get('Version'),
|
||||||
|
'parent_theme' => $theme->parent() ? $theme->parent()->get('Name') : null,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getThemeWidgetAreas(array $args): array {
|
||||||
|
global $wp_registered_sidebars;
|
||||||
|
$areas = [];
|
||||||
|
foreach ((array)$wp_registered_sidebars as $id => $s) {
|
||||||
|
$areas[] = ['id'=>$id,'name'=>$s['name'],'description'=>$s['description']];
|
||||||
|
}
|
||||||
|
return ['theme'=>wp_get_theme()->get('Name'),'widget_areas'=>$areas,'count'=>count($areas)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getThemeCustomPostTypes(array $args): array {
|
||||||
|
$args_query = ['public'=>true,'_builtin'=>false];
|
||||||
|
$cpts = get_post_types($args_query, 'objects');
|
||||||
|
$result = [];
|
||||||
|
foreach ($cpts as $cpt) {
|
||||||
|
$result[] = [
|
||||||
|
'name' => $cpt->name,
|
||||||
|
'label' => $cpt->label,
|
||||||
|
'description' => $cpt->description,
|
||||||
|
'hierarchical' => $cpt->hierarchical,
|
||||||
|
'supports' => get_all_post_type_supports($cpt->name),
|
||||||
|
'taxonomies' => get_object_taxonomies($cpt->name),
|
||||||
|
'has_archive' => $cpt->has_archive,
|
||||||
|
'menu_icon' => $cpt->menu_icon,
|
||||||
|
'rewrite' => $cpt->rewrite,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
// Also include built-in if requested
|
||||||
|
if (!empty($args['include_builtin'])) {
|
||||||
|
foreach (get_post_types(['public'=>true,'_builtin'=>true], 'objects') as $cpt) {
|
||||||
|
$result[] = ['name'=>$cpt->name,'label'=>$cpt->label,'builtin'=>true];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ['post_types'=>$result,'count'=>count($result)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getThemeTaxonomies(array $args): array {
|
||||||
|
$taxs = get_taxonomies(['public'=>true], 'objects');
|
||||||
|
$result = [];
|
||||||
|
foreach ($taxs as $tax) {
|
||||||
|
$result[] = [
|
||||||
|
'name' => $tax->name,
|
||||||
|
'label' => $tax->label,
|
||||||
|
'hierarchical' => $tax->hierarchical,
|
||||||
|
'object_types' => $tax->object_type,
|
||||||
|
'builtin' => $tax->_builtin,
|
||||||
|
'show_in_rest' => $tax->show_in_rest,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return ['taxonomies'=>$result,'count'=>count($result)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getElementorKitTokens(array $args): array {
|
||||||
|
if (!defined('ELEMENTOR_VERSION')) {
|
||||||
|
throw new \RuntimeException("Elementor is not active.");
|
||||||
|
}
|
||||||
|
$kit_id = \Elementor\Plugin::$instance->kits_manager->get_active_id();
|
||||||
|
if (!$kit_id) throw new \RuntimeException("No active Elementor kit found.");
|
||||||
|
|
||||||
|
$document = \Elementor\Plugin::$instance->documents->get($kit_id);
|
||||||
|
$settings = $document ? $document->get_data('settings') : [];
|
||||||
|
|
||||||
|
// Extract design tokens
|
||||||
|
$colors = $settings['system_colors'] ?? $settings['custom_colors'] ?? [];
|
||||||
|
$fonts = $settings['system_typography'] ?? $settings['custom_typography'] ?? [];
|
||||||
|
|
||||||
|
// Also grab global colors from Elementor kit CSS vars
|
||||||
|
$color_tokens = [];
|
||||||
|
foreach ($colors as $c) {
|
||||||
|
$color_tokens[] = [
|
||||||
|
'_id' => $c['_id'] ?? '',
|
||||||
|
'title' => $c['title'] ?? '',
|
||||||
|
'color' => $c['color'] ?? '',
|
||||||
|
'css_var'=> '--e-global-color-' . ($c['_id'] ?? ''),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$font_tokens = [];
|
||||||
|
foreach ($fonts as $f) {
|
||||||
|
$font_tokens[] = [
|
||||||
|
'_id' => $f['_id'] ?? '',
|
||||||
|
'title' => $f['title'] ?? '',
|
||||||
|
'font' => $f['typography_font_family'] ?? '',
|
||||||
|
'size' => $f['typography_font_size']['size'] ?? '',
|
||||||
|
'weight' => $f['typography_font_weight'] ?? '',
|
||||||
|
'css_var'=> '--e-global-typography-' . ($f['_id'] ?? '') . '-font-family',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'kit_id' => $kit_id,
|
||||||
|
'kit_name' => get_the_title($kit_id),
|
||||||
|
'colors' => $color_tokens,
|
||||||
|
'typography' => $font_tokens,
|
||||||
|
'color_count' => count($color_tokens),
|
||||||
|
'font_count' => count($font_tokens),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getWoodmartElements(array $args): array {
|
||||||
|
$is_woodmart = (strpos(strtolower(wp_get_theme()->get('Name')), 'woodmart') !== false
|
||||||
|
|| strpos(strtolower(wp_get_theme()->get('Template')), 'woodmart') !== false
|
||||||
|
|| defined('WOODMART_THEME_DIR'));
|
||||||
|
|
||||||
|
if (!$is_woodmart) {
|
||||||
|
return ['woodmart_active'=>false,'message'=>'WoodMart theme is not active.'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Discover WoodMart custom Elementor widgets
|
||||||
|
$woodmart_widgets = [];
|
||||||
|
$search_dirs = [
|
||||||
|
get_template_directory() . '/inc/integrations/elementor/elements/',
|
||||||
|
get_template_directory() . '/inc/modules/elementor/',
|
||||||
|
get_stylesheet_directory() . '/inc/integrations/elementor/elements/',
|
||||||
|
];
|
||||||
|
foreach ($search_dirs as $dir) {
|
||||||
|
if (!is_dir($dir)) continue;
|
||||||
|
foreach (glob($dir . '*.php') as $file) {
|
||||||
|
$name = basename($file, '.php');
|
||||||
|
$woodmart_widgets[] = [
|
||||||
|
'file' => basename($file),
|
||||||
|
'name' => str_replace(['-','_'], ' ', $name),
|
||||||
|
'slug' => $name,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WoodMart shortcodes registered
|
||||||
|
global $shortcode_tags;
|
||||||
|
$wm_shortcodes = array_filter(array_keys((array)$shortcode_tags), fn($s) => str_starts_with($s, 'woodmart_') || str_starts_with($s, 'wd_'));
|
||||||
|
|
||||||
|
// WoodMart widget areas
|
||||||
|
global $wp_registered_sidebars;
|
||||||
|
$wm_sidebars = array_filter((array)$wp_registered_sidebars, fn($s) => stripos($s['id'],'woodmart')!==false || stripos($s['name'],'woodmart')!==false);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'woodmart_active' => true,
|
||||||
|
'version' => defined('WOODMART_VERSION') ? WOODMART_VERSION : 'unknown',
|
||||||
|
'elementor_widgets' => $woodmart_widgets,
|
||||||
|
'widget_count' => count($woodmart_widgets),
|
||||||
|
'shortcodes' => array_values($wm_shortcodes),
|
||||||
|
'shortcode_count' => count($wm_shortcodes),
|
||||||
|
'widget_areas' => array_values($wm_sidebars),
|
||||||
|
'elementor_available' => defined('ELEMENTOR_VERSION'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAllPageTemplates(array $args): array {
|
||||||
|
$templates = wp_get_theme()->get_page_templates();
|
||||||
|
$result = [];
|
||||||
|
foreach ($templates as $file => $name) {
|
||||||
|
$result[] = ['file'=>$file,'name'=>$name,'value'=>$file];
|
||||||
|
}
|
||||||
|
// Add built-in templates
|
||||||
|
array_unshift($result, ['file'=>'default','name'=>'Default Template','value'=>'']);
|
||||||
|
return ['templates'=>$result,'count'=>count($result),'theme'=>wp_get_theme()->get('Name')];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user