Skip to content

Commit 363199a

Browse files
committed
👌 IMPROVE: Updated the WPCom Check composer script to restrict plugin usage on wordpress.com
1 parent 3bbb277 commit 363199a

19 files changed

+1582
-113
lines changed

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"robertdevore/wpcom-check": "^1.0"
4+
}
5+
}

composer.lock

+72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

customer-loyalty-for-woocommerce.php

+10-113
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
wp_die();
2929
}
3030

31-
/**
32-
* Current plugin version.
33-
*/
31+
// Current plugin version.
3432
define( 'CUSTOMER_LOYALTY_VERSION', '2.0.0' );
3533

34+
// Check if Composer's autoloader is already registered globally.
35+
if ( ! class_exists( 'RobertDevore\WPComCheck\WPComPluginHandler' ) ) {
36+
require_once __DIR__ . '/vendor/autoload.php';
37+
}
38+
39+
use RobertDevore\WPComCheck\WPComPluginHandler;
40+
41+
new WPComPluginHandler( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' );
42+
3643
/**
3744
* Create the CLWC Loyalty Log database table.
3845
*
@@ -330,113 +337,3 @@ function clwc_update_loyalty_points_callback() {
330337
wp_send_json_success( [ 'message' => __( 'Points updated successfully.', 'customer-loyalty-for-woocommerce' ) ] );
331338
}
332339
add_action( 'wp_ajax_clwc_update_loyalty_points', 'clwc_update_loyalty_points_callback' );
333-
334-
/**
335-
* Helper function to handle WordPress.com environment checks.
336-
*
337-
* @param string $plugin_slug The plugin slug.
338-
* @param string $learn_more_link The link to more information.
339-
*
340-
* @since 2.1.0
341-
* @return bool
342-
*/
343-
function wp_com_plugin_check( $plugin_slug, $learn_more_link ) {
344-
// Check if the site is hosted on WordPress.com.
345-
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
346-
// Ensure the deactivate_plugins function is available.
347-
if ( ! function_exists( 'deactivate_plugins' ) ) {
348-
require_once ABSPATH . 'wp-admin/includes/plugin.php';
349-
}
350-
351-
// Deactivate the plugin if in the admin area.
352-
if ( is_admin() ) {
353-
deactivate_plugins( $plugin_slug );
354-
355-
// Add a deactivation notice for later display.
356-
add_option( 'wpcom_deactivation_notice', $learn_more_link );
357-
358-
// Prevent further execution.
359-
return true;
360-
}
361-
}
362-
363-
return false;
364-
}
365-
366-
/**
367-
* Auto-deactivate the plugin if running in an unsupported environment.
368-
*
369-
* @since 2.1.0
370-
* @return void
371-
*/
372-
function wpcom_auto_deactivation() {
373-
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ) ) {
374-
return; // Stop execution if deactivated.
375-
}
376-
}
377-
add_action( 'plugins_loaded', 'wpcom_auto_deactivation' );
378-
379-
/**
380-
* Display an admin notice if the plugin was deactivated due to hosting restrictions.
381-
*
382-
* @since 2.1.0
383-
* @return void
384-
*/
385-
function wpcom_admin_notice() {
386-
$notice_link = get_option( 'wpcom_deactivation_notice' );
387-
if ( $notice_link ) {
388-
?>
389-
<div class="notice notice-error">
390-
<p>
391-
<?php
392-
echo wp_kses_post(
393-
sprintf(
394-
__( 'My Plugin has been deactivated because it cannot be used on WordPress.com-hosted websites. %s', 'customer-loyalty-for-woocommerce' ),
395-
'<a href="' . esc_url( $notice_link ) . '" target="_blank" rel="noopener">' . __( 'Learn more', 'customer-loyalty-for-woocommerce' ) . '</a>'
396-
)
397-
);
398-
?>
399-
</p>
400-
</div>
401-
<?php
402-
delete_option( 'wpcom_deactivation_notice' );
403-
}
404-
}
405-
add_action( 'admin_notices', 'wpcom_admin_notice' );
406-
407-
/**
408-
* Prevent plugin activation on WordPress.com-hosted sites.
409-
*
410-
* @since 2.1.0
411-
* @return void
412-
*/
413-
function wpcom_activation_check() {
414-
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ) ) {
415-
// Display an error message and stop activation.
416-
wp_die(
417-
wp_kses_post(
418-
sprintf(
419-
'<h1>%s</h1><p>%s</p><p><a href="%s" target="_blank" rel="noopener">%s</a></p>',
420-
__( 'Plugin Activation Blocked', 'customer-loyalty-for-woocommerce' ),
421-
__( 'This plugin cannot be activated on WordPress.com-hosted websites. It is restricted due to concerns about WordPress.com policies impacting the community.', 'customer-loyalty-for-woocommerce' ),
422-
esc_url( 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ),
423-
__( 'Learn more', 'customer-loyalty-for-woocommerce' )
424-
)
425-
),
426-
esc_html__( 'Plugin Activation Blocked', 'customer-loyalty-for-woocommerce' ),
427-
[ 'back_link' => true ]
428-
);
429-
}
430-
}
431-
register_activation_hook( __FILE__, 'wpcom_activation_check' );
432-
433-
/**
434-
* Add a deactivation flag when the plugin is deactivated.
435-
*
436-
* @since 2.1.0
437-
* @return void
438-
*/
439-
function wpcom_deactivation_flag() {
440-
add_option( 'wpcom_deactivation_notice', 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' );
441-
}
442-
register_deactivation_hook( __FILE__, 'wpcom_deactivation_flag' );

vendor/autoload.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
if (PHP_VERSION_ID < 50600) {
6+
if (!headers_sent()) {
7+
header('HTTP/1.1 500 Internal Server Error');
8+
}
9+
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
10+
if (!ini_get('display_errors')) {
11+
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
12+
fwrite(STDERR, $err);
13+
} elseif (!headers_sent()) {
14+
echo $err;
15+
}
16+
}
17+
trigger_error(
18+
$err,
19+
E_USER_ERROR
20+
);
21+
}
22+
23+
require_once __DIR__ . '/composer/autoload_real.php';
24+
25+
return ComposerAutoloaderInitca9049066c1270338606f5739f59d5ab::getLoader();

0 commit comments

Comments
 (0)