| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MultiSafepay\WooCommerce\Settings; |
| 4 |
|
| 5 |
/** |
| 6 |
* The settings page controller. |
| 7 |
* Defines all the functionalities needed on the settings page |
| 8 |
*/ |
| 9 |
class SettingsController { |
| 10 |
|
| 11 |
/** |
| 12 |
* In plugin version < 4.0.0 the options multisafepay_testmode, and multisafepay_debugmode |
| 13 |
* had been stored as strings and returns yes - no. |
| 14 |
* |
| 15 |
* This function also works to returns booleans instead of strings for |
| 16 |
* multisafepay_second_chance, multisafepay_testmode, multisafepay_debugmode options |
| 17 |
* |
| 18 |
* @see https://developer.wordpress.org/reference/hooks/option_option/ |
| 19 |
* |
| 20 |
* @param string $value |
| 21 |
* @return boolean |
| 22 |
*/ |
| 23 |
public function filter_multisafepay_settings_as_booleans( string $value ): bool { |
| 24 |
if ( 'yes' === $value || '1' === $value ) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* This function returns int instead of strings for multisafepay_time_active |
| 32 |
* |
| 33 |
* @see https://developer.wordpress.org/reference/hooks/option_option/ |
| 34 |
* |
| 35 |
* @param string $value |
| 36 |
* @return integer |
| 37 |
*/ |
| 38 |
public function filter_multisafepay_settings_as_int( string $value ): int { |
| 39 |
return (int) $value; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Register the stylesheets and javascript files for the settings page. |
| 44 |
* |
| 45 |
* @see https://developer.wordpress.org/reference/functions/wp_enqueue_style/ |
| 46 |
* @see https://developer.wordpress.org/reference/functions/wp_enqueue_script/ |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function enqueue_styles_and_scripts(): void { |
| 51 |
if ( get_current_screen()->base === 'toplevel_page_multisafepay-settings' ) { |
| 52 |
wp_enqueue_style( 'multisafepay-admin-css', MULTISAFEPAY_PLUGIN_URL . '/assets/admin/css/multisafepay-admin.css', array(), MULTISAFEPAY_PLUGIN_VERSION, 'all' ); |
| 53 |
} |
| 54 |
$sections = array( 'multisafepay_applepay', 'multisafepay_googlepay', 'multisafepay_bancontact' ); |
| 55 |
if ( isset( $_GET['section'] ) && in_array( $_GET['section'], $sections, true ) ) { |
| 56 |
wp_enqueue_script( 'multisafepay-admin-js', MULTISAFEPAY_PLUGIN_URL . '/assets/admin/js/multisafepay-admin.js', array(), MULTISAFEPAY_PLUGIN_VERSION, true ); |
| 57 |
wp_localize_script( |
| 58 |
'multisafepay-admin-js', |
| 59 |
'multisafepayAdminData', |
| 60 |
array( |
| 61 |
'directPaymentConfirmation' => array( |
| 62 |
'title' => __( 'Direct payment activation confirmation', 'multisafepay' ), |
| 63 |
'messageTemplate' => __( "Before enabling %payment_method% Direct, ensure all technical and configuration prerequisites are completed.\n\nBy clicking OK, you explicitly confirm that all prerequisites have been fulfilled.", 'multisafepay' ), |
| 64 |
), |
| 65 |
) |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Register the top-level "MultiSafepay" admin menu and the Settings entry. |
| 72 |
* |
| 73 |
* The parent menu slug is `multisafepay-settings` so the existing URL |
| 74 |
* (admin.php?page=multisafepay-settings) keeps working without any |
| 75 |
* redirect. Sibling plugins can attach their own |
| 76 |
* sub-pages to this same parent via `add_submenu_page`. |
| 77 |
* |
| 78 |
* @see https://developer.wordpress.org/reference/functions/add_menu_page/ |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function register_common_settings_page(): void { |
| 83 |
$title = sprintf( __( 'MultiSafepay Settings v. %s', 'multisafepay' ), MULTISAFEPAY_PLUGIN_VERSION ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment |
| 84 |
|
| 85 |
add_menu_page( |
| 86 |
esc_html( $title ), |
| 87 |
__( 'MultiSafepay', 'multisafepay' ), |
| 88 |
'manage_woocommerce', |
| 89 |
'multisafepay-settings', |
| 90 |
array( $this, 'display_multisafepay_settings' ), |
| 91 |
$this->get_menu_icon(), |
| 92 |
57 |
| 93 |
); |
| 94 |
|
| 95 |
// Override the auto-generated first submenu label ("MultiSafepay") with "Settings". |
| 96 |
add_submenu_page( |
| 97 |
'multisafepay-settings', |
| 98 |
esc_html( $title ), |
| 99 |
__( 'Settings', 'multisafepay' ), |
| 100 |
'manage_woocommerce', |
| 101 |
'multisafepay-settings', |
| 102 |
array( $this, 'display_multisafepay_settings' ) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Resolve the icon shown next to the top-level "MultiSafepay" admin menu. |
| 108 |
* |
| 109 |
* Returns a base64-encoded data URI of the SVG file if it exists, allowing |
| 110 |
* WordPress to apply its automatic admin-theme color filters. Falls back to |
| 111 |
* a Dashicon if the file is missing. |
| 112 |
* |
| 113 |
* The result is cached in a static variable to avoid repeated filesystem reads |
| 114 |
* and base64 encoding on every admin request. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
private function get_menu_icon(): string { |
| 119 |
static $cached_icon = null; |
| 120 |
|
| 121 |
if ( null !== $cached_icon ) { |
| 122 |
return $cached_icon; |
| 123 |
} |
| 124 |
|
| 125 |
$svg_path = MULTISAFEPAY_PLUGIN_DIR_PATH . 'assets/admin/img/multisafepay-menu-icon.svg'; |
| 126 |
|
| 127 |
if ( is_readable( $svg_path ) ) { |
| 128 |
$svg = file_get_contents( $svg_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 129 |
if ( ! empty( $svg ) ) { |
| 130 |
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Used for SVG data URI, not for obfuscation. |
| 131 |
$cached_icon = 'data:image/svg+xml;base64,' . base64_encode( $svg ); |
| 132 |
return $cached_icon; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$cached_icon = 'dashicons-cart'; |
| 137 |
return $cached_icon; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Display the common settings page view. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function display_multisafepay_settings(): void { |
| 146 |
$tab_active = $this->get_tab_active(); |
| 147 |
require_once MULTISAFEPAY_PLUGIN_DIR_PATH . 'templates/multisafepay-settings-display.php'; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Display the support settings page view. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public function display_multisafepay_support_section(): void { |
| 156 |
require_once MULTISAFEPAY_PLUGIN_DIR_PATH . 'templates/partials/multisafepay-settings-support-display.php'; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Display the status tab. |
| 161 |
* |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
public function display_multisafepay_status_section(): void { |
| 165 |
$status_controller = new StatusController(); |
| 166 |
$status_controller->display(); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Display the log tab. |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function display_multisafepay_logs_section() { |
| 175 |
$logs_controller = new LogsController(); |
| 176 |
$logs_controller->display(); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns active tab defined in get variable |
| 181 |
* |
| 182 |
* @return string |
| 183 |
*/ |
| 184 |
private function get_tab_active(): string { |
| 185 |
if ( isset( $_GET['tab'] ) && '' !== $_GET['tab'] ) { |
| 186 |
return wp_unslash( sanitize_key( $_GET['tab'] ) ); |
| 187 |
} |
| 188 |
return 'general'; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Register general settings in common settings page |
| 193 |
* |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public function register_common_settings(): void { |
| 197 |
$settings_fields = new SettingsFields(); |
| 198 |
$settings = $settings_fields->get_settings(); |
| 199 |
foreach ( $settings as $tab_key => $section ) { |
| 200 |
$this->add_settings_section( $tab_key, $section['title'] ); |
| 201 |
foreach ( $section['fields'] as $field ) { |
| 202 |
$this->register_setting( $field, $tab_key ); |
| 203 |
$this->add_settings_field( $field, $tab_key ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add settings field |
| 210 |
* |
| 211 |
* @see https://developer.wordpress.org/reference/functions/add_settings_field/ |
| 212 |
* |
| 213 |
* @param array $field The field |
| 214 |
* @param string $tab_key The key of the tab |
| 215 |
* @return void |
| 216 |
*/ |
| 217 |
private function add_settings_field( array $field, string $tab_key ): void { |
| 218 |
add_settings_field( |
| 219 |
$field['id'], |
| 220 |
$this->generate_label_for_settings_field( $field ), |
| 221 |
array( $this, 'display_field' ), |
| 222 |
'multisafepay-settings-' . $tab_key, |
| 223 |
$tab_key, |
| 224 |
array( |
| 225 |
'field' => $field, |
| 226 |
) |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Return the label tag to be used in add_settings_field |
| 232 |
* |
| 233 |
* @param array $field The settings field array |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
private function generate_label_for_settings_field( array $field ): string { |
| 237 |
if ( '' === $field['tooltip'] ) { |
| 238 |
return sprintf( '<label for="%s">%s</label>', $field['id'], $field['label'] ); |
| 239 |
} |
| 240 |
return sprintf( '<label for="%s">%s %s</label>', $field['id'], $field['label'], wc_help_tip( $field['tooltip'] ) ); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Filter which set the settings page and adds a screen options of WooCommerce |
| 245 |
* |
| 246 |
* @see http://hookr.io/filters/woocommerce_screen_ids/ |
| 247 |
* |
| 248 |
* @param array $screen |
| 249 |
* @return array |
| 250 |
*/ |
| 251 |
public function set_wc_screen_options_in_common_settings_page( array $screen ): array { |
| 252 |
$screen[] = 'toplevel_page_multisafepay-settings'; |
| 253 |
return $screen; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Register setting |
| 258 |
* |
| 259 |
* @see https://developer.wordpress.org/reference/functions/register_setting/ |
| 260 |
* |
| 261 |
* @param array $field |
| 262 |
* @param string $tab_key |
| 263 |
* @return void |
| 264 |
*/ |
| 265 |
private function register_setting( array $field, string $tab_key ): void { |
| 266 |
register_setting( |
| 267 |
'multisafepay-settings-' . $tab_key, |
| 268 |
$field['id'], |
| 269 |
array( |
| 270 |
'type' => $field['setting_type'], |
| 271 |
'show_in_rest' => false, |
| 272 |
'sanitize_callback' => $field['callback'], |
| 273 |
) |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Add settings section |
| 279 |
* |
| 280 |
* @see https://developer.wordpress.org/reference/functions/add_settings_section/ |
| 281 |
* |
| 282 |
* @param string $section_key |
| 283 |
* @param string $section_title |
| 284 |
* @return void |
| 285 |
*/ |
| 286 |
private function add_settings_section( string $section_key, string $section_title ): void { |
| 287 |
add_settings_section( |
| 288 |
$section_key, |
| 289 |
$section_title, |
| 290 |
array( $this, 'display_intro_section' ), |
| 291 |
'multisafepay-settings-' . $section_key |
| 292 |
); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Callback to display the title on each settings sections |
| 297 |
* |
| 298 |
* @see https://developer.wordpress.org/reference/functions/add_settings_section/ |
| 299 |
* |
| 300 |
* @param array $args |
| 301 |
* @return void |
| 302 |
*/ |
| 303 |
public function display_intro_section( array $args ): void { |
| 304 |
$settings_fields = new SettingsFields(); |
| 305 |
$settings = $settings_fields->get_settings(); |
| 306 |
if ( ! empty( $settings[ $args['id'] ]['intro'] ) ) { |
| 307 |
esc_html( (string) printf( '<p>%s</p>', esc_html( $settings[ $args['id'] ]['intro'] ) ) ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Return the HTML view by field. |
| 313 |
* Is the callback function in add_settings_field |
| 314 |
* |
| 315 |
* @param array $args |
| 316 |
* @return void |
| 317 |
*/ |
| 318 |
public function display_field( $args ): void { |
| 319 |
$field = $args['field']; |
| 320 |
$settings_field_display = new SettingsFieldsDisplay( $field ); |
| 321 |
$settings_field_display->display(); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Filter the settings field and return sort by sort_order key. |
| 326 |
* |
| 327 |
* @param array $settings |
| 328 |
* @return array |
| 329 |
*/ |
| 330 |
public function filter_multisafepay_common_settings_fields( array $settings ): array { |
| 331 |
foreach ( $settings as $key => $section ) { |
| 332 |
$sort_order = array(); |
| 333 |
foreach ( $section['fields'] as $field_key => $field ) { |
| 334 |
$sort_order[ $field_key ] = $field['sort_order']; |
| 335 |
} |
| 336 |
array_multisort( $sort_order, SORT_ASC, $settings[ $key ]['fields'] ); |
| 337 |
} |
| 338 |
return $settings; |
| 339 |
} |
| 340 |
} |
| 341 |
|