Skip to content

Commit 284fa80

Browse files
committed
📦 NEW: Added plugin usage restrictions for websites hosted on wordpress.com
1 parent bfdbdad commit 284fa80

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

benchpress.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,113 @@ function benchpress_download_snapshots() {
578578
exit;
579579
}
580580
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' );

0 commit comments

Comments
 (0)