AI.php
197 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | namespace ACF\AI; |
| 13 | |
| 14 | use ACF\AI\Abilities\Abilities; |
| 15 | use ACF\AI\GEO\GEO; |
| 16 | |
| 17 | /** |
| 18 | * Initializes the ACF AI functionality if enabled. |
| 19 | */ |
| 20 | class AI { |
| 21 | |
| 22 | /** |
| 23 | * Constructs the AI class. |
| 24 | * |
| 25 | * @since 6.8.0 |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | add_action( 'acf/init', array( $this, 'initialize' ) ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Initializes the AI functionality. |
| 35 | * |
| 36 | * @since 6.8.0 |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function initialize() { |
| 41 | if ( $this->is_geo_enabled() ) { |
| 42 | new GEO(); |
| 43 | } |
| 44 | |
| 45 | if ( $this->is_ai_enabled() ) { |
| 46 | new Abilities(); |
| 47 | $this->add_admin_ui_hooks(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks if AI functionality is enabled. |
| 53 | * |
| 54 | * @since 6.8.0 |
| 55 | * |
| 56 | * @return boolean |
| 57 | */ |
| 58 | public function is_ai_enabled(): bool { |
| 59 | return (bool) acf_get_setting( 'enable_acf_ai' ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Checks if GEO functionality is enabled. |
| 64 | * |
| 65 | * @since 6.8.0 |
| 66 | * |
| 67 | * @return boolean |
| 68 | */ |
| 69 | public function is_geo_enabled(): bool { |
| 70 | return (bool) acf_get_setting( 'enable_schema' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Adds admin UI hooks. |
| 75 | * |
| 76 | * @since 6.8.0 |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function add_admin_ui_hooks() { |
| 81 | // Add ACF AI tab to field groups. |
| 82 | add_filter( 'acf/field_group/additional_group_settings_tabs', array( $this, 'add_acf_ai_tab' ) ); |
| 83 | add_action( 'acf/field_group/render_group_settings_tab/acf-ai', array( $this, 'render_acf_ai_tab' ) ); |
| 84 | |
| 85 | // Add ACF AI tab to post types. |
| 86 | add_filter( 'acf/post_type/additional_settings_tabs', array( $this, 'add_acf_ai_tab' ) ); |
| 87 | add_action( 'acf/post_type/render_settings_tab/acf-ai', array( $this, 'render_acf_ai_tab' ) ); |
| 88 | |
| 89 | // Add ACF AI tab to taxonomies. |
| 90 | add_filter( 'acf/taxonomy/additional_settings_tabs', array( $this, 'add_acf_ai_tab' ) ); |
| 91 | add_action( 'acf/taxonomy/render_settings_tab/acf-ai', array( $this, 'render_acf_ai_tab' ) ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Registers the AI tab in various contexts. |
| 96 | * |
| 97 | * @since 6.8.0 |
| 98 | * |
| 99 | * @param array $tabs The existing tabs array. |
| 100 | * @return array |
| 101 | */ |
| 102 | public function add_acf_ai_tab( array $tabs ): array { |
| 103 | $tabs['acf-ai'] = __( 'ACF AI', 'acf' ); |
| 104 | return $tabs; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Renders the ACF AI tab in various contexts. |
| 109 | * |
| 110 | * @since 6.8.0 |
| 111 | * |
| 112 | * @param array $item The field group, post type, taxonomy, etc. being edited. |
| 113 | * @return void |
| 114 | */ |
| 115 | public function render_acf_ai_tab( array $item ) { |
| 116 | if ( empty( $item['key'] ) ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | $item_type = acf_determine_internal_post_type( $item['key'] ); |
| 121 | if ( ! $item_type ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | $item_type = str_replace( '-', '_', $item_type ); |
| 126 | $post_id = (int) acf_request_arg( 'post', 0 ); |
| 127 | $allow_ai_access_val = ! empty( $item['allow_ai_access'] ) ? 1 : 0; |
| 128 | |
| 129 | // If this is a new item, default to allowing AI access. |
| 130 | if ( ! $post_id ) { |
| 131 | $allow_ai_access_val = 1; |
| 132 | } |
| 133 | |
| 134 | $allow_access_label = __( 'Allow AI Access', 'acf' ); |
| 135 | $allow_access_desc = __( 'Allow AI systems to access and modify this content through the WordPress Abilities API.', 'acf' ); |
| 136 | $ai_description_label = __( 'AI Description', 'acf' ); |
| 137 | $ai_description_desc = __( 'Provide a description that will help AI systems understand the purpose and how to use this effectively.', 'acf' ); |
| 138 | |
| 139 | if ( 'acf_post_type' === $item_type ) { |
| 140 | $allow_access_label = __( 'Allow AI Access to Post Content', 'acf' ); |
| 141 | $allow_access_desc = __( 'When enabled, AI models can access and interact with the content of posts in this post type using supported integrations. This feature uses the WordPress Abilities API.', 'acf' ); |
| 142 | $ai_description_label = __( 'AI Guidance for Post Type', 'acf' ); |
| 143 | $ai_description_desc = __( "Add a short explanation of what this post type is for. The clearer your description, the better the AI's output will be.", 'acf' ); |
| 144 | } |
| 145 | |
| 146 | if ( 'acf_taxonomy' === $item_type ) { |
| 147 | $allow_access_label = __( 'Allow AI Access to Taxonomy Terms', 'acf' ); |
| 148 | $allow_access_desc = __( 'When enabled, AI models can access and interact with the terms in this taxonomy using supported integrations. This feature uses the WordPress Abilities API.', 'acf' ); |
| 149 | $ai_description_label = __( 'AI Guidance for Taxonomy', 'acf' ); |
| 150 | $ai_description_desc = __( "Add a short explanation of what this taxonomy is for. The clearer your description, the better the AI's output will be.", 'acf' ); |
| 151 | } |
| 152 | |
| 153 | if ( 'acf_field_group' === $item_type ) { |
| 154 | $allow_access_label = __( 'Allow AI Access to Field Data', 'acf' ); |
| 155 | $allow_access_desc = __( 'When enabled, AI models can access and interact with the fields in this group using supported integrations. This feature uses the WordPress Abilities API.', 'acf' ); |
| 156 | $ai_description_label = __( 'AI Context Description', 'acf' ); |
| 157 | $ai_description_desc = __( "Add a short explanation of what this field group is for. The clearer your description, the better the AI's output will be.", 'acf' ); |
| 158 | } |
| 159 | |
| 160 | acf_render_field_wrap( |
| 161 | array( |
| 162 | 'type' => 'true_false', |
| 163 | 'name' => 'allow_ai_access', |
| 164 | 'key' => 'allow_ai_access', |
| 165 | 'prefix' => $item_type, |
| 166 | 'value' => $allow_ai_access_val, |
| 167 | 'label' => $allow_access_label, |
| 168 | 'instructions' => $allow_access_desc, |
| 169 | 'ui' => true, |
| 170 | 'default' => 1, |
| 171 | ) |
| 172 | ); |
| 173 | |
| 174 | acf_render_field_wrap( |
| 175 | array( |
| 176 | 'type' => 'textarea', |
| 177 | 'name' => 'ai_description', |
| 178 | 'key' => 'ai_description', |
| 179 | 'prefix' => $item_type, |
| 180 | 'value' => $item['ai_description'] ?? '', |
| 181 | 'label' => $ai_description_label, |
| 182 | 'instructions' => $ai_description_desc, |
| 183 | 'rows' => 4, |
| 184 | 'conditions' => array( |
| 185 | array( |
| 186 | 'field' => 'allow_ai_access', |
| 187 | 'operator' => '==', |
| 188 | 'value' => '1', |
| 189 | ), |
| 190 | ), |
| 191 | ), |
| 192 | 'div', |
| 193 | 'field' |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 |