| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
if ( !class_exists( 'WooCommerce_PostNL_Assets' ) ) : |
| 5 |
|
| 6 |
class WooCommerce_PostNL_Assets { |
| 7 |
|
| 8 |
function __construct() { |
| 9 |
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts_styles' ) ); |
| 10 |
add_action( 'admin_enqueue_scripts', array( $this, 'backend_scripts_styles' ) ); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Load styles & scripts |
| 15 |
*/ |
| 16 |
public function frontend_scripts_styles ( $hook ) { |
| 17 |
if ( is_checkout() && isset(WooCommerce_PostNL()->checkout_settings['postnl_checkout']) && is_order_received_page() === false ) { |
| 18 |
// checkout scripts |
| 19 |
wp_enqueue_script( |
| 20 |
'wc-postnl-frontend', |
| 21 |
WooCommerce_PostNL()->plugin_url() . '/assets/js/wcmp-frontend.js', |
| 22 |
array( 'jquery' ), |
| 23 |
WC_POSTNL_VERSION |
| 24 |
); |
| 25 |
wp_localize_script( |
| 26 |
'wc-postnl-frontend', |
| 27 |
'wc_postnl_frontend', |
| 28 |
array( |
| 29 |
'iframe_url' => WooCommerce_PostNL()->plugin_url() . '/includes/views/wcmp-delivery-options.php?v=' . time(), |
| 30 |
) |
| 31 |
); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Load styles & scripts |
| 37 |
*/ |
| 38 |
public function backend_scripts_styles ( $hook ) { |
| 39 |
global $post_type; |
| 40 |
$screen = get_current_screen(); |
| 41 |
|
| 42 |
if( $post_type == 'shop_order' || ( is_object( $screen ) && strpos( $screen->id, 'postnl' ) !== false ) ) { |
| 43 |
// WC2.3+ load all WC scripts for shipping_method search! |
| 44 |
if ( version_compare( WOOCOMMERCE_VERSION, '2.3', '>=' ) ) { |
| 45 |
wp_enqueue_script( 'woocommerce_admin' ); |
| 46 |
wp_enqueue_script( 'iris' ); |
| 47 |
if (!wp_script_is( 'wc-enhanced-select', 'registered' )) { |
| 48 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 49 |
wp_register_script( |
| 50 |
'wc-enhanced-select', |
| 51 |
WC()->plugin_url() . '/assets/js/admin/wc-enhanced-select' . $suffix . '.js', |
| 52 |
array( |
| 53 |
'jquery', |
| 54 |
version_compare( WC()->version, '3.2.0', '>=' ) ? 'selectWoo' : 'select2', |
| 55 |
), |
| 56 |
WC_VERSION |
| 57 |
); |
| 58 |
} |
| 59 |
wp_enqueue_script( 'wc-enhanced-select' ); |
| 60 |
wp_enqueue_script( 'jquery-ui-sortable' ); |
| 61 |
wp_enqueue_script( 'jquery-ui-autocomplete' ); |
| 62 |
wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), WC_VERSION ); |
| 63 |
} |
| 64 |
|
| 65 |
// Add the color picker css file |
| 66 |
wp_enqueue_style( 'wp-color-picker' ); |
| 67 |
wp_enqueue_script( 'thickbox' ); |
| 68 |
wp_enqueue_style( 'thickbox' ); |
| 69 |
wp_enqueue_script( |
| 70 |
'wcpostnl-export', |
| 71 |
WooCommerce_PostNL()->plugin_url() . '/assets/js/wcmp-admin.js', |
| 72 |
array( 'jquery', 'thickbox', 'wp-color-picker' ), |
| 73 |
WC_POSTNL_VERSION |
| 74 |
); |
| 75 |
|
| 76 |
wp_localize_script( |
| 77 |
'wcpostnl-export', |
| 78 |
'wc_postnl', |
| 79 |
array( |
| 80 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 81 |
'nonce' => wp_create_nonce('wc_postnl'), |
| 82 |
'download_display' => $this->get_download_display(), |
| 83 |
'offset' => $this->get_label_position(), |
| 84 |
'offset_icon' => WooCommerce_PostNL()->plugin_url() . '/assets/img/print-offset-icon.png', |
| 85 |
'offset_label' => __( 'Labels to skip', 'woocommerce-postnl' ), |
| 86 |
) |
| 87 |
); |
| 88 |
|
| 89 |
wp_enqueue_style( |
| 90 |
'wcmp-admin-styles', |
| 91 |
WooCommerce_PostNL()->plugin_url() . '/assets/css/wcmp-admin-styles.css', |
| 92 |
array(), |
| 93 |
WC_POSTNL_VERSION, |
| 94 |
'all' |
| 95 |
); |
| 96 |
|
| 97 |
// Legacy styles (WC 2.1+ introduced MP6 style with larger buttons) |
| 98 |
if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) { |
| 99 |
wp_enqueue_style( |
| 100 |
'wcmp-admin-styles-legacy', |
| 101 |
WooCommerce_PostNL()->plugin_url() . '/assets/css/wcmp-admin-styles-legacy.css', |
| 102 |
array(), |
| 103 |
WC_POSTNL_VERSION, |
| 104 |
'all' |
| 105 |
); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
private function get_label_position() |
| 114 |
{ |
| 115 |
$generalSettings = WooCommerce_PostNL()->general_settings; |
| 116 |
|
| 117 |
if ($generalSettings['label_format'] == 'A4') { |
| 118 |
return isset($generalSettings['print_position_offset']) ? $generalSettings['print_position_offset'] : ''; |
| 119 |
} |
| 120 |
|
| 121 |
return ''; |
| 122 |
} |
| 123 |
|
| 124 |
private function get_download_display() |
| 125 |
{ |
| 126 |
if (isset(WooCommerce_PostNL()->general_settings['download_display'])) { |
| 127 |
return WooCommerce_PostNL()->general_settings['download_display']; |
| 128 |
} |
| 129 |
|
| 130 |
return ''; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
endif; // class_exists |
| 135 |
|
| 136 |
return new WooCommerce_PostNL_Assets(); |