| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the plugin settings UI. |
| 4 |
* |
| 5 |
* @package civist |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'WPINC' ) ) { |
| 9 |
die; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Imports |
| 14 |
*/ |
| 15 |
require_once 'class-civist-registration.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Handles the plugin settings UI. |
| 19 |
*/ |
| 20 |
class Civist_Settings { |
| 21 |
/** |
| 22 |
* The plugin settings name. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private $plugin_settings_name; |
| 27 |
/** |
| 28 |
* The plugin slug. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private $plugin_slug; |
| 33 |
/** |
| 34 |
* The plugin settings manager. |
| 35 |
* |
| 36 |
* @var Civist_Settings_Manager |
| 37 |
*/ |
| 38 |
private $settings_manager; |
| 39 |
/** |
| 40 |
* The plugin connected flag. |
| 41 |
* |
| 42 |
* @var bool |
| 43 |
*/ |
| 44 |
private $is_plugin_connected; |
| 45 |
|
| 46 |
/** |
| 47 |
* The Civist_Settings class constructor. |
| 48 |
* |
| 49 |
* @param string $plugin_slug The slug of the plugin. |
| 50 |
* @param Civist_Settings_Manager $settings_manager The plugin settings manager. |
| 51 |
*/ |
| 52 |
public function __construct( $plugin_slug, Civist_Settings_Manager $settings_manager ) { |
| 53 |
$this->plugin_slug = $plugin_slug; |
| 54 |
$this->plugin_settings_name = $this->plugin_slug . '_settings'; |
| 55 |
$this->settings_manager = $settings_manager; |
| 56 |
$this->is_plugin_connected = $this->settings_manager->is_connected(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Renders the options page. |
| 61 |
*/ |
| 62 |
public function options_page() { |
| 63 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 64 |
wp_die( "You don't have sufficient permissions to access this page." ); |
| 65 |
} |
| 66 |
require_once 'civist-container.php'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register the settings link in the plugins page. |
| 71 |
* |
| 72 |
* @param array $links The list of links. |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function register_settings_plugin_action_link( $links ) { |
| 76 |
$is_connected = $this->is_plugin_connected; |
| 77 |
// translators: Settings link text. |
| 78 |
$text = _x( 'Settings', 'wp.plugin.link.settings', 'civist' ); |
| 79 |
$page = $is_connected ? $this->plugin_slug . '-settings' : $this->plugin_slug; |
| 80 |
$url = $is_connected ? get_admin_url( null, 'options-general.php?page=' . $page ) : get_admin_url( null, 'admin.php?page=' . $page ); |
| 81 |
$links[] = '<a href="' . esc_url( $url ) . '">' . $text . '</a>'; |
| 82 |
return $links; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register the plugin settings using the WordPress settings API. |
| 87 |
*/ |
| 88 |
public function register_settings() { |
| 89 |
register_setting( |
| 90 |
$this->plugin_settings_name, |
| 91 |
$this->plugin_slug, |
| 92 |
array( |
| 93 |
'sanitize_callback' => array( $this, 'sanitize_settings_callback' ), |
| 94 |
) |
| 95 |
); |
| 96 |
add_settings_section( |
| 97 |
$this->plugin_settings_name . '_settings_section', |
| 98 |
'Advanced Settings', |
| 99 |
'', |
| 100 |
$this->plugin_settings_name |
| 101 |
); |
| 102 |
|
| 103 |
add_settings_field( |
| 104 |
'api_key_id', |
| 105 |
'API Key ID', |
| 106 |
array( $this, 'render_text_field' ), |
| 107 |
$this->plugin_settings_name, |
| 108 |
$this->plugin_settings_name . '_settings_section', |
| 109 |
'api_key_id' |
| 110 |
); |
| 111 |
|
| 112 |
add_settings_field( |
| 113 |
'api_key', |
| 114 |
'API Key', |
| 115 |
array( $this, 'render_text_field' ), |
| 116 |
$this->plugin_settings_name, |
| 117 |
$this->plugin_settings_name . '_settings_section', |
| 118 |
'api_key' |
| 119 |
); |
| 120 |
|
| 121 |
add_settings_field( |
| 122 |
'api_url', |
| 123 |
'API URL', |
| 124 |
array( $this, 'render_text_field' ), |
| 125 |
$this->plugin_settings_name, |
| 126 |
$this->plugin_settings_name . '_settings_section', |
| 127 |
'api_url' |
| 128 |
); |
| 129 |
|
| 130 |
add_settings_field( |
| 131 |
'widget_url', |
| 132 |
'Widget URL', |
| 133 |
array( $this, 'render_text_field' ), |
| 134 |
$this->plugin_settings_name, |
| 135 |
$this->plugin_settings_name . '_settings_section', |
| 136 |
'widget_url' |
| 137 |
); |
| 138 |
|
| 139 |
add_settings_field( |
| 140 |
'registration_url', |
| 141 |
'Registration URL', |
| 142 |
array( $this, 'render_text_field' ), |
| 143 |
$this->plugin_settings_name, |
| 144 |
$this->plugin_settings_name . '_settings_section', |
| 145 |
'registration_url' |
| 146 |
); |
| 147 |
|
| 148 |
add_settings_field( |
| 149 |
'oembed_url', |
| 150 |
'oEmbed URL', |
| 151 |
array( $this, 'render_text_field' ), |
| 152 |
$this->plugin_settings_name, |
| 153 |
$this->plugin_settings_name . '_settings_section', |
| 154 |
'oembed_url' |
| 155 |
); |
| 156 |
|
| 157 |
add_settings_field( |
| 158 |
'geoip_url', |
| 159 |
'GeoIP URL', |
| 160 |
array( $this, 'render_text_field' ), |
| 161 |
$this->plugin_settings_name, |
| 162 |
$this->plugin_settings_name . '_settings_section', |
| 163 |
'geoip_url' |
| 164 |
); |
| 165 |
|
| 166 |
add_settings_field( |
| 167 |
'version', |
| 168 |
'Version', |
| 169 |
array( $this, 'render_text_field' ), |
| 170 |
$this->plugin_settings_name, |
| 171 |
$this->plugin_settings_name . '_settings_section', |
| 172 |
'version' |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Renders a text field in the settings form. |
| 178 |
* |
| 179 |
* @param string $option_key The plugin option field identifier. |
| 180 |
*/ |
| 181 |
public function render_text_field( $option_key ) { |
| 182 |
$options = $this->settings_manager->get_all(); |
| 183 |
?> |
| 184 |
<input |
| 185 |
type="text" |
| 186 |
id="<?php echo esc_attr( $option_key ); ?>" |
| 187 |
name="<?php echo esc_attr( $this->plugin_slug . '[' . $option_key . ']' ); ?>" |
| 188 |
value="<?php echo array_key_exists( $option_key, $options ) ? esc_html( $options[ $option_key ] ) : ''; ?>" |
| 189 |
<?php |
| 190 |
if ( $this->settings_manager->is_overriden( $option_key ) ) { |
| 191 |
echo 'disabled'; |
| 192 |
} |
| 193 |
?> |
| 194 |
> |
| 195 |
<?php |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Renders the advanced options page. |
| 200 |
* |
| 201 |
* For internal/testing purposes only. |
| 202 |
* TODO: Remove when advanced settings are no longer necessary. |
| 203 |
*/ |
| 204 |
public function advanced_options_page() { |
| 205 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 206 |
wp_die( "You don't have sufficient permissions to access this page." ); |
| 207 |
} |
| 208 |
?> |
| 209 |
<form id="civist-settings-form" action='options.php' method='post'> |
| 210 |
<h2>Civist</h2> |
| 211 |
<?php |
| 212 |
settings_fields( $this->plugin_settings_name ); |
| 213 |
do_settings_sections( $this->plugin_settings_name ); |
| 214 |
submit_button(); |
| 215 |
?> |
| 216 |
</form> |
| 217 |
<?php |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Sanitizes the settings array before it is saved to the database. |
| 222 |
* |
| 223 |
* @param array $input The raw settings input submitted from the form. |
| 224 |
* @return array The sanitized settings. |
| 225 |
*/ |
| 226 |
public function sanitize_settings_callback( $input ) { |
| 227 |
// Ensure we are working with an array. |
| 228 |
if ( ! is_array( $input ) ) { |
| 229 |
return array(); |
| 230 |
} |
| 231 |
|
| 232 |
$sanitized = array(); |
| 233 |
|
| 234 |
// Safely sanitize standard text fields. |
| 235 |
if ( isset( $input['api_key_id'] ) ) { |
| 236 |
$sanitized['api_key_id'] = sanitize_text_field( $input['api_key_id'] ); |
| 237 |
} |
| 238 |
if ( isset( $input['version'] ) ) { |
| 239 |
$sanitized['version'] = sanitize_text_field( $input['version'] ); |
| 240 |
} |
| 241 |
|
| 242 |
// Safely sanitize URLs. |
| 243 |
$url_fields = array( 'api_url', 'widget_url', 'registration_url', 'oembed_url', 'geoip_url' ); |
| 244 |
foreach ( $url_fields as $field ) { |
| 245 |
if ( isset( $input[ $field ] ) ) { |
| 246 |
$sanitized[ $field ] = sanitize_url( $input[ $field ] ); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// Custom sanitizer for API key. |
| 251 |
if ( isset( $input['api_key'] ) ) { |
| 252 |
$sanitized['api_key'] = Civist_Settings_Manager::sanitize_key( $input['api_key'] ); |
| 253 |
} |
| 254 |
|
| 255 |
return $sanitized; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|