Skip to content

Commit d05d138

Browse files
committed
📦 NEW: Added WPCom Check to restrict plugin usage on wordpress.com
1 parent 284fa80 commit d05d138

19 files changed

Lines changed: 1581 additions & 110 deletions

benchpress.php

Lines changed: 9 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ function benchpress_settings_link( $links ) {
6868
// Set the branch that contains the stable release.
6969
$myUpdateChecker->setBranch( 'main' );
7070

71+
// Check if Composer's autoloader is already registered globally.
72+
if ( ! class_exists( 'RobertDevore\WPComCheck\WPComPluginHandler' ) ) {
73+
require_once __DIR__ . '/vendor/autoload.php';
74+
}
75+
76+
use RobertDevore\WPComCheck\WPComPluginHandler;
77+
78+
new WPComPluginHandler( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' );
79+
7180
/**
7281
* Summary of benchpress_create_snapshots_table
7382
*
@@ -578,113 +587,3 @@ function benchpress_download_snapshots() {
578587
exit;
579588
}
580589
add_action( 'wp_ajax_benchpress_download_snapshots', 'benchpress_download_snapshots' );
581-
582-
/**
583-
* Helper function to handle WordPress.com environment checks.
584-
*
585-
* @param string $plugin_slug The plugin slug.
586-
* @param string $learn_more_link The link to more information.
587-
*
588-
* @since 1.2.0
589-
* @return bool
590-
*/
591-
function wp_com_plugin_check( $plugin_slug, $learn_more_link ) {
592-
// Check if the site is hosted on WordPress.com.
593-
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
594-
// Ensure the deactivate_plugins function is available.
595-
if ( ! function_exists( 'deactivate_plugins' ) ) {
596-
require_once ABSPATH . 'wp-admin/includes/plugin.php';
597-
}
598-
599-
// Deactivate the plugin if in the admin area.
600-
if ( is_admin() ) {
601-
deactivate_plugins( $plugin_slug );
602-
603-
// Add a deactivation notice for later display.
604-
add_option( 'wpcom_deactivation_notice', $learn_more_link );
605-
606-
// Prevent further execution.
607-
return true;
608-
}
609-
}
610-
611-
return false;
612-
}
613-
614-
/**
615-
* Auto-deactivate the plugin if running in an unsupported environment.
616-
*
617-
* @since 1.2.0
618-
* @return void
619-
*/
620-
function wpcom_auto_deactivation() {
621-
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ) ) {
622-
return; // Stop execution if deactivated.
623-
}
624-
}
625-
add_action( 'plugins_loaded', 'wpcom_auto_deactivation' );
626-
627-
/**
628-
* Display an admin notice if the plugin was deactivated due to hosting restrictions.
629-
*
630-
* @since 1.2.0
631-
* @return void
632-
*/
633-
function wpcom_admin_notice() {
634-
$notice_link = get_option( 'wpcom_deactivation_notice' );
635-
if ( $notice_link ) {
636-
?>
637-
<div class="notice notice-error">
638-
<p>
639-
<?php
640-
echo wp_kses_post(
641-
sprintf(
642-
__( 'My Plugin has been deactivated because it cannot be used on WordPress.com-hosted websites. %s', 'benchpress' ),
643-
'<a href="' . esc_url( $notice_link ) . '" target="_blank" rel="noopener">' . __( 'Learn more', 'benchpress' ) . '</a>'
644-
)
645-
);
646-
?>
647-
</p>
648-
</div>
649-
<?php
650-
delete_option( 'wpcom_deactivation_notice' );
651-
}
652-
}
653-
add_action( 'admin_notices', 'wpcom_admin_notice' );
654-
655-
/**
656-
* Prevent plugin activation on WordPress.com-hosted sites.
657-
*
658-
* @since 1.2.0
659-
* @return void
660-
*/
661-
function wpcom_activation_check() {
662-
if ( wp_com_plugin_check( plugin_basename( __FILE__ ), 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ) ) {
663-
// Display an error message and stop activation.
664-
wp_die(
665-
wp_kses_post(
666-
sprintf(
667-
'<h1>%s</h1><p>%s</p><p><a href="%s" target="_blank" rel="noopener">%s</a></p>',
668-
__( 'Plugin Activation Blocked', 'benchpress' ),
669-
__( 'This plugin cannot be activated on WordPress.com-hosted websites. It is restricted due to concerns about WordPress.com policies impacting the community.', 'benchpress' ),
670-
esc_url( 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' ),
671-
__( 'Learn more', 'benchpress' )
672-
)
673-
),
674-
esc_html__( 'Plugin Activation Blocked', 'benchpress' ),
675-
[ 'back_link' => true ]
676-
);
677-
}
678-
}
679-
register_activation_hook( __FILE__, 'wpcom_activation_check' );
680-
681-
/**
682-
* Add a deactivation flag when the plugin is deactivated.
683-
*
684-
* @since 1.2.0
685-
* @return void
686-
*/
687-
function wpcom_deactivation_flag() {
688-
add_option( 'wpcom_deactivation_notice', 'https://robertdevore.com/why-this-plugin-doesnt-support-wordpress-com-hosting/' );
689-
}
690-
register_deactivation_hook( __FILE__, 'wpcom_deactivation_flag' );

composer.json

Lines changed: 5 additions & 0 deletions
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

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/autoload.php

Lines changed: 25 additions & 0 deletions
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)