Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/tutorai/SettingsController.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Manage Settings.
4 + *
5 + * @package TutorPro\TutorAI
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 2.1.8
9 + */
10 +
11 + namespace TutorPro\TutorAI;
12 +
13 + use Tutor\Helpers\HttpHelper;
14 + use TUTOR\Input;
15 + use Tutor\Traits\JsonResponse;
16 + use TUTOR\User;
17 +
18 + /**
19 + * SettingsController Class.
20 + *
21 + * @since 2.1.8
22 + */
23 + class SettingsController {
24 + use JsonResponse;
25 +
26 + const CHATGPT_API_KEY = 'chatgpt_api_key';
27 + const CHATGPT_ENABLE = 'chatgpt_enable';
28 +
29 + /**
30 + * Register hooks.
31 + *
32 + * @since 2.1.8
33 + *
34 + * @return void
35 + */
36 + public function __construct() {
37 + add_filter( 'tutor/options/extend/attr', array( $this, 'add_chatgpt_settings_option' ) );
38 + add_action( 'wp_ajax_tutor_pro_chatgpt_save_settings', array( $this, 'save_settings' ) );
39 + }
40 +
41 + /**
42 + * Add ChatGPT settings to Tutor Settings > Advance section.
43 + *
44 + * @since 2.1.8
45 + *
46 + * @param array $attr existing settings attributes.
47 + *
48 + * @return array
49 + */
50 + public function add_chatgpt_settings_option( $attr ) {
51 + $chatgpt_settings = array(
52 + 'label' => __( 'AI Studio', 'tutor-pro' ),
53 + 'slug' => 'options',
54 + 'block_type' => 'uniform',
55 + 'fields' => array(
56 + array(
57 + 'key' => self::CHATGPT_ENABLE,
58 + 'type' => 'toggle_switch',
59 + 'label' => __( 'Enable', 'tutor-pro' ),
60 + 'default' => 'on',
61 + 'desc' => '',
62 + ),
63 + array(
64 + 'key' => self::CHATGPT_API_KEY,
65 + 'type' => 'text',
66 + 'label' => __( 'Insert OpenAI API Key', 'tutor-pro' ),
67 + 'default' => '',
68 + 'desc' => __( 'Find your Secret API key in your <a href="https://platform.openai.com/account/api-keys" target="blank">OpenAI User settings</a> and paste it here.', 'tutor-pro' ),
69 + 'placeholder' => __( 'API key', 'tutor-pro' ),
70 + ),
71 + ),
72 + );
73 +
74 + array_push( $attr['advanced']['blocks'], $chatgpt_settings );
75 +
76 + return $attr;
77 + }
78 +
79 + /**
80 + * API for saving ChatGPT API.
81 + *
82 + * @since 3.0.0
83 + *
84 + * @return void
85 + */
86 + public function save_settings() {
87 + tutor_utils()->check_nonce();
88 +
89 + if ( ! User::is_admin() ) {
90 + $this->json_response( tutor_utils()->error_message() );
91 + }
92 +
93 + $chatgpt_enable = Input::post( 'chatgpt_enable', true, Input::TYPE_BOOL );
94 + $api_key = Input::post( 'chatgpt_api_key', '' );
95 +
96 + if ( $chatgpt_enable && empty( $api_key ) ) {
97 + $this->json_response( __( 'API key required', 'tutor-pro' ), null, HttpHelper::STATUS_BAD_REQUEST );
98 + }
99 +
100 + $options = get_option( 'tutor_option' );
101 + $chatgpt_enable = $chatgpt_enable ? 'on' : 'off';
102 + if ( false === $options ) {
103 + $options = array(
104 + self::CHATGPT_API_KEY => $api_key,
105 + self::CHATGPT_ENABLE => $chatgpt_enable,
106 + );
107 + }
108 +
109 + $options[ self::CHATGPT_API_KEY ] = $api_key;
110 + $options[ self::CHATGPT_ENABLE ] = $chatgpt_enable;
111 +
112 + update_option( 'tutor_option', $options );
113 +
114 + $this->json_response( __( 'API key saved successfully!', 'tutor-pro' ) );
115 + }
116 + }
117 +