PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.28
Booktics – Appointment Booking Calendar for Service Businesses v1.0.28
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / core / module / woocommerce-addon / hook / woocommerce-hook.php

woocommerce-hook.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.28, at core/module/woocommerce-addon/hook/woocommerce-hook.php

171 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics\Module\Woocommerce_Addon\Hook;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 use Booktics\Contracts\Hookable_Service_Contract;
8 use Booktics\Module\Woocommerce_Addon\Service\Woocommerce_Payment_Method;
9
10 /**
11 * Wires the WooCommerce payment module into Booktics.
12 *
13 * Registers the payment method, feeds the settings panel the state it needs to
14 * describe the WooCommerce install, and warns when the module is switched on
15 * while WooCommerce is not usable. The checkout and order work lives in the
16 * other hooks in this module.
17 */
18 class Woocommerce_Hook implements Hookable_Service_Contract {
19
20 /**
21 * Register WordPress hooks.
22 *
23 * @return void
24 */
25 public function register() {
26 add_filter( 'booktics_payment_methods', array( $this, 'register_payment_method' ) );
27 add_filter( 'booktics_format_setting_response', array( $this, 'settings_response' ) );
28 add_filter( 'booktics_rest_public_settings', array( $this, 'public_settings' ) );
29
30 add_action( 'admin_notices', array( $this, 'dependency_notice' ) );
31 }
32
33 /**
34 * Add WooCommerce to the registered payment methods.
35 *
36 * Registered only when WooCommerce is actually running: an order that names
37 * a payment method Booktics cannot resolve is rejected outright, which is
38 * the behaviour we want if the plugin is deactivated mid-flight.
39 *
40 * @param array $methods Registered payment methods.
41 *
42 * @return array
43 */
44 public function register_payment_method( $methods ) {
45 if ( ! class_exists( 'WooCommerce' ) ) {
46 return $methods;
47 }
48
49 $methods['woocommerce'] = Woocommerce_Payment_Method::class;
50
51 return $methods;
52 }
53
54 /**
55 * Expose WooCommerce state to the admin settings screen.
56 *
57 * Deliberately does not use booktics_is_woocommerce_active(): the panel has
58 * to describe WooCommerce even while the payment setting is still off.
59 *
60 * @param array $settings Settings array.
61 *
62 * @return array
63 */
64 public function settings_response( $settings ) {
65 $installed = $this->is_woocommerce_installed();
66
67 $settings['woocommerce_install_status'] = class_exists( 'WooCommerce' ) ? 1 : 0;
68 $settings['woocommerce_version'] = defined( 'WC_VERSION' ) ? WC_VERSION : '';
69 $settings['woocommerce_currency'] = function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : '';
70 $settings['woocommerce_guest_checkout'] = 'yes' === get_option( 'woocommerce_enable_guest_checkout', 'yes' ) ? 1 : 0;
71
72 // Installed but switched off: offer a real one-click activation. Not
73 // installed at all: send the admin to WordPress' own install screen.
74 // add_query_arg, not wp_nonce_url: the latter esc_html's its output
75 // (& -> &#038;), which breaks once the URL is carried through JSON and
76 // assigned to window.location rather than rendered as an HTML href.
77 $settings['woocommerce_activate_url'] = $installed && ! class_exists( 'WooCommerce' )
78 ? add_query_arg(
79 array(
80 'action' => 'activate',
81 'plugin' => 'woocommerce/woocommerce.php',
82 '_wpnonce' => wp_create_nonce( 'activate-plugin_woocommerce/woocommerce.php' ),
83 ),
84 admin_url( 'plugins.php' )
85 )
86 : '';
87 $settings['woocommerce_install_url'] = admin_url( 'plugin-install.php?tab=plugin-information&plugin=woocommerce&TB_iframe=true' );
88
89 // Where a WooCommerce order is edited depends on whether the store runs
90 // High-Performance Order Storage, so the admin UI is given the base URL
91 // rather than guessing and producing dead links on half the sites.
92 $settings['woocommerce_order_edit_base'] = $this->order_edit_base();
93
94 return $settings;
95 }
96
97 /**
98 * Expose the booking frontend's share of the WooCommerce settings.
99 *
100 * The frontend only needs to know whether it may offer the gateway; the
101 * checkout URL is returned by the order endpoint at hand-off time.
102 *
103 * @param array $public Public settings.
104 *
105 * @return array
106 */
107 public function public_settings( $public ) {
108 $public['woocommerce_ready'] = booktics_is_woocommerce_active() ? 1 : 0;
109
110 return $public;
111 }
112
113 /**
114 * Warn when the module is enabled but WooCommerce cannot serve it.
115 *
116 * @return void
117 */
118 public function dependency_notice() {
119 if ( ! current_user_can( 'manage_options' ) ) {
120 return;
121 }
122
123 if ( ! booktics_get_option( 'enable_woocommerce_payment' ) ) {
124 return;
125 }
126
127 if ( class_exists( 'WooCommerce' ) ) {
128 return;
129 }
130
131 printf(
132 '<div class="notice notice-warning"><p>%s</p></div>',
133 esc_html__( 'Booktics WooCommerce payment is enabled, but WooCommerce is not active. Booking customers cannot pay until WooCommerce is installed and activated.', 'booktics' )
134 );
135 }
136
137 /**
138 * URL prefix for editing a WooCommerce order, minus the id.
139 *
140 * @return string
141 */
142 private function order_edit_base(): string {
143 $hpos = class_exists( \Automattic\WooCommerce\Utilities\OrderUtil::class )
144 && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
145
146 return $hpos
147 ? admin_url( 'admin.php?page=wc-orders&action=edit&id=' )
148 : admin_url( 'post.php?action=edit&post=' );
149 }
150
151 /**
152 * Whether the WooCommerce plugin exists on disk, active or not.
153 *
154 * Lets the settings panel offer "Activate" rather than "Install" when the
155 * plugin is merely switched off.
156 *
157 * @return bool
158 */
159 private function is_woocommerce_installed(): bool {
160 if ( class_exists( 'WooCommerce' ) ) {
161 return true;
162 }
163
164 if ( ! function_exists( 'get_plugins' ) ) {
165 require_once ABSPATH . 'wp-admin/includes/plugin.php';
166 }
167
168 return array_key_exists( 'woocommerce/woocommerce.php', get_plugins() );
169 }
170 }
171