-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathFeature.php
More file actions
1525 lines (1336 loc) · 43.5 KB
/
Feature.php
File metadata and controls
1525 lines (1336 loc) · 43.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Classifai\Features;
use WP_REST_Request;
use WP_Error;
use function Classifai\find_provider_class;
use function Classifai\should_use_legacy_settings_panel;
use function Classifai\get_asset_info;
use function Classifai\safe_wp_remote_get;
abstract class Feature {
/**
* ID of the current feature.
*
* To be set in the subclass.
*
* @var string
*/
const ID = '';
/**
* Plugin area script handle.
*
* Every feature that injects content into the plugin area
* should add this script as a dependency.
*
* @var string
*/
const PLUGIN_AREA_SCRIPT = 'classifai-plugin-fill-js';
/**
* Plugin feature instruction directory URL.
*
* @var string
*/
const PLUGIN_FEATURE_INSTRUCTION_DIR = 'https://raw.githubusercontent.com/10up/classifai/refs/heads/develop/wp-hooks-docs/docs/02.feature-configuration';
/**
* Locally hosted instruction file.
*
* @var string
*/
const PLUGIN_LOCALLY_HOSTED_INSTRUCTION_URL = 'https://raw.githubusercontent.com/10up/classifai/refs/heads/develop/wp-hooks-docs/docs/03.advanced-docs/06.run-locally-hosted-llms.md';
/**
* Feature label.
*
* @var string
*/
public $label = '';
/**
* User role array.
*
* @var array
*/
public $roles = [];
/**
* Array of provider classes.
*
* This contains all the providers that are registered to the service.
*
* @var \Classifai\Providers\Provider[]
*/
public $provider_instances = [];
/**
* Array of providers supported by the feature.
*
* @var \Classifai\Providers\Provider[]
*/
public $supported_providers = [];
/**
* Array of providers data supported by the feature.
*
* @var \Classifai\Providers\Provider[]
*/
public $supported_providers_data = [];
/**
* Instruction file name for the feature.
*
* @var string
*/
public $instruction_file = '';
/**
* Set up necessary hooks.
*/
public function setup() {
add_action( 'admin_init', [ $this, 'setup_roles' ] );
add_action( 'rest_api_init', [ $this, 'setup_roles' ] );
if ( should_use_legacy_settings_panel() ) {
add_action( 'admin_init', [ $this, 'register_setting' ] );
add_action( 'admin_init', [ $this, 'setup_fields_sections' ] );
}
add_action( 'admin_enqueue_scripts', [ $this, 'register_plugin_area_script' ] );
if ( $this->is_feature_enabled() ) {
$this->feature_setup();
}
}
/**
* Setup any hooks the feature needs.
*
* Only fires if the feature is enabled, configured and user has access.
*/
public function feature_setup() {
}
/**
* Assigns user roles to the $roles array.
*/
public function setup_roles() {
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
$default_settings = $this->get_default_settings();
$this->roles = get_editable_roles() ?? [];
$this->roles = array_combine( array_keys( $this->roles ), array_column( $this->roles, 'name' ) );
// Remove subscriber from the list of roles.
unset( $this->roles['subscriber'] );
/**
* Filter the allowed WordPress roles for a feature.
*
* @since 3.0.0
* @hook classifai_{feature}_roles
*
* @param array $roles Array of arrays containing role information.
* @param array $default_settings Default setting values.
*
* @return array Roles array.
*/
$this->roles = apply_filters( 'classifai_' . static::ID . '_roles', $this->roles, $default_settings );
}
/**
* Returns the roles for the feature.
*
* @return array Array of roles.
*/
public function get_roles(): array {
return $this->roles;
}
/**
* Returns the label of the feature.
*
* @return string
*/
public function get_label(): string {
/**
* Filter the feature label.
*
* @since 3.0.0
* @hook classifai_{feature}_label
*
* @param string $label Feature label.
*
* @return string Filtered label.
*/
return apply_filters(
'classifai_' . static::ID . '_label',
$this->label
);
}
/**
* Registers the plugin area script.
*/
public function register_plugin_area_script() {
wp_register_script(
self::PLUGIN_AREA_SCRIPT,
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-fill.js',
get_asset_info( 'classifai-plugin-fill', 'dependencies' ),
get_asset_info( 'classifai-plugin-fill', 'version' ),
true
);
}
/**
* Set up the fields for each section.
*
* @internal
*/
public function setup_fields_sections() {
$settings = $this->get_settings();
add_settings_section(
$this->get_option_name() . '_section',
esc_html__( 'Feature settings', 'classifai' ),
'__return_empty_string',
$this->get_option_name()
);
// Add the enable field.
add_settings_field(
'status',
esc_html__( 'Enable feature', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'status',
'input_type' => 'checkbox',
'default_value' => $settings['status'],
'description' => $this->get_enable_description(),
]
);
// Add all the needed provider fields.
$this->add_provider_fields();
// Add any needed custom fields.
$this->add_custom_settings_fields();
// Add user/role-based access fields.
$this->add_access_control_fields();
}
/**
* Get the description for the enable field.
*
* @return string
*/
public function get_enable_description(): string {
return '';
}
/**
* Add any needed custom fields.
*/
public function add_custom_settings_fields() {
}
/**
* Returns the default settings for the feature.
*
* The root-level keys are the setting keys that are independent of the provider.
* Provider specific settings should be nested under the provider key.
*
* @internal
* @return array
*/
protected function get_default_settings(): array {
$shared_defaults = [
'status' => '0',
'roles' => array_combine( array_keys( $this->roles ), array_keys( $this->roles ) ),
'users' => [],
'user_based_opt_out' => 'no',
];
$provider_settings = $this->get_provider_default_settings();
$feature_settings = $this->get_feature_default_settings();
/**
* Filter the default settings for a feature.
*
* @since 3.0.0
* @hook classifai_{feature}_get_default_settings
*
* @param array $defaults Default feature settings.
* @param object $this Feature instance.
*
* @return array Filtered default feature settings.
*/
return apply_filters(
'classifai_' . static::ID . '_get_default_settings',
array_merge(
$shared_defaults,
$feature_settings,
$provider_settings
),
$this
);
}
/**
* Sanitizes the settings before saving.
*
* @internal
* @param array $settings The settings to be sanitized on save.
* @return array
*/
public function sanitize_settings( array $settings ): array {
$new_settings = $settings;
$current_settings = $this->get_settings();
// Sanitize the shared settings.
$new_settings['status'] = $settings['status'] ?? $current_settings['status'];
$new_settings['provider'] = isset( $settings['provider'] ) ? sanitize_text_field( $settings['provider'] ) : $current_settings['provider'];
// Allowed roles.
if ( isset( $settings['roles'] ) && is_array( $settings['roles'] ) ) {
$new_settings['roles'] = array_map( 'sanitize_text_field', $settings['roles'] );
} else {
$new_settings['roles'] = $current_settings['roles'];
}
// Allowed users.
if ( isset( $settings['users'] ) && ! empty( $settings['users'] ) ) {
if ( is_array( $settings['users'] ) ) {
$new_settings['users'] = array_map( 'absint', $settings['users'] );
} else {
$new_settings['users'] = array_map( 'absint', explode( ',', $settings['users'] ) );
}
} else {
$new_settings['users'] = array();
}
// User-based opt-out.
if ( empty( $settings['user_based_opt_out'] ) || 1 !== (int) $settings['user_based_opt_out'] ) {
$new_settings['user_based_opt_out'] = 'no';
} else {
$new_settings['user_based_opt_out'] = '1';
}
// Sanitize the feature specific settings.
$new_settings = $this->sanitize_default_feature_settings( $new_settings );
// Sanitize the provider specific settings.
$provider_instance = $this->get_feature_provider_instance( $new_settings['provider'] );
if ( $provider_instance ) {
$new_settings = $provider_instance->sanitize_settings( $new_settings );
}
/**
* Filter to change settings before they're saved.
*
* @since 3.0.0
* @hook classifai_{$feature}_sanitize_settings
*
* @param array $new_settings Settings being saved.
* @param array $current_settings Existing settings.
*
* @return array Filtered settings.
*/
return apply_filters(
'classifai_' . static::ID . '_sanitize_settings',
$new_settings,
$current_settings
);
}
/**
* Sanitize the default feature settings.
*
* @param array $settings Settings to sanitize.
* @return array
*/
public function sanitize_default_feature_settings( array $settings ): array {
return $settings;
}
/**
* Registers the settings for the feature.
*/
public function register_setting() {
register_setting(
$this->get_option_name(),
$this->get_option_name(),
[
'sanitize_callback' => [ $this, 'sanitize_settings' ],
]
);
}
/**
* Returns the option name for the feature.
*
* @return string
*/
public function get_option_name(): string {
return 'classifai_' . static::ID;
}
/**
* Returns the settings for the feature.
*
* @param string $index The index of the setting to return.
* @return array|mixed
*/
public function get_settings( $index = false ) {
$defaults = $this->get_default_settings();
$settings = get_option( $this->get_option_name(), [] );
$settings = $this->merge_settings( (array) $settings, (array) $defaults );
// If saved provider is not supported anymore, reset it.
if ( ! in_array( $settings['provider'], array_keys( $this->get_providers() ), true ) ) {
$settings['provider'] = '';
}
if ( $index && isset( $settings[ $index ] ) ) {
return $settings[ $index ];
}
return $settings;
}
/**
* Returns the default settings for the provider selected for the feature.
*
* @return array
*/
public function get_provider_default_settings(): array {
$provider_settings = [];
foreach ( array_keys( $this->get_providers() ) as $provider_id ) {
$provider = $this->get_feature_provider_instance( $provider_id );
if ( $provider && method_exists( $provider, 'get_default_provider_settings' ) ) {
$provider_settings[ $provider_id ] = $provider->get_default_provider_settings();
}
}
return $provider_settings;
}
/**
* Returns the default settings for the feature.
*
* @return array
*/
abstract public function get_feature_default_settings(): array;
/**
* Add the provider fields.
*
* Will add a field to choose the provider and any
* fields the selected provider has registered.
*/
public function add_provider_fields() {
$settings = $this->get_settings();
add_settings_field(
'provider',
esc_html__( 'Select a provider', 'classifai' ),
[ $this, 'render_select' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'provider',
'options' => $this->get_providers(),
'default_value' => $settings['provider'],
]
);
foreach ( array_keys( $this->get_providers() ) as $provider_id ) {
$provider = $this->get_feature_provider_instance( $provider_id );
if ( $provider && method_exists( $provider, 'render_provider_fields' ) ) {
$provider->render_provider_fields();
}
}
}
/**
* Merges the data settings with the default settings recursively.
*
* @internal
*
* @param array $settings Settings data from the database.
* @param array $defaults Default feature and providers settings data.
* @return array
*/
protected function merge_settings( array $settings = [], array $defaults = [] ): array {
foreach ( $defaults as $key => $value ) {
if ( ! array_key_exists( $key, $settings ) ) {
$settings[ $key ] = $defaults[ $key ];
} elseif ( is_array( $value ) ) {
if ( is_array( $settings[ $key ] ) ) {
$settings[ $key ] = $this->merge_settings( $settings[ $key ], $defaults[ $key ] );
} else {
$settings[ $key ] = $defaults[ $key ];
}
}
}
return $settings;
}
/**
* Returns the providers supported by the feature.
*
* @return array
*/
public function get_providers(): array {
/**
* Filter the feature providers.
*
* @since 3.0.0
* @hook classifai_{feature}_providers
*
* @param array $providers Feature providers.
*
* @return array Filtered providers.
*/
return apply_filters(
'classifai_' . static::ID . '_providers',
$this->supported_providers
);
}
/**
* Resets settings for the provider.
*/
public function reset_settings() {
update_option( $this->get_option_name(), $this->get_default_settings() );
}
/**
* Updates the settings for the feature.
*
* @param array $new_settings New settings to update.
*/
public function update_settings( array $new_settings ) {
$settings = $this->get_settings();
if ( empty( $new_settings ) ) {
return;
}
// Update the settings with the new values.
$new_settings = array_merge( $settings, $new_settings );
update_option( $this->get_option_name(), $new_settings );
}
/**
* Add settings fields for Role/User based access.
*/
protected function add_access_control_fields() {
$settings = $this->get_settings();
add_settings_field(
'roles',
esc_html__( 'Allowed roles', 'classifai' ),
[ $this, 'render_checkbox_group' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'roles',
'options' => $this->roles,
'default_values' => $settings['roles'],
'description' => __( 'Choose which roles are allowed to access this feature.', 'classifai' ),
'class' => 'allowed_roles_row',
]
);
add_settings_field(
'users',
esc_html__( 'Allowed users', 'classifai' ),
[ $this, 'render_allowed_users' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'users',
'default_value' => $settings['users'],
'description' => __( 'Users who have access to this feature.', 'classifai' ),
'class' => 'allowed_users_row',
]
);
add_settings_field(
'user_based_opt_out',
esc_html__( 'Enable user-based opt-out', 'classifai' ),
[ $this, 'render_input' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'user_based_opt_out',
'input_type' => 'checkbox',
'default_value' => $settings['user_based_opt_out'],
'description' => __( 'Enables ability for users to opt-out from their user profile page.', 'classifai' ),
'class' => 'classifai-user-based-opt-out',
]
);
}
/**
* Generic text input field callback
*
* @param array $args The args passed to add_settings_field.
*/
public function render_input( array $args ) {
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings( $option_index );
$type = $args['input_type'] ?? 'text';
$value = ( isset( $setting_index[ $args['label_for'] ] ) ) ? $setting_index[ $args['label_for'] ] : '';
// Check for a default value
$value = ( empty( $value ) && isset( $args['default_value'] ) ) ? $args['default_value'] : $value;
$attrs = '';
$class = '';
switch ( $type ) {
case 'text':
case 'password':
$attrs = ' value="' . esc_attr( $value ) . '"';
$class = 'regular-text';
break;
case 'number':
$attrs = ' value="' . esc_attr( $value ) . '"';
if ( isset( $args['max'] ) && is_numeric( $args['max'] ) ) {
$attrs .= ' max="' . esc_attr( (float) $args['max'] ) . '"';
}
if ( isset( $args['min'] ) && is_numeric( $args['min'] ) ) {
$attrs .= ' min="' . esc_attr( (float) $args['min'] ) . '"';
}
if ( isset( $args['step'] ) && is_numeric( $args['step'] ) ) {
$attrs .= ' step="' . esc_attr( (float) $args['step'] ) . '"';
}
$class = 'small-text';
break;
case 'checkbox':
$attrs = ' value="1"' . checked( '1', $value, false );
?>
<input
type="hidden"
name="<?php echo esc_attr( $this->get_option_name() ); ?><?php echo $option_index ? '[' . esc_attr( $option_index ) . ']' : ''; ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
value="0"
/>
<?php
break;
}
?>
<input
type="<?php echo esc_attr( $type ); ?>"
id="<?php echo esc_attr( $args['label_for'] ); ?>"
class="<?php echo esc_attr( $class ); ?>"
name="<?php echo esc_attr( $this->get_option_name() ); ?><?php echo $option_index ? '[' . esc_attr( $option_index ) . ']' : ''; ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
<?php echo $this->get_data_attribute( $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php echo $attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> />
<?php
if ( ! empty( $args['description'] ) ) {
echo '<span class="description classifai-input-description">' . wp_kses_post( $args['description'] ) . '</span>';
}
}
/**
* Generic prompt repeater field callback
*
* @since 2.4.0
*
* @param array $args The args passed to add_settings_field.
*/
public function render_prompt_repeater_field( array $args ) {
$option_index = $args['option_index'] ?? false;
$setting_index = $this->get_settings( $option_index );
$prompts = $setting_index[ $args['label_for'] ] ?? [];
$class = $args['class'] ?? 'large-text';
$placeholder = $args['placeholder'] ?? '';
$field_name_prefix = sprintf(
'%1$s%2$s[%3$s]',
$this->get_option_name(),
$option_index ? "[$option_index]" : '',
$args['label_for']
);
$prompts = empty( $prompts ) && isset( $args['default_value'] ) ? $args['default_value'] : $prompts;
$prompt_count = count( $prompts );
$field_index = 0;
?>
<?php foreach ( $prompts as $prompt ) : ?>
<?php
$is_default_prompt = ( isset( $prompt['default'] ) && 1 === (int) $prompt['default'] ) || 1 === $prompt_count;
$is_original_prompt = isset( $prompt['original'] ) && 1 === (int) $prompt['original'];
?>
<fieldset class="classifai-field-type-prompt-setting">
<?php if ( $is_original_prompt ) : ?>
<p class="classifai-original-prompt">
<?php
printf(
/* translators: %1$s is replaced with <strong>; %2$s with </strong>; %3$s with prompt. */
esc_html__( '%1$sClassifAI default prompt%2$s: %3$s', 'classifai' ),
'<strong>',
'</strong>',
esc_html( $placeholder )
);
?>
</p>
<?php endif; ?>
<input type="hidden"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][default]" ); ?>"
value="<?php echo esc_attr( $prompt['default'] ?? '' ); ?>"
class="js-setting-field__default">
<input type="hidden"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][original]" ); ?>"
value="<?php echo esc_attr( $prompt['original'] ?? '' ); ?>">
<label>
<?php esc_html_e( 'Title', 'classifai' ); ?> *
<span class="dashicons dashicons-editor-help"
title="<?php esc_attr_e( 'Short description of prompt to use for identification', 'classifai' ); ?>"></span>
<input type="text"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][title]" ); ?>"
placeholder="<?php esc_attr_e( 'Prompt title', 'classifai' ); ?>"
value="<?php echo esc_attr( $prompt['title'] ?? '' ); ?>"
<?php echo $is_original_prompt ? 'readonly' : ''; ?>
required>
</label>
<label>
<?php esc_html_e( 'Prompt', 'classifai' ); ?>
<textarea
class="<?php echo esc_attr( $class ); ?>"
rows="4"
name="<?php echo esc_attr( $field_name_prefix . "[$field_index][prompt]" ); ?>"
placeholder="<?php echo esc_attr( $placeholder ); ?>"
<?php echo $is_original_prompt ? 'readonly' : ''; ?>
><?php echo esc_textarea( $prompt['prompt'] ?? '' ); ?></textarea>
</label>
<div class="actions-rows">
<a href="#" class="action__set_default <?php echo $is_default_prompt ? 'selected' : ''; ?>">
<?php if ( $is_default_prompt ) : ?>
<?php esc_html_e( 'Default prompt', 'classifai' ); ?>
<?php else : ?>
<?php esc_html_e( 'Set as default prompt', 'classifai' ); ?>
<?php endif; ?>
</a>
<a href="#" class="action__remove_prompt" style="<?php echo 1 === $prompt_count || $is_original_prompt ? 'display:none;' : ''; ?>">
<?php esc_html_e( 'Trash', 'classifai' ); ?>
</a>
</div>
</fieldset>
<?php ++$field_index; ?>
<?php endforeach; ?>
<button
class="button-secondary js-classifai-add-prompt-fieldset">
<?php esc_html_e( 'Add new prompt', 'classifai' ); ?>
</button>
<?php
if ( ! empty( $args['description'] ) ) {
echo '<br /><span class="description classifai-input-description">' . wp_kses_post( $args['description'] ) . '</span>';
}
}
/**
* Renders a select menu
*
* @param array $args The args passed to add_settings_field.
*/
public function render_select( array $args ) {
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings( $option_index );
$saved = ( isset( $setting_index[ $args['label_for'] ] ) ) ? $setting_index[ $args['label_for'] ] : '';
// Check for a default value
$saved = ( empty( $saved ) && isset( $args['default_value'] ) ) ? $args['default_value'] : $saved;
$options = isset( $args['options'] ) ? $args['options'] : [];
?>
<select
id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="<?php echo esc_attr( $this->get_option_name() ); ?><?php echo $option_index ? '[' . esc_attr( $option_index ) . ']' : ''; ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
<?php echo $this->get_data_attribute( $args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
>
<?php foreach ( $options as $value => $name ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $saved, $value ); ?>>
<?php echo esc_attr( $name ); ?>
</option>
<?php endforeach; ?>
</select>
<?php
if ( ! empty( $args['description'] ) ) {
echo '<br /><span class="description">' . wp_kses_post( $args['description'] ) . '</span>';
}
}
/**
* Render a group of checkboxes.
*
* @param array $args The args passed to add_settings_field
*/
public function render_checkbox_group( array $args = array() ) {
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings();
// Iterate through all of our options.
foreach ( $args['options'] as $option_value => $option_label ) {
$value = '';
$default_key = array_search( $option_value, $args['default_values'], true );
// Get saved value, if any.
if ( isset( $setting_index[ $args['label_for'] ] ) ) {
$value = $setting_index[ $args['label_for'] ][ $option_value ] ?? '';
}
// If no saved value, check if we have a default value.
if ( empty( $value ) && '0' !== $value && isset( $args['default_values'][ $default_key ] ) ) {
$value = $args['default_values'][ $default_key ];
}
// Render checkbox.
printf(
'<p>
<label for="%1$s_%3$s_%4$s">
<input type="hidden" name="%1$s%2$s[%3$s][%4$s]" value="0" />
<input type="checkbox" id="%1$s_%3$s_%4$s" name="%1$s%2$s[%3$s][%4$s]" value="%4$s" %5$s />
%6$s
</label>
</p>',
esc_attr( $this->get_option_name() ),
$option_index ? '[' . esc_attr( $option_index ) . ']' : '',
esc_attr( $args['label_for'] ),
esc_attr( $option_value ),
checked( $value, $option_value, false ),
esc_html( $option_label )
);
}
// Render description, if any.
if ( ! empty( $args['description'] ) ) {
printf(
'<span class="description classifai-input-description">%s</span>',
esc_html( $args['description'] )
);
}
}
/**
* Renders the checkbox group for 'Generate descriptive text' setting.
*
* @param array $args The args passed to add_settings_field.
*/
public function render_auto_caption_fields( array $args ) {
$setting_index = $this->get_settings();
$default_value = '';
if ( isset( $setting_index['enable_image_captions'] ) ) {
if ( ! is_array( $setting_index['enable_image_captions'] ) ) {
if ( '1' === $setting_index['enable_image_captions'] ) {
$default_value = 'alt';
} elseif ( 'no' === $setting_index['enable_image_captions'] ) {
$default_value = '';
}
}
}
$checkbox_options = array(
'alt' => esc_html__( 'Alt text', 'classifai' ),
'caption' => esc_html__( 'Image caption', 'classifai' ),
'description' => esc_html__( 'Image description', 'classifai' ),
);
foreach ( $checkbox_options as $option_value => $option_label ) {
if ( isset( $setting_index['enable_image_captions'] ) ) {
if ( ! is_array( $setting_index['enable_image_captions'] ) ) {
$default_value = '1' === $setting_index['enable_image_captions'] ? 'alt' : '';
} else {
$default_value = $setting_index['enable_image_captions'][ $option_value ];
}
}
printf(
'<p>
<label for="%1$s_%2$s_%3$s">
<input type="hidden" name="%1$s[%2$s][%3$s]" value="0" />
<input type="checkbox" id="%1$s_%2$s_%3$s" name="%1$s[%2$s][%3$s]" value="%3$s" %4$s />
%5$s
</label>
</p>',
esc_attr( $this->get_option_name() ),
esc_attr( $args['label_for'] ),
esc_attr( $option_value ),
checked( $default_value, $option_value, false ),
esc_html( $option_label )
);
}
// Render description, if any.
if ( ! empty( $args['description'] ) ) {
printf(
'<span class="description classifai-input-description">%s</span>',
esc_html( $args['description'] )
);
}
}
/**
* Render a group of radio.
*
* @param array $args The args passed to add_settings_field
*/
public function render_radio_group( array $args = array() ) {
$option_index = isset( $args['option_index'] ) ? $args['option_index'] : false;
$setting_index = $this->get_settings( $option_index );
$value = $setting_index[ $args['label_for'] ] ?? '';
$options = $args['options'] ?? [];
if ( ! is_array( $options ) ) {
return;
}
// Iterate through all of our options.
foreach ( $options as $option_value => $option_label ) {
// Render radio button.
printf(
'<p>
<label for="%1$s_%3$s_%4$s">
<input type="radio" id="%1$s_%3$s_%4$s" name="%1$s%2$s[%3$s]" value="%4$s" %5$s />
%6$s
</label>
</p>',
esc_attr( $this->get_option_name() ),
$option_index ? '[' . esc_attr( $option_index ) . ']' : '',
esc_attr( $args['label_for'] ),
esc_attr( $option_value ),
checked( $value, $option_value, false ),
esc_html( $option_label )
);
}
// Render description, if any.
if ( ! empty( $args['description'] ) ) {
printf(
'<span class="description">%s</span>',
esc_html( $args['description'] )
);
}
}
/**
* Render allowed users input field.
*
* @param array $args The args passed to add_settings_field
*/
public function render_allowed_users( array $args = array() ) {
$setting_index = $this->get_settings();
$value = $setting_index[ $args['label_for'] ] ?? array();
?>
<div class="classifai-search-users-container">
<div class="classifai-user-selector" data-id="<?php echo esc_attr( $args['label_for'] ); ?>" id="<?php echo esc_attr( $args['label_for'] ); ?>-container"></div>
<input
id="<?php echo esc_attr( $args['label_for'] ); ?>"
class="classifai-search-users"
type="hidden"
name="<?php echo esc_attr( $this->get_option_name() ); ?>[<?php echo esc_attr( $args['label_for'] ); ?>]"
value="<?php echo esc_attr( implode( ',', $value ) ); ?>"
/>
</div>
<?php
if ( ! empty( $args['description'] ) ) {
echo '<span class="description">' . wp_kses_post( $args['description'] ) . '</span>';
}
}
/**
* Determine if the current user has access to the feature
*
* @return bool
*/
public function has_access(): bool {
$access = false;
$user_id = get_current_user_id();
$user = get_user_by( 'id', $user_id );
$user_roles = $user->roles ?? [];
$settings = $this->get_settings();
$feature_roles = $settings['roles'] ?? [];
$feature_users = array_map( 'absint', $settings['users'] ?? [] );
$user_based_opt_out_enabled = isset( $settings['user_based_opt_out'] ) && 1 === (int) $settings['user_based_opt_out'];
/*
* Checks if the user role has access to the feature.
*/
// For super admins that don't have a specific role on a site, treat them as admins.
if ( is_multisite() && is_super_admin( $user_id ) && empty( $user_roles ) ) {
$user_roles = [ 'administrator' ];
}
$access = ( ! empty( $feature_roles ) && ! empty( array_intersect( $user_roles, $feature_roles ) ) );
/*
* Checks if has access to the feature.
*/
if ( ! $access ) {
$access = ( ! empty( $feature_users ) && ! empty( in_array( $user_id, $feature_users, true ) ) );
}
/*