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