base === 'toplevel_page_multisafepay-settings' ) { wp_enqueue_style( 'multisafepay-admin-css', MULTISAFEPAY_PLUGIN_URL . '/assets/admin/css/multisafepay-admin.css', array(), MULTISAFEPAY_PLUGIN_VERSION, 'all' ); } $sections = array( 'multisafepay_applepay', 'multisafepay_googlepay', 'multisafepay_bancontact' ); if ( isset( $_GET['section'] ) && in_array( $_GET['section'], $sections, true ) ) { wp_enqueue_script( 'multisafepay-admin-js', MULTISAFEPAY_PLUGIN_URL . '/assets/admin/js/multisafepay-admin.js', array(), MULTISAFEPAY_PLUGIN_VERSION, true ); wp_localize_script( 'multisafepay-admin-js', 'multisafepayAdminData', array( 'directPaymentConfirmation' => array( 'title' => __( 'Direct payment activation confirmation', 'multisafepay' ), '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' ), ), ) ); } } /** * Register the top-level "MultiSafepay" admin menu and the Settings entry. * * The parent menu slug is `multisafepay-settings` so the existing URL * (admin.php?page=multisafepay-settings) keeps working without any * redirect. Sibling plugins can attach their own * sub-pages to this same parent via `add_submenu_page`. * * @see https://developer.wordpress.org/reference/functions/add_menu_page/ * * @return void */ public function register_common_settings_page(): void { $title = sprintf( __( 'MultiSafepay Settings v. %s', 'multisafepay' ), MULTISAFEPAY_PLUGIN_VERSION ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment add_menu_page( esc_html( $title ), __( 'MultiSafepay', 'multisafepay' ), 'manage_woocommerce', 'multisafepay-settings', array( $this, 'display_multisafepay_settings' ), $this->get_menu_icon(), 57 ); // Override the auto-generated first submenu label ("MultiSafepay") with "Settings". add_submenu_page( 'multisafepay-settings', esc_html( $title ), __( 'Settings', 'multisafepay' ), 'manage_woocommerce', 'multisafepay-settings', array( $this, 'display_multisafepay_settings' ) ); } /** * Resolve the icon shown next to the top-level "MultiSafepay" admin menu. * * Returns a base64-encoded data URI of the SVG file if it exists, allowing * WordPress to apply its automatic admin-theme color filters. Falls back to * a Dashicon if the file is missing. * * The result is cached in a static variable to avoid repeated filesystem reads * and base64 encoding on every admin request. * * @return string */ private function get_menu_icon(): string { static $cached_icon = null; if ( null !== $cached_icon ) { return $cached_icon; } $svg_path = MULTISAFEPAY_PLUGIN_DIR_PATH . 'assets/admin/img/multisafepay-menu-icon.svg'; if ( is_readable( $svg_path ) ) { $svg = file_get_contents( $svg_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( ! empty( $svg ) ) { // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Used for SVG data URI, not for obfuscation. $cached_icon = 'data:image/svg+xml;base64,' . base64_encode( $svg ); return $cached_icon; } } $cached_icon = 'dashicons-cart'; return $cached_icon; } /** * Display the common settings page view. * * @return void */ public function display_multisafepay_settings(): void { $tab_active = $this->get_tab_active(); require_once MULTISAFEPAY_PLUGIN_DIR_PATH . 'templates/multisafepay-settings-display.php'; } /** * Display the support settings page view. * * @return void */ public function display_multisafepay_support_section(): void { require_once MULTISAFEPAY_PLUGIN_DIR_PATH . 'templates/partials/multisafepay-settings-support-display.php'; } /** * Display the status tab. * * @return void */ public function display_multisafepay_status_section(): void { $status_controller = new StatusController(); $status_controller->display(); } /** * Display the log tab. * * @return void */ public function display_multisafepay_logs_section() { $logs_controller = new LogsController(); $logs_controller->display(); } /** * Returns active tab defined in get variable * * @return string */ private function get_tab_active(): string { if ( isset( $_GET['tab'] ) && '' !== $_GET['tab'] ) { return wp_unslash( sanitize_key( $_GET['tab'] ) ); } return 'general'; } /** * Register general settings in common settings page * * @return void */ public function register_common_settings(): void { $settings_fields = new SettingsFields(); $settings = $settings_fields->get_settings(); foreach ( $settings as $tab_key => $section ) { $this->add_settings_section( $tab_key, $section['title'] ); foreach ( $section['fields'] as $field ) { $this->register_setting( $field, $tab_key ); $this->add_settings_field( $field, $tab_key ); } } } /** * Add settings field * * @see https://developer.wordpress.org/reference/functions/add_settings_field/ * * @param array $field The field * @param string $tab_key The key of the tab * @return void */ private function add_settings_field( array $field, string $tab_key ): void { add_settings_field( $field['id'], $this->generate_label_for_settings_field( $field ), array( $this, 'display_field' ), 'multisafepay-settings-' . $tab_key, $tab_key, array( 'field' => $field, ) ); } /** * Return the label tag to be used in add_settings_field * * @param array $field The settings field array * @return string */ private function generate_label_for_settings_field( array $field ): string { if ( '' === $field['tooltip'] ) { return sprintf( '', $field['id'], $field['label'] ); } return sprintf( '', $field['id'], $field['label'], wc_help_tip( $field['tooltip'] ) ); } /** * Filter which set the settings page and adds a screen options of WooCommerce * * @see http://hookr.io/filters/woocommerce_screen_ids/ * * @param array $screen * @return array */ public function set_wc_screen_options_in_common_settings_page( array $screen ): array { $screen[] = 'toplevel_page_multisafepay-settings'; return $screen; } /** * Register setting * * @see https://developer.wordpress.org/reference/functions/register_setting/ * * @param array $field * @param string $tab_key * @return void */ private function register_setting( array $field, string $tab_key ): void { register_setting( 'multisafepay-settings-' . $tab_key, $field['id'], array( 'type' => $field['setting_type'], 'show_in_rest' => false, 'sanitize_callback' => $field['callback'], ) ); } /** * Add settings section * * @see https://developer.wordpress.org/reference/functions/add_settings_section/ * * @param string $section_key * @param string $section_title * @return void */ private function add_settings_section( string $section_key, string $section_title ): void { add_settings_section( $section_key, $section_title, array( $this, 'display_intro_section' ), 'multisafepay-settings-' . $section_key ); } /** * Callback to display the title on each settings sections * * @see https://developer.wordpress.org/reference/functions/add_settings_section/ * * @param array $args * @return void */ public function display_intro_section( array $args ): void { $settings_fields = new SettingsFields(); $settings = $settings_fields->get_settings(); if ( ! empty( $settings[ $args['id'] ]['intro'] ) ) { esc_html( (string) printf( '
%s
', esc_html( $settings[ $args['id'] ]['intro'] ) ) ); } } /** * Return the HTML view by field. * Is the callback function in add_settings_field * * @param array $args * @return void */ public function display_field( $args ): void { $field = $args['field']; $settings_field_display = new SettingsFieldsDisplay( $field ); $settings_field_display->display(); } /** * Filter the settings field and return sort by sort_order key. * * @param array $settings * @return array */ public function filter_multisafepay_common_settings_fields( array $settings ): array { foreach ( $settings as $key => $section ) { $sort_order = array(); foreach ( $section['fields'] as $field_key => $field ) { $sort_order[ $field_key ] = $field['sort_order']; } array_multisort( $sort_order, SORT_ASC, $settings[ $key ]['fields'] ); } return $settings; } }