'; echo esc_html__('Adbear Random Order IDs for WooCommerce requires WooCommerce to be installed and active.', 'adbear-random-order-ids-for-woocommerce'); echo '
'; } public function admin_menu() { add_submenu_page( 'woocommerce', __('Adbear Random Order IDs', 'adbear-random-order-ids-for-woocommerce'), __('Adbear Random Order IDs', 'adbear-random-order-ids-for-woocommerce'), 'manage_woocommerce', 'ronfw-settings', array($this, 'settings_page') ); } public function register_settings() { register_setting( 'ronfw_settings_group', self::OPTION_PREFIX, array( 'type' => 'string', 'sanitize_callback' => array($this, 'sanitize_prefix'), 'default' => self::DEFAULT_PREFIX, ) ); register_setting( 'ronfw_settings_group', self::OPTION_LENGTH, array( 'type' => 'integer', 'sanitize_callback' => array($this, 'sanitize_length'), 'default' => self::DEFAULT_LENGTH, ) ); register_setting( 'ronfw_settings_group', self::OPTION_CHARSET, array( 'type' => 'string', 'sanitize_callback' => array($this, 'sanitize_charset'), 'default' => self::DEFAULT_CHARSET, ) ); } public function sanitize_prefix($value) { $value = sanitize_text_field($value); return substr($value, 0, 20); } public function sanitize_length($value) { $value = absint($value); if ($value < 4) { $value = 4; } if ($value > 20) { $value = 20; } return $value; } public function sanitize_charset($value) { $allowed = array('digits', 'alphanumeric'); return in_array($value, $allowed, true) ? $value : self::DEFAULT_CHARSET; } /** * Load settings screen stylesheet (external file; cache-busted by version). * * @param string $hook_suffix Current admin page hook. */ public function enqueue_settings_assets($hook_suffix) { if (!$this->should_enqueue_settings_assets($hook_suffix)) { return; } wp_enqueue_style( 'adbear-ronfw-admin-settings', plugins_url('assets/admin-settings.css', __FILE__), array(), self::PLUGIN_VERSION ); } /** * Detect our WooCommerce submenu screen across WP/WooCommerce hook variants. * * @param string $hook_suffix Current admin page hook. * @return bool */ private function should_enqueue_settings_assets($hook_suffix) { if (is_string($hook_suffix) && false !== strpos($hook_suffix, 'ronfw-settings')) { return true; } if ('woocommerce_page_ronfw-settings' === $hook_suffix) { return true; } if (function_exists('get_current_screen')) { $screen = get_current_screen(); if ($screen && 'woocommerce_page_ronfw-settings' === $screen->id) { return true; } } return false; } /** * If the primary hook did not match, enqueue before styles print (some WooCommerce builds use a different $hook_suffix). */ public function enqueue_settings_assets_fallback() { if (wp_style_is('adbear-ronfw-admin-settings', 'enqueued') || wp_style_is('adbear-ronfw-admin-settings', 'done')) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- routing only if (empty($_GET['page']) || 'ronfw-settings' !== sanitize_key(wp_unslash($_GET['page']))) { return; } if (!current_user_can('manage_woocommerce')) { return; } wp_enqueue_style( 'adbear-ronfw-admin-settings', plugins_url('assets/admin-settings.css', __FILE__), array(), self::PLUGIN_VERSION ); } /** * Example string for the settings preview (not a real order number). * * @param string $prefix Sanitized prefix. * @param int $length Length for placeholder. * @param string $charset digits|alphanumeric. * @return string */ private function get_preview_sample($prefix, $length, $charset) { $length = max(4, min(20, (int) $length)); if ('alphanumeric' === $charset) { $rand = str_repeat('X', $length); } else { $rand = str_repeat('0', $length); } return $prefix . $rand; } public function settings_page() { if (!current_user_can('manage_woocommerce')) { return; } $prefix = get_option(self::OPTION_PREFIX, self::DEFAULT_PREFIX); $length = get_option(self::OPTION_LENGTH, self::DEFAULT_LENGTH); $charset = get_option(self::OPTION_CHARSET, self::DEFAULT_CHARSET); $preview = $this->get_preview_sample($prefix, $length, $charset); ?>