| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP\Accounting\Includes\Classes; |
| 4 |
|
| 5 |
/** |
| 6 |
* Scripts and Styles Class |
| 7 |
*/ |
| 8 |
class Assets { |
| 9 |
public function __construct() { |
| 10 |
if ( is_admin() ) { |
| 11 |
add_action( 'admin_enqueue_scripts', [ $this, 'register' ], 5 ); |
| 12 |
} else { |
| 13 |
add_action( 'wp_enqueue_scripts', [ $this, 'register' ], 5 ); |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Register our app scripts and styles |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function register() { |
| 23 |
$section = isset( $_GET['section'] ) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : ''; |
| 24 |
|
| 25 |
if ( is_admin() ) { |
| 26 |
$screen = get_current_screen(); |
| 27 |
|
| 28 |
if ( 'wp-erp_page_erp-settings' === $screen->base ) { |
| 29 |
wp_enqueue_script( 'accounting-helper', ERP_ACCOUNTING_ASSETS . '/js/accounting-helper.js', [ 'jquery', 'erp-tiptip' ], false, true ); |
| 30 |
|
| 31 |
wp_localize_script( |
| 32 |
'accounting-helper', |
| 33 |
'erp_acct_helper', |
| 34 |
[ |
| 35 |
'fin_overlap_msg' => __( 'Financial year values must not be overlapped!', 'erp' ), |
| 36 |
'fin_val_comp_msg' => __( 'Second value must be greater than the first value!', 'erp' ), |
| 37 |
] |
| 38 |
); |
| 39 |
|
| 40 |
return; |
| 41 |
} elseif ( 'wp-erp_page_erp-accounting' !== $screen->base && $section !== 'reimbursement' ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
$this->register_scripts( $this->get_scripts() ); |
| 47 |
$this->register_styles( $this->get_styles() ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Register scripts |
| 52 |
* |
| 53 |
* @param array $scripts |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
private function register_scripts( $scripts ) { |
| 58 |
global $current_user; |
| 59 |
$u_id = $current_user->ID; |
| 60 |
$site_url = site_url(); |
| 61 |
$logout_url = esc_url( wp_logout_url() ); |
| 62 |
$acct_url = admin_url( 'admin.php' ) . '?page=erp-accounting#/'; |
| 63 |
|
| 64 |
foreach ( $scripts as $handle => $script ) { |
| 65 |
$deps = isset( $script['deps'] ) ? $script['deps'] : false; |
| 66 |
$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false; |
| 67 |
$version = isset( $script['version'] ) ? $script['version'] : WPERP_VERSION; |
| 68 |
|
| 69 |
wp_register_script( $handle, $script['src'], $deps, $version, $in_footer ); |
| 70 |
} |
| 71 |
|
| 72 |
$menus = ''; |
| 73 |
|
| 74 |
if ( is_admin() ) { |
| 75 |
$component = 'accounting'; |
| 76 |
$menu = erp_menu(); |
| 77 |
$menus = $menu[ $component ]; |
| 78 |
|
| 79 |
//check items for capabilities |
| 80 |
$items = array_filter( |
| 81 |
$menus, |
| 82 |
function ( $item ) { |
| 83 |
if ( ! isset( $item['capability'] ) ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return current_user_can( $item['capability'] ); |
| 88 |
} |
| 89 |
); |
| 90 |
|
| 91 |
//sort items for position |
| 92 |
uasort( |
| 93 |
$menus, |
| 94 |
function ( $a, $b ) { |
| 95 |
return $a['position'] > $b['position']; |
| 96 |
} |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
$erp_acct_dec_separator = erp_get_option( 'erp_ac_de_separator', false, '.' ); |
| 101 |
$erp_acct_ths_separator = erp_get_option( 'erp_ac_th_separator', false, ',' ); |
| 102 |
|
| 103 |
$fy_ranges = erp_acct_get_date_boundary(); |
| 104 |
$ledgers = erp_acct_get_ledgers_with_balances(); |
| 105 |
$trn_statuses = erp_acct_get_all_trn_statuses(); |
| 106 |
|
| 107 |
wp_localize_script( 'accounting-bootstrap', 'erp_acct_var', [ |
| 108 |
'user_id' => $u_id, |
| 109 |
'site_url' => $site_url, |
| 110 |
'logout_url' => $logout_url, |
| 111 |
'acct_assets' => ERP_ACCOUNTING_ASSETS, |
| 112 |
'erp_assets' => WPERP_ASSETS, |
| 113 |
'erp_acct_menus' => $menus, |
| 114 |
'erp_acct_url' => $acct_url, |
| 115 |
'decimal_separator' => $erp_acct_dec_separator, |
| 116 |
'thousand_separator' => $erp_acct_ths_separator, |
| 117 |
'currency_format' => erp_acct_get_price_format(), |
| 118 |
'symbol' => erp_acct_get_currency_symbol(), |
| 119 |
'erp_debug_mode' => erp_get_option( 'erp_debug_mode', 'erp_settings_general', 0 ), |
| 120 |
'current_date' => date( 'Y-m-d' ), |
| 121 |
'fy_lower_range' => $fy_ranges['lower'], |
| 122 |
'fy_upper_range' => $fy_ranges['upper'], |
| 123 |
'ledgers' => $ledgers, |
| 124 |
'trn_statuses' => $trn_statuses, |
| 125 |
'pdf_plugin_active' => is_plugin_active( 'erp-pdf-invoice/wp-erp-pdf.php' ), |
| 126 |
'link_copy_success' => __( 'Link has been successfully copied.', 'erp' ), |
| 127 |
'link_copy_error' => __( 'Failed to copy the link.', 'erp' ), |
| 128 |
'banner_dimension' => [ |
| 129 |
'width' => 600, |
| 130 |
'height' => 600, |
| 131 |
'flex-width' => true, |
| 132 |
'flex-height' => true, |
| 133 |
], |
| 134 |
'rest' => [ |
| 135 |
'root' => esc_url_raw( get_rest_url() ), |
| 136 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 137 |
'version' => 'erp/v1', |
| 138 |
], |
| 139 |
] ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Register styles |
| 144 |
* |
| 145 |
* @param array $styles |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
public function register_styles( $styles ) { |
| 150 |
foreach ( $styles as $handle => $style ) { |
| 151 |
$deps = isset( $style['deps'] ) ? $style['deps'] : false; |
| 152 |
|
| 153 |
wp_register_style( $handle, $style['src'], $deps, WPERP_VERSION ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get all registered scripts |
| 159 |
* |
| 160 |
* @return array |
| 161 |
*/ |
| 162 |
public function get_scripts() { |
| 163 |
$scripts = [ |
| 164 |
'accounting-vendor' => [ |
| 165 |
'src' => WPERP_ASSETS . '/js/vendor.js', |
| 166 |
'version' => filemtime( WPERP_PATH . '/assets/js/vendor.js' ), |
| 167 |
'in_footer' => true, |
| 168 |
], |
| 169 |
'accounting-bootstrap' => [ |
| 170 |
'src' => ERP_ACCOUNTING_ASSETS . '/js/bootstrap.js', |
| 171 |
'deps' => [ 'accounting-vendor' ], |
| 172 |
'version' => filemtime( ERP_ACCOUNTING_PATH . '/assets/js/bootstrap.js' ), |
| 173 |
'in_footer' => true, |
| 174 |
], |
| 175 |
'accounting-frontend' => [ |
| 176 |
'src' => ERP_ACCOUNTING_ASSETS . '/js/frontend.js', |
| 177 |
'deps' => [ 'jquery', 'accounting-vendor' ], |
| 178 |
'version' => filemtime( ERP_ACCOUNTING_PATH . '/assets/js/frontend.js' ), |
| 179 |
'in_footer' => true, |
| 180 |
], |
| 181 |
'accounting-admin' => [ |
| 182 |
'src' => ERP_ACCOUNTING_ASSETS . '/js/admin.js', |
| 183 |
'deps' => [ 'jquery', 'accounting-vendor' ], |
| 184 |
'version' => filemtime( ERP_ACCOUNTING_PATH . '/assets/js/admin.js' ), |
| 185 |
'in_footer' => true, |
| 186 |
], |
| 187 |
]; |
| 188 |
|
| 189 |
return $scripts; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get registered styles |
| 194 |
* |
| 195 |
* @return array |
| 196 |
*/ |
| 197 |
public function get_styles() { |
| 198 |
$styles = [ |
| 199 |
'accounting-style' => [ |
| 200 |
'src' => ERP_ACCOUNTING_ASSETS . '/css/style.css', |
| 201 |
], |
| 202 |
'accounting-frontend' => [ |
| 203 |
'src' => ERP_ACCOUNTING_ASSETS . '/css/frontend.css', |
| 204 |
], |
| 205 |
'accounting-admin' => [ |
| 206 |
'src' => ERP_ACCOUNTING_ASSETS . '/css/admin.css', |
| 207 |
], |
| 208 |
]; |
| 209 |
|
| 210 |
return $styles; |
| 211 |
} |
| 212 |
} |
| 213 |
|