Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.02% covered (danger)
6.02%
5 / 83
10.00% covered (danger)
10.00%
1 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreInfo
6.02% covered (danger)
6.02%
5 / 83
10.00% covered (danger)
10.00%
1 / 10
256.85
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
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 enqueue_scripts
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
20
 output_root
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_store_info
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 get_countries
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_states
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_store_industries
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
2
 format_localized_array
0.00% covered (danger)
0.00%
0 / 11
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
1<?php
2
3namespace NewfoldLabs\WP\Module\ECommerce;
4
5use NewfoldLabs\WP\Module\ECommerce\Data\Brands;
6use NewfoldLabs\WP\ModuleLoader\Container;
7
8/**
9 * Store Info Class
10 */
11class StoreInfo {
12
13    /**
14     * Holds the container object
15     *
16     * @var Container
17     */
18    protected $container;
19
20    /**
21     * Constructor
22     *
23     * @param  Container $container  Plugin container.
24     */
25    public function __construct( Container $container ) {
26        $this->container = $container;
27    }
28
29    /**
30     * Initialize class hooks/filters
31     *
32     * @return void
33     */
34    public function init() {
35
36        // Enable it only for Bluehost, web brand plugin.
37        if ( ! Brands::is_brand_compatible( $this->container->plugin()->id ) ) {
38            return;
39        }
40
41        \add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 25 );
42        \add_action( 'admin_footer', array( $this, 'output_root' ) );
43        // Register API routes.
44        \add_action( 'rest_api_init', array( $this, 'register_routes' ) );
45    }
46
47    /**
48     * Enqueue scripts
49     *
50     * @return void
51     */
52    public function enqueue_scripts() {
53        global $current_screen;
54
55        $asset_file = NFD_ECOMMERCE_BUILD_DIR . 'store-info/index.asset.php';
56        if ( file_exists( $asset_file ) ) {
57            $asset = require $asset_file;
58
59            // Register script.
60            wp_register_script(
61                'store-info',
62                NFD_ECOMMERCE_PLUGIN_URL . 'vendor/newfold-labs/wp-module-ecommerce/build/store-info/index.js',
63                $asset['dependencies'],
64                $asset['version'],
65                true
66            );
67
68            wp_localize_script(
69                'store-info',
70                'NFDStoreInfo',
71                array(
72                    'data'           => $this->get_store_info(),
73                    'countryOptions' => $this->get_countries(),
74                    'stateOptions'   => $this->get_states(),
75                    'industries'     => $this->get_store_industries(),
76                )
77            );
78
79            wp_register_style(
80                'store-info-style',
81                NFD_ECOMMERCE_PLUGIN_URL . 'vendor/newfold-labs/wp-module-ecommerce/build/store-info/store-info.css',
82                array(),
83                $asset['version']
84            );
85
86            if ( isset( $_GET['page'] ) && Brands::is_brand_compatible( sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) ) {
87
88                wp_enqueue_global_styles_css_custom_properties();
89                wp_enqueue_media();
90
91                wp_enqueue_script( 'store-info' );
92                wp_enqueue_style( 'store-info-style' );
93            }
94        }
95    }
96
97    /**
98     * Output root elem
99     *
100     * @return void
101     */
102    public function output_root() {
103        echo '<div id="nfd-store-info-modal-wrapper"></div>';
104    }
105
106    /**
107     * Get current store info
108     *
109     * @return array
110     */
111    protected function get_store_info() {
112        if ( ! function_exists( 'WC' ) ) {
113            return array();
114        }
115
116        return array_merge(
117            array(
118                'address'  => \get_option( 'woocommerce_store_address', '' ),
119                'city'     => \get_option( 'woocommerce_store_city', '' ),
120                'postcode' => \get_option( 'woocommerce_store_postcode', '' ),
121                'industry' => \get_option( 'nfd_store_industry', 'other' ), // default is set here to populate form
122            ),
123            wc_get_base_location()
124        );
125    }
126
127    /**
128     * Get countries array
129     *
130     * @return array
131     */
132    protected function get_countries() {
133        if ( ! function_exists( 'WC' ) ) {
134            return array();
135        }
136
137        return $this->format_localized_array( WC()->countries->get_countries() );
138    }
139
140    /**
141     * Get states array
142     *
143     * @return array
144     */
145    protected function get_states() {
146        if ( ! function_exists( 'WC' ) ) {
147            return array();
148        }
149
150        $states = array_filter( wc()->countries->get_states() );
151        return array_map( array( $this, 'format_localized_array' ), $states );
152    }
153
154    /**
155     * Return an array of store industries
156     *
157     * @return array
158     */
159    protected function get_store_industries() {
160        return $this->format_localized_array(
161            array(
162                'fashion-apparel-accessories'     => __( 'Fashion, apparel, and accessories', 'wp-module-ecommerce' ),
163                'health-beauty'                   => __( 'Health and beauty', 'wp-module-ecommerce' ),
164                'electronics-computers'           => __( 'Electronics and computers', 'wp-module-ecommerce' ),
165                'food-drink'                      => __( 'Food and drink', 'wp-module-ecommerce' ),
166                'home-furniture-garden'           => __( 'Home, furniture, and garden', 'wp-module-ecommerce' ),
167                'cbd-other-hemp-derived-products' => __( 'CBD and other hemp-derived products', 'wp-module-ecommerce' ),
168                'education-and-learning'          => __( 'Education and learning', 'wp-module-ecommerce' ),
169                'sports-and-recreation'           => __( 'Sports and recreation', 'wp-module-ecommerce' ),
170                'arts-and-crafts'                 => __( 'Arts and crafts', 'wp-module-ecommerce' ),
171                'other'                           => __( 'Other', 'wp-module-ecommerce' ),
172            )
173        );
174    }
175
176    /**
177     * Format data for localized script
178     *
179     * @param array $data The data array.
180     * @return array
181     */
182    protected function format_localized_array( $data ) {
183        $formatted = array();
184
185        array_walk(
186            $data,
187            function ( &$value, $key ) use ( &$formatted ) {
188                $formatted[] = array(
189                    'value' => $key,
190                    'label' => $value,
191                );
192            }
193        );
194
195        return $formatted;
196    }
197
198    /**
199     * Register create product API route
200     *
201     * @return void
202     */
203    public function register_routes() {
204        ( new \NewfoldLabs\WP\Module\ECommerce\RestApi\StoreInfoController() )->register_routes();
205    }
206}