is_using_block_checkout()) { // checkout block version add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_block_assets')); add_action('enqueue_block_editor_assets', array($this, 'enqueue_editor_assets')); add_action('enqueue_block_assets', array($this, 'enqueue_block_assets')); } else { // checkout classic version // add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets')); } add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_assets')); } private function is_using_block_checkout() { // Check if WooCommerce blocks are active if (!class_exists('Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils')) { return false; } // Check if the checkout page uses blocks $checkout_page_id = wc_get_page_id('checkout'); if (!$checkout_page_id) { return false; } // Check if the checkout page contains the checkout block $checkout_page = get_post($checkout_page_id); if (!$checkout_page) { return false; } // Check if the page content contains the checkout block return has_block('woocommerce/checkout', $checkout_page_id); } public function enqueue_block_assets() { wp_enqueue_style( 'enspireplugins-acfw-checkout-block-style', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/css/checkout-fields-block.css', array(), ENSPIREPLUGINS_ACFW_VERSION ); } public function enqueue_frontend_block_assets() { if (!is_checkout() && !wp_doing_ajax()) { return; } wp_enqueue_style( 'enspireplugins-acfw-checkout-block-style', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/css/checkout-fields-block.css', array('wc-blocks-style'), ENSPIREPLUGINS_ACFW_VERSION ); wp_enqueue_script( 'enspireplugins-acfw-checkout-block-frontend', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/js/checkout-fields-block-frontend.js', array('wp-element', 'wp-components', 'wc-blocks-checkout'), ENSPIREPLUGINS_ACFW_VERSION, true ); wp_localize_script('enspireplugins-acfw-checkout-block-frontend', 'enspireACFWFrontend', array( 'fields' => $this->get_active_fields_for_editor(), 'nonce' => wp_create_nonce('enspireplugins_acfw_checkout'), 'ajaxUrl' => admin_url('admin-ajax.php'), )); } public function enqueue_editor_assets() { wp_enqueue_script( 'enspireplugins-acfw-checkout-block-editor', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/js/checkout-fields-block-editor.js', array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-editor', 'wp-block-editor', 'wp-compose', 'wp-data', 'wc-blocks-checkout' ), ENSPIREPLUGINS_ACFW_VERSION, true ); wp_localize_script('enspireplugins-acfw-checkout-block-editor', 'enspireACFWBlock', array( 'title' => __('Additional Checkout Fields', 'additional-checkout-fields-for-woo'), 'description' => __('Display custom checkout fields configured in the plugin settings.', 'additional-checkout-fields-for-woo'), 'fields' => $this->get_active_fields_for_editor(), 'fieldsCount' => count($this->get_active_fields()), 'labels' => array( 'fieldsSettings' => __('Fields Settings', 'additional-checkout-fields-for-woo'), 'enableFields' => __('Enable Fields Display', 'additional-checkout-fields-for-woo'), 'showLabels' => __('Show Field Labels', 'additional-checkout-fields-for-woo'), 'fieldsLayout' => __('Fields Layout', 'additional-checkout-fields-for-woo'), 'layoutDefault' => __('Default', 'additional-checkout-fields-for-woo'), 'layoutCompact' => __('Compact', 'additional-checkout-fields-for-woo'), 'layoutWide' => __('Wide', 'additional-checkout-fields-for-woo'), 'noFields' => __('No active fields found. Configure fields in plugin settings first.', 'additional-checkout-fields-for-woo'), // translators: %d is the number of active fields 'fieldsFound' => __('%d active fields configured in plugin settings.', 'additional-checkout-fields-for-woo'), // translators: %d is the number of custom checkout fields 'blockDescription' => __('This block will display %d custom checkout fields on the frontend.', 'additional-checkout-fields-for-woo'), 'configureFirst' => __('Configure custom fields in plugin settings to see them here.', 'additional-checkout-fields-for-woo'), 'fieldsDisabled' => __('Fields display is disabled.', 'additional-checkout-fields-for-woo'), 'noFieldsConfigured' => __('No custom fields configured. Please add fields in the plugin settings.', 'additional-checkout-fields-for-woo'), // translators: %s is the field type (e.g., text, email, select) 'fieldType' => __('(%s field)', 'additional-checkout-fields-for-woo'), 'enableFieldsHelp' => __('Toggle to show/hide all custom fields.', 'additional-checkout-fields-for-woo'), 'showLabelsHelp' => __('Display labels above each field.', 'additional-checkout-fields-for-woo'), 'layoutHelp' => __('Choose how fields should be displayed.', 'additional-checkout-fields-for-woo'), ), )); } public function enqueue_frontend_assets() { if (is_checkout()) { wp_enqueue_style( 'enspireplugins_acfw-frontend-styles', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/css/frontend.css', array(), ENSPIREPLUGINS_ACFW_VERSION ); $custom_css = "/* CSS */"; wp_add_inline_style('enspireplugins_acfw-frontend-styles', $custom_css); } } public function enqueue_admin_assets($hook) { if ('toplevel_page_woo-checkout-fields' === $hook) { $this->enqueue_settings_page_assets(); } if ($this->is_order_page()) { $this->enqueue_order_page_assets(); } } private function enqueue_settings_page_assets() { wp_enqueue_script('jquery-ui-sortable'); wp_enqueue_script('jquery-ui-tabs'); wp_register_script( 'sweetalert2', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/libs/sweetalert2/sweetalert2.min.js', array(), '11.22.2', true ); wp_script_add_data('sweetalert2', 'defer', true); wp_enqueue_script('sweetalert2'); wp_enqueue_style( 'sweetalert2', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/libs/sweetalert2/sweetalert2.min.css', array(), '11.22.2' ); wp_enqueue_style( 'enspireplugins_acfw-admin-styles', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/css/admin.css', array('sweetalert2'), ENSPIREPLUGINS_ACFW_VERSION ); wp_register_script( 'enspireplugins_acfw-admin-scripts', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/js/admin.js', array('jquery', 'jquery-ui-sortable', 'sweetalert2'), ENSPIREPLUGINS_ACFW_VERSION, true ); wp_script_add_data('enspireplugins_acfw-admin-scripts', 'defer', true); wp_enqueue_script('enspireplugins_acfw-admin-scripts'); wp_localize_script('enspireplugins_acfw-admin-scripts', 'enspireplugins_acfw_admin', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('enspireplugins_acfw_admin_nonce'), 'orderNonce' => wp_create_nonce('edit-order'), 'strings' => array( 'confirmDelete' => esc_html__('Are you sure?', 'additional-checkout-fields-for-woo'), 'confirmDeleteText' => esc_html__('Do you want to delete this field? This action cannot be undone.', 'additional-checkout-fields-for-woo'), 'fieldDeleted' => esc_html__('Deleted!', 'additional-checkout-fields-for-woo'), 'fieldDeletedText' => esc_html__('The field has been deleted.', 'additional-checkout-fields-for-woo'), 'fieldAdded' => esc_html__('Success!', 'additional-checkout-fields-for-woo'), 'fieldAddedText' => esc_html__('Field added successfully!', 'additional-checkout-fields-for-woo'), 'fieldUpdated' => esc_html__('Success!', 'additional-checkout-fields-for-woo'), 'fieldUpdatedText' => esc_html__('Field updated successfully!', 'additional-checkout-fields-for-woo'), 'addNewField' => esc_html__('Add New Field', 'additional-checkout-fields-for-woo'), 'editField' => esc_html__('Edit Field', 'additional-checkout-fields-for-woo'), 'addField' => esc_html__('Add Field', 'additional-checkout-fields-for-woo'), 'updateField' => esc_html__('Update Field', 'additional-checkout-fields-for-woo'), 'cancel' => esc_html__('Cancel', 'additional-checkout-fields-for-woo'), 'yesDelete' => esc_html__('Yes, delete it!', 'additional-checkout-fields-for-woo'), 'saving' => esc_html__('Saving...', 'additional-checkout-fields-for-woo'), 'savingText' => esc_html__('Please wait while we save your changes.', 'additional-checkout-fields-for-woo') ) )); $fields_data = get_option('enspireplugins_acfw_settings', array('custom_fields' => array())); $custom_fields = isset($fields_data['custom_fields']) ? $fields_data['custom_fields'] : array(); $type_labels = array( 'text' => esc_html__('Text', 'additional-checkout-fields-for-woo'), 'number' => esc_html__('Number', 'additional-checkout-fields-for-woo'), 'email' => esc_html__('Email', 'additional-checkout-fields-for-woo'), 'url' => esc_html__('URL', 'additional-checkout-fields-for-woo'), 'select' => esc_html__('Select', 'additional-checkout-fields-for-woo'), 'checkbox' => esc_html__('Checkbox', 'additional-checkout-fields-for-woo'), 'radio' => esc_html__('Radio', 'additional-checkout-fields-for-woo'), 'textarea' => esc_html__('Textarea', 'additional-checkout-fields-for-woo'), 'heading' => esc_html__('Heading', 'additional-checkout-fields-for-woo') ); wp_add_inline_script( 'enspireplugins_acfw-admin-scripts', sprintf( 'var enspireplugins_acfw_fields_data = %s; var enspireplugins_acfw_type_labels = %s;', wp_json_encode($custom_fields), wp_json_encode($type_labels) ), 'before' ); } private function enqueue_order_page_assets() { wp_enqueue_style( 'enspireplugins_acfw-order-display-styles', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/css/order-display.css', array(), ENSPIREPLUGINS_ACFW_VERSION ); wp_register_script( 'enspireplugins_acfw-order-display-scripts', ENSPIREPLUGINS_ACFW_PLUGIN_URL . 'assets/js/order-display.js', array('jquery'), ENSPIREPLUGINS_ACFW_VERSION, true ); wp_script_add_data('enspireplugins_acfw-order-display-scripts', 'defer', true); wp_enqueue_script('enspireplugins_acfw-order-display-scripts'); } private function is_order_page() { if (!current_user_can('edit_shop_orders') && !current_user_can('manage_woocommerce')) { return false; } global $post_type, $pagenow, $typenow; // Check for classic order pages if ($post_type === 'shop_order' || $typenow === 'shop_order') { return true; } // Check for HPOS (High-Performance Order Storage) pages if ($pagenow === 'admin.php') { $screen = get_current_screen(); if ($screen && strpos($screen->id, 'wc-orders') !== false) { return true; } // Check page parameter for orders list - using WordPress's built-in sanitization // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check for page identification $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; if ($page === 'wc-orders') { return true; } } // Check for post.php (classic order edit page) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check for page identification if ($pagenow === 'post.php' && isset($_GET['post'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check for page identification $post_id = absint(wp_unslash($_GET['post'])); if ($post_id) { $post = get_post($post_id); if ($post && $post->post_type === 'shop_order') { return true; } } } return false; } /** * Get active fields from saved settings. */ private function get_active_fields() { $settings = get_option('enspireplugins_acfw_settings', array()); $custom_fields = isset($settings['custom_fields']) ? $settings['custom_fields'] : array(); return array_values(array_filter($custom_fields, function($field) { return isset($field['active']) && $field['active'] === 'yes'; })); } /** * Shape active fields data for editor/frontend scripts. */ private function get_active_fields_for_editor() { $active_fields = $this->get_active_fields(); $editor_fields = array(); foreach ($active_fields as $field) { $editor_fields[] = array( 'id' => isset($field['id']) ? $field['id'] : '', 'label' => isset($field['label']) ? $field['label'] : '', 'type' => isset($field['type']) ? $field['type'] : 'text', 'required' => isset($field['required']) && $field['required'] === 'yes', 'placeholder' => isset($field['placeholder']) ? $field['placeholder'] : '', 'options' => isset($field['options']) ? $field['options'] : array(), ); } return $editor_fields; } }