| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die; |
| 5 |
} |
| 6 |
|
| 7 |
class BDTHEMES_OPENAI_ADMIN_SETTINGS_PAGE { |
| 8 |
public function __construct() { |
| 9 |
add_action('admin_init', [$this, 'admin_settings_page_init']); |
| 10 |
add_filter( 'plugin_action_links_' . plugin_basename( BDT_AI_IMAGE__FILE__ ), [$this, 'plugin_action_links'] ); |
| 11 |
} |
| 12 |
|
| 13 |
public function admin_settings_page_init() { |
| 14 |
add_settings_section('bdthemes_openai_section', '<span class="ai-title-settings">OpenAI Settings</span>', [$this, 'openai_settings_section'], 'bdthemes-ai-image-options'); |
| 15 |
add_settings_field('bdthemes_openai_api_key', __('OpenAI API Key', 'ai-image'), [$this, 'openai_content_generator_callback'], 'bdthemes-ai-image-options', 'bdthemes_openai_section'); |
| 16 |
register_setting('bdthemes-ai-image-options', 'bdthemes_openai_api_key', ['sanitize_callback' => 'sanitize_text_field']); |
| 17 |
|
| 18 |
add_settings_section('bdthemes_gemini_section', '<span class="ai-title-settings">Google Gemini Settings</span>', [$this, 'gemini_settings_section'], 'bdthemes-ai-image-options'); |
| 19 |
add_settings_field('bdthemes_gemini_api_key', __('Gemini API Key', 'ai-image'), [$this, 'gemini_api_key_callback'], 'bdthemes-ai-image-options', 'bdthemes_gemini_section'); |
| 20 |
register_setting('bdthemes-ai-image-options', 'bdthemes_gemini_api_key', ['sanitize_callback' => 'sanitize_text_field']); |
| 21 |
|
| 22 |
add_settings_section('bdthemes_unsplash_section', '<span class="ai-title-settings">Unsplash Settings</span>', [$this, 'unsplash_settings_section'], 'bdthemes-ai-image-options'); |
| 23 |
add_settings_field('bdthemes_unsplash_access_key', __('Unsplash Access Key (Optional)', 'ai-image'), [$this, 'unsplash_api_key_callback'], 'bdthemes-ai-image-options', 'bdthemes_unsplash_section'); |
| 24 |
register_setting('bdthemes-ai-image-options', 'bdthemes_unsplash_access_key', ['sanitize_callback' => function( $v ) { return sanitize_text_field( is_string( $v ) ? trim( $v ) : $v ); }]); |
| 25 |
|
| 26 |
add_settings_section('bdthemes_giphy_section', '<span class="ai-title-settings">Giphy Settings</span>', [$this, 'giphy_settings_section'], 'bdthemes-ai-image-options'); |
| 27 |
add_settings_field('bdthemes_giphy_api_key', __('Giphy API Key (Optional)', 'ai-image'), [$this, 'giphy_api_key_callback'], 'bdthemes-ai-image-options', 'bdthemes_giphy_section'); |
| 28 |
register_setting('bdthemes-ai-image-options', 'bdthemes_giphy_api_key', ['sanitize_callback' => function( $v ) { return sanitize_text_field( is_string( $v ) ? trim( $v ) : $v ); }]); |
| 29 |
|
| 30 |
add_settings_section('bdthemes_pexels_section', '<span class="ai-title-settings">Pexels Settings</span>', [$this, 'pexels_settings_section'], 'bdthemes-ai-image-options'); |
| 31 |
add_settings_field('bdthemes_pexels_api_key', __('Pexels API Key (Optional)', 'ai-image'), [$this, 'pexels_api_key_callback'], 'bdthemes-ai-image-options', 'bdthemes_pexels_section'); |
| 32 |
register_setting('bdthemes-ai-image-options', 'bdthemes_pexels_api_key', ['sanitize_callback' => function( $v ) { return sanitize_text_field( is_string( $v ) ? trim( $v ) : $v ); }]); |
| 33 |
|
| 34 |
add_settings_section('bdthemes_pixabay_section', '<span class="ai-title-settings">Pixabay Settings</span>', [$this, 'pixabay_settings_section'], 'bdthemes-ai-image-options'); |
| 35 |
add_settings_field('bdthemes_pixabay_api_key', __('Pixabay API Key (Optional)', 'ai-image'), [$this, 'pixabay_api_key_callback'], 'bdthemes-ai-image-options', 'bdthemes_pixabay_section'); |
| 36 |
register_setting('bdthemes-ai-image-options', 'bdthemes_pixabay_api_key', ['sanitize_callback' => function( $v ) { return sanitize_text_field( is_string( $v ) ? trim( $v ) : $v ); }]); |
| 37 |
|
| 38 |
|
| 39 |
$provider_ids = array( 'pexels', 'pixabay', 'unsplash', 'openverse', 'wikimedia', 'giphy', 'openai', 'gemini' ); |
| 40 |
$sanitize_provider = function( $v ) { |
| 41 |
if ( $v === true || $v === '1' || $v === 1 ) return '1'; |
| 42 |
return '0'; |
| 43 |
}; |
| 44 |
foreach ( $provider_ids as $id ) { |
| 45 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_provider_' . $id, array( |
| 46 |
'sanitize_callback' => $sanitize_provider, |
| 47 |
) ); |
| 48 |
} |
| 49 |
|
| 50 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_max_upload_width', array( 'sanitize_callback' => 'absint' ) ); |
| 51 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_max_upload_height', array( 'sanitize_callback' => 'absint' ) ); |
| 52 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_default_provider', array( 'sanitize_callback' => 'sanitize_text_field' ) ); |
| 53 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_attribution', array( 'sanitize_callback' => $sanitize_provider ) ); |
| 54 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_hide_media_modal_tab', array( 'sanitize_callback' => $sanitize_provider ) ); |
| 55 |
register_setting( 'bdthemes-ai-image-options', 'bdthemes_ai_image_custom_sizes', array( 'sanitize_callback' => array( $this, 'sanitize_custom_sizes' ) ) ); |
| 56 |
} |
| 57 |
|
| 58 |
public function sanitize_custom_sizes( $value ) { |
| 59 |
if ( ! is_array( $value ) ) { |
| 60 |
return array(); |
| 61 |
} |
| 62 |
$sanitized = array(); |
| 63 |
foreach ( $value as $name => $size ) { |
| 64 |
if ( ! is_array( $size ) || empty( $size['width'] ) || empty( $size['height'] ) ) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
$clean_name = sanitize_title( $name ); |
| 68 |
if ( empty( $clean_name ) ) continue; |
| 69 |
$sanitized[ $clean_name ] = array( |
| 70 |
'width' => absint( $size['width'] ), |
| 71 |
'height' => absint( $size['height'] ), |
| 72 |
'crop' => ! empty( $size['crop'] ), |
| 73 |
); |
| 74 |
} |
| 75 |
return $sanitized; |
| 76 |
} |
| 77 |
public function openai_settings_section() { |
| 78 |
?> |
| 79 |
<p> |
| 80 |
<?php |
| 81 |
/* translators: %s: Link to OpenAI API keys page */ |
| 82 |
printf(esc_html__('Please enter your Openai API key. You can get your API key from %s .', 'ai-image'), '<a href="https://platform.openai.com/api-keys" target="_blank">here</a>'); |
| 83 |
?> |
| 84 |
<br> |
| 85 |
<?php |
| 86 |
/* translators: %s: Link to OpenAI documentation */ |
| 87 |
printf(esc_html__('Learn more about %s', 'ai-image'), '<a href="https://platform.openai.com/" target="_blank">how to use OpenAI API</a>'); |
| 88 |
?> |
| 89 |
</p> |
| 90 |
<?php |
| 91 |
} |
| 92 |
public function openai_content_generator_callback() { |
| 93 |
$api_key = get_option('bdthemes_openai_api_key'); |
| 94 |
printf( |
| 95 |
'<input type="text" name="bdthemes_openai_api_key" value="%s" class="large-text" placeholder="sk-..." />', |
| 96 |
isset($api_key) ? esc_attr($api_key) : '' |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
public function unsplash_settings_section() { |
| 101 |
printf( |
| 102 |
'<p>%s <strong>%s</strong></p>', |
| 103 |
esc_html__('Unsplash works with a default API key. You can optionally provide your own Access Key from', 'ai-image'), |
| 104 |
'<a href="https://unsplash.com/developers" target="_blank" rel="noopener">' . esc_html__('Unsplash Developers', 'ai-image') . '</a>' |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
public function unsplash_api_key_callback() { |
| 109 |
$api_key = get_option('bdthemes_unsplash_access_key'); |
| 110 |
printf( |
| 111 |
'<input type="text" name="bdthemes_unsplash_access_key" value="%s" class="large-text" placeholder="" /><p class="description">%s</p>', |
| 112 |
isset($api_key) ? esc_attr($api_key) : '', |
| 113 |
esc_html__('Leave empty to use the default API key.', 'ai-image') |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
public function giphy_settings_section() { |
| 118 |
printf( |
| 119 |
'<p>%s <strong>%s</strong></p>', |
| 120 |
esc_html__('Giphy works with a default API key. You can optionally provide your own API key from', 'ai-image'), |
| 121 |
'<a href="https://developers.giphy.com/" target="_blank" rel="noopener">' . esc_html__('GIPHY Developers', 'ai-image') . '</a>' |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
public function giphy_api_key_callback() { |
| 126 |
$api_key = get_option('bdthemes_giphy_api_key'); |
| 127 |
printf( |
| 128 |
'<input type="text" name="bdthemes_giphy_api_key" value="%s" class="large-text" placeholder="" /><p class="description">%s</p>', |
| 129 |
isset($api_key) ? esc_attr($api_key) : '', |
| 130 |
esc_html__('Leave empty to use the default API key.', 'ai-image') |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
public function gemini_settings_section() { |
| 135 |
printf( |
| 136 |
'<p>%s <a href="https://aistudio.google.com/app/apikey" target="_blank" rel="noopener">%s</a>.</p>', |
| 137 |
esc_html__('Get your free Google Gemini API key from', 'ai-image'), |
| 138 |
esc_html__('Google AI Studio', 'ai-image') |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
public function gemini_api_key_callback() { |
| 143 |
$api_key = get_option('bdthemes_gemini_api_key'); |
| 144 |
printf( |
| 145 |
'<input type="text" name="bdthemes_gemini_api_key" value="%s" class="large-text" placeholder="AIza..." />', |
| 146 |
isset($api_key) ? esc_attr($api_key) : '' |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
public function pexels_settings_section() { |
| 151 |
printf( |
| 152 |
'<p>%s <strong>%s</strong></p>', |
| 153 |
esc_html__('Pexels works with a default API key. You can optionally provide your own API key from', 'ai-image'), |
| 154 |
'<a href="https://www.pexels.com/api/" target="_blank" rel="noopener">' . esc_html__('Pexels API', 'ai-image') . '</a>' |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
public function pexels_api_key_callback() { |
| 159 |
$api_key = get_option('bdthemes_pexels_api_key'); |
| 160 |
printf( |
| 161 |
'<input type="text" name="bdthemes_pexels_api_key" value="%s" class="large-text" placeholder="" /><p class="description">%s</p>', |
| 162 |
isset($api_key) ? esc_attr($api_key) : '', |
| 163 |
esc_html__('Leave empty to use the default API key.', 'ai-image') |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
public function pixabay_settings_section() { |
| 168 |
printf( |
| 169 |
'<p>%s <strong>%s</strong></p>', |
| 170 |
esc_html__('Pixabay works with a default API key. You can optionally provide your own API key from', 'ai-image'), |
| 171 |
'<a href="https://pixabay.com/api/docs/" target="_blank" rel="noopener">' . esc_html__('Pixabay API', 'ai-image') . '</a>' |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
public function pixabay_api_key_callback() { |
| 176 |
$api_key = get_option('bdthemes_pixabay_api_key'); |
| 177 |
printf( |
| 178 |
'<input type="text" name="bdthemes_pixabay_api_key" value="%s" class="large-text" placeholder="" /><p class="description">%s</p>', |
| 179 |
isset($api_key) ? esc_attr($api_key) : '', |
| 180 |
esc_html__('Leave empty to use the default API key.', 'ai-image') |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Plugin action links |
| 186 |
* @access public |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
|
| 190 |
public function plugin_action_links( $plugin_meta ) { |
| 191 |
|
| 192 |
$row_meta = [ |
| 193 |
'settings' => '<a href="' . esc_url( admin_url( 'options-general.php?page=ai-image-settings' ) ) . '" aria-label="' . esc_attr(__('Go to Settings', 'ai-image')) . '">' . __('Settings', 'ai-image') . '</a>', |
| 194 |
]; |
| 195 |
|
| 196 |
$plugin_meta = array_merge($plugin_meta, $row_meta); |
| 197 |
|
| 198 |
return $plugin_meta; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
new BDTHEMES_OPENAI_ADMIN_SETTINGS_PAGE(); |
| 203 |
|