Skip to content

Commit

Permalink
add pro min version
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jul 2, 2023
1 parent 70e9d28 commit a02a1c9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 70 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"firebase/php-jwt": "v6.4.0",
"ramsey/uuid": "^4.2.3",
"salesforce/handlebars-php": "^3.0.1",
"vlucas/phpdotenv": "^v5.5.0"
"vlucas/phpdotenv": "^v5.5.0",
"yahnis-elsts/plugin-update-checker": "^5.1"
},
"config": {
"platform": {
Expand Down
99 changes: 70 additions & 29 deletions includes/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@

namespace WCPOS\WooCommercePOS;

use function add_action;
use function add_role;
use function admin_url;
use function class_exists;
use function deactivate_plugins;
use function defined;
use function did_action;
use function function_exists;
use function is_admin;
use function is_multisite;
use function register_activation_hook;
use function restore_current_blog;
use function sprintf;
use function switch_to_blog;
use function version_compare;
use const DOING_AJAX;

class Activator {
// minimum requirements
public const WC_MIN_VERSION = '5.3';
public const PHP_MIN_VERSION = '7.0';


public function __construct() {
register_activation_hook( PLUGIN_FILE, array( $this, 'activate' ) );
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
Expand All @@ -31,12 +41,13 @@ public function init(): void {
// Check for min requirements to run
if ( $this->php_check() && $this->woocommerce_check() ) {
// check permalinks
if ( is_admin() && ( ! \defined( '\DOING_AJAX' ) || ! DOING_AJAX ) ) {
if ( is_admin() && ( ! defined( '\DOING_AJAX' ) || ! DOING_AJAX ) ) {
$this->permalink_check();
}

// Init update script if required
$this->version_check();
$this->pro_version_check();

// resolve plugin plugins
$this->plugin_check();
Expand All @@ -51,7 +62,7 @@ public function init(): void {
* @param $network_wide
*/
public function activate( $network_wide ): void {
if ( \function_exists( 'is_multisite' ) && is_multisite() ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids
$blog_ids = $this->get_blog_ids();
Expand Down Expand Up @@ -79,8 +90,8 @@ public function single_activate(): void {

// add pos capabilities to non POS roles
$this->add_pos_capability(array(
'administrator' => array( 'manage_woocommerce_pos', 'access_woocommerce_pos' ),
'shop_manager' => array( 'manage_woocommerce_pos', 'access_woocommerce_pos' ),
'administrator' => array( 'manage_woocommerce_pos', 'access_woocommerce_pos' ),
'shop_manager' => array( 'manage_woocommerce_pos', 'access_woocommerce_pos' ),
));

// set the auto redirection on next page load
Expand All @@ -107,14 +118,14 @@ public function activate_new_site( $blog_id ): void {
*/
private function php_check() {
$php_version = PHP_VERSION;
if ( version_compare( $php_version, self::PHP_MIN_VERSION, '>' ) ) {
if ( version_compare( $php_version, PHP_MIN_VERSION, '>' ) ) {
return true;
}

$message = sprintf(
__( '<strong>WooCommerce POS</strong> requires PHP %1$s or higher. Read more information about <a href="%2$s">how you can update</a>', 'woocommerce-pos' ),
self::PHP_MIN_VERSION,
'http://www.wpupdatephp.com/update/'
__( '<strong>WooCommerce POS</strong> requires PHP %1$s or higher. Read more information about <a href="%2$s">how you can update</a>', 'woocommerce-pos' ),
PHP_MIN_VERSION,
'http://www.wpupdatephp.com/update/'
) . ' &raquo;';

Admin\Notices::add( $message );
Expand All @@ -124,15 +135,15 @@ private function php_check() {
* Check min version of WooCommerce installed.
*/
private function woocommerce_check() {
if ( class_exists( '\WooCommerce' ) && version_compare( WC()->version, self::WC_MIN_VERSION, '>=' ) ) {
if ( class_exists( '\WooCommerce' ) && version_compare( WC()->version, WC_MIN_VERSION, '>=' ) ) {
return true;
}

$message = sprintf(
__( '<strong>WooCommerce POS</strong> requires <a href="%1$s">WooCommerce %2$s or higher</a>. Please <a href="%3$s">install and activate WooCommerce</a>', 'woocommerce-pos' ),
'http://wordpress.org/plugins/woocommerce/',
self::WC_MIN_VERSION,
admin_url( 'plugins.php' )
__( '<strong>WooCommerce POS</strong> requires <a href="%1$s">WooCommerce %2$s or higher</a>. Please <a href="%3$s">install and activate WooCommerce</a>', 'woocommerce-pos' ),
'http://wordpress.org/plugins/woocommerce/',
WC_MIN_VERSION,
admin_url( 'plugins.php' )
) . ' &raquo;';

Admin\Notices::add( $message );
Expand All @@ -159,9 +170,9 @@ private function permalink_check(): void {
* Check version number, runs every admin page load.
*/
private function version_check(): void {
$old = Admin\Settings::get_db_version();
$old = Services\Settings::get_db_version();
if ( version_compare( $old, VERSION, '<' ) ) {
Admin\Settings::bump_versions();
Services\Settings::bump_versions();
$this->db_upgrade( $old, VERSION );
}
}
Expand All @@ -173,9 +184,9 @@ private function version_check(): void {
*/
private function plugin_check(): void {
// disable NextGEN Gallery resource manager
// if ( ! \defined( 'NGG_DISABLE_RESOURCE_MANAGER' ) ) {
// \define( 'NGG_DISABLE_RESOURCE_MANAGER', true );
// }
// if ( ! \defined( 'NGG_DISABLE_RESOURCE_MANAGER' ) ) {
// \define( 'NGG_DISABLE_RESOURCE_MANAGER', true );
// }
}

/**
Expand Down Expand Up @@ -207,17 +218,17 @@ private function create_pos_roles(): void {
'publish_shop_orders' => true,
'list_users' => true,
'read_private_shop_coupons' => true,
'manage_product_terms' => true,
'manage_product_terms' => true,
);

add_role(
'cashier',
__( 'Cashier', 'woocommerce-pos' ),
$cashier_capabilities
'cashier',
__( 'Cashier', 'woocommerce-pos' ),
$cashier_capabilities
);

$this->add_pos_capability(array(
'cashier' => array( 'access_woocommerce_pos' ),
'cashier' => array( 'access_woocommerce_pos' ),
));
}

Expand Down Expand Up @@ -252,9 +263,39 @@ private function db_upgrade( $old, $current ): void {
);
foreach ( $db_updates as $version => $updater ) {
if ( version_compare( $version, $old, '>' ) &&
version_compare( $version, $current, '<=' ) ) {
version_compare( $version, $current, '<=' ) ) {
include $updater;
}
}
}

/**
* If \WCPOS\WooCommercePOSPro\ is installed, check the version is above MIN_PRO_VERSION
*/
private function pro_version_check() {
if ( class_exists( '\WCPOS\WooCommercePOSPro\Activator' ) ) {
if ( version_compare( \WCPOS\WooCommercePOSPro\VERSION, MIN_PRO_VERSION, '<' ) ) {
/**
* NOTE: the deactivate_plugins function is not available in the frontend or ajax
* This is an extreme situation where the Pro plugin could crash the site, so we need to deactivate it
*/
if ( ! function_exists( 'deactivate_plugins' ) ) {
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
}

// WooCommerce POS Pro is activated, but the version is too low
deactivate_plugins( 'woocommerce-pos-pro/woocommerce-pos-pro.php' );

$message = sprintf(
__( '<strong>WooCommerce POS</strong> requires <a href="%1$s">WooCommerce POS Pro %2$s or higher</a>. Please <a href="%3$s">install and activate WooCommerce POS Pro</a>', 'woocommerce-pos' ),
'https://wcpos.com/my-account',
MIN_PRO_VERSION,
admin_url( 'plugins.php' )
) . ' &raquo;';

Admin\Notices::add( $message );
}
}
}

}
40 changes: 0 additions & 40 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,44 +112,4 @@ private function inline_script(): string {
order_statuses: %s
}', json_encode( $barcodes ), json_encode( $order_statuses ) );
}

/**
* Delete settings in WP options table
*
* @param $id
* @return bool
*/
public static function delete_settings( $id ): bool {
return delete_option( 'woocommerce_pos_' . $id );
}

/**
* Delete all settings in WP options table
*/
public static function delete_all_settings() {
global $wpdb;
$wpdb->query(
$wpdb->prepare( "
DELETE FROM {$wpdb->options}
WHERE option_name
LIKE '%s'",
'woocommerce_pos_%'
)
);
}

/**
* @return int
*/
public static function get_db_version() {
return get_option( 'woocommerce_pos_db_version', 0 );
}

/**
* updates db to new version number
* bumps the idb version number
*/
public static function bump_versions() {
add_option( 'woocommerce_pos_db_version', VERSION, '', 'no' );
}
}
41 changes: 41 additions & 0 deletions includes/Services/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WC_Payment_Gateways;
use WP_Error;
use WP_REST_Request;
use const WCPOS\WooCommercePOS\VERSION;

class Settings {

Expand Down Expand Up @@ -316,4 +317,44 @@ public function get_payment_gateways_settings() {
*/
return apply_filters( 'woocommerce_pos_payment_gateways_settings', $response );
}

/**
* Delete settings in WP options table
*
* @param $id
* @return bool
*/
public static function delete_settings( $id ): bool {
return delete_option( 'woocommerce_pos_' . $id );
}

/**
* Delete all settings in WP options table
*/
public static function delete_all_settings() {
global $wpdb;
$wpdb->query(
$wpdb->prepare( "
DELETE FROM {$wpdb->options}
WHERE option_name
LIKE '%s'",
'woocommerce_pos_%'
)
);
}

/**
* @return string
*/
public static function get_db_version(): string {
return get_option( 'woocommerce_pos_db_version', '0' );
}

/**
* updates db to new version number
* bumps the idb version number
*/
public static function bump_versions() {
add_option( 'woocommerce_pos_db_version', VERSION, '', 'no' );
}
}
5 changes: 5 additions & 0 deletions woocommerce-pos.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
define( __NAMESPACE__ . '\PLUGIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
define( __NAMESPACE__ . '\PLUGIN_URL', trailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );

// minimum requirements
const WC_MIN_VERSION = '5.3';
const PHP_MIN_VERSION = '7.2';
const MIN_PRO_VERSION = '1.2.0';

// Autoloader
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
Expand Down

0 comments on commit a02a1c9

Please sign in to comment.