| Total Complexity | 48 |
| Total Lines | 496 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Admin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Admin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Admin { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Holds instance of the class |
||
| 21 | * |
||
| 22 | * @var object \lsx\currencies\classes\Admin() |
||
| 23 | */ |
||
| 24 | private static $instance; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor |
||
| 28 | */ |
||
| 29 | public function __construct() { |
||
|
|
|||
| 30 | add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); |
||
| 31 | add_action( 'init', array( $this, 'create_settings_page' ), 100 ); |
||
| 32 | add_filter( 'lsx_framework_settings_tabs', array( $this, 'register_tabs' ), 100, 1 ); |
||
| 33 | add_filter( 'lsx_to_tour_custom_fields', array( $this, 'fields' ), 80, 1 ); |
||
| 34 | add_action( 'customize_register', array( $this, 'customize_register' ), 20 ); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Return an instance of this class. |
||
| 39 | * |
||
| 40 | * @return object |
||
| 41 | */ |
||
| 42 | public static function init() { |
||
| 43 | // If the single instance hasn't been set, set it now. |
||
| 44 | if ( ! isset( self::$instance ) ) { |
||
| 45 | self::$instance = new self(); |
||
| 46 | } |
||
| 47 | return self::$instance; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Enques the assets |
||
| 52 | */ |
||
| 53 | public function assets() { |
||
| 54 | //wp_enqueue_script( 'lsx-currencies-admin', LSX_CURRENCIES_URL . 'assets/js/lsx-currencies-admin.min.js', array( 'jquery' ), LSX_CURRENCIES_VER, true ); |
||
| 55 | wp_enqueue_style( 'lsx-currencies-admin', LSX_CURRENCIES_URL . 'assets/css/lsx-currencies-admin.css', array(), LSX_CURRENCIES_VER ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Returns the array of settings to the UIX Class |
||
| 60 | */ |
||
| 61 | public function create_settings_page() { |
||
| 62 | if ( is_admin() ) { |
||
| 63 | if ( ! class_exists( '\lsx\ui\uix' ) && ! function_exists( 'tour_operator' ) ) { |
||
| 64 | include_once LSX_CURRENCIES_PATH . 'vendor/uix/uix.php'; |
||
| 65 | $pages = $this->settings_page_array(); |
||
| 66 | $uix = \lsx\ui\uix::get_instance( 'lsx' ); |
||
| 67 | $uix->register_pages( $pages ); |
||
| 68 | } |
||
| 69 | add_action( 'lsx_to_framework_dashboard_tab_content', array( $this, 'general_settings' ), 11, 1 ); |
||
| 70 | add_action( 'lsx_to_framework_api_tab_content', array( $this, 'api_settings' ), 11, 1 ); |
||
| 71 | |||
| 72 | add_action( 'lsx_framework_display_tab_content', array( $this, 'general_settings' ), 11, 1 ); |
||
| 73 | add_action( 'lsx_framework_api_tab_content', array( $this, 'api_settings' ), 11, 1 ); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the array of settings to the UIX Class |
||
| 79 | */ |
||
| 80 | public function settings_page_array() { |
||
| 81 | $tabs = apply_filters( 'lsx_framework_settings_tabs', array() ); |
||
| 82 | |||
| 83 | return array( |
||
| 84 | 'settings' => array( |
||
| 85 | 'page_title' => esc_html__( 'Theme Options', 'lsx-currencies' ), |
||
| 86 | 'menu_title' => esc_html__( 'Theme Options', 'lsx-currencies' ), |
||
| 87 | 'capability' => 'manage_options', |
||
| 88 | 'icon' => 'dashicons-book-alt', |
||
| 89 | 'parent' => 'themes.php', |
||
| 90 | 'save_button' => esc_html__( 'Save Changes', 'lsx-currencies' ), |
||
| 91 | 'tabs' => $tabs, |
||
| 92 | ), |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Register tabs |
||
| 98 | */ |
||
| 99 | public function register_tabs( $tabs ) { |
||
| 100 | $default = true; |
||
| 101 | |||
| 102 | if ( false !== $tabs && is_array( $tabs ) && count( $tabs ) > 0 ) { |
||
| 103 | $default = false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if ( ! function_exists( 'tour_operator' ) ) { |
||
| 107 | /*if ( ! array_key_exists( 'general', $tabs ) ) { |
||
| 108 | $tabs['general'] = array( |
||
| 109 | 'page_title' => '', |
||
| 110 | 'page_description' => '', |
||
| 111 | 'menu_title' => esc_html__( 'General', 'lsx-currencies' ), |
||
| 112 | 'template' => LSX_CURRENCIES_PATH . 'includes/settings/general.php', |
||
| 113 | 'default' => $default, |
||
| 114 | ); |
||
| 115 | |||
| 116 | $default = false; |
||
| 117 | }*/ |
||
| 118 | |||
| 119 | if ( ! array_key_exists( 'display', $tabs ) ) { |
||
| 120 | $tabs['display'] = array( |
||
| 121 | 'page_title' => '', |
||
| 122 | 'page_description' => '', |
||
| 123 | 'menu_title' => esc_html__( 'Display', 'lsx-currencies' ), |
||
| 124 | 'template' => LSX_CURRENCIES_PATH . 'includes/settings/display.php', |
||
| 125 | 'default' => $default, |
||
| 126 | ); |
||
| 127 | |||
| 128 | $default = false; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( ! array_key_exists( 'api', $tabs ) ) { |
||
| 132 | $tabs['api'] = array( |
||
| 133 | 'page_title' => '', |
||
| 134 | 'page_description' => '', |
||
| 135 | 'menu_title' => esc_html__( 'API', 'lsx-currencies' ), |
||
| 136 | 'template' => LSX_CURRENCIES_PATH . 'includes/settings/api.php', |
||
| 137 | 'default' => $default, |
||
| 138 | ); |
||
| 139 | |||
| 140 | $default = false; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $tabs; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Outputs the dashboard tabs settings |
||
| 149 | * |
||
| 150 | * @param $tab string |
||
| 151 | * @return null |
||
| 152 | */ |
||
| 153 | public function general_settings( $tab = 'general' ) { |
||
| 154 | if ( 'currency_switcher' === $tab ) { |
||
| 155 | $this->base_currency_field(); |
||
| 156 | $this->additional_currencies_field(); |
||
| 157 | $this->remove_decimals_field(); |
||
| 158 | if ( function_exists( 'tour_operator' ) ) { |
||
| 159 | $this->enable_multiple_prices_field(); |
||
| 160 | $this->enable_convert_to_single_currency_field(); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Outputs the dashboard tabs settings |
||
| 167 | * |
||
| 168 | * @param $tab string |
||
| 169 | * @return null |
||
| 170 | */ |
||
| 171 | public function api_settings( $tab = 'general' ) { |
||
| 172 | if ( 'settings' === $tab ) { |
||
| 173 | $this->currency_api_heading(); |
||
| 174 | $this->api_key_field(); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Outputs the base currency drop down |
||
| 180 | */ |
||
| 181 | public function base_currency_field() { |
||
| 182 | ?> |
||
| 183 | <tr data-trigger="additional_currencies" class="lsx-select-trigger form-field-wrap"> |
||
| 184 | <th scope="row"> |
||
| 185 | <label for="currency"> |
||
| 186 | <?php |
||
| 187 | esc_html_e( 'Base Currency', 'lsx-currencies' ); |
||
| 188 | ?> |
||
| 189 | </label> |
||
| 190 | </th> |
||
| 191 | <td> |
||
| 192 | <?php |
||
| 193 | if ( ! function_exists( 'WC' ) ) { |
||
| 194 | ?> |
||
| 195 | <select value="{{currency}}" name="currency"> |
||
| 196 | <?php |
||
| 197 | foreach ( lsx_currencies()->available_currencies as $currency_id => $currency_label ) { |
||
| 198 | $selected = ''; |
||
| 199 | |||
| 200 | if ( lsx_currencies()->base_currency === $currency_id ) { |
||
| 201 | $selected = 'selected="selected"'; |
||
| 202 | } |
||
| 203 | $allowed_html = array( |
||
| 204 | 'option' => array( |
||
| 205 | 'value' => array(), |
||
| 206 | 'selected' => array(), |
||
| 207 | ), |
||
| 208 | ); |
||
| 209 | echo wp_kses( '<option value="' . $currency_id . '" ' . $selected . '>' . $currency_label . '</option>', $allowed_html ); |
||
| 210 | } |
||
| 211 | ?> |
||
| 212 | </select> |
||
| 213 | <?php |
||
| 214 | } else { |
||
| 215 | $currency_label = ''; |
||
| 216 | if ( isset( lsx_currencies()->available_currencies[ lsx_currencies()->base_currency ] ) ) { |
||
| 217 | $currency_label = lsx_currencies()->available_currencies[ lsx_currencies()->base_currency ]; |
||
| 218 | } |
||
| 219 | ?> |
||
| 220 | <p><?php echo wp_kses_post( lsx_currencies()->get_currency_flag( lsx_currencies()->base_currency ) ); ?> <?php echo esc_attr( $currency_label ); ?></p> |
||
| 221 | <p><small><?php esc_html_e( 'When WooCommerce is active, that currency is used as the base currency.', 'lsx-currencies' ); ?></small></p> |
||
| 222 | <?php |
||
| 223 | } |
||
| 224 | ?> |
||
| 225 | </td> |
||
| 226 | </tr> |
||
| 227 | <?php |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Outputs the additional currencies checkboxes |
||
| 232 | */ |
||
| 233 | public function additional_currencies_field() { |
||
| 234 | ?> |
||
| 235 | <tr data-trigger="currency" class="lsx-checkbox-action form-field-wrap"> |
||
| 236 | <th scope="row"> |
||
| 237 | <label for="modules"> |
||
| 238 | <?php |
||
| 239 | esc_html_e( 'Additional Currencies', 'lsx-currencies' ); |
||
| 240 | ?> |
||
| 241 | </label> |
||
| 242 | </th> |
||
| 243 | <td> |
||
| 244 | <ul> |
||
| 245 | <?php |
||
| 246 | foreach ( lsx_currencies()->available_currencies as $slug => $label ) { |
||
| 247 | $checked = ''; |
||
| 248 | $hidden = $checked; |
||
| 249 | if ( array_key_exists( $slug, lsx_currencies()->additional_currencies ) || lsx_currencies()->base_currency === $slug ) { |
||
| 250 | $checked = 'checked="checked"'; |
||
| 251 | } |
||
| 252 | |||
| 253 | if ( lsx_currencies()->base_currency === $slug ) { |
||
| 254 | $hidden = 'style="display:none;" class="hidden"'; |
||
| 255 | } |
||
| 256 | ?> |
||
| 257 | <li <?php echo wp_kses_post( $hidden ); ?>> |
||
| 258 | <input type="checkbox" <?php echo esc_attr( $checked ); ?> data-name="additional_currencies" data-value="<?php echo esc_attr( $slug ); ?>" name="additional_currencies[<?php echo esc_attr( $slug ); ?>]" /> <label for="additional_currencies"><?php echo wp_kses_post( lsx_currencies()->get_currency_flag( $slug ) . $label ); ?></label> |
||
| 259 | </li> |
||
| 260 | <?php |
||
| 261 | } |
||
| 262 | ?> |
||
| 263 | </ul> |
||
| 264 | </td> |
||
| 265 | </tr> |
||
| 266 | <?php |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Outputs the multiple prices checkbox |
||
| 271 | */ |
||
| 272 | public function remove_decimals_field() { |
||
| 273 | ?> |
||
| 274 | <tr class="form-field"> |
||
| 275 | <th scope="row"> |
||
| 276 | <label for="remove_decimals"><?php esc_html_e( 'Remove Decimals', 'lsx-currencies' ); ?></label> |
||
| 277 | </th> |
||
| 278 | <td> |
||
| 279 | <input type="checkbox" {{#if remove_decimals}} checked="checked" {{/if}} name="remove_decimals" /> |
||
| 280 | <small><?php esc_html_e( 'Round down the amount to the nearest full value.', 'lsx-currencies' ); ?></small> |
||
| 281 | </td> |
||
| 282 | </tr> |
||
| 283 | <?php |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Outputs the multiple prices checkbox |
||
| 288 | */ |
||
| 289 | public function enable_multiple_prices_field() { |
||
| 290 | ?> |
||
| 291 | <tr class="form-field"> |
||
| 292 | <th scope="row"> |
||
| 293 | <label for="multi_price"><?php esc_html_e( 'Enable Multiple Prices', 'lsx-currencies' ); ?></label> |
||
| 294 | </th> |
||
| 295 | <td> |
||
| 296 | <input type="checkbox" {{#if multi_price}} checked="checked" {{/if}} name="multi_price" /> |
||
| 297 | <small><?php esc_html_e( 'Allowing you to add specific prices per active currency.', 'lsx-currencies' ); ?></small> |
||
| 298 | </td> |
||
| 299 | </tr> |
||
| 300 | <?php |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Outputs the multiple prices checkbox |
||
| 305 | */ |
||
| 306 | public function enable_convert_to_single_currency_field() { |
||
| 307 | ?> |
||
| 308 | <tr class="form-field"> |
||
| 309 | <th scope="row"> |
||
| 310 | <label for="convert_to_single_currency"><?php esc_html_e( 'Enable Convert to Single Currency', 'lsx-currencies' ); ?></label> |
||
| 311 | </th> |
||
| 312 | <td> |
||
| 313 | <input type="checkbox" {{#if convert_to_single_currency}} checked="checked" {{/if}} name="convert_to_single_currency" /> |
||
| 314 | <small><?php esc_html_e( 'This will convert all prices added to the base currency, the currency switcher will not work.', 'lsx-currencies' ); ?></small> |
||
| 315 | </td> |
||
| 316 | </tr> |
||
| 317 | <?php |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Outputs the currency heading |
||
| 322 | */ |
||
| 323 | public function currency_api_heading() { |
||
| 324 | ?> |
||
| 325 | <tr class="form-field banner-wrap"> |
||
| 326 | <th class="table_heading" style="padding-bottom:0px;" scope="row" colspan="2"> |
||
| 327 | <h4 style="margin-bottom:0px;"><?php esc_html_e( 'Openexchange API', 'lsx-currencies' ); ?></h4> |
||
| 328 | </th> |
||
| 329 | </tr> |
||
| 330 | <?php |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Outputs the api key text field |
||
| 335 | */ |
||
| 336 | public function api_key_field() { |
||
| 337 | ?> |
||
| 338 | <tr class="form-field"> |
||
| 339 | <th scope="row"> |
||
| 340 | <i class="dashicons-before dashicons-admin-network"></i><label for="openexchange_api"> <?php esc_html_e( 'Key', 'to-maps' ); ?></label> |
||
| 341 | </th> |
||
| 342 | <td> |
||
| 343 | <input type="text" {{#if openexchange_api}} value="{{openexchange_api}}" {{/if}} name="openexchange_api" /> |
||
| 344 | <br /><small><?php esc_html_e( 'Get your API key here', 'lsx-currencies' ); ?> - <a target="_blank" rel="noopener noreferrer" href="https://openexchangerates.org/signup/free">openexchangerates.org</a></small> |
||
| 345 | </td> |
||
| 346 | </tr> |
||
| 347 | <?php |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * adds in our multiple prices field |
||
| 352 | */ |
||
| 353 | public function fields( $meta_boxes ) { |
||
| 354 | if ( true === lsx_currencies()->multi_prices && ! empty( lsx_currencies()->additional_currencies ) ) { |
||
| 355 | $currency_options = array(); |
||
| 356 | |||
| 357 | foreach ( lsx_currencies()->additional_currencies as $key => $values ) { |
||
| 358 | if ( lsx_currencies()->base_currency === $key ) { |
||
| 359 | continue; |
||
| 360 | } |
||
| 361 | |||
| 362 | $currency_options[ $key ] = lsx_currencies()->available_currencies[ $key ]; |
||
| 363 | } |
||
| 364 | |||
| 365 | $new_boxes = array(); |
||
| 366 | $injected = false; |
||
| 367 | if ( ! empty( $meta_boxes ) ) { |
||
| 368 | foreach ( $meta_boxes as $meta_box ) { |
||
| 369 | if ( 'price' === $meta_box['id'] ) { |
||
| 370 | $new_boxes[] = array( |
||
| 371 | 'id' => 'price', |
||
| 372 | 'name' => 'Base Price (' . lsx_currencies()->base_currency . ')', |
||
| 373 | 'type' => 'text', |
||
| 374 | ); |
||
| 375 | $new_boxes[] = array( |
||
| 376 | 'id' => 'additional_prices', |
||
| 377 | 'name' => '', |
||
| 378 | 'single_name' => 'Price', |
||
| 379 | 'type' => 'group', |
||
| 380 | 'repeatable' => true, |
||
| 381 | 'sortable' => true, |
||
| 382 | 'fields' => array( |
||
| 383 | array( |
||
| 384 | 'id' => 'amount', |
||
| 385 | 'name' => 'Amount', |
||
| 386 | 'type' => 'text', |
||
| 387 | ), |
||
| 388 | array( |
||
| 389 | 'id' => 'currency', |
||
| 390 | 'name' => 'Currency', |
||
| 391 | 'type' => 'select', |
||
| 392 | 'options' => $currency_options, |
||
| 393 | ), |
||
| 394 | ), |
||
| 395 | ); |
||
| 396 | $injected = true; |
||
| 397 | continue; |
||
| 398 | } |
||
| 399 | $new_boxes[] = $meta_box; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | if ( true === $injected ) { |
||
| 403 | $meta_boxes = $new_boxes; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | return $meta_boxes; |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Customizer Controls and Settings. |
||
| 411 | * |
||
| 412 | * @since 1.1.1 |
||
| 413 | */ |
||
| 414 | public function customize_register( $wp_customize ) { |
||
| 513 | ), |
||
| 514 | ) ) ); |
||
| 515 | } |
||
| 516 | } |
||
| 517 |