Skip to content

Commit 3bbb277

Browse files
committed
📦 NEW: Added plugin usage restrictions for websites hosted on wordpress.com
1 parent 4c8fe2b commit 3bbb277

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

customer-loyalty-for-woocommerce.php

+110
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,113 @@ function clwc_update_loyalty_points_callback() {
330330
wp_send_json_success( [ 'message' => __( 'Points updated successfully.', 'customer-loyalty-for-woocommerce' ) ] );
331331
}
332332
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' );

0 commit comments

Comments
 (0)