Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.00% |
14 / 20 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Admin_Order_UI | |
70.00% |
14 / 20 |
|
66.67% |
4 / 6 |
17.56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| add_to_payment_url | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| remove_arrow_from_link_text | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| enqueue_script | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| enqueue_styles | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| is_on_shop_order_edit_screen | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Add an autologin code to the "Customer payment page" link on pending orders. |
| 4 | * Change the link so it is click-to-clipboard. |
| 5 | * |
| 6 | * @package brianhenryie/bh-wp-autologin-urls |
| 7 | * @since 1.4.0 |
| 8 | */ |
| 9 | |
| 10 | namespace BrianHenryIE\WP_Autologin_URLs\WooCommerce; |
| 11 | |
| 12 | use BrianHenryIE\WP_Autologin_URLs\API_Interface; |
| 13 | use BrianHenryIE\WP_Autologin_URLs\Settings_Interface; |
| 14 | use WC_Order; |
| 15 | |
| 16 | /** |
| 17 | * Filter the URL to add the code. |
| 18 | * Filter the link text to remove the arrow. |
| 19 | * Add CSS to display a clipboard icon beside the link. |
| 20 | * Add JS to add the link to the computer clipboard. |
| 21 | */ |
| 22 | class Admin_Order_UI { |
| 23 | |
| 24 | /** |
| 25 | * The plugin settings, the version is used to cache JS and CSS in the browser. |
| 26 | * |
| 27 | * @uses Settings_Interface::get_plugin_version() |
| 28 | * |
| 29 | * @var Settings_Interface |
| 30 | */ |
| 31 | protected Settings_Interface $settings; |
| 32 | |
| 33 | /** |
| 34 | * The main plugin functions, used to add the autologin code to the customer payment page url. |
| 35 | * |
| 36 | * @uses API_Interface::add_autologin_to_url() |
| 37 | * |
| 38 | * @var API_Interface |
| 39 | */ |
| 40 | protected API_Interface $api; |
| 41 | |
| 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param API_Interface $api The main plugin functions. |
| 46 | * @param Settings_Interface $settings The settings, to find the plugin version for cache versioning. |
| 47 | */ |
| 48 | public function __construct( API_Interface $api, Settings_Interface $settings ) { |
| 49 | $this->settings = $settings; |
| 50 | $this->api = $api; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Adds an autologin code to "Customer payment page" link, which is displayed above the order status when |
| 55 | * the status is "pending". |
| 56 | * |
| 57 | * "http://localhost:8080/bh-wp-autologin-urls/checkout/order-pay/14/?pay_for_order=true&key=wc_order_s3TZsxvleY0uW" |
| 58 | * |
| 59 | * @hooked woocommerce_get_checkout_payment_url |
| 60 | * |
| 61 | * @param string $payment_url The existing link to pay for the order. |
| 62 | * @param WC_Order $order The WooCommerce order object. |
| 63 | * |
| 64 | * @return string Updated link with autologin code. |
| 65 | */ |
| 66 | public function add_to_payment_url( string $payment_url, WC_Order $order ): string { |
| 67 | |
| 68 | // Without this check for admin UI, autologin codes were being created on every REST request by the 3PF fulfillment. |
| 69 | if ( ! is_admin() ) { |
| 70 | return $payment_url; |
| 71 | } |
| 72 | |
| 73 | $payment_url = $this->api->add_autologin_to_url( $payment_url, $order->get_billing_email() ); |
| 74 | |
| 75 | return $payment_url; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The |
| 80 | * |
| 81 | * @hooked gettext_woocommerce |
| 82 | * |
| 83 | * @param string $translation The text after any existing translations applied. |
| 84 | * @param string $text The original untranslated text. |
| 85 | * @param string $domain The text domain, will always be "woocommerce" due to the hook. |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function remove_arrow_from_link_text( string $translation, string $text, string $domain ): string { |
| 90 | |
| 91 | if ( 'Customer payment page →' === $text ) { |
| 92 | return str_replace( '→', '', $translation ); |
| 93 | } |
| 94 | |
| 95 | return $translation; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Register the JavaScript for copying the URL to the clipboard. |
| 100 | * |
| 101 | * @hooked admin_enqueue_scripts |
| 102 | * |
| 103 | * @since 1.4.0 |
| 104 | */ |
| 105 | public function enqueue_script(): void { |
| 106 | |
| 107 | if ( ! $this->is_on_shop_order_edit_screen() ) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | wp_enqueue_script( 'bh-wp-autologin-urls-woocommerce-admin', plugin_dir_url( $this->settings->get_plugin_basename() ) . 'assets/bh-wp-autologin-urls-woocommerce-admin.js', array( 'jquery' ), $this->settings->get_plugin_version(), true ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Add the CSS for the clipboard icon after the link. |
| 116 | * |
| 117 | * @hooked admin_enqueue_scripts |
| 118 | * |
| 119 | * @since 1.4.0 |
| 120 | */ |
| 121 | public function enqueue_styles(): void { |
| 122 | |
| 123 | if ( ! $this->is_on_shop_order_edit_screen() ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | wp_enqueue_style( 'bh-wp-autologin-urls-woocommerce-admin', plugin_dir_url( $this->settings->get_plugin_basename() ) . 'assets/bh-wp-autologin-urls-woocommerce-admin.css', array(), $this->settings->get_plugin_version(), 'all' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Use `get_current_screen()` and `global $action` to determine are we on the admin order edit screen, |
| 132 | * otherwise we won't want to enqueue the styles and script. |
| 133 | * |
| 134 | * @return bool |
| 135 | */ |
| 136 | protected function is_on_shop_order_edit_screen(): bool { |
| 137 | |
| 138 | $screen = get_current_screen(); |
| 139 | global $action; |
| 140 | |
| 141 | if ( is_null( $screen ) || 'shop_order' !== $screen->id || 'edit' !== $action ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | } |