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