| @@ -1,11 +1,16 @@ | ||
| 1 | -<?php namespace TierPricingTable\Admin; | |
| 1 | +<?php | |
| 2 | 2 | |
| 3 | -use Premmerce\SDK\V2\FileManager\FileManager; | |
| 4 | -use TierPricingTable\PriceManager; | |
| 5 | -use WP_Post; | |
| 6 | -use WC_Product; | |
| 3 | +namespace TierPricingTable\Admin; | |
| 7 | 4 | |
| 5 | +use TierPricingTable\Freemius ; | |
| 6 | +use TierPricingTable\Settings\Settings ; | |
| 7 | +use TierPricingTable\TierPricingTablePlugin ; | |
| 8 | +use Premmerce\SDK\V2\FileManager\FileManager ; | |
| 9 | +use TierPricingTable\Admin\ProductManagers\SimpleProductManager ; | |
| 10 | +use TierPricingTable\Admin\ProductManagers\VariationProductManager ; | |
| 11 | +use TierPricingTable\Admin\Export\Woocommerce as WooCommerceExport ; | |
| 12 | +use TierPricingTable\Admin\Import\Woocommerce as WooCommerceImport ; | |
| 8 | 13 | /** |
| 9 | 14 | * Class Admin |
| 10 | 15 | * |
| 11 | 16 | * @package TierPricingTable\Admin |
| @@ -14,133 +19,108 @@ | ||
| 14 | 19 | { |
| 15 | 20 | /** |
| 16 | 21 | * @var FileManager |
| 17 | 22 | */ |
| 18 | - private $fileManager; | |
| 19 | - | |
| 23 | + private $fileManager ; | |
| 20 | 24 | /** |
| 21 | 25 | * @var Settings |
| 22 | 26 | */ |
| 23 | - private $settings; | |
| 24 | - | |
| 27 | + private $settings ; | |
| 25 | 28 | /** |
| 29 | + * @var array | |
| 30 | + */ | |
| 31 | + private $managers ; | |
| 32 | + /** | |
| 33 | + * @var Freemius | |
| 34 | + */ | |
| 35 | + private $licence ; | |
| 36 | + /** | |
| 26 | 37 | * Admin constructor. |
| 27 | 38 | * |
| 28 | 39 | * Register menu items and handlers |
| 29 | 40 | * |
| 30 | 41 | * @param FileManager $fileManager |
| 42 | + * @param Freemius $licence | |
| 31 | 43 | * @param Settings $settings |
| 32 | 44 | */ |
| 33 | - public function __construct(FileManager $fileManager, Settings $settings) | |
| 45 | + public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings ) | |
| 34 | 46 | { |
| 35 | 47 | $this->fileManager = $fileManager; |
| 36 | 48 | $this->settings = $settings; |
| 37 | - | |
| 38 | - add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); | |
| 39 | - | |
| 40 | - add_action('woocommerce_product_options_pricing', [$this, 'renderPriceRulesSimple']); | |
| 41 | - add_action('woocommerce_variation_options_pricing', [$this, 'renderPriceRulesVariation'], 1, 3); | |
| 42 | - | |
| 43 | - add_action('woocommerce_save_product_variation', [$this, 'updateVariationPriceRules'], 1, 3); | |
| 44 | - add_action('save_post_product', [$this, 'updateSimplePriceRules']); | |
| 45 | - | |
| 46 | - if(get_transient('tier_pricing_table_activated')){ | |
| 47 | - add_action('admin_notices', [$this, 'showActivationMessage']); | |
| 49 | + $this->licence = $licence; | |
| 50 | + $this->initManagers(); | |
| 51 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] ); | |
| 52 | + if ( get_transient( 'tier_pricing_table_activated' ) ) { | |
| 53 | + add_action( 'admin_notices', [ $this, 'showActivationMessage' ] ); | |
| 48 | 54 | } |
| 55 | + | |
| 56 | + if ( $this->licence->isFree() ) { | |
| 57 | + | |
| 58 | + if ( tpt_fs()->is_anonymous() || !tpt_fs()->is_installed_on_site() ) { | |
| 59 | + $template = 'opt-in-alert.php'; | |
| 60 | + } else { | |
| 61 | + $template = 'upgrade-alert.php'; | |
| 62 | + } | |
| 63 | + | |
| 64 | + add_action( 'woocommerce_settings_' . Settings::SETTINGS_PAGE, function () use( $template ) { | |
| 65 | + $this->fileManager->includeTemplate( 'admin/alerts/' . $template, [ | |
| 66 | + 'upgradeUrl' => $this->licence->getUpgradeLink(), | |
| 67 | + 'contactUsUrl' => $this->licence->getContactUsPageUrl(), | |
| 68 | + ] ); | |
| 69 | + $this->fileManager->includeTemplate( 'admin/only-in-premium-script.php' ); | |
| 70 | + } ); | |
| 71 | + } | |
| 72 | + | |
| 49 | 73 | } |
| 50 | - | |
| 51 | - public function showActivationMessage(){ | |
| 52 | - $link = $this->settings->getLink(); | |
| 53 | - $this->fileManager->includeTemplate('admin/activation-message.php', ['link' => $link]); | |
| 54 | - | |
| 55 | - delete_transient('tier_pricing_table_activated'); | |
| 56 | - } | |
| 57 | - | |
| 74 | + | |
| 58 | 75 | /** |
| 59 | - * Update price quantity rules for variation product | |
| 60 | - * | |
| 61 | - * @param int $variation_id | |
| 62 | - * @param int $loop | |
| 76 | + * Init Managers | |
| 63 | 77 | */ |
| 64 | - public function updateVariationPriceRules($variation_id, $loop) | |
| 78 | + public function initManagers() | |
| 65 | 79 | { |
| 66 | - if(isset($_POST['_dynamic_price_amount'][$loop])){ | |
| 67 | - $amounts = $_POST['_dynamic_price_amount'][$loop]; | |
| 68 | - $prices = !empty($_POST['_dynamic_price_value'][$loop]) ? $_POST['_dynamic_price_value'][$loop] : []; | |
| 69 | - | |
| 70 | - $this->updatePriceRules($amounts, $prices, $variation_id); | |
| 71 | - } | |
| 80 | + $this->managers = [ | |
| 81 | + SimpleProductManager::class => new SimpleProductManager( $this->fileManager, $this->settings, $this->licence ), | |
| 82 | + VariationProductManager::class => new VariationProductManager( $this->fileManager, $this->settings, $this->licence ), | |
| 83 | + WooCommerceExport::class => new WooCommerceExport(), | |
| 84 | + WooCommerceImport::class => new WooCommerceImport(), | |
| 85 | + ]; | |
| 72 | 86 | } |
| 73 | - | |
| 87 | + | |
| 74 | 88 | /** |
| 75 | - * Update price quantity rules for simple product | |
| 76 | - * | |
| 77 | - * @param int $post_id | |
| 89 | + * Show message about activation plugin and advise next step | |
| 78 | 90 | */ |
| 79 | - public function updateSimplePriceRules($post_id) | |
| 91 | + public function showActivationMessage() | |
| 80 | 92 | { |
| 81 | - if(isset($_POST['_dynamic_price_amount'])){ | |
| 82 | - $amounts = $_POST['_dynamic_price_amount']; | |
| 83 | - $prices = !empty($_POST['_dynamic_price_value']) ? $_POST['_dynamic_price_value'] : []; | |
| 84 | - | |
| 85 | - $this->updatePriceRules($amounts, $prices, $post_id); | |
| 86 | - } | |
| 93 | + $link = $this->settings->getLink(); | |
| 94 | + $this->fileManager->includeTemplate( 'admin/alerts/activation-alert.php', [ | |
| 95 | + 'link' => $link, | |
| 96 | + ] ); | |
| 97 | + delete_transient( 'tiered_pricing_table_activated' ); | |
| 87 | 98 | } |
| 99 | + | |
| 88 | 100 | /** |
| 89 | 101 | * Register assets on product create/update page |
| 90 | 102 | * |
| 91 | 103 | * @param $page |
| 92 | 104 | */ |
| 93 | - public function enqueueAssets($page) | |
| 105 | + public function enqueueAssets( $page ) | |
| 94 | 106 | { |
| 95 | - global $post; | |
| 96 | - | |
| 97 | - if(($page == 'post.php' || $page = 'post_new.php') && $post && $post->post_type == 'product'){ | |
| 98 | - wp_enqueue_script('tier-pricing-table-admin-js', $this->fileManager->locateAsset('admin/main.js'), 'jquery'); | |
| 99 | - wp_enqueue_style('tier-pricing-table-admin-css', $this->fileManager->locateAsset('admin/style.css')); | |
| 107 | + global $post, $page ; | |
| 108 | + | |
| 109 | + if ( isset( $_GET['tab'] ) && $_GET['tab'] == Settings::SETTINGS_PAGE || ($page == 'post.php' || ($page = 'post_new.php')) && $post && $post->post_type == 'product' ) { | |
| 110 | + wp_enqueue_script( | |
| 111 | + 'tier-pricing-table-admin-js', | |
| 112 | + $this->fileManager->locateAsset( 'admin/main.js' ), | |
| 113 | + 'jquery', | |
| 114 | + TierPricingTablePlugin::VERSION | |
| 115 | + ); | |
| 116 | + wp_enqueue_style( | |
| 117 | + 'tier-pricing-table-admin-css', | |
| 118 | + $this->fileManager->locateAsset( 'admin/style.css' ), | |
| 119 | + null, | |
| 120 | + TierPricingTablePlugin::VERSION | |
| 121 | + ); | |
| 100 | 122 | } |
| 123 | + | |
| 101 | 124 | } |
| 102 | 125 | |
| 103 | - /** | |
| 104 | - * Update price rules | |
| 105 | - * | |
| 106 | - * @param array $amounts | |
| 107 | - * @param array $prices | |
| 108 | - * @param int $post_id | |
| 109 | - */ | |
| 110 | - protected function updatePriceRules($amounts, $prices, $post_id) | |
| 111 | - { | |
| 112 | - $rules = []; | |
| 113 | - | |
| 114 | - foreach ($amounts as $key => $amount){ | |
| 115 | - if(!empty($amount) && !empty($prices[$key]) && !key_exists($amount, $rules)){ | |
| 116 | - $rules[$amount] = wc_format_decimal($prices[$key]); | |
| 117 | - } | |
| 118 | - } | |
| 119 | - | |
| 120 | - update_post_meta($post_id, '_price_rules', $rules); | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * Render inputs for price rules on simple product | |
| 125 | - * | |
| 126 | - * @global WC_Product $product_object | |
| 127 | - */ | |
| 128 | - public function renderPriceRulesSimple() | |
| 129 | - { | |
| 130 | - global $product_object; | |
| 131 | - | |
| 132 | - $this->fileManager->includeTemplate('admin/add-price-rule-simple.php', ['price_rules' => PriceManager::getPriceRules($product_object->get_id())]); | |
| 133 | - } | |
| 134 | - | |
| 135 | - /** | |
| 136 | - * Render inputs for price rules on variation | |
| 137 | - * | |
| 138 | - * @param int $loop | |
| 139 | - * @param array $variation_data | |
| 140 | - * @param WP_Post $variation | |
| 141 | - */ | |
| 142 | - public function renderPriceRulesVariation($loop, $variation_data, $variation) | |
| 143 | - { | |
| 144 | - $this->fileManager->includeTemplate('admin/add-price-rule-variation.php', ['price_rules' => PriceManager::getPriceRules($variation->ID), 'i' => $loop, 'variation_data' => $variation_data]); | |
| 145 | - } | |
| 146 | 126 | } |