init(); } /** * Bootstraps plugin constants, CPT registration, and the Helper class. * * @return void */ public function init() { Constants::init(); // When you declare a static property in a class, you can only assign constant values ​​to it, such as numbers or fixed strings. We need to use a separate method to initialize the static property new Cpt(); new Helper(); } /** * Checks if WooCommerce is installed and activated. * * Displays an admin notice and deactivates this plugin if WooCommerce is missing. * This guards against WooCommerce being deactivated after this plugin was already active, * a case that `Requires Plugins` (WP 6.5+) does not cover at runtime. * * @return void */ public function advanced_shipping_rules_for_woocommerce_check_dependencies(): void { if ( ! class_exists( 'WooCommerce' ) ) { add_action( 'admin_notices', function () { echo '

' . esc_html__( 'Advanced Shipping Rules For WooCommerce requires WooCommerce to be installed and activated.', 'advanced-shipping-rules-for-woocommerce' ) . '

'; } ); deactivate_plugins( Constants::$plugin_basename ); } } /** * Initializes the WooCommerce Advanced Shipping Rules only after Woocommerce is complete loaded. * * Checks if the `WC_Shipping_Method` class exists before proceeding. * Adds a custom shipping method via the `woocommerce_shipping_methods` filter. * * @return void */ public function advanced_shipping_rules_for_woocommerce_init(): void { if ( ! class_exists( 'WC_Shipping_Method' ) ) { return; } add_filter( 'woocommerce_shipping_methods', array( $this, 'wc_add_advanced_shipping_method' ) ); } /** * Adds the Advanced Shipping Method to WooCommerce. * * @param array $methods The existing WooCommerce shipping methods. * @return array The modified shipping methods including the Advanced Shipping method. */ public function wc_add_advanced_shipping_method( $methods ): array { $methods['advanced_shipping_rules_for_woocommerce'] = 'AdvancedShippingRulesForWoocommerce\\Shipping'; return $methods; } /** * Enqueues admin scripts for the WooCommerce shipping settings page. * * This function ensures that scripts are only loaded on the WooCommerce * shipping settings page. It registers and localizes the `wc-advanced-shipping-groups` script. * * @param string $hook The current admin page hook. * @return void */ public function enqueue_admin_scripts( $hook ): void { // Check if we are on the WooCommerce settings page // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only tab detection, no form data processed. if ( ! ( $hook === 'woocommerce_page_wc-settings' && isset( $_GET['tab'] ) && sanitize_text_field( wp_unslash( $_GET['tab'] ) ) === 'shipping' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } wp_register_script( 'wc-advanced-shipping-groups', Constants::$plugin_dir_url . 'assets/js/admin-shipping-conditions.js', array( 'jquery' ), Constants::$plugin_version, true ); wp_localize_script( 'wc-advanced-shipping-groups', 'wc_advanced_shipping_scripts_admin', array( 'html_group' => Helper::generate_html_shipping_group(), 'html_condition' => Helper::generate_html_shipping_condition(), 'html_separator_or' => Helper::generate_html_shipping_separator_or(), 'conditionTypes' => Helper::get_condition_types(), ) ); wp_enqueue_script( 'wc-advanced-shipping-groups' ); } /** * Enqueues admin styles for the WooCommerce shipping settings page. * * Ensures that styles are only loaded on the WooCommerce shipping settings page. * * @param string $hook The current admin page hook. * @return void */ public function enqueue_admin_styles( $hook ): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only tab detection, no form data processed. if ( ! ( $hook === 'woocommerce_page_wc-settings' && isset( $_GET['tab'] ) && sanitize_text_field( wp_unslash( $_GET['tab'] ) ) === 'shipping' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } wp_enqueue_style( 'wc-advanced-shipping-admin-style', Constants::$plugin_dir_url . 'assets/css/admin-style.css', array(), Constants::$plugin_version ); } } }