Actions
1 year ago
Connectors
1 year ago
Traits
1 year ago
Cmbird_Acf.php
1 year ago
Cors.php
1 year ago
Template.php
1 year ago
index.php
1 year ago
Template.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\Admin; |
| 4 | |
| 5 | use CommerceBird\Admin\Traits\Singleton; |
| 6 | use CommerceBird\API\CreateOrderWebhook; |
| 7 | use CommerceBird\API\ProductWebhook; |
| 8 | use CommerceBird\API\ShippingWebhook; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | final class Template { |
| 15 | use Singleton; |
| 16 | |
| 17 | const NAME = 'commercebird-app'; |
| 18 | |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_menu', array( $this, 'menu' ) ); |
| 21 | add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) ); |
| 22 | } |
| 23 | |
| 24 | public function menu(): void { |
| 25 | $svg = CMBIRD_URL . 'admin/commercebird-icon.svg'; |
| 26 | add_menu_page( |
| 27 | __( 'CommerceBird', 'commercebird' ), |
| 28 | __( 'CommerceBird', 'commercebird' ), |
| 29 | 'manage_woocommerce', |
| 30 | self::NAME, |
| 31 | function () { |
| 32 | wp_enqueue_style( self::NAME ); |
| 33 | wp_enqueue_style( self::NAME . '-notify', CMBIRD_URL . 'admin/css/notyf.min.css', array(), CMBIRD_VERSION ); |
| 34 | // Pass version number to the Vue script |
| 35 | wp_localize_script( |
| 36 | self::NAME, |
| 37 | 'cmbirdData', |
| 38 | array( |
| 39 | 'version' => CMBIRD_VERSION, |
| 40 | ) |
| 41 | ); |
| 42 | wp_enqueue_script( self::NAME ); |
| 43 | add_filter( 'script_loader_tag', array( $this, 'add_module' ), 10, 3 ); |
| 44 | printf( '<div id="%s">Loading...</div>', esc_attr( self::NAME ) ); |
| 45 | }, |
| 46 | $svg, |
| 47 | 29 |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * It will add module attribute to script tag. |
| 53 | * |
| 54 | * @param string $tag of script. |
| 55 | * @param string $id of script. |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function add_module( string $tag, string $id ): string { |
| 60 | if ( self::NAME === $id ) { |
| 61 | $tag = str_replace( '<script ', '<script type="module" ', $tag ); |
| 62 | } |
| 63 | |
| 64 | return $tag; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * It loads scripts based on plugin's mode, dev or prod. |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public function scripts(): void { |
| 73 | global $wp_roles; |
| 74 | wp_register_style( self::NAME, CMBIRD_URL . 'admin/assets/dist/index.css', array(), CMBIRD_VERSION ); |
| 75 | wp_register_script( self::NAME, CMBIRD_URL . 'admin/assets/dist/index.js', array(), CMBIRD_VERSION, true ); |
| 76 | wp_add_inline_style( self::NAME, '#wpcontent, .auto-fold #wpcontent{padding-left: 0px} #wpcontent .notice, #wpcontent #message{display: none} input[type=checkbox]:checked::before{content:unset}' ); |
| 77 | $active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); |
| 78 | wp_localize_script( |
| 79 | self::NAME, |
| 80 | 'commercebird_admin', |
| 81 | array( |
| 82 | 'security_token' => wp_create_nonce( self::NAME ), |
| 83 | 'api_token' => get_option( 'cmbird_zi_webhook_password', false ), |
| 84 | 'webhooks' => array( |
| 85 | 'Items' => ProductWebhook::endpoint(), |
| 86 | 'Order Create' => CreateOrderWebhook::endpoint(), |
| 87 | 'Shipping Status' => ShippingWebhook::endpoint(), |
| 88 | ), |
| 89 | 'redirect_uri' => admin_url( 'admin.php?page=commercebird-app' ), |
| 90 | 'url' => admin_url( 'admin-ajax.php' ), |
| 91 | 'wc_tax_enabled' => is_plugin_active( 'woocommerce/woocommerce.php' ) ? wc_tax_enabled() : false, |
| 92 | 'roles' => $wp_roles->get_names(), |
| 93 | 'b2b_enabled' => class_exists( 'Addify_B2B_Plugin' ), |
| 94 | 'fileinfo_enabled' => extension_loaded( 'fileinfo' ), |
| 95 | 'acf_enabled' => class_exists( 'ACF' ), |
| 96 | 'cosw_enabled' => self::is_wc_shipped_status_exists(), |
| 97 | 'wcb2b_enabled' => class_exists( 'WooCommerceB2B' ), |
| 98 | 'wcb2b_groups' => get_transient( 'wc_b2b_groups' ), |
| 99 | 'site_url' => site_url(), |
| 100 | 'eo_sync' => get_option( 'cmbird_exact_online_sync_orders' ), |
| 101 | ), |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Check if the status "shipped" exists in WooCommerce statuses. |
| 107 | * |
| 108 | * @return bool |
| 109 | */ |
| 110 | public static function is_wc_shipped_status_exists(): bool { |
| 111 | $shipped_status = wc_get_order_statuses(); |
| 112 | return isset( $shipped_status['wc-shipped'] ); |
| 113 | } |
| 114 | } |
| 115 |