-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommerce7-for-wordpress.php
More file actions
412 lines (359 loc) · 13.1 KB
/
commerce7-for-wordpress.php
File metadata and controls
412 lines (359 loc) · 13.1 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* Integrate Commerce7 functionality into your WordPress site easily
*
* @package wp-commerce7
* @author Michael Bourne
* @license GPL3
* @link https://ursa6.com
* @since 1.0.0
*
* @wordpress-plugin
* Plugin Name: Commerce7 for WordPress
* Description: Integrate Commerce7 functionality into your WordPress site easily
* Version: 1.7.1
* Author: URSA6 & 5forests
* Author URI: https://5forests.com
* Plugin URI: https://c7wp.com
* Requires at least: 6.0
* Tested up to: 7.0
* Stable tag: 1.7.1
* Requires PHP: 8.0
* License: GPL3
* License URI: https://www.gnu.org/licenses/gpl-3.0.en.html
* Text Domain: wp-commerce7
* Domain Path: /languages
*
* Created Date: Friday September 27th 2019
* Author: Michael Bourne
* -----
* Last Modified: Monday, April 27th 2026, 10:51:00 am
* Modified By: Michael Bourne
* -----
* Copyright (c) 2019-2026 URSA6
*
* Commerce7 for WordPress is a plugin for WordPress that enables you to add Commerce7 ecommerce integration into your site.
* Plugin developed with permission of the Commerce7 company. Commerce7 logo and name used with permission.
* The Commerce7 for WordPress Plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>
* You can contact me at michael@ursa6.com
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
defined( 'C7WP_ROOT' ) || define( 'C7WP_ROOT', __DIR__ );
defined( 'C7WP_URI' ) || define( 'C7WP_URI', plugin_dir_url( __FILE__ ) );
defined( 'C7WP_VERSION' ) || define( 'C7WP_VERSION', '1.7.1' );
if ( ! defined( 'C7WP_NOTICES_URL' ) ) {
define( 'C7WP_NOTICES_URL', 'https://c7wp.com/notices.json' );
}
/**
* On plugin activation
* 1. Create the pages needed for integration to work
* 2. Inject content into those pages
* 3. Set privacy policy page
*/
function c7wp_activate_plugin() {
add_option( 'c7wp_activation', true );
// Schedule remote notices cron job
c7wp_schedule_notices_cron();
$pages = array( 'profile', 'collection', 'product', 'club', 'checkout', 'cart', 'reservation' );
$fail = array();
foreach ( $pages as $page ) {
if ( get_page_by_path( $page, 'ARRAY_N', 'page' ) ) {
$fail[] = $page;
continue;
}
$c7_post = array(
'post_title' => wp_strip_all_tags( ucfirst( $page ) ),
'post_content' => '<!-- wp:c7wp/default --><div class="wp-block-c7wp-default"><div id="c7-content"></div></div><!-- /wp:c7wp/default -->',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
$pageid = wp_insert_post( $c7_post );
// Check if page creation was successful
if ( is_wp_error( $pageid ) ) {
// Log error if WP_DEBUG is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Commerce7: Failed to create page "' . $page . '": ' . $pageid->get_error_message() );
}
$fail[] = $page;
} elseif ( ! $pageid ) {
// Log error if WP_DEBUG is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Commerce7: Failed to create page "' . $page . '": Unknown error' );
}
$fail[] = $page;
}
}
if ( ! empty( $fail ) ) {
set_transient( 'c7wp-admin-notice-pages', $fail, 5 );
}
$c7options = array(
'c7wp_tenant' => '',
'c7wp_display_cart' => 'no',
'c7wp_display_cart_location' => 'tr',
'c7wp_display_cart_color' => 'light',
'c7wp_widget_version' => 'v2',
'c7wp_enable_custom_routes' => 'no',
'c7wp_frontend_routes' => array(
'profile' => 'profile',
'collection' => 'collection',
'product' => 'product',
'club' => 'club',
'checkout' => 'checkout',
'cart' => 'cart',
'reservation' => 'reservation',
),
);
update_option( 'c7wp_settings', $c7options, true );
// set a default permalink structure if the installation has not yet done this
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
if ( function_exists( 'wp_install_maybe_enable_pretty_permalinks' ) ) {
wp_install_maybe_enable_pretty_permalinks();
}
}
register_activation_hook( __FILE__, 'c7wp_activate_plugin' );
/**
* On plugin upgrade
*/
add_action( 'upgrader_process_complete', 'c7wp_upgrade_function', 10, 2 );
/**
* Upgrade function
*
* @see https://developer.wordpress.org/reference/hooks/upgrader_process_complete/
*
* @param object $upgrader_object WP_Upgrader instance.
* @param array $options Array of bulk item update data.
* @return void
*/
function c7wp_upgrade_function( $upgrader_object, $options ) {
$current_plugin_path_name = plugin_basename( __FILE__ );
// If a plugin is being updated.
if ( 'update' === $options['action'] && 'plugin' === $options['type'] && isset( $options['plugins'] ) ) {
foreach ( $options['plugins'] as $each_plugin ) {
// If the plugin being updated is this plugin.
if ( $each_plugin == $current_plugin_path_name ) { // phpcs:ignore
$options = get_option( 'c7wp_settings' );
if ( isset( $options['c7wp_widget_version'] ) && 'v2' === $options['c7wp_widget_version']
&& isset( $options['c7wp_enable_custom_routes'] ) && 'yes' === $options['c7wp_enable_custom_routes']
&& isset( $options['c7wp_frontend_routes'] ) && is_array( $options['c7wp_frontend_routes'] ) ) {
$pages = $options['c7wp_frontend_routes'];
} else {
$pages = array(
'profile' => 'profile',
'collection' => 'collection',
'product' => 'product',
'club' => 'club',
'checkout' => 'checkout',
'cart' => 'cart',
'reservation' => 'reservation',
);
}
$fail = array();
// Loop through required paged for C7.
foreach ( $pages as $page => $slug ) {
// if the page is missing, add it to the notice
if ( ! get_page_by_path( $slug, 'ARRAY_N', 'page' ) ) {
$fail[] = wp_strip_all_tags( ucfirst( $page ) );
continue;
}
}
// If we have missing pages, let's set a transient to display a notice.
if ( ! empty( $fail ) ) {
set_transient( 'c7wp-admin-notice-pages-missing', $fail, 0 );
}
$c7options = array(
'c7wp_tenant' => '',
'c7wp_display_cart' => 'no',
'c7wp_display_cart_location' => 'tr',
'c7wp_display_cart_color' => 'light',
'c7wp_widget_version' => 'v2',
'c7wp_enable_custom_routes' => 'no',
'c7wp_frontend_routes' => array(
'profile' => 'profile',
'collection' => 'collection',
'product' => 'product',
'club' => 'club',
'checkout' => 'checkout',
'cart' => 'cart',
'reservation' => 'reservation',
),
);
// Merge client set options with default options to fix any unset array keys.
$normalized_options = array_merge( $c7options, $options );
update_option( 'c7wp_settings', $normalized_options, true );
// Only flush rewrite rules if route settings have changed
$old_routes = isset( $options['c7wp_frontend_routes'] ) ? $options['c7wp_frontend_routes'] : array();
$new_routes = $normalized_options['c7wp_frontend_routes'];
if ( $old_routes !== $new_routes ) {
if ( function_exists( 'flush_rewrite_rules' ) ) {
flush_rewrite_rules();
}
}
}
}
}
}
/**
* Notices
*/
function c7wp_admin_notice_pages() {
if ( $data = get_transient( 'c7wp-admin-notice-pages' ) ) { // phpcs:ignore
$pages = implode( ', ', $data );
echo '<div class="notice notice-warning is-dismissible"><p>';
printf(
/* translators: 1: List of missing pages 2: List of missing pages */
esc_html__(
'Commerce7 requires specific pages exist in order for integration to work properly. The following pages are needed but were not created, as they already exist: %1$s.
Please ensure these pages contain the proper content.',
'wp-commerce7'
),
'<strong>' . esc_html( $pages ) . '</strong>'
);
echo '</p></div>';
/* Delete transient, only display this notice once. */
delete_transient( 'c7wp-admin-notice-pages' );
}
if ( $data = get_transient( 'c7wp-admin-notice-pages-missing' ) ) { // phpcs:ignore
$pages = implode( ', ', $data );
echo '<div class="notice notice-warning is-dismissible"><p>';
printf(
/* translators: %s: List of missing pages */
esc_html__(
'Commerce7 requires specific pages exist in order for integration to work properly. The following pages were missing: %s.
These pages were not recreated in case you have removed them on purpose. Please review and crate these pages if needed.',
'wp-commerce7'
),
'<strong>' . esc_html( $pages ) . '</strong>'
);
echo '</p></div>';
/* Delete transient, only display this notice once. */
delete_transient( 'c7wp-admin-notice-pages-missing' );
}
}
add_action( 'admin_notices', 'c7wp_admin_notice_pages' );
/**
* On plugin deactivation
* 1. Flush rewrite rules to clear redirects
*/
function c7wp_deactivate_plugin() {
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'c7wp_deactivate_plugin' );
// load plugin
require_once 'includes/class-c7wp.php';
function commerce7_wp_manager() {
return C7WP::getInstance();
}
commerce7_wp_manager();
/**
* Fetch remote admin notices via cron job
*/
function c7wp_fetch_remote_notices() {
$response = wp_remote_get(
C7WP_NOTICES_URL,
array(
'timeout' => 10,
'headers' => array(
'User-Agent' => 'Commerce7-WordPress-Plugin/' . C7WP_VERSION,
),
)
);
if ( is_wp_error( $response ) ) {
// Log error if WP_DEBUG is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Commerce7: Failed to fetch remote notices: ' . $response->get_error_message() );
}
return;
}
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $response_code ) {
// Log error if WP_DEBUG is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Commerce7: Remote notices API returned HTTP ' . $response_code );
}
return;
}
$notices = json_decode( wp_remote_retrieve_body( $response ), true );
if ( json_last_error() !== JSON_ERROR_NONE ) {
// Log error if WP_DEBUG is enabled
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Commerce7: Failed to parse remote notices JSON: ' . json_last_error_msg() );
}
return;
}
if ( is_array( $notices ) ) {
set_transient( 'c7wp_remote_notices', $notices, 12 * HOUR_IN_SECONDS );
}
}
/**
* Schedule remote notices cron job
*/
function c7wp_schedule_notices_cron() {
if ( ! wp_next_scheduled( 'c7wp_fetch_remote_notices' ) ) {
wp_schedule_event( time(), 'twicedaily', 'c7wp_fetch_remote_notices' );
}
}
/**
* Display remote admin notices
*/
function c7wp_display_remote_notices() {
$notices = get_transient( 'c7wp_remote_notices' );
if ( ! empty( $notices ) && is_array( $notices ) ) {
foreach ( $notices as $notice ) {
if ( ! empty( $notice['message'] ) && ! get_user_meta( get_current_user_id(), 'c7wp_notice_dismissed_' . md5( $notice['message'] ), true ) ) {
add_action(
'admin_notices',
function() use ( $notice ) {
$dismiss_url = add_query_arg(
array(
'c7wp_dismiss_notice' => md5( $notice['message'] ),
'_wpnonce' => wp_create_nonce( 'c7wp_dismiss_notice' ),
)
);
?>
<div class="notice notice-<?php echo esc_attr( $notice['type'] ?? 'info' ); ?> is-dismissible">
<?php if ( ! empty( $notice['format'] ) && 'html' === $notice['format'] ) : ?>
<?php echo wp_kses_post( $notice['message'] ); ?>
<?php else : ?>
<p><?php echo wp_kses_post( $notice['message'] ); ?></p>
<?php endif; ?>
<a href="<?php echo esc_url( $dismiss_url ); ?>" class="notice-dismiss"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'wp-commerce7' ); ?></span></a>
</div>
<?php
}
);
}
}
}
}
// Clean up cron job on deactivation
register_deactivation_hook(
__FILE__,
function() {
wp_clear_scheduled_hook( 'c7wp_fetch_remote_notices' );
}
);
// Hook the cron job
add_action( 'c7wp_fetch_remote_notices', 'c7wp_fetch_remote_notices' );
// Display notices on admin pages
add_action( 'admin_init', 'c7wp_display_remote_notices' );
/**
* Handle notice dismissal
*/
function c7wp_handle_notice_dismissal() {
// Check user capabilities first
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( isset( $_GET['c7wp_dismiss_notice'] ) && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'c7wp_dismiss_notice' ) ) {
$notice_id = sanitize_text_field( wp_unslash( $_GET['c7wp_dismiss_notice'] ) );
update_user_meta( get_current_user_id(), 'c7wp_notice_dismissed_' . $notice_id, true );
wp_safe_redirect( remove_query_arg( array( 'c7wp_dismiss_notice', '_wpnonce' ) ) );
exit;
}
}
add_action( 'admin_init', 'c7wp_handle_notice_dismissal' );