| 1 |
<?php |
| 2 |
|
| 3 |
namespace UiCoreElements; |
| 4 |
|
| 5 |
use UiCoreElements\Utils\Newsletter_Services as Services; |
| 6 |
|
| 7 |
/** |
| 8 |
* Admin Pages Handler |
| 9 |
*/ |
| 10 |
class Admin |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Constructor function to initialize hooks |
| 14 |
* |
| 15 |
* @return void |
| 16 |
* @author Andrei Voica <andrei@uicore.co> |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
add_action('admin_menu', [$this, 'admin_menu']); |
| 22 |
add_action('admin_init', [$this, 'init_hooks']); |
| 23 |
|
| 24 |
// TODO: Remove this after, at least, 5 releases or 1 year from 1.3.3 |
| 25 |
// Mailchimp to generic api key fields migration |
| 26 |
if ( |
| 27 |
get_option('uicore_elements_mailchimp_secret_key') |
| 28 |
&& !get_option('uicore_elements_newsletter_service_key') |
| 29 |
) { |
| 30 |
$value = get_option('uicore_elements_mailchimp_secret_key'); |
| 31 |
update_option('uicore_elements_newsletter_service_key', $value); |
| 32 |
delete_option('uicore_elements_mailchimp_secret_key'); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Add admin menu page |
| 38 |
* |
| 39 |
* @return void |
| 40 |
* @author Andrei Voica <andrei@uicore.co> |
| 41 |
* @since 1.0.5 |
| 42 |
*/ |
| 43 |
public function admin_menu() |
| 44 |
{ |
| 45 |
// Settings page (only required if uicore framework is not active) |
| 46 |
// if (!\class_exists('\UiCore\Helper')) { |
| 47 |
$hook = add_submenu_page( |
| 48 |
'options-general.php', |
| 49 |
'UiCore Elements', |
| 50 |
'UiCore Elements', |
| 51 |
'manage_options', |
| 52 |
'uicore-elements', |
| 53 |
[$this, 'plugin_page'] |
| 54 |
); |
| 55 |
|
| 56 |
// } |
| 57 |
|
| 58 |
// Connect handle |
| 59 |
// add_submenu_page( |
| 60 |
// null, |
| 61 |
// 'UiCore Connect', |
| 62 |
// 'UiCore Connect', |
| 63 |
// 'manage_options', |
| 64 |
// 'uicore_connect_free', |
| 65 |
// [$this, 'connect_page_callback'] |
| 66 |
// ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Initialize hooks for settings fields and sections |
| 71 |
* |
| 72 |
* @return void |
| 73 |
* @author Andrei Voica <andrei@uicore.co> |
| 74 |
* @since 1.0.5 |
| 75 |
*/ |
| 76 |
public function init_hooks() |
| 77 |
{ |
| 78 |
// Register settings for reCAPTCHA |
| 79 |
register_setting('uicore_elements_recaptcha', 'uicore_elements_recaptcha_site_key', ['sanitize_callback' => 'sanitize_text_field']); |
| 80 |
register_setting('uicore_elements_recaptcha', 'uicore_elements_recaptcha_secret_key', ['sanitize_callback' => 'sanitize_text_field']); |
| 81 |
|
| 82 |
// Register settings for Mailchimp |
| 83 |
register_setting('uicore_elements_newsletter_service', 'uicore_elements_newsletter_service_key', ['sanitize_callback' => 'sanitize_text_field']); |
| 84 |
|
| 85 |
// Add settings sections |
| 86 |
add_settings_section('uicore_elements_recaptcha_section', 'reCAPTCHA Keys', [$this, 'recaptcha_section'], 'uicore_elements_recaptcha'); |
| 87 |
add_settings_section('uicore_elements_newsletter_service_section', 'Newsletter Service Key', [$this, 'newsletter_service_section'], 'uicore_elements_newsletter_service'); |
| 88 |
|
| 89 |
// Add settings fields |
| 90 |
add_settings_field('uicore_elements_recaptcha_site_key', 'Site Key', [$this, 'site_key'], 'uicore_elements_recaptcha', 'uicore_elements_recaptcha_section'); |
| 91 |
add_settings_field('uicore_elements_recaptcha_secret_key', 'Secret Key', [$this, 'secret_key'], 'uicore_elements_recaptcha', 'uicore_elements_recaptcha_section'); |
| 92 |
add_settings_field('uicore_elements_newsletter_service_key', 'API Key', [$this, 'newsletter_service_key'], 'uicore_elements_newsletter_service', 'uicore_elements_newsletter_service_section'); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Render plugin page |
| 97 |
* |
| 98 |
* @return void |
| 99 |
* @author Andrei Voica <andrei@uicore.co> |
| 100 |
* @since 1.0.5 |
| 101 |
*/ |
| 102 |
public function plugin_page() |
| 103 |
{ |
| 104 |
// check user capabilities |
| 105 |
if (!current_user_can('manage_options')) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
// show error/update messages |
| 111 |
settings_errors('uicoreelements_messages'); |
| 112 |
|
| 113 |
// display plugin page |
| 114 |
?> |
| 115 |
<div class="wrap"> |
| 116 |
<h1>UiCore Elements Settings</h1> |
| 117 |
|
| 118 |
<form method="post" action="options.php" style="margin-top:40px"> |
| 119 |
<?php |
| 120 |
settings_fields('uicore_elements_recaptcha'); |
| 121 |
do_settings_sections('uicore_elements_recaptcha'); |
| 122 |
submit_button(); |
| 123 |
?> |
| 124 |
</form> |
| 125 |
|
| 126 |
<form method="post" action="options.php" style="margin-top:40px"> |
| 127 |
<?php |
| 128 |
settings_fields('uicore_elements_newsletter_service'); |
| 129 |
do_settings_sections('uicore_elements_newsletter_service'); |
| 130 |
submit_button(); |
| 131 |
?> |
| 132 |
</form> |
| 133 |
|
| 134 |
</div> |
| 135 |
<?php |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Newsletter Services sections |
| 140 |
* |
| 141 |
* @return void |
| 142 |
* @author Andrei Voica <andrei@uicore.co> |
| 143 |
* |
| 144 |
* recaptcha @since 1.0.5 |
| 145 |
* mailchimp @since 1.0.7 |
| 146 |
* constant contact, brevo, a, c @since 1.3.3 |
| 147 |
*/ |
| 148 |
|
| 149 |
public function recaptcha_section() |
| 150 |
{ |
| 151 |
echo '<p class="description">Go to your Google <a href="https://www.google.com/recaptcha/admin/create" target="_blank">reCAPTCHA</a>, choose between V2 or V3 versions and create your API keys</p>'; |
| 152 |
} |
| 153 |
public function newsletter_service_section() |
| 154 |
{ |
| 155 |
$services = '<strong>' . implode('</strong>, <strong>', Services::get_services_list('names')) . '</strong>'; |
| 156 |
printf( |
| 157 |
'<p class="description">%s %s.</p>', |
| 158 |
esc_html__('Set your newsletter service provider API key. We support the following services:', 'uicore-elements'), |
| 159 |
Helper::esc_string($services) //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Render Uicore Elements settings fields |
| 165 |
* |
| 166 |
* @return void |
| 167 |
* @author Andrei Voica <andrei@uicore.co> |
| 168 |
* |
| 169 |
* site key, secret key @since 1.0.5 |
| 170 |
* mailchimp key @since 1.0.7 |
| 171 |
* mailchim update for XX @since 1.3.3 |
| 172 |
*/ |
| 173 |
|
| 174 |
public function site_key() |
| 175 |
{ |
| 176 |
$site_key = get_option('uicore_elements_recaptcha_site_key'); |
| 177 |
echo '<input type="text" name="uicore_elements_recaptcha_site_key" value="' . esc_attr($site_key) . '" class="regular-text" />'; |
| 178 |
} |
| 179 |
public function secret_key() |
| 180 |
{ |
| 181 |
$secret_key = get_option('uicore_elements_recaptcha_secret_key'); |
| 182 |
echo '<input type="text" name="uicore_elements_recaptcha_secret_key" value="' . esc_attr($secret_key) . '" class="regular-text" />'; |
| 183 |
} |
| 184 |
public function newsletter_service_key() |
| 185 |
{ |
| 186 |
$secret_key = get_option('uicore_elements_newsletter_service_key'); |
| 187 |
echo '<input type="text" name="uicore_elements_newsletter_service_key" value="' . esc_attr($secret_key) . '" class="regular-text" />'; |
| 188 |
} |
| 189 |
} |
| 190 |
|