init_hooks(); } /** * Initialize hooks. */ private function init_hooks() { add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) ); add_filter( 'body_class', array( $this, 'add_page_body_class' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); add_filter( 'plugin_action_links_' . AFRIDIRECT_PAYMENT_PLUGIN_BASENAME, array( $this, 'add_plugin_action_links' ) ); } /** * Mark the plugin's own pages (payment request, pending and receipt) so their titles can be centred over the cards. * * @param string[] $classes Body classes. * @return string[] */ public function add_page_body_class( $classes ) { if ( ! is_page() ) { return $classes; } $own_pages = array_filter( array( (int) get_option( 'afridirect_request_page_id', 0 ), (int) get_option( 'afridirect_pending_page_id', 0 ), (int) get_option( 'afridirect_receipt_page_id', 0 ), ) ); if ( in_array( get_queried_object_id(), $own_pages, true ) ) { $classes[] = 'afridirect-own-page'; } return $classes; } /** * Enqueue frontend scripts and styles. */ public function enqueue_frontend_scripts() { // Cache-bust assets to avoid stale JS/CSS in browsers/CDNs. $css_ver = afridirect_payment_asset_ver( 'public/css/frontend.css' ); $js_ver = afridirect_payment_asset_ver( 'public/js/frontend.js' ); // Register unconditionally so shortcodes can enqueue on demand, then // auto-enqueue on the pages we can detect up front. wp_register_style( 'afridirect-frontend', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'public/css/frontend.css', array(), $css_ver ); wp_register_script( 'afridirect-frontend', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'public/js/frontend.js', array( 'jquery' ), $js_ver, true ); wp_localize_script( 'afridirect-frontend', 'afridirectPayment', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'afridirect-payment-nonce' ), 'i18n' => array( 'copied' => __( 'Copied!', 'afridirect-payment' ), 'copy' => __( 'Copy', 'afridirect-payment' ), 'selectFile' => __( 'Please select a file to upload.', 'afridirect-payment' ), 'uploading' => __( 'Uploading...', 'afridirect-payment' ), 'uploadComplete' => __( 'Upload complete!', 'afridirect-payment' ), 'errorOccurred' => __( 'An error occurred during upload. Please try again.', 'afridirect-payment' ), 'timeExpired' => __( 'Time expired!', 'afridirect-payment' ), 'proofReceived' => __( 'Proof received — under review', 'afridirect-payment' ), ), ) ); // Auto-enqueue where we can detect the context; shortcodes enqueue the rest. $post = get_post(); $content = $post instanceof WP_Post ? $post->post_content : ''; $has_shortcode = has_shortcode( $content, 'afridirect_form' ) || has_shortcode( $content, 'afridirect_payment_pending' ) || has_shortcode( $content, 'afridirect_payment_receipt' ) || has_shortcode( $content, 'afridirect_payment_request' ); $is_order_received = class_exists( 'WooCommerce' ) && is_wc_endpoint_url( 'order-received' ); $is_account_page = class_exists( 'WooCommerce' ) && is_account_page(); $is_view_order = class_exists( 'WooCommerce' ) && is_wc_endpoint_url( 'view-order' ); if ( $has_shortcode || $is_order_received || $is_account_page || $is_view_order ) { wp_enqueue_style( 'afridirect-frontend' ); wp_enqueue_script( 'afridirect-frontend' ); } } /** * Enqueue admin scripts and styles. */ public function enqueue_admin_scripts( $hook ) { $screen = get_current_screen(); // Enqueue for standalone payment edit screen. if ( $screen && 'afridirect_payment' === $screen->post_type && 'post' === $screen->base ) { wp_enqueue_style( 'afridirect-payment-admin', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'admin/css/admin.css', array(), afridirect_payment_asset_ver( 'admin/css/admin.css' ) ); wp_enqueue_script( 'afridirect-admin-payments', AFRIDIRECT_PAYMENT_PLUGIN_URL . 'admin/js/payments.js', array( 'jquery' ), afridirect_payment_asset_ver( 'admin/js/payments.js' ), true ); wp_localize_script( 'afridirect-admin-payments', 'afridirectPaymentAdmin', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'afridirect_payment_actions' ), 'i18n' => array( 'confirmApprove' => __( 'Are you sure you want to approve this payment?', 'afridirect-payment' ), 'confirmReject' => __( 'Are you sure you want to reject this payment?', 'afridirect-payment' ), 'provideReason' => __( 'Please provide a rejection reason.', 'afridirect-payment' ), ), ) ); } } /** * Add plugin action links. * * @param array $links Existing links. * @return array */ public function add_plugin_action_links( $links ) { // The plugin has its own Settings page, which works with or without WooCommerce. $settings_url = admin_url( 'admin.php?page=afridirect-settings' ); $settings_link = '' . esc_html__( 'Settings', 'afridirect-payment' ) . ''; array_unshift( $links, $settings_link ); return $links; } } /** * Check if WooCommerce checkout blocks are active. * * Returns true if WooCommerce is active AND the currently assigned checkout page uses blocks. * Defensive: returns false if checkout page is missing or WooCommerce is not active. * * @return bool */ function afridirect_is_checkout_blocks_active() { // Must have WooCommerce active. if ( ! class_exists( 'WooCommerce' ) ) { return false; } // Get checkout page ID. $checkout_page_id = get_option( 'woocommerce_checkout_page_id', 0 ); if ( ! $checkout_page_id ) { return false; } // Load the post content. $checkout_page = get_post( $checkout_page_id ); if ( ! $checkout_page || 'page' !== $checkout_page->post_type ) { return false; } $content = $checkout_page->post_content; // Check for checkout blocks. // Check for both 'woocommerce/checkout' and 'woocommerce/checkout-block' block names. if ( function_exists( 'has_block' ) ) { if ( has_block( 'woocommerce/checkout', $content ) || has_block( 'woocommerce/checkout-block', $content ) ) { return true; } } // Also check for block patterns (Gutenberg block syntax). if ( false !== strpos( $content, '