-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
175 lines (144 loc) · 6.34 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Plugin Name: Simple Loyalty Program
* Description: Loyalty settings for WooCommerce
* Version: 1.1
* Author: Soczó Kristóf
* Author URI: https://hellowp.io/en/
* Plugin URI: https://github.com/Lonsdale201/Simple-Loyalty-Program
* Text Domain: hw-woo-loyalty
* Domain Path: /languages
* Requires Plugins: woocommerce
* Requires at least: 6.0
* Tested up to: 6.7.1
* Requires PHP: 8.0
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace HelloWP\HWLoyalty;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'HWLoyalty_URL', plugin_dir_url( __FILE__ ) );
define( 'HWLoyalty_PATH', plugin_dir_path( __FILE__ ) );
require_once __DIR__ . '/vendor/autoload.php';
require dirname(__FILE__) . '/plugin-update-checker/plugin-update-checker.php';
use YahnisElsts\PluginUpdateChecker\v5p0\PucFactory;
use HelloWP\HWLoyalty\App\Helper\SettingsConfig;
use HelloWP\HWLoyalty\App\Helper\FreeShippingManager;
use HelloWP\HWLoyalty\App\Modules\FreeShipping;
/**
* The main class for the Loyalty Program settings plugin
*/
final class HWLoyalty {
const MINIMUM_WOOCOMMERCE_VERSION = '9.2.0';
const MINIMUM_WORDPRESS_VERSION = '6.0';
const MINIMUM_PHP_VERSION = '8.0';
private static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
$this->add_hooks();
}
private function add_hooks() {
add_action('init', [$this, 'load_plugin_textdomain'], -999);
add_action('plugins_loaded', [$this, 'on_plugins_loaded']);
add_action('update_option_hw_loyalty_enable_inactivity', [$this, 'handle_inactivity_option_update'], 10, 2);
add_action('before_woocommerce_init', [$this, 'declare_hpos_compatibility']);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), [$this, 'add_settings_link']);
}
public function load_plugin_textdomain() {
if ( version_compare( $GLOBALS['wp_version'], '6.7', '<' ) ) {
load_plugin_textdomain( 'hw-woo-loyalty', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
} else {
load_textdomain( 'hw-woo-loyalty', HWLoyalty_PATH . 'languages/hw-woo-loyalty-' . determine_locale() . '.mo' );
}
}
public function add_settings_link($links) {
$settings_link = '<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=hw_loyalty')) . '">' . __('Settings', 'hw-woo-loyalty') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
public function on_plugins_loaded() {
if ( ! $this->is_compatible() ) {
return;
}
\HelloWP\HWLoyalty\App\Admin\AdminSettings::get_instance();
\HelloWP\HWLoyalty\App\Modules\Manager::init();
\HelloWP\HWLoyalty\App\Helper\Hooks::init();
add_action('woocommerce_shipping_init', [$this, 'init_free_shipping_classes']);
$myUpdateChecker = PucFactory::buildUpdateChecker(
'https://plugin-uodater.alex.hellodevs.dev/plugins/hw-woo-loyalty.json',
__FILE__,
'hw-woo-loyalty'
);
}
public function init_free_shipping_classes() {
new FreeShippingManager();
new FreeShipping();
}
public function handle_inactivity_option_update($old_value, $value) {
$permanent_program = \HelloWP\HWLoyalty\App\Helper\SettingsConfig::get('hw_loyalty_permanent_program', 'no');
$enable_inactivity = \HelloWP\HWLoyalty\App\Helper\SettingsConfig::get('hw_loyalty_enable_inactivity', 'no');
if ($permanent_program === 'yes' && $enable_inactivity === 'yes') {
\HelloWP\HWLoyalty\App\Helper\CronHandler::schedule_cron_event();
} else {
\HelloWP\HWLoyalty\App\Helper\CronHandler::clear_cron_event();
}
}
public function declare_hpos_compatibility() {
if (!class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
return;
}
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
public static function deactivate_plugin() {
if (class_exists('HelloWP\HWLoyalty\App\Helper\CronHandler')) {
\HelloWP\HWLoyalty\App\Helper\CronHandler::clear_cron_event();
}
}
public function is_compatible() {
if ( ! class_exists( 'WooCommerce' ) || version_compare( WC_VERSION, self::MINIMUM_WOOCOMMERCE_VERSION, '<' ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_woocommerce_version' ] );
return false;
}
if ( version_compare( get_bloginfo( 'version' ), self::MINIMUM_WORDPRESS_VERSION, '<' ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_wordpress_version' ] );
return false;
}
if ( version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '<' ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_minimum_php_version' ] );
return false;
}
return true;
}
public function admin_notice_minimum_woocommerce_version() {
if ( ! current_user_can('manage_options') ) {
return;
}
echo '<div class="notice notice-warning is-dismissible"><p>';
echo sprintf(__('This plugin requires WooCommerce %s or later. Please update WooCommerce.', 'hw-woo-loyalty'), self::MINIMUM_WOOCOMMERCE_VERSION);
echo '</p></div>';
}
public function admin_notice_minimum_wordpress_version() {
if ( ! current_user_can('manage_options') ) {
return;
}
echo '<div class="notice notice-warning is-dismissible"><p>';
echo sprintf(__('This plugin requires WordPress %s or later. Please update WordPress.', 'hw-woo-loyalty'), self::MINIMUM_WORDPRESS_VERSION);
echo '</p></div>';
}
public function admin_notice_minimum_php_version() {
if ( ! current_user_can('manage_options') ) {
return;
}
echo '<div class="notice notice-warning is-dismissible"><p>';
echo sprintf(__('This plugin requires PHP %s or later. Please update your PHP version.', 'hw-woo-loyalty'), self::MINIMUM_PHP_VERSION);
echo '</p></div>';
}
}
HWLoyalty::instance();