all_abilities = $abilities; parent::__construct([ 'singular' => 'ability', 'plural' => 'abilities', 'ajax' => false, ]); } public function get_columns() { return [ 'name' => __('Name', 'aiutoma'), 'slug' => __('Slug', 'aiutoma'), 'description' => __('Description', 'aiutoma'), 'provider' => __('Provider', 'aiutoma'), 'category' => __('Category', 'aiutoma'), 'actions' => __('Actions', 'aiutoma') ]; } public function get_sortable_columns() { return [ 'name' => ['name', false], 'slug' => ['slug', false], 'provider' => ['provider', false], 'category' => ['category', false], ]; } public function prepare_items() { $columns = $this->get_columns(); $hidden = []; $sortable = $this->get_sortable_columns(); $this->_column_headers = [$columns, $hidden, $sortable]; $abilities = $this->all_abilities; $search = isset($_REQUEST['s']) ? sanitize_text_field(wp_unslash($_REQUEST['s'])) : ''; if (!empty($search)) { $abilities = array_filter($abilities, function($a) use ($search) { return stripos($a['name'], $search) !== false || stripos($a['slug'], $search) !== false || stripos($a['description'], $search) !== false; }); } $provider_filter = isset($_REQUEST['provider']) ? sanitize_text_field(wp_unslash($_REQUEST['provider'])) : ''; if (!empty($provider_filter) && 'all' !== $provider_filter) { $abilities = array_filter($abilities, function($a) use ($provider_filter) { if (strpos($provider_filter, 'Plugin:') === 0) { return $a['provider'] === 'Plugin' && ($a['plugin_name'] ?? '') === substr($provider_filter, 7); } if ($provider_filter === 'Plugin') { return $a['provider'] === 'Plugin'; } return $a['provider'] === $provider_filter; }); } $category_filter = isset($_REQUEST['category']) ? sanitize_text_field(wp_unslash($_REQUEST['category'])) : ''; if (!empty($category_filter) && 'all' !== $category_filter) { $abilities = array_filter($abilities, function($a) use ($category_filter) { return ($a['category'] ?? '') === $category_filter; }); } $orderby = isset($_REQUEST['orderby']) ? sanitize_text_field(wp_unslash($_REQUEST['orderby'])) : 'name'; $order = isset($_REQUEST['order']) ? sanitize_text_field(wp_unslash($_REQUEST['order'])) : 'asc'; usort($abilities, function($a, $b) use ($orderby, $order) { $result = 0; if (isset($a[$orderby]) && isset($b[$orderby])) { $result = strcasecmp($a[$orderby], $b[$orderby]); } return 'asc' === $order ? $result : -$result; }); $per_page = 20; $current_page = $this->get_pagenum(); $total_items = count($abilities); $this->set_pagination_args([ 'total_items' => $total_items, 'per_page' => $per_page, 'total_pages' => (int) ceil($total_items / $per_page), ]); $this->items = array_slice($abilities, ($current_page - 1) * $per_page, $per_page); } public function get_unique_categories() { $categories = []; foreach ($this->all_abilities as $a) { if (!empty($a['category'])) { $categories[] = $a['category']; } } $categories = array_unique($categories); sort($categories); return $categories; } public function get_unique_plugin_names() { $plugins = []; foreach ($this->all_abilities as $a) { if ($a['provider'] === 'Plugin' && !empty($a['plugin_name'])) { $plugins[] = $a['plugin_name']; } } $plugins = array_unique($plugins); sort($plugins); return $plugins; } public function column_default($item, $column_name) { return isset($item[$column_name]) ? esc_html($item[$column_name]) : '—'; } public function column_name($item) { return sprintf('%s', esc_html($item['name'])); } public function column_slug($item) { return sprintf('%s', esc_html($item['slug'])); } public function column_description($item) { return esc_html($item['description']); } public function column_provider($item) { $provider = $item['provider']; $class = 'ability-provider ability-provider-' . strtolower($provider); $label = $provider; if ($provider === 'Core') $label = __('Core', 'aiutoma'); if ($provider === 'Theme') $label = __('Theme', 'aiutoma'); if ($provider === 'Plugin') { $label = !empty($item['plugin_name']) ? $item['plugin_name'] : __('Plugin', 'aiutoma'); } return sprintf('%s', esc_attr($class), esc_html($label)); } public function column_actions($item) { $detail_url = add_query_arg([ 'page' => 'aiutoma-abilities', 'action' => 'view', 'ability' => $item['slug'], ], admin_url('admin.php')); $test_url = add_query_arg([ 'page' => 'aiutoma-abilities', 'action' => 'test', 'ability' => $item['slug'], ], admin_url('admin.php')); return sprintf( '%s %s', esc_url($detail_url), esc_html__('View', 'aiutoma'), esc_url($test_url), esc_html__('Test', 'aiutoma') ); } public function extra_tablenav($which) { if ('top' !== $which) return; $provider_filter = isset($_REQUEST['provider']) ? sanitize_text_field(wp_unslash($_REQUEST['provider'])) : 'all'; $category_filter = isset($_REQUEST['category']) ? sanitize_text_field(wp_unslash($_REQUEST['category'])) : 'all'; ?>
get_meta(); $parts = explode('/', $ability_obj->get_name()); $provider = 'Plugin'; $ns = ''; if (isset($meta['provider'])) { $provider = $meta['provider']; } elseif (count($parts) === 2) { $ns = $parts[0]; if (in_array($ns, ['wordpress', 'wp', 'core'])) $provider = 'Core'; elseif (get_stylesheet() === $ns || get_template() === $ns) $provider = 'Theme'; } $plugin_name = ''; if ($provider === 'Plugin') { if (!empty($meta['plugin_name'])) { $plugin_name = $meta['plugin_name']; } elseif (!empty($ns)) { if (!function_exists('get_plugins')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins(); foreach ($plugins as $plugin_path => $plugin_data) { if (strpos($plugin_path, $ns . '/') === 0 || $plugin_path === $ns . '.php') { $plugin_name = $plugin_data['Name']; break; } } if (empty($plugin_name)) { $plugin_name = ucwords(str_replace(['-', '_'], ' ', $ns)); } } if (stripos($plugin_name, 'aiutoma') !== false || strtolower($plugin_name) === 'wizard ai' || $ns === 'aiutoma') { $plugin_name = 'Aiutoma'; } } $cat_slug = $ability_obj->get_category(); $cat_label = 'Other'; if (!empty($cat_slug)) { if (function_exists('wp_get_ability_category') && function_exists('wp_has_ability_category') && wp_has_ability_category($cat_slug)) { $cat_obj = wp_get_ability_category($cat_slug); $cat_label = $cat_obj->get_label(); } else { $cat_label = ucwords(str_replace(['-', '_'], ' ', $cat_slug)); if (strtolower($cat_label) === 'woocommerce') $cat_label = 'WooCommerce'; } } elseif (!empty($ns)) { $cat_label = ucwords(str_replace(['-', '_'], ' ', $ns)); if (strtolower($cat_label) === 'woocommerce') $cat_label = 'WooCommerce'; } return [ 'slug' => $ability_obj->get_name(), 'name' => $ability_obj->get_label(), 'description' => $ability_obj->get_description(), 'provider' => $provider, 'plugin_name' => $plugin_name, 'input_schema' => $ability_obj->get_input_schema(), 'output_schema' => $ability_obj->get_output_schema(), 'raw_data' => [ 'name' => $ability_obj->get_name(), 'label' => $ability_obj->get_label(), 'description' => $ability_obj->get_description(), 'category' => $cat_slug, 'input_schema' => $ability_obj->get_input_schema(), 'output_schema' => $ability_obj->get_output_schema(), 'meta' => $meta ] ]; } private function render_detail_view() { $ability_slug = isset($_GET['ability']) ? sanitize_text_field(wp_unslash($_GET['ability'])) : ''; if (empty($ability_slug)) { echo '

' . esc_html__('No ability specified.', 'aiutoma') . '

'; return; } $ability = $this->get_ability_array($ability_slug); if (!$ability) { echo '

' . esc_html__('Ability not found.', 'aiutoma') . '

'; return; } $back_url = admin_url('admin.php?page=aiutoma-abilities'); $test_url = add_query_arg([ 'page' => 'aiutoma-abilities', 'action' => 'test', 'ability' => $ability_slug, ], admin_url('admin.php')); $provider_label = $ability['provider']; if ($provider_label === 'Core') $provider_label = __('Core', 'aiutoma'); if ($provider_label === 'Theme') $provider_label = __('Theme', 'aiutoma'); if ($provider_label === 'Plugin') { $provider_label = !empty($ability['plugin_name']) ? $ability['plugin_name'] : __('Plugin', 'aiutoma'); } ?>

' . esc_html__('No ability specified.', 'aiutoma') . '

'; return; } $ability = $this->get_ability_array($ability_slug); if (!$ability) { echo '

' . esc_html__('Ability not found.', 'aiutoma') . '

'; return; } $back_url = admin_url('admin.php?page=aiutoma-abilities'); $detail_url = add_query_arg([ 'page' => 'aiutoma-abilities', 'action' => 'view', 'ability' => $ability_slug, ], admin_url('admin.php')); $example_input = $this->generate_example_input($ability['input_schema']); ?>



$prop_schema) { if (isset($prop_schema['default'])) { $input[$prop_name] = $prop_schema['default']; } elseif (isset($prop_schema['example'])) { $input[$prop_name] = $prop_schema['example']; } else { $type = $prop_schema['type'] ?? 'string'; if ($type === 'string') $input[$prop_name] = ''; elseif ($type === 'integer' || $type === 'number') $input[$prop_name] = 0; elseif ($type === 'boolean') $input[$prop_name] = false; elseif ($type === 'array') $input[$prop_name] = []; elseif ($type === 'object') $input[$prop_name] = new \stdClass(); else $input[$prop_name] = null; } } return $input; } public function aiutoma_abilities_page_html() { if (!current_user_can('manage_options')) { wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'aiutoma')); } $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'list'; echo '
'; echo '

' . esc_html__('AI Abilities', 'aiutoma') . '

'; if ($action === 'view') { $this->render_detail_view(); echo '
'; return; } elseif ($action === 'test') { $this->render_test_runner(); echo ''; return; } $abilities_raw = function_exists('wp_get_abilities') ? wp_get_abilities() : []; $abilities_raw = apply_filters('aiutoma/abilities', $abilities_raw); $abilities = []; $stats = [ 'total' => 0, 'by_provider' => ['Core' => 0, 'Plugin' => 0, 'Theme' => 0] ]; foreach ($abilities_raw as $ability) { $name = $ability->get_name(); $meta = $ability->get_meta(); $parts = explode('/', $name); $provider_type = 'Plugin'; $ns = ''; if (isset($meta['provider'])) { $provider_type = $meta['provider']; } elseif (count($parts) === 2) { $ns = $parts[0]; if (in_array($ns, ['wordpress', 'wp', 'core'])) $provider_type = 'Core'; elseif (get_stylesheet() === $ns || get_template() === $ns) $provider_type = 'Theme'; } $plugin_name = ''; if ($provider_type === 'Plugin') { if (!empty($meta['plugin_name'])) { $plugin_name = $meta['plugin_name']; } elseif (!empty($ns)) { if (!function_exists('get_plugins')) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins(); foreach ($plugins as $plugin_path => $plugin_data) { if (strpos($plugin_path, $ns . '/') === 0 || $plugin_path === $ns . '.php') { $plugin_name = $plugin_data['Name']; break; } } if (empty($plugin_name)) { $plugin_name = ucwords(str_replace(['-', '_'], ' ', $ns)); } } if (stripos($plugin_name, 'aiutoma') !== false || strtolower($plugin_name) === 'wizard ai' || $ns === 'aiutoma') { $plugin_name = 'Aiutoma'; } } $cat_slug = $ability->get_category(); $cat_label = 'Other'; if (!empty($cat_slug)) { if (function_exists('wp_get_ability_category') && function_exists('wp_has_ability_category') && wp_has_ability_category($cat_slug)) { $cat_obj = wp_get_ability_category($cat_slug); $cat_label = $cat_obj->get_label(); } else { $cat_label = ucwords(str_replace(['-', '_'], ' ', $cat_slug)); if (strtolower($cat_label) === 'woocommerce') $cat_label = 'WooCommerce'; } } elseif (!empty($ns)) { $cat_label = ucwords(str_replace(['-', '_'], ' ', $ns)); if (strtolower($cat_label) === 'woocommerce') $cat_label = 'WooCommerce'; } $abilities[] = [ 'slug' => $name, 'name' => $ability->get_label(), 'description' => $ability->get_description(), 'provider' => $provider_type, 'plugin_name' => $plugin_name, 'category' => $cat_label ]; $stats['total']++; if (!isset($stats['by_provider'][$provider_type])) $stats['by_provider'][$provider_type] = 0; $stats['by_provider'][$provider_type]++; } ?>

' . esc_html__('Activate Dev Companion', 'aiutoma') . ''; } ?>
prepare_items(); ?>
search_box(__('Search Abilities', 'aiutoma'), 'ability'); $table->display(); ?>