| 1 |
<?php namespace TierPricingTable\Admin; |
| 2 |
|
| 3 |
use TierPricingTable\Admin\ProductManagers\ProductManager; |
| 4 |
use TierPricingTable\Freemius; |
| 5 |
use TierPricingTable\Settings\Settings; |
| 6 |
use TierPricingTable\TierPricingTablePlugin; |
| 7 |
use Premmerce\SDK\V2\FileManager\FileManager; |
| 8 |
use TierPricingTable\Admin\ProductManagers\SimpleProductManager; |
| 9 |
use TierPricingTable\Admin\ProductManagers\VariationProductManager; |
| 10 |
use TierPricingTable\Admin\Export\Woocommerce as WooCommerceExport; |
| 11 |
use TierPricingTable\Admin\Import\Woocommerce as WooCommerceImport; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Admin |
| 15 |
* |
| 16 |
* @package TierPricingTable\Admin |
| 17 |
*/ |
| 18 |
class Admin { |
| 19 |
|
| 20 |
/** |
| 21 |
* @var FileManager |
| 22 |
*/ |
| 23 |
private $fileManager; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Settings |
| 27 |
*/ |
| 28 |
private $settings; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
private $managers; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Freemius |
| 37 |
*/ |
| 38 |
private $licence; |
| 39 |
|
| 40 |
/** |
| 41 |
* Admin constructor. |
| 42 |
* |
| 43 |
* Register menu items and handlers |
| 44 |
* |
| 45 |
* @param FileManager $fileManager |
| 46 |
* @param Freemius $licence |
| 47 |
* @param Settings $settings |
| 48 |
*/ |
| 49 |
public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings ) { |
| 50 |
$this->fileManager = $fileManager; |
| 51 |
$this->settings = $settings; |
| 52 |
$this->licence = $licence; |
| 53 |
|
| 54 |
$this->initManagers(); |
| 55 |
|
| 56 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] ); |
| 57 |
|
| 58 |
if ( get_transient( 'tier_pricing_table_activated' ) ) { |
| 59 |
add_action( 'admin_notices', [ $this, 'showActivationMessage' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( $this->licence->isFree() ) { |
| 63 |
|
| 64 |
$template = 'upgrade-alert.php'; |
| 65 |
|
| 66 |
add_action( 'woocommerce_settings_' . Settings::SETTINGS_PAGE, function () use ( $template ) { |
| 67 |
$this->fileManager->includeTemplate( 'admin/alerts/' . $template, [ |
| 68 |
'upgradeUrl' => tpt_fs()->get_upgrade_url(), |
| 69 |
'contactUsUrl' => $this->licence->getContactUsPageUrl() |
| 70 |
] ); |
| 71 |
|
| 72 |
$this->fileManager->includeTemplate( 'admin/only-in-premium-script.php' ); |
| 73 |
} ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( tpt_fs()->is__premium_only() ) { |
| 77 |
add_action( 'woocommerce_settings_' . Settings::SETTINGS_PAGE, function () { |
| 78 |
$this->fileManager->includeTemplate( 'admin/alerts/premium-thanking-alert.php', [ |
| 79 |
'accountUrl' => $this->licence->getAccountPageUrl(), |
| 80 |
'contactUsUrl' => $this->licence->getContactUsPageUrl() |
| 81 |
] ); |
| 82 |
} ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Init Managers |
| 88 |
*/ |
| 89 |
public function initManagers() { |
| 90 |
|
| 91 |
$this->managers = [ |
| 92 |
ProductManager::class => new ProductManager( $this->fileManager, $this->settings, |
| 93 |
$this->licence ), |
| 94 |
VariationProductManager::class => new VariationProductManager( $this->fileManager, $this->settings, |
| 95 |
$this->licence ), |
| 96 |
WooCommerceExport::class => new WooCommerceExport(), |
| 97 |
WooCommerceImport::class => new WooCommerceImport() |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Show message about activation plugin and advise next step |
| 103 |
*/ |
| 104 |
public function showActivationMessage() { |
| 105 |
$link = $this->settings->getLink(); |
| 106 |
$this->fileManager->includeTemplate( 'admin/alerts/activation-alert.php', [ 'link' => $link ] ); |
| 107 |
|
| 108 |
delete_transient( 'tiered_pricing_table_activated' ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register assets on product create/update page |
| 113 |
* |
| 114 |
* @param $page |
| 115 |
*/ |
| 116 |
public function enqueueAssets( $page ) { |
| 117 |
global $post, $page; |
| 118 |
|
| 119 |
if ( ( isset( $_GET['tab'] ) && $_GET['tab'] == Settings::SETTINGS_PAGE ) || ( ( $page == 'post.php' || $page = 'post-new.php' ) && $post && $post->post_type == 'product' ) ) { |
| 120 |
wp_enqueue_script( 'tier-pricing-table-admin-js', $this->fileManager->locateAsset( 'admin/main.js' ), |
| 121 |
'jquery', TierPricingTablePlugin::VERSION ); |
| 122 |
wp_enqueue_style( 'tier-pricing-table-admin-css', $this->fileManager->locateAsset( 'admin/style.css' ), |
| 123 |
null, TierPricingTablePlugin::VERSION ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |