Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SolutionsUpsell
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 load_assets
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
30
 get_entitlement_upsell
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 can_access_global_ctb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace NewfoldLabs\WP\Module\Solutions;
4
5use NewfoldLabs\WP\ModuleLoader\Container;
6
7/**
8 * Manages all the functionalities for the module.
9 */
10class SolutionsUpsell {
11
12    /**
13     * Dependency injection container.
14     *
15     * @var Container
16     */
17    protected $container;
18
19    /**
20     * Class construct
21     *
22     * @param  Container $container  The module container.
23     */
24    public function __construct( Container $container ) {
25        $this->container = $container;
26        $this->init();
27    }
28
29    /**
30     * Init class hooks and filters
31     */
32    protected function init(): void {
33        if ( ! $this->can_access_global_ctb() ) {
34            return;
35        }
36
37        \add_action( 'admin_enqueue_scripts', array( $this, 'load_assets' ), 20 );
38    }
39
40    /**
41     * Enqueue assets for solutions upsell sections
42     */
43    public function load_assets(): void {
44        $asset_file = NFD_SOLUTIONS_DIR . '/build/solutions-upsell/bundle.asset.php';
45        if ( is_readable( $asset_file ) ) {
46            $asset = include_once $asset_file;
47        } else {
48            return;
49        }
50
51        \wp_register_script(
52            'solutions-upsell',
53            NFD_SOLUTIONS_PLUGIN_URL . 'vendor/newfold-labs/wp-module-solutions/build/solutions-upsell/bundle.js',
54            array_merge(
55                $asset['dependencies'],
56                array( 'newfold-global-ctb', 'nfd-installer-listener', 'nfd-installer', 'selectWoo' ),
57            ),
58            $asset['version'],
59            true
60        );
61
62        \wp_register_style(
63            'solutions-upsell-style',
64            NFD_SOLUTIONS_PLUGIN_URL . 'vendor/newfold-labs/wp-module-solutions/build/solutions-upsell/style-solutions-upsell.css',
65            array(),
66            $asset['version']
67        );
68
69        // Only enqueue on solutions page.
70        $screen = \get_current_screen();
71        if ( isset( $screen->id, $screen->post_type ) && 'product' === $screen->id && 'product' === $screen->post_type ) {
72            \wp_enqueue_script( 'solutions-upsell' );
73            \wp_enqueue_style( 'solutions-upsell-style' );
74            \wp_enqueue_style( 'newfold-global-ctb-style' );
75            \wp_enqueue_style( 'nfd-installer' );
76
77            \wp_localize_script(
78                'solutions-upsell',
79                'NewfoldSolutionsUpsell',
80                array_merge(
81                    $this->get_entitlement_upsell(),
82                )
83            );
84        }
85    }
86
87    /**
88     * Get upsell
89     */
90    protected function get_entitlement_upsell(): array {
91
92        $upsell            = array();
93        $entitlements_data = Solutions::get_enhanced_entitlment_data();
94        $entitlements      = array_merge(
95            isset( $entitlements_data['entitlements'] ) ? $entitlements_data['entitlements'] : array(),
96            isset( $entitlements_data['premium'] ) ? $entitlements_data['premium'] : array()
97        );
98
99        $entitlements_with_upsell = array(
100            'yith-woocommerce-booking-premium/init.php'    => _x( 'Bookable product', 'Solution Upsell product type label', 'wp-module-solutions' ),
101            'yith-woocommerce-gift-cards-premium/init.php' => _x( 'Gift card', 'Solution Upsell product type label', 'wp-module-solutions' ),
102        );
103
104        foreach ( $entitlements as $entitlement ) {
105            if ( ! is_array( $entitlement ) || is_plugin_active( $entitlement['basename'] ) || ! array_key_exists( $entitlement['basename'], $entitlements_with_upsell ) ) {
106                continue;
107            }
108
109            $upsell[] = array_merge(
110                array( 'option_label' => $entitlements_with_upsell[ $entitlement['basename'] ] ),
111                $entitlement,
112            );
113        }
114
115        return $upsell;
116    }
117
118    /**
119     * Can current user access global CTB?
120     */
121    protected function can_access_global_ctb(): bool {
122        return (bool) $this->container->get( 'capabilities' )->get( 'canAccessGlobalCTB' );
123    }
124}