PluginProbe
Tiered Pricing Table for WooCommerce / 6.5.0
Tiered Pricing Table for WooCommerce v6.5.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.5.0, at src/TierPricingTablePlugin.php

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