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