-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbigcommerce.php
163 lines (139 loc) · 5.44 KB
/
bigcommerce.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
<?php
/*
Plugin Name: BigCommerce for WordPress
Description: Scale your ecommerce business with WordPress on the front-end and BigCommerce on the back end. Free up server resources from things like catalog management, processing payments, and managing fulfillment logistics.
Author: BigCommerce
Version: 5.1.2
Author URI: https://www.bigcommerce.com/wordpress
Requires PHP: 7.4.0
Text Domain: bigcommerce
License: GPLv2 or later
*/
use BigCommerce\Pages\Login_Page;
use BigCommerce\Taxonomies\Channel\BC_Status;
define( 'BIGCOMMERCE_PHP_MINIMUM_VERSION', '7.0' );
define( 'BIGCOMMERCE_PHP_OPTIMAL_VERSION', '7.4' );
define( 'BIGCOMMERCE_WP_MINIMUM_VERSION', '4.8' );
define( 'BIGCOMMERCE_WP_OPTIMAL_VERSION', '5.8' );
if ( version_compare( PHP_VERSION, BIGCOMMERCE_PHP_MINIMUM_VERSION, '<' ) || version_compare( get_bloginfo( 'version' ), BIGCOMMERCE_WP_MINIMUM_VERSION, '<' ) ) {
add_action( 'admin_notices', function() {
$message = sprintf( esc_html__( 'BigCommerce requires PHP version %s+ and WP version %s+, plugin is currently NOT RUNNING.', 'bigcommerce' ), BIGCOMMERCE_PHP_OPTIMAL_VERSION, BIGCOMMERCE_WP_OPTIMAL_VERSION );
echo wp_kses_post( sprintf( '<div class="error">%s</div>', wpautop( $message ) ) );
} );
return;
} elseif ( version_compare( PHP_VERSION, BIGCOMMERCE_PHP_OPTIMAL_VERSION, '<' ) || version_compare( get_bloginfo( 'version' ), BIGCOMMERCE_WP_OPTIMAL_VERSION, '<' ) ) {
add_action( 'admin_notices', function() {
$message = sprintf( esc_html__( 'BigCommerce requires PHP version %s+ and WP version %s+', 'bigcommerce' ), BIGCOMMERCE_PHP_OPTIMAL_VERSION, BIGCOMMERCE_WP_OPTIMAL_VERSION );
echo wp_kses_post( sprintf( '<div class="notice">%s</div>', wpautop( $message ) ) );
} );
}
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
register_activation_hook( __FILE__, [ \BigCommerce\Plugin::class, 'activate' ] );
// Start the plugin
add_action( 'plugins_loaded', 'bigcommerce_init', 2, 0 );
/**
* @return \BigCommerce\Plugin
*/
function bigcommerce_init() {
// Don't load on frontend for non-active channel status
$channel_status = bigcommerce_get_primary_channel_status();
$is_front_page = ! defined( 'WP_CLI' ) && ! is_admin() && $channel_status !== null;
// Don't load on frontend for non-active channel status
if ( $is_front_page && ! in_array( $channel_status, [ BC_Status::STATUS_ACTIVE, BC_Status::STATUS_PRE_LAUNCH ] ) ) {
return;
}
if ( ! defined( 'DOING_CRON' ) && $is_front_page && $channel_status === BC_Status::STATUS_PRE_LAUNCH && ! current_user_can( 'publish_posts' ) ) {
handle_404_redirect_in_prelaunch();
return;
}
$container = new \Pimple\Container( [ 'plugin_file' => __FILE__ ] );
$plugin = \BigCommerce\Plugin::instance( $container );
$plugin->init();
/**
* Fires after the plugin has initialized
*
* @param \BigCommerce\Plugin $plugin The global instance of the plugin controller
* @param \Pimple\Container $container The plugin's dependency injection container
*/
do_action( 'bigcommerce/init', $plugin, $container );
return $plugin;
}
function handle_404_redirect_in_prelaunch() {
// We have to do that here because we don't initialize the plugin in case of prelaunch status
add_filter( 'pre_handle_404', function ( $preempt ) {
if ( is_admin() || ! ( is_page() && is_main_query() ) ) {
return $preempt;
}
$plugin_pages = [
\BigCommerce\Pages\Account_Page::SLUG,
\BigCommerce\Pages\Address_Page::SLUG,
\BigCommerce\Pages\Cart_Page::SLUG,
\BigCommerce\Pages\Check_Balance_Page::SLUG,
\BigCommerce\Pages\Checkout_Complete_Page::SLUG,
\BigCommerce\Pages\Checkout_Page::SLUG,
\BigCommerce\Pages\Login_Page::SLUG,
\BigCommerce\Pages\Gift_Certificate_Page::SLUG,
\BigCommerce\Pages\Orders_Page::SLUG,
\BigCommerce\Pages\Registration_Page::SLUG,
\BigCommerce\Pages\Shipping_Returns_Page::SLUG,
\BigCommerce\Pages\Wishlist_Page::SLUG
];
if ( ! in_array( get_queried_object()->post_name, $plugin_pages ) ) {
return $preempt;
}
global $wp_query;
$wp_query->set_404();
status_header( 404 );
nocache_headers();
return '';
}, 10, 1 );
}
function bigcommerce() {
try {
return \BigCommerce\Plugin::instance();
} catch ( \Exception $e ) {
return bigcommerce_init();
}
}
/**
* Look for a value in an environment variable, falling back
* to a constant. This allows configuration options to be set
* either in the environment or in wp-config.php.
*
* @param string $key The name of an environment variable or constant
*
* @return mixed The found value. false if not found.
*/
function bigcommerce_get_env( $key ) {
$value = getenv( $key, true ) ?: getenv( $key );
if ( $value === false && defined( $key ) ) {
$value = constant( $key );
}
return $value;
}
/**
* Get the status of the primary channel
*
* @return mixed The found value. null if not found.
*/
function bigcommerce_get_primary_channel_status() {
$cache_key = 'bigcommerce_primary_channel_status';
$status = wp_cache_get( $cache_key );
if ( empty( $status ) ) {
global $wpdb;
$sql = "SELECT tm.meta_value
FROM {$wpdb->termmeta} tm
INNER JOIN {$wpdb->termmeta} tm2 ON tm2.term_id=tm.term_id
WHERE tm.meta_key=%s AND tm2.meta_key=%s AND tm2.meta_value=%s";
$status = $wpdb->get_var( $wpdb->prepare(
$sql,
BC_Status::STATUS,
\BigCommerce\Taxonomies\Channel\Channel::STATUS,
\BigCommerce\Taxonomies\Channel\Channel::STATUS_PRIMARY
) );
wp_cache_set( $cache_key, $status );
}
return $status;
}