settings = $settings; $this->tax_rates = $tax_rates; $this->documents = $documents; } /** * Hooks. */ public function init() { add_action( 'gstlayer_render_tax_section', array( $this, 'render_tax_section' ) ); add_action( 'admin_post_gstlayer_build_tax_rates', array( $this, 'handle_build_tax_rates' ) ); add_action( 'admin_post_gstlayer_issue_invoice', array( $this, 'handle_issue_invoice' ) ); add_action( 'admin_post_gstlayer_recompute', array( $this, 'handle_recompute' ) ); add_action( 'admin_post_gstlayer_download_invoice', array( $this, 'handle_download_invoice' ) ); add_action( 'woocommerce_admin_field_gstlayer_image', array( $this, 'render_image_field' ) ); add_action( 'admin_notices', array( $this, 'notices' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_filter( 'plugin_action_links_' . GSTLAYER_BASENAME, array( $this, 'action_links' ) ); } /** * Settings link on the Plugins screen. * * @param array $links Links. * @return array */ public function action_links( $links ) { $url = admin_url( 'admin.php?page=wc-settings&tab=' . Settings::TAB ); array_unshift( $links, '' . esc_html__( 'Settings', 'gstlayer' ) . '' ); return $links; } /** * Admin CSS on our screens only. * * @param string $hook Hook suffix. */ public function assets( $hook ) { $screen = get_current_screen(); $order_ids = array( 'shop_order', function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order' ); $is_ours = ( 'woocommerce_page_wc-settings' === $hook ) || ( $screen && in_array( $screen->id, $order_ids, true ) ); if ( ! $is_ours ) { return; } wp_enqueue_style( 'gstlayer-admin', GSTLAYER_URL . 'assets/css/admin.css', array(), GSTLAYER_VERSION ); if ( 'woocommerce_page_wc-settings' === $hook ) { wp_enqueue_media(); wp_enqueue_script( 'gstlayer-admin-media', GSTLAYER_URL . 'assets/js/admin-media.js', array( 'jquery', 'wp-a11y' ), GSTLAYER_VERSION, true ); } } /** * Tax-rate setup screen (Settings > GST > Tax rates). */ public function render_tax_section() { $GLOBALS['hide_save_button'] = true; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- WooCommerce settings API flag. $status = $this->tax_rates->status(); $configured = $this->settings->is_configured(); $state_code = $this->settings->supplier_gst_code(); $built_at = (int) get_option( 'gstlayer_tax_rates_built_at', 0 ); ?>

' . esc_html__( 'Supplier', 'gstlayer' ) . '' ); ?>

(tax_rates->state_component_label() ); ?>)

tax_rates->format_rate( $row['slab'] ) ); ?>% ' . esc_html__( 'Missing', 'gstlayer' ) . ''; } elseif ( $row['slab'] > 0 && 3 !== $row['rate_count'] ) { echo '' . esc_html__( 'Needs rebuild', 'gstlayer' ) . ''; } else { echo '' . esc_html__( 'OK', 'gstlayer' ) . ''; } ?>

; a nested form would be dropped by the browser, so actions are nonce-protected links. $build_label = $built_at ? __( 'Rebuild GST tax rates', 'gstlayer' ) : __( 'Create GST tax rates', 'gstlayer' ); if ( $configured ) { echo '' . esc_html( $build_label ) . ''; } else { // A real, disabled button: it is announced as unavailable and explained by the notice above. echo ''; } ?>

' . esc_html__( 'WooCommerce → Settings → Tax', 'gstlayer' ) . '' ); ?>

tax_rates->create_or_refresh(); $redirect = admin_url( 'admin.php?page=wc-settings&tab=' . Settings::TAB . '§ion=tax' ); if ( is_wp_error( $result ) ) { self::flash( 'error', $result->get_error_message() ); } else { /* translators: 1: number of classes 2: number of rates */ self::flash( 'success', sprintf( __( 'GST tax rates ready: %1$d tax classes, %2$d rates.', 'gstlayer' ), (int) $result['classes'], (int) $result['rates'] ) ); } wp_safe_redirect( $redirect ); exit; } /** * Issue invoice now (admin-post). */ public function handle_issue_invoice() { $order = $this->order_from_request( 'gstlayer_issue_invoice_' ); $doc = Plugin::instance()->order_gst->issue_invoice( $order ); $url = $order->get_edit_order_url(); if ( is_wp_error( $doc ) ) { self::flash( 'error', $doc->get_error_message() ); } else { self::flash( 'success', __( 'Tax invoice issued.', 'gstlayer' ) ); } wp_safe_redirect( $url ); exit; } /** * Recompute breakdown (admin-post). */ public function handle_recompute() { $order = $this->order_from_request( 'gstlayer_recompute_' ); Plugin::instance()->order_gst->compute_and_store( $order ); $order->save(); self::flash( 'success', __( 'GST breakdown recalculated.', 'gstlayer' ) ); wp_safe_redirect( $order->get_edit_order_url() ); exit; } /** * Nonce-protected admin-post URL for an action (optionally bound to an order). * * @param string $action admin_post action. * @param int $order_id Order ID or 0. * @return string */ public static function action_url( $action, $order_id = 0 ) { $args = array( 'action' => $action ); if ( $order_id ) { $args['order_id'] = (int) $order_id; } return wp_nonce_url( add_query_arg( $args, admin_url( 'admin-post.php' ) ), $order_id ? $action . '_' . (int) $order_id : $action ); } /** * Shared capability + nonce + order lookup for order actions. * * @param string $nonce_prefix Nonce action prefix (order id appended). * @return \WC_Order */ private function order_from_request( $nonce_prefix ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'gstlayer' ), 403 ); } // The nonce action includes the order id, so the id is read first and verified on the next line. $order_id = isset( $_REQUEST['order_id'] ) ? absint( $_REQUEST['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended check_admin_referer( $nonce_prefix . $order_id ); $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { wp_die( esc_html__( 'Order not found.', 'gstlayer' ), 404 ); } return $order; } /** * Keep a one-time notice for the current user; it is shown on their next admin page and then removed. * * @param string $type success|error. * @param string $text Plain text. */ private static function flash( $type, $text ) { $user = get_current_user_id(); if ( $user ) { set_transient( 'gstlayer_notice_' . $user, array( 'type' => 'error' === $type ? 'error' : 'success', 'text' => (string) $text, ), MINUTE_IN_SECONDS ); } } /** * Show the one-time result notice of a GSTLayer action to the user who triggered it. */ public function notices() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $key = 'gstlayer_notice_' . get_current_user_id(); $notice = get_transient( $key ); if ( ! is_array( $notice ) || empty( $notice['text'] ) ) { return; } delete_transient( $key ); $class = isset( $notice['type'] ) && 'error' === $notice['type'] ? 'notice-error' : 'notice-success'; echo '

GSTLayer: ' . esc_html( $notice['text'] ) . '

'; } /** * Stream the invoice PDF to an admin (admin-post). */ public function handle_download_invoice() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'gstlayer' ), 403 ); } // The nonce action includes the order id, so the id is read first and verified on the next line. $order_id = isset( $_GET['order_id'] ) ? absint( $_GET['order_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended check_admin_referer( 'gstlayer_download_invoice_' . $order_id ); $inline = isset( $_GET['inline'] ) && '1' === sanitize_key( wp_unslash( $_GET['inline'] ) ); $order = wc_get_order( $order_id ); if ( ! $order instanceof \WC_Order ) { wp_die( esc_html__( 'Order not found.', 'gstlayer' ), 404 ); } Plugin::instance()->pdf->stream( $order, $inline ); } /** * Admin URL to download or preview the invoice PDF. * * @param int $order_id Order ID. * @param bool $inline Preview inline. * @return string */ public static function download_url( $order_id, $inline = false ) { $url = add_query_arg( array( 'action' => 'gstlayer_download_invoice', 'order_id' => (int) $order_id, 'inline' => $inline ? '1' : '0', ), admin_url( 'admin-post.php' ) ); return wp_nonce_url( $url, 'gstlayer_download_invoice_' . (int) $order_id ); } /** * Image (media library) field for the WooCommerce settings API. * * @param array $value Field definition. */ public function render_image_field( $value ) { $id = (int) get_option( $value['id'], 0 ); $src = $id ? wp_get_attachment_image_url( $id, 'medium' ) : ''; $tooltip = ! empty( $value['desc_tip'] ) ? wc_help_tip( $value['desc'] ) : ''; ?> cannot name; the title labels the group of buttons instead. ?>
<?php esc_attr_e( 'Current invoice logo', 'gstlayer' ); ?>