| 1 |
<?php namespace TierPricingTable\Addons\ReactProductEditorAddon; |
| 2 |
|
| 3 |
use Automattic\WooCommerce\Admin\BlockTemplates\BlockTemplateInterface; |
| 4 |
use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\ProductTemplates\ProductFormTemplateInterface; |
| 5 |
use TierPricingTable\Addons\ReactProductEditorAddon\Blocks\PremiumWrapper; |
| 6 |
use TierPricingTable\Addons\ReactProductEditorAddon\Blocks\RoleBasedPricing; |
| 7 |
use TierPricingTable\Addons\ReactProductEditorAddon\Blocks\UpgradeNotice; |
| 8 |
use TierPricingTable\Addons\ReactProductEditorAddon\Sections\AdvanceProductOptionSection; |
| 9 |
use TierPricingTable\Addons\ReactProductEditorAddon\Sections\MainSection; |
| 10 |
use TierPricingTable\Addons\ReactProductEditorAddon\Sections\RoleBasedPricingSection; |
| 11 |
|
| 12 |
class ProductEditor { |
| 13 |
|
| 14 |
/** |
| 15 |
* Blocks |
| 16 |
* |
| 17 |
* @var Block[] |
| 18 |
*/ |
| 19 |
protected $blocks; |
| 20 |
|
| 21 |
const GROUP_ID = 'tiered-pricing/group'; |
| 22 |
|
| 23 |
/** |
| 24 |
* Sections |
| 25 |
* |
| 26 |
* @var object |
| 27 |
*/ |
| 28 |
protected $sections; |
| 29 |
|
| 30 |
public function __construct() { |
| 31 |
|
| 32 |
$blocks = array( |
| 33 |
new Blocks\TieredPricing(), |
| 34 |
); |
| 35 |
|
| 36 |
if ( ! tpt_fs()->can_use_premium_code() ) { |
| 37 |
$blocks[] = new UpgradeNotice(); |
| 38 |
} |
| 39 |
|
| 40 |
$blocks[] = new Blocks\AdvanceProductOptions(); |
| 41 |
$blocks[] = new Blocks\MinQuantity(); |
| 42 |
$blocks[] = new RoleBasedPricing(); |
| 43 |
$blocks[] = new PremiumWrapper(); |
| 44 |
|
| 45 |
$this->blocks = apply_filters( 'tiered_pricing_table/product_editor/blocks', $blocks ); |
| 46 |
|
| 47 |
$this->sections = apply_filters( 'tiered_pricing_table/product_editor/sections', array( |
| 48 |
new MainSection(), |
| 49 |
new AdvanceProductOptionSection(), |
| 50 |
new RoleBasedPricingSection(), |
| 51 |
) ); |
| 52 |
|
| 53 |
new TieredPricingGroup(); |
| 54 |
|
| 55 |
add_action( 'init', array( $this, 'registerBlocks' ) ); |
| 56 |
add_filter( 'woocommerce_block_template_register', array( $this, 'addBlocks' ), 100 ); |
| 57 |
} |
| 58 |
|
| 59 |
public function addBlocks( BlockTemplateInterface $template ) { |
| 60 |
|
| 61 |
if ( $template instanceof ProductFormTemplateInterface ) { |
| 62 |
|
| 63 |
$group = $template->get_group_by_id( self::GROUP_ID ); |
| 64 |
|
| 65 |
if ( ! $group ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
foreach ( $this->blocks as $block ) { |
| 70 |
|
| 71 |
$section = $template->get_section_by_id( $block->getSectionId() ); |
| 72 |
|
| 73 |
if ( ! $section ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
$blockWrapper = $section; |
| 78 |
|
| 79 |
if ( $block->wrapToPremium() ) { |
| 80 |
$premiumWrapper = $section->add_block( array( |
| 81 |
'id' => 'tiered-pricing-table/premium-wrapper', |
| 82 |
'blockName' => 'tiered-pricing-table/premium-wrapper', |
| 83 |
'attributes' => array( |
| 84 |
'isPremium' => tpt_fs()->can_use_premium_code(), |
| 85 |
), |
| 86 |
) ); |
| 87 |
|
| 88 |
$blockWrapper = $premiumWrapper; |
| 89 |
} |
| 90 |
|
| 91 |
$blockWrapper->add_block( [ |
| 92 |
'id' => $block->getId(), |
| 93 |
'order' => $block->getOrder(), |
| 94 |
'blockName' => $block->getBlockName(), |
| 95 |
'attributes' => $block->getAttributes(), |
| 96 |
] ); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public function registerBlocks() { |
| 102 |
|
| 103 |
if ( ! class_exists( 'Automattic\WooCommerce\Admin\Features\ProductBlockEditor\BlockRegistry' ) ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if ( isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'] ) { |
| 108 |
foreach ( $this->blocks as $block ) { |
| 109 |
$block->register(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
|