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