| 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 |
add_action( 'admin_notices', array( $this, 'subscription_expired_notice' ) ); |
| 23 |
add_action( 'admin_head', array( $this, 'admin_notice_styles' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
public function menu(): void { |
| 27 |
$svg = CMBIRD_URL . 'admin/commercebird-icon.svg'; |
| 28 |
add_menu_page( |
| 29 |
__( 'CommerceBird', 'commercebird' ), |
| 30 |
__( 'CommerceBird', 'commercebird' ), |
| 31 |
'manage_woocommerce', |
| 32 |
self::NAME, |
| 33 |
function () { |
| 34 |
wp_enqueue_style( self::NAME ); |
| 35 |
wp_enqueue_style( self::NAME . '-notify', CMBIRD_URL . 'admin/css/notyf.min.css', array(), CMBIRD_VERSION ); |
| 36 |
// Pass version number to the Vue script. |
| 37 |
wp_localize_script( |
| 38 |
self::NAME, |
| 39 |
'cmbirdData', |
| 40 |
array( |
| 41 |
'version' => CMBIRD_VERSION, |
| 42 |
) |
| 43 |
); |
| 44 |
wp_enqueue_script( self::NAME ); |
| 45 |
add_filter( 'script_loader_tag', array( $this, 'add_module' ), 10, 2 ); |
| 46 |
printf( '<div id="%s">Loading...</div>', esc_attr( self::NAME ) ); |
| 47 |
}, |
| 48 |
$svg, |
| 49 |
29 |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* It will add module attribute to script tag. |
| 55 |
* |
| 56 |
* @param string $tag of script. |
| 57 |
* @param string $id of script. |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function add_module( string $tag, string $id ): string { |
| 62 |
if ( self::NAME === $id ) { |
| 63 |
$tag = str_replace( '<script ', '<script type="module" ', $tag ); |
| 64 |
} |
| 65 |
|
| 66 |
return $tag; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* It loads scripts based on plugin's mode, dev or prod. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function scripts(): void { |
| 75 |
global $wp_roles; |
| 76 |
wp_register_style( self::NAME, CMBIRD_URL . 'admin/assets/dist/index.css', array(), CMBIRD_VERSION ); |
| 77 |
wp_register_script( self::NAME, CMBIRD_URL . 'admin/assets/dist/index.js', array(), CMBIRD_VERSION, true ); |
| 78 |
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}' ); |
| 79 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WordPress core filter. |
| 80 |
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); |
| 81 |
wp_localize_script( |
| 82 |
self::NAME, |
| 83 |
'commercebird_admin', |
| 84 |
array( |
| 85 |
'security_token' => wp_create_nonce( self::NAME ), |
| 86 |
'api_token' => get_option( 'cmbird_zi_webhook_password', false ), |
| 87 |
'webhooks' => array( |
| 88 |
'Items' => ProductWebhook::endpoint(), |
| 89 |
'Order Create' => CreateOrderWebhook::endpoint(), |
| 90 |
'Shipping Status' => ShippingWebhook::endpoint(), |
| 91 |
), |
| 92 |
'redirect_uri' => admin_url( 'admin.php?page=commercebird-app' ), |
| 93 |
'url' => admin_url( 'admin-ajax.php' ), |
| 94 |
'wc_tax_enabled' => is_plugin_active( 'woocommerce/woocommerce.php' ) ? wc_tax_enabled() : false, |
| 95 |
'roles' => $wp_roles->get_names(), |
| 96 |
'b2b_enabled' => class_exists( 'Addify_B2B_Plugin' ), |
| 97 |
'fileinfo_enabled' => extension_loaded( 'fileinfo' ), |
| 98 |
'acf_enabled' => class_exists( 'ACF' ), |
| 99 |
'wcb2b_enabled' => \CMBIRD_Common_Functions::is_wcb2b_active(), |
| 100 |
'wcb2b_groups' => get_option( 'cmbird_wcb2b_groups' ), |
| 101 |
'site_url' => site_url(), |
| 102 |
'eo_sync' => get_option( 'cmbird_exact_online_sync_orders' ), |
| 103 |
'wc_countries' => is_plugin_active( 'woocommerce/woocommerce.php' ) ? WC()->countries->get_countries() : array(), |
| 104 |
), |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Add custom styles for admin notices |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function admin_notice_styles(): void { |
| 114 |
?> |
| 115 |
<style> |
| 116 |
.cmbird-notice h3 { |
| 117 |
color: # d63638; |
| 118 |
margin: .75em 0 0 0; |
| 119 |
} |
| 120 |
|
| 121 |
.cmbird-notice p { |
| 122 |
margin: 0.5em 0; |
| 123 |
} |
| 124 |
|
| 125 |
.cmbird-license-notice { |
| 126 |
border-left-color: # d63638; |
| 127 |
} |
| 128 |
</style> |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Display subscription expired/on-hold notice in wp-admin |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function subscription_expired_notice(): void { |
| 138 |
// Only show on admin pages and to users who can manage woocommerce. |
| 139 |
if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Get subscription ID from options. |
| 144 |
$subscription_id = get_option( 'commercebird-subscription-id', 0 ); |
| 145 |
if ( empty( $subscription_id ) ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
// Check subscription status. |
| 150 |
$subscription_data = $this->get_subscription_status(); |
| 151 |
|
| 152 |
if ( isset( $subscription_data['status'] ) && in_array( $subscription_data['status'], array( 'on-hold', 'expired', 'cancelled' ) ) ) { |
| 153 |
$class = 'notice notice-error cmbird-notice cmbird-license-notice'; |
| 154 |
$exclamation_icon_url = CMBIRD_URL . 'admin/exclamation-triangle.svg'; |
| 155 |
$renew_url = "https://commercebird.eu/my-account/view-subscription/{$subscription_id}"; |
| 156 |
|
| 157 |
$message = sprintf( |
| 158 |
'<h3 style="margin: .75em 0 0 0;"><img src="%s" style="vertical-align: text-top; width: 20px; margin-right: 7px;">%s</h3><p>%s</p><p><a href="%s" class="button-primary">%s</a> <a href="%s" class="button-secondary">%s</a></p>', |
| 159 |
esc_url( $exclamation_icon_url ), |
| 160 |
esc_html__( 'Heads up! Your CommerceBird license has expired.', 'commercebird' ), |
| 161 |
esc_html__( 'An active license is needed to use the Zoho & Exact Online integrations. It also provides access to new features, plugin updates (including security improvements), and our world class support!', 'commercebird' ), |
| 162 |
esc_url( $renew_url ), |
| 163 |
esc_html__( 'Renew Now', 'commercebird' ), |
| 164 |
esc_url( 'https://support.commercebird.com' ), |
| 165 |
esc_html__( 'Learn More', 'commercebird' ) |
| 166 |
); |
| 167 |
|
| 168 |
printf( '<div class="%1$s" id="cmbird-notice-license-expired">%2$s</div>', esc_attr( $class ), wp_kses_post( $message ) ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get subscription status data |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
private function get_subscription_status(): array { |
| 178 |
// Check transient first to avoid excessive API calls. |
| 179 |
$transient = get_transient( 'subscription_details' ); |
| 180 |
if ( ! empty( $transient ) && isset( $transient['status'] ) ) { |
| 181 |
return $transient; |
| 182 |
} |
| 183 |
|
| 184 |
// Fallback: try to get from a stored option or return empty. |
| 185 |
// This will need to be populated by the subscription check process. |
| 186 |
$stored_status = get_option( 'commercebird-subscription-status', '' ); |
| 187 |
if ( ! empty( $stored_status ) ) { |
| 188 |
return array( 'status' => $stored_status ); |
| 189 |
} |
| 190 |
|
| 191 |
return array(); |
| 192 |
} |
| 193 |
} |
| 194 |
|