Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
2.65% covered (danger)
2.65%
3 / 113
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuickAddProduct
2.65% covered (danger)
2.65%
3 / 113
10.00% covered (danger)
10.00%
1 / 10
804.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 init
28.57% covered (danger)
28.57%
2 / 7
0.00% covered (danger)
0.00%
0 / 1
6.28
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
20
 register_dashboard_widgets
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 output_dashboard_widget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 output_modal_root
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_product_types
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
90
 can_access_global_ctb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle_product_type_on_add_product
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace NewfoldLabs\WP\Module\ECommerce;
4
5use NewfoldLabs\WP\Module\ECommerce\Data\Brands;
6use NewfoldLabs\WP\Module\Solutions\Solutions;
7use NewfoldLabs\WP\ModuleLoader\Container;
8
9/**
10 * Quick Add Product Class
11 */
12class QuickAddProduct {
13
14    /**
15     * Holds the container object
16     *
17     * @var Container
18     */
19    protected $container;
20
21    /**
22     * Constructor
23     *
24     * @param Container $container Plugin container.
25     */
26    public function __construct( Container $container ) {
27        $this->container = $container;
28    }
29
30    /**
31     * Initialize class hooks/filters
32     *
33     * @return void
34     */
35    public function init() {
36        // Enable it only for Bluehost, web brand plugin.
37        if ( ! Brands::is_brand_compatible( $this->container->plugin()->id ) || ! function_exists( 'WC' ) ) {
38            return;
39        }
40
41        // Enqueue scripts in admin.
42        \add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
43        // Register dashboard widget.
44        \add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widgets' ) );
45        // Register API routes.
46        \add_action( 'rest_api_init', array( $this, 'register_routes' ) );
47        // Output root for modal.
48        \add_action( 'admin_footer', array( $this, 'output_modal_root' ) );
49
50        // Handle special product type page.
51        \add_action( 'admin_footer', array( $this, 'handle_product_type_on_add_product' ) );
52    }
53
54
55    /**
56     * Enqueue scripts
57     *
58     * @return void
59     */
60    public function enqueue_scripts() {
61        global $current_screen;
62
63        $asset_file = NFD_ECOMMERCE_BUILD_DIR . 'quick-add-product/index.asset.php';
64        if ( file_exists( $asset_file ) ) {
65            $asset = require $asset_file;
66
67            // Add WC accounting script to the dependencies array.
68            $asset['dependencies'][] = 'accounting';
69
70            // Register script.
71            wp_register_script(
72                'quick-add-product',
73                NFD_ECOMMERCE_PLUGIN_URL . 'vendor/newfold-labs/wp-module-ecommerce/build/quick-add-product/index.js',
74                array_merge(
75                    $asset['dependencies'],
76                    array( 'newfold-global-ctb', 'nfd-installer-listener', 'nfd-installer' ),
77                ),
78                $asset['version'],
79                true
80            );
81
82            wp_localize_script(
83                'quick-add-product',
84                'quickAddProduct',
85                array(
86                    'productTypes'  => $this->get_product_types(),
87                    'addProductUrl' => add_query_arg( array( 'post_type' => 'product' ), \admin_url( 'post-new.php' ) ),
88                    'money'         => array(
89                        'decimals'          => wc_get_price_decimals(),
90                        'thousandSeparator' => wc_get_price_thousand_separator(),
91                        'decimalSeparator'  => wc_get_price_decimal_separator(),
92                        'currencySymbol'    => get_woocommerce_currency_symbol(),
93                    ),
94                )
95            );
96
97            wp_register_style(
98                'quick-add-product',
99                NFD_ECOMMERCE_PLUGIN_URL . 'vendor/newfold-labs/wp-module-ecommerce/build/quick-add-product/quick-add-product.css',
100                array( 'newfold-global-ctb-style', 'nfd-installer' ),
101                $asset['version']
102            );
103
104            // Maybe enqueue scripts.
105            if ( ! empty( $current_screen ) && in_array( $current_screen->id, array( 'dashboard', 'edit-product' ), true ) ) {
106
107                wp_enqueue_global_styles_css_custom_properties();
108                wp_enqueue_media();
109
110                wp_enqueue_script( 'quick-add-product' );
111                wp_enqueue_style( 'quick-add-product' );
112            }
113        }
114    }
115
116    /**
117     * Register dashboard widget
118     *
119     * @return void
120     */
121    public function register_dashboard_widgets() {
122        wp_add_dashboard_widget(
123            'nfd_quick_add_product_widget',
124            esc_html__( 'Quick Add Product', 'wp-module-ecommerce' ),
125            array( $this, 'output_dashboard_widget' ),
126            null,
127            null,
128            'column3',
129            'high'
130        );
131    }
132
133    /**
134     * Output the dashboard widget
135     *
136     * @return void
137     */
138    public function output_dashboard_widget() {
139        echo '<div id="nfd-quick-add-product-widget"></div>';
140    }
141
142    /**
143     * Register create product API route
144     *
145     * @return void
146     */
147    public function register_routes() {
148        ( new \NewfoldLabs\WP\Module\ECommerce\RestApi\QuickAddProductController() )->register_routes();
149    }
150
151    /**
152     * Output modal root
153     *
154     * @return void
155     */
156    public function output_modal_root() {
157        if ( ! wp_script_is( 'quick-add-product', 'enqueued' ) ) {
158            return;
159        }
160
161        echo '<div id="nfd-quick-add-product-modal"></div>';
162    }
163
164    /**
165     * Return an array of product types.
166     *
167     * @return array
168     */
169    protected function get_product_types() {
170
171        // Init types with default.
172        $types = array(
173            array(
174                'key'         => 'virtual',
175                'title'       => __( 'Digital products or services', 'wp-module-ecommerce' ),
176                'description' => __( 'EX: eBooks, stock photos, software, templates, podcasts, apps, videos, etc. No physical products or shipping management.', 'wp-module-ecommerce' ),
177            ),
178            array(
179                'key'         => 'physical',
180                'title'       => __( 'Physical products', 'wp-module-ecommerce' ),
181                'description' => __( 'Ex: clothing, furniture, accessories — any type of product that needs to be physically shipped to your customers.', 'wp-module-ecommerce' ),
182            ),
183        );
184
185        // Get entitlements.
186        if ( ! class_exists( 'NewfoldLabs\WP\Module\Solutions\Solutions' ) ) {
187            return $types;
188        }
189
190        $entitlements_data = Solutions::get_enhanced_entitlment_data();
191        $entitlements      = array_merge(
192            isset( $entitlements_data['entitlements'] ) ? $entitlements_data['entitlements'] : array(),
193            isset( $entitlements_data['premium'] ) ? $entitlements_data['premium'] : array()
194        );
195
196        $premium_types = array(
197            'yith-woocommerce-booking-premium/init.php' => array(
198                'key'         => 'booking',
199                'title'       => __( 'Bookings/Appointments', 'wp-module-ecommerce' ),
200                'description' => __( 'Ex: apartment bookings, rental of products, medical appointments, personal training, etc — any type of bookable product or service.', 'wp-module-ecommerce' ),
201            ),
202            'wp-plugin-subscriptions/wp-plugin-subscriptions.php'          => array(
203                'key'         => 'subscription',
204                'title'       => __( 'Subscription', 'wp-module-ecommerce' ),
205                'description' => __( 'Ex: a monthly subscription box, a magazine subscription, streaming service like Netflix, etc - any type of product your customer pays for on a recurring basis.', 'wp-module-ecommerce' ),
206            ),
207        );
208
209        foreach ( $entitlements as $entitlement ) {
210            if ( ! is_array( $entitlement ) || ! array_key_exists( $entitlement['basename'], $premium_types ) ) {
211                continue;
212            }
213
214            $premium_type = $premium_types[ $entitlement['basename'] ];
215            // If plugin is active add redirect flag to handle modal type correctly.
216            if ( is_plugin_active( $entitlement['basename'] ) ) {
217                $premium_type['redirect'] = true;
218            } elseif ( $this->can_access_global_ctb() ) { // Can access global CTB, handle it.
219                $premium_type['premiumData'] = $entitlement;
220            }
221
222            $types[] = $premium_type;
223        }
224
225        return $types;
226    }
227
228    /**
229     * Can current user access global CTB?
230     */
231    protected function can_access_global_ctb(): bool {
232        return (bool) $this->container->get( 'capabilities' )->get( 'canAccessGlobalCTB' );
233    }
234
235    /**
236     * Handle product type (booking|subscription) for single product add-new page.
237     *
238     * @return void
239     */
240    public function handle_product_type_on_add_product() {
241        global $pagenow;
242
243        if ( 'post-new.php' !== $pagenow || ! isset( $_GET['post_type'], $_GET['product_type'] )
244            || 'product' !== sanitize_text_field( wp_unslash( $_GET['post_type'] ) )
245        ) {
246            return;
247        }
248
249        $product_type = sanitize_text_field( wp_unslash( $_GET['product_type'] ) );
250
251        if ( 'booking' === $product_type ) {
252            \wp_print_inline_script_tag( "jQuery(() => jQuery('#product-type').val('booking').change())" );
253        } elseif ( 'subscription' === $product_type ) {
254            \wp_print_inline_script_tag( "jQuery(() => jQuery('input[name=_bh_subscriptions_subscription]').prop('checked', true).change())" );
255        }
256    }
257}