| @@ -1,145 +1,108 @@ | ||
| 1 | -<?php namespace TierPricingTable\Admin; | |
| 2 | - | |
| 3 | -use Premmerce\SDK\V2\FileManager\FileManager; | |
| 4 | -use TierPricingTable\PriceManager; | |
| 5 | -use WP_Post; | |
| 6 | -use WC_Product; | |
| 7 | - | |
| 8 | -/** | |
| 9 | - * Class Admin | |
| 10 | - * | |
| 11 | - * @package TierPricingTable\Admin | |
| 12 | - */ | |
| 13 | -class Admin | |
| 14 | -{ | |
| 15 | - /** | |
| 16 | - * @var FileManager | |
| 17 | - */ | |
| 18 | - private $fileManager; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * @var Settings | |
| 22 | - */ | |
| 23 | - private $settings; | |
| 24 | - | |
| 25 | - /** | |
| 26 | - * Admin constructor. | |
| 27 | - * | |
| 28 | - * Register menu items and handlers | |
| 29 | - * | |
| 30 | - * @param FileManager $fileManager | |
| 31 | - * @param Settings $settings | |
| 32 | - */ | |
| 33 | - public function __construct(FileManager $fileManager, Settings $settings) | |
| 34 | - { | |
| 35 | - $this->fileManager = $fileManager; | |
| 36 | - $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']); | |
| 48 | - } | |
| 49 | - } | |
| 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 | - | |
| 58 | - /** | |
| 59 | - * Update price quantity rules for variation product | |
| 60 | - * | |
| 61 | - * @param int $variation_id | |
| 62 | - * @param int $loop | |
| 63 | - */ | |
| 64 | - public function updateVariationPriceRules($variation_id, $loop) | |
| 65 | - { | |
| 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 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Update price quantity rules for simple product | |
| 76 | - * | |
| 77 | - * @param int $post_id | |
| 78 | - */ | |
| 79 | - public function updateSimplePriceRules($post_id) | |
| 80 | - { | |
| 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 | - } | |
| 87 | - } | |
| 88 | - /** | |
| 89 | - * Register assets on product create/update page | |
| 90 | - * | |
| 91 | - * @param $page | |
| 92 | - */ | |
| 93 | - public function enqueueAssets($page) | |
| 94 | - { | |
| 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')); | |
| 100 | - } | |
| 101 | - } | |
| 102 | - | |
| 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] = $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 | - * @global WC_Product $product_object | |
| 126 | - */ | |
| 127 | - public function renderPriceRulesSimple() | |
| 128 | - { | |
| 129 | - global $product_object; | |
| 130 | - | |
| 131 | - $this->fileManager->includeTemplate('admin/add-price-rule-simple.php', ['price_rules' => PriceManager::getPriceRules($product_object->get_id())]); | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Render inputs for price rules on variation | |
| 136 | - * | |
| 137 | - * @param int $loop | |
| 138 | - * @param array $variation_data | |
| 139 | - * @param WP_Post $variation | |
| 140 | - */ | |
| 141 | - public function renderPriceRulesVariation($loop, $variation_data, $variation) | |
| 142 | - { | |
| 143 | - $this->fileManager->includeTemplate('admin/add-price-rule-variation.php', ['price_rules' => PriceManager::getPriceRules($variation->ID), 'i' => $loop, 'variation_data' => $variation_data]); | |
| 144 | - } | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace TierPricingTable\Admin; | |
| 4 | + | |
| 5 | +use Premmerce\SDK\V2\FileManager\FileManager ; | |
| 6 | +use TierPricingTable\Admin\ProductManagers\SimpleProductManager ; | |
| 7 | +use TierPricingTable\Admin\ProductManagers\VariationProductManager ; | |
| 8 | +use TierPricingTable\Freemius ; | |
| 9 | +use TierPricingTable\Settings\Settings ; | |
| 10 | +use TierPricingTable\TierPricingTablePlugin ; | |
| 11 | +/** | |
| 12 | + * Class Admin | |
| 13 | + * | |
| 14 | + * @package TierPricingTable\Admin | |
| 15 | + */ | |
| 16 | +class Admin | |
| 17 | +{ | |
| 18 | + /** | |
| 19 | + * @var FileManager | |
| 20 | + */ | |
| 21 | + private $fileManager ; | |
| 22 | + /** | |
| 23 | + * @var Settings | |
| 24 | + */ | |
| 25 | + private $settings ; | |
| 26 | + /** | |
| 27 | + * @var array | |
| 28 | + */ | |
| 29 | + private $managers ; | |
| 30 | + /** | |
| 31 | + * @var Freemius | |
| 32 | + */ | |
| 33 | + private $licence ; | |
| 34 | + /** | |
| 35 | + * Admin constructor. | |
| 36 | + * | |
| 37 | + * Register menu items and handlers | |
| 38 | + * | |
| 39 | + * @param FileManager $fileManager | |
| 40 | + * @param Freemius $licence | |
| 41 | + * @param Settings $settings | |
| 42 | + */ | |
| 43 | + public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings ) | |
| 44 | + { | |
| 45 | + $this->fileManager = $fileManager; | |
| 46 | + $this->settings = $settings; | |
| 47 | + $this->licence = $licence; | |
| 48 | + $this->initManagers(); | |
| 49 | + add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAssets' ] ); | |
| 50 | + if ( get_transient( 'tier_pricing_table_activated' ) ) { | |
| 51 | + add_action( 'admin_notices', [ $this, 'showActivationMessage' ] ); | |
| 52 | + } | |
| 53 | + if ( $this->licence->isFree() ) { | |
| 54 | + add_action( 'woocommerce_settings_' . Settings::SETTINGS_PAGE, function () { | |
| 55 | + $this->fileManager->includeTemplate( 'admin/upgrade-message.php', [ | |
| 56 | + 'upgradeLink' => $this->licence->getUpgradeLink(), | |
| 57 | + ] ); | |
| 58 | + } ); | |
| 59 | + } | |
| 60 | + } | |
| 61 | + | |
| 62 | + public function initManagers() | |
| 63 | + { | |
| 64 | + $this->managers = [ | |
| 65 | + SimpleProductManager::class => new SimpleProductManager( $this->fileManager, $this->settings, $this->licence ), | |
| 66 | + VariationProductManager::class => new VariationProductManager( $this->fileManager, $this->settings, $this->licence ), | |
| 67 | + ]; | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Show message about activation plugin and advise next step | |
| 72 | + */ | |
| 73 | + public function showActivationMessage() | |
| 74 | + { | |
| 75 | + $link = $this->settings->getLink(); | |
| 76 | + $this->fileManager->includeTemplate( 'admin/activation-message.php', [ | |
| 77 | + 'link' => $link, | |
| 78 | + ] ); | |
| 79 | + delete_transient( 'tiered_pricing_table_activated' ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Register assets on product create/update page | |
| 84 | + * | |
| 85 | + * @param $page | |
| 86 | + */ | |
| 87 | + public function enqueueAssets( $page ) | |
| 88 | + { | |
| 89 | + global $post ; | |
| 90 | + | |
| 91 | + if ( ($page == 'post.php' || ($page = 'post_new.php')) && $post && $post->post_type == 'product' ) { | |
| 92 | + wp_enqueue_script( | |
| 93 | + 'tier-pricing-table-admin-js', | |
| 94 | + $this->fileManager->locateAsset( 'admin/main.js' ), | |
| 95 | + 'jquery', | |
| 96 | + TierPricingTablePlugin::VERSION | |
| 97 | + ); | |
| 98 | + wp_enqueue_style( | |
| 99 | + 'tier-pricing-table-admin-css', | |
| 100 | + $this->fileManager->locateAsset( 'admin/style.css' ), | |
| 101 | + null, | |
| 102 | + TierPricingTablePlugin::VERSION | |
| 103 | + ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + } | |
| 107 | + | |
| 145 | 108 | } |