-
Notifications
You must be signed in to change notification settings - Fork 140
/
facebook-for-woocommerce.php
326 lines (258 loc) · 7.19 KB
/
facebook-for-woocommerce.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
// phpcs:ignoreFile
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* Plugin Name: Facebook for WooCommerce
* Plugin URI: https://github.com/woocommerce/facebook-for-woocommerce/
* Description: Grow your business on Facebook! Use this official plugin to help sell more of your products using Facebook. After completing the setup, you'll be ready to create ads that promote your products and you can also create a shop section on your Page where customers can browse your products on Facebook.
* Author: Facebook
* Author URI: https://www.facebook.com/
* Version: 3.3.0
* Requires at least: 5.6
* Requires PHP: 7.4
* Text Domain: facebook-for-woocommerce
* Requires Plugins: woocommerce
* Tested up to: 6.7
* WC requires at least: 6.4
* WC tested up to: 9.4
*
* @package FacebookCommerce
*/
require_once __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Grow\Tools\CompatChecker\v0_0_1\Checker;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
defined( 'ABSPATH' ) || exit;
// HPOS compatibility declaration.
add_action(
'before_woocommerce_init',
function() {
if ( class_exists( FeaturesUtil::class ) ) {
FeaturesUtil::declare_compatibility( 'custom_order_tables', plugin_basename( __FILE__ ), true );
}
}
);
/**
* The plugin loader class.
*
* @since 1.10.0
*/
class WC_Facebook_Loader {
/**
* @var string the plugin version. This must be in the main plugin file to be automatically bumped by Woorelease.
*/
const PLUGIN_VERSION = '3.3.0'; // WRCS: DEFINED_VERSION.
// Minimum PHP version required by this plugin.
const MINIMUM_PHP_VERSION = '7.4.0';
// Minimum WordPress version required by this plugin.
const MINIMUM_WP_VERSION = '4.4';
// Minimum WooCommerce version required by this plugin.
const MINIMUM_WC_VERSION = '5.3';
// SkyVerge plugin framework version used by this plugin.
const FRAMEWORK_VERSION = '5.10.0';
// The plugin name, for displaying notices.
const PLUGIN_NAME = 'Facebook for WooCommerce';
/**
* This class instance.
*
* @var \WC_Facebook_Loader single instance of this class.
*/
private static $instance;
/**
* Admin notices to add.
*
* @var array Array of admin notices.
*/
private $notices = array();
/**
* Constructs the class.
*
* @since 1.10.0
*/
protected function __construct() {
register_activation_hook( __FILE__, array( $this, 'activation_check' ) );
add_action( 'admin_init', array( $this, 'check_environment' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
// If the environment check fails, initialize the plugin.
if ( $this->is_environment_compatible() ) {
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
}
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.10.0
*/
public function __clone() {
wc_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '1.10.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.10.0
*/
public function __wakeup() {
wc_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '1.10.0' );
}
/**
* Initializes the plugin.
*
* @since 1.10.0
*/
public function init_plugin() {
if ( ! Checker::instance()->is_compatible( __FILE__, self::PLUGIN_VERSION ) ) {
return;
}
require_once plugin_dir_path( __FILE__ ) . 'class-wc-facebookcommerce.php';
// fire it up!
if ( function_exists( 'facebook_for_woocommerce' ) ) {
facebook_for_woocommerce();
}
}
/**
* Gets the framework version in namespace form.
*
* @since 1.10.0
*
* @return string
*/
public function get_framework_version_namespace() {
return 'v' . str_replace( '.', '_', $this->get_framework_version() );
}
/**
* Gets the framework version used by this plugin.
*
* @since 1.10.0
*
* @return string
*/
public function get_framework_version() {
return self::FRAMEWORK_VERSION;
}
/**
* Checks the server environment and other factors and deactivates plugins as necessary.
*
* Based on http://wptavern.com/how-to-prevent-wordpress-plugins-from-activating-on-sites-with-incompatible-hosting-environments
*
* @internal
*
* @since 1.10.0
*/
public function activation_check() {
if ( ! $this->is_environment_compatible() ) {
$this->deactivate_plugin();
wp_die( esc_html( self::PLUGIN_NAME . ' could not be activated. ' . $this->get_environment_message() ) );
}
}
/**
* Checks the environment on loading WordPress, just in case the environment changes after activation.
*
* @internal
*
* @since 1.10.0
*/
public function check_environment() {
if ( ! $this->is_environment_compatible() && is_plugin_active( plugin_basename( __FILE__ ) ) ) {
$this->deactivate_plugin();
$this->add_admin_notice( 'bad_environment', 'error', self::PLUGIN_NAME . ' has been deactivated. ' . $this->get_environment_message() );
}
}
/**
* Deactivates the plugin.
*
* @internal
*
* @since 1.10.0
*/
protected function deactivate_plugin() {
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
/**
* Adds an admin notice to be displayed.
*
* @since 1.10.0
*
* @param string $slug The slug for the notice.
* @param string $class The css class for the notice.
* @param string $message The notice message.
*/
private function add_admin_notice( $slug, $class, $message ) {
$this->notices[ $slug ] = array(
'class' => $class,
'message' => $message,
);
}
/**
* Displays any admin notices added with \WC_Facebook_Loader::add_admin_notice()
*
* @internal
*
* @since 1.10.0
*/
public function admin_notices() {
foreach ( (array) $this->notices as $notice_key => $notice ) {
?>
<div class="<?php echo esc_attr( $notice['class'] ); ?>">
<p>
<?php
echo wp_kses(
$notice['message'],
array(
'a' => array(
'href' => array(),
),
'strong' => array(),
)
);
?>
</p>
</div>
<?php
}
}
/**
* Determines if the server environment is compatible with this plugin.
*
* Override this method to add checks for more than just the PHP version.
*
* @since 1.10.0
*
* @return bool
*/
private function is_environment_compatible() {
return version_compare( PHP_VERSION, self::MINIMUM_PHP_VERSION, '>=' );
}
/**
* Gets the message for display when the environment is incompatible with this plugin.
*
* @since 1.10.0
*
* @return string
*/
private function get_environment_message() {
return sprintf( 'The minimum PHP version required for this plugin is %1$s. You are running %2$s.', self::MINIMUM_PHP_VERSION, PHP_VERSION );
}
/**
* Gets the main \WC_Facebook_Loader instance.
*
* Ensures only one instance can be loaded.
*
* @since 1.10.0
*
* @return \WC_Facebook_Loader
*/
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}
// fire it up!
WC_Facebook_Loader::instance();