PluginProbe
Tiered Pricing Table for WooCommerce / 6.4.0
Tiered Pricing Table for WooCommerce v6.4.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / TierPricingTablePlugin.php

TierPricingTablePlugin.php in Tiered Pricing Table for WooCommerce 6.4.0, at src/TierPricingTablePlugin.php

273 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable;
4
5 use Automattic\WooCommerce\Utilities\FeaturesUtil;
6 use TierPricingTable\Addons\Addons;
7 use TierPricingTable\Admin\Admin;
8 use TierPricingTable\Admin\Notifications\Notifications;
9 use TierPricingTable\Blocks\GutenbergBlocks;
10 use TierPricingTable\Core\AdminNotifier;
11 use TierPricingTable\Core\Cache;
12 use TierPricingTable\Core\FileManager;
13 use TierPricingTable\Core\ServiceContainerTrait;
14 use TierPricingTable\Frontend\Frontend;
15 use TierPricingTable\Integrations\Integrations;
16 use TierPricingTable\Services\API\WooCommerceRestAPI;
17 use TierPricingTable\Services\DebugService;
18 use TierPricingTable\Services\ImportExport\WoocommerceExportService;
19 use TierPricingTable\Services\ImportExport\WoocommerceImportService;
20 use TierPricingTable\Services\ProductPageService;
21 use TierPricingTable\Services\RegularPricingService;
22 use TierPricingTable\Services\SystemStatusReportService;
23 use TierPricingTable\Settings\Settings;
24 use WC_Product;
25 use WP_User;
26 /**
27 * Class TierPricingTablePlugin
28 *
29 * @package TierPricingTable
30 */
31 class TierPricingTablePlugin {
32 use ServiceContainerTrait;
33 /**
34 * License instance
35 *
36 * @var Freemius
37 */
38 private $licence;
39
40 const VERSION = '6.4.0';
41
42 /**
43 * TierPricingTablePlugin constructor.
44 *
45 * @param string $mainFile
46 */
47 public function __construct( string $mainFile ) {
48 $this->licence = new Freemius($mainFile);
49 // Core
50 $this->getContainer()->add( 'fileManager', new FileManager($mainFile, 'tiered-pricing-table') );
51 $this->getContainer()->add( 'adminNotifier', new AdminNotifier() );
52 $this->saveActivationTime();
53 $this->declareCompatibilities();
54 }
55
56 public function declareCompatibilities() {
57 add_action( 'before_woocommerce_init', function () {
58 if ( class_exists( FeaturesUtil::class ) ) {
59 $mainFile = $this->getContainer()->getFileManager()->getMainFile();
60 FeaturesUtil::declare_compatibility( 'custom_order_tables', $mainFile );
61 FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', $mainFile );
62 FeaturesUtil::declare_compatibility( 'product_block_editor', $mainFile );
63 }
64 } );
65 }
66
67 public function initializationHooks() {
68 add_action( 'init', array($this, 'loadTextDomain'), -999 );
69 add_filter(
70 'plugin_row_meta',
71 array($this, 'addPluginsMeta'),
72 10,
73 2
74 );
75 add_filter(
76 'plugin_action_links_' . plugin_basename( $this->getContainer()->getFileManager()->getMainFile() ),
77 array($this, 'addPluginActions'),
78 10,
79 4
80 );
81 }
82
83 public function checkRequirements() : bool {
84 if ( !function_exists( 'is_plugin_active' ) ) {
85 include_once ABSPATH . 'wp-admin/includes/plugin.php';
86 }
87 // Check if WooCommerce is active
88 if ( !(is_plugin_active( 'woocommerce/woocommerce.php' ) || is_plugin_active_for_network( 'woocommerce/woocommerce.php' )) ) {
89 /* translators: %s: required plugin */
90 $message = sprintf( __( '<b>Tiered Pricing Table</b> plugin requires %s to be installed and activated.', 'tier-pricing-table' ), '<a target="_blank" href="https://wordpress.org/plugins/woocommerce/">WooCommerce</a>' );
91 $this->getContainer()->getAdminNotifier()->push( $message, AdminNotifier::ERROR );
92 return false;
93 }
94 // Check license
95 if ( !$this->licence->isValid() ) {
96 return false;
97 }
98 return true;
99 }
100
101 /**
102 * Entry point when every requirement is passed
103 */
104 public function run() {
105 $this->getContainer()->add( 'settings', new Settings() );
106 $this->getContainer()->add( 'cache', new Cache() );
107 $this->initializationHooks();
108 new Frontend();
109 new Admin();
110 new Addons();
111 new Integrations();
112 $this->getContainer()->initService( Notifications::class );
113 // Init Services
114 add_action( 'init', function () {
115 $this->getContainer()->initService( DebugService::class );
116 $this->getContainer()->initService( SystemStatusReportService::class );
117 $this->getContainer()->initService( RegularPricingService::class );
118 $this->getContainer()->initService( ProductPageService::class );
119 $this->getContainer()->initService( WooCommerceRestAPI::class );
120 $this->getContainer()->initService( WoocommerceImportService::class );
121 $this->getContainer()->initService( WoocommerceExportService::class );
122 $this->getContainer()->initService( GutenbergBlocks::class );
123 } );
124 }
125
126 /**
127 * Add setting to plugin actions at plugins list
128 *
129 * @param array $actions
130 *
131 * @return array
132 */
133 public function addPluginActions( array $actions ) : array {
134 $actions[] = '<a href="' . $this->getContainer()->getSettings()->getLink() . '">' . __( 'Settings', 'tier-pricing-table' ) . '</a>';
135 if ( !tpt_fs()->can_use_premium_code() ) {
136 $actions[] = '<a href="' . tpt_fs_activation_url() . '"><b style="color: red">' . __( 'Go Premium', 'tier-pricing-table' ) . '</b></a>';
137 }
138 return $actions;
139 }
140
141 public function addPluginsMeta( $links, $file ) {
142 if ( strpos( $file, 'tier-pricing-table' ) === false ) {
143 return $links;
144 }
145 $links['docs'] = '<a target="_blank" href="' . self::getDocumentationURL() . '">' . __( 'Documentation', 'tier-pricing-table' ) . '</a>';
146 $links['contact-us'] = '<a href="' . self::getContactUsURL() . '"><b style="color: green">' . __( 'Contact Us', 'tier-pricing-table' ) . '</b></a>';
147 if ( !tpt_fs()->is_anonymous() && tpt_fs()->is_installed_on_site() ) {
148 $links['account'] = '<a href="' . self::getAccountPageURL() . '"><b>' . __( 'My Account', 'tier-pricing-table' ) . '</b></a>';
149 }
150 return $links;
151 }
152
153 /**
154 * Load plugin translations
155 */
156 public function loadTextDomain() {
157 $name = $this->getContainer()->getFileManager()->getPluginName();
158 load_plugin_textdomain( 'tier-pricing-table', false, $name . '/languages/' );
159 }
160
161 /**
162 * Fired after plugin uninstallation.
163 * This function is used to remove the activation timestamp option.
164 */
165 public static function uninstall() {
166 delete_option( 'tpt_plugin_activation_timestamp' );
167 }
168
169 public static function getSupportedSimpleProductTypes() : array {
170 return (array) apply_filters( 'tiered_pricing_table/supported_simple_product_types', array(
171 'simple',
172 'variation',
173 'subscription',
174 'subscription-variation'
175 ) );
176 }
177
178 public static function getSupportedVariableProductTypes() : array {
179 return (array) apply_filters( 'tiered_pricing_table/supported_variable_product_types', array('variable', 'variable-subscription') );
180 }
181
182 public static function getSupportedVariationProductTypes() : array {
183 return (array) apply_filters( 'tiered_pricing_table/supported_variation_product_types', array('variation', 'subscription-variation') );
184 }
185
186 public static function isSimpleProductSupported( WC_Product $product ) : bool {
187 return in_array( $product->get_type(), self::getSupportedSimpleProductTypes() );
188 }
189
190 public static function isVariableProductSupported( WC_Product $product ) : bool {
191 return in_array( $product->get_type(), self::getSupportedVariableProductTypes() );
192 }
193
194 public static function isVariationProductSupported( WC_Product $product ) : bool {
195 return in_array( $product->get_type(), self::getSupportedVariationProductTypes() );
196 }
197
198 /**
199 * Plugin activation
200 */
201 public function activate() {
202 }
203
204 public static function getDocumentationURL() : string {
205 return 'https://tiered-pricing.com/documentation/user/';
206 }
207
208 public static function getContactUsURL() : string {
209 return admin_url( 'admin.php?page=tiered-pricing-table-contact-us' );
210 }
211
212 public static function getAccountPageURL() : string {
213 return admin_url( 'admin.php?page=tiered-pricing-table-account' );
214 }
215
216 /**
217 * Uses for separate rules during the import
218 *
219 * @return string
220 */
221 public static function getRulesSeparator() : string {
222 return apply_filters( 'tiered_pricing_table/rules_separator', ',' );
223 }
224
225 /**
226 * Get current user the prices calculate for. Sometimes current user can be different from the logged-in user.
227 * For example, when admin creates an order for customer in administration panel
228 *
229 * @return WP_User
230 */
231 public static function getCurrentUser() : WP_User {
232 if ( !empty( $GLOBALS['tpt_current_user_id'] ) ) {
233 $user = new WP_User(intval( $GLOBALS['tpt_current_user_id'] ));
234 } else {
235 $user = wp_get_current_user();
236 }
237 return apply_filters( 'tiered_pricing_table/current_user', $user );
238 }
239
240 public static function getAvailablePricingLayouts() {
241 return apply_filters( 'tiered_pricing_table/pricing_layouts', array(
242 'table' => __( 'Table', 'tier-pricing-table' ),
243 'blocks' => __( 'Blocks', 'tier-pricing-table' ),
244 'options' => __( 'Options', 'tier-pricing-table' ),
245 'dropdown' => __( 'Dropdown', 'tier-pricing-table' ),
246 'horizontal-table' => __( 'Horizontal table', 'tier-pricing-table' ),
247 'plain-text' => __( 'Plain text', 'tier-pricing-table' ),
248 'tooltip' => __( 'Tooltip', 'tier-pricing-table' ),
249 ) );
250 }
251
252 /**
253 * Get current user roles
254 *
255 * @return array
256 */
257 public static function getCurrentUserRoles() : array {
258 $user = self::getCurrentUser();
259 return apply_filters( 'tiered_pricing_table/current_user_roles', $user->roles, get_current_user_id() );
260 }
261
262 public function saveActivationTime() {
263 if ( !get_option( 'tpt_plugin_activation_timestamp', false ) ) {
264 update_option( 'tpt_plugin_activation_timestamp', time() );
265 }
266 }
267
268 public static function getPluginActivationDate() : ?int {
269 return intval( get_option( 'tpt_plugin_activation_timestamp', 0 ) );
270 }
271
272 }
273