plugin_name = $plugin_name;
$this->plugin_path = $plugin_path;
$parts = explode('/', $this->plugin_path);
$this->plugin_slug = $parts[0];
$this->enable_tracking = 'pisol_'.$this->plugin_slug;
$this->enable_tracking_action = 'pisol_'.$this->plugin_slug.'_action';
$this->analytics_start_date = 'pisol_analytics_'.$this->plugin_slug.'_start_date';
$this->show_after_this_days = 30;
$this->version = $version;
$this->url = 'https://www.piwebsolution.com/plugin-tracker/';
add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action( 'admin_footer-plugins.php', [ $this, 'print_deactivation_modal' ] );
add_action('admin_post_pi_handle_deactivation_' . $this->plugin_slug, array($this, 'handle_deactivation_form'));
add_action('admin_notices', array($this, 'show_tracker_notice'));
add_action('admin_post_' . $this->enable_tracking_action, array($this, 'handle_tracker_action'));
}
public function show_tracker_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$activation_time = $this->getInstallationDate();
if(current_time('timestamp') < strtotime($activation_time." +{$this->show_after_this_days} days")) {
return;
}
//delete_option($this->enable_tracking);
if (!empty(get_option($this->enable_tracking, ''))) {
return;
}
$notice = '
';
$notice .= '
Help to Improve ' . esc_html($this->plugin_name) . ' plugin
';
$notice .= '
'.esc_html__("Hi, your support can make a big difference!").'
';
$notice .= '
'.esc_html__("We collect only technical data — including the plugin version, WordPress version, WooCommerce version, and site url — solely to improve compatibility and enhance plugin features.").'
';
$notice .= '
';
$notice .= sprintf(
'%s',
esc_url(wp_nonce_url(admin_url('admin-post.php?enable=0&action=' . $this->enable_tracking_action), $this->enable_tracking_action)),
esc_html__('I Don\'t Help')
);
$notice .= sprintf(
'%s',
esc_url(wp_nonce_url(admin_url('admin-post.php?enable=1&action=' . $this->enable_tracking_action), $this->enable_tracking_action)),
esc_html__('I Will Help')
);
$notice .= '
';
$notice .= '
';
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $notice;
}
public function handle_tracker_action() {
if (!current_user_can('manage_options')) {
wp_die(esc_html__('You do not have permission to perform this action.'));
}
check_admin_referer($this->enable_tracking_action);
if (isset($_GET['enable']) && in_array($_GET['enable'], array('1', '0'), true)) {
$enable = $_GET['enable'] === '1' ? 'enable' : 'disable';
update_option($this->enable_tracking, $enable);
$redirect_url = isset($_SERVER['HTTP_REFERER']) ? esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER'])) : admin_url();
if ($enable === 'enable') {
$this->send_activation_data('enable');
}
wp_safe_redirect($redirect_url);
exit;
}
}
public function send_activation_data($action, $message = '') {
$data = array(
'plugin_slug' => $this->plugin_slug,
'version' => $this->version,
'site_url' => get_site_url(),
'wp_version' => get_bloginfo('version'),
'wc_version' => function_exists('WC') ? WC()->version : '',
'action' => $action,
'message' => $message,
);
// Make the request non-blocking by setting 'blocking' => false
wp_remote_post($this->url, array(
'body' => wp_json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'blocking' => false,
));
}
function enqueue_scripts($hook) {
if ($hook !== 'plugins.php') return;
$js = '
jQuery(document).ready(function($) {
var id = "deactivate-'.$this->plugin_slug.'";
jQuery(document).on("click", "#"+id, function(e) {
var href = $(this).attr("href");
var popup = "#pi-deactivation-reason-'.$this->plugin_slug.'";
if ($(popup).length) {
e.preventDefault();
$(popup).show();
}
});
jQuery(document).on("click", "#pi-deactivator-close-'.$this->plugin_slug.'", function(e) {
var popup = "#pi-deactivation-reason-'.$this->plugin_slug.'";
if ($(popup).length) {
e.preventDefault();
$(popup).hide();
}
});
jQuery(document).on("change", "#pi-deact-form-'.esc_attr($this->plugin_slug).' input[name=\'reason_radio\']", function () {
if (jQuery(this).val() === "Other") {
jQuery("#pi-deact-form-'.esc_attr($this->plugin_slug).' textarea").show();
} else {
jQuery("#pi-deact-form-'.esc_attr($this->plugin_slug).' textarea").hide();
}
});
});
';
wp_add_inline_script('jquery', $js);
// Add the deactivation modal CSS as inline style
$css = '.pi-deactivation-overlay-' . esc_attr($this->plugin_slug) . ' {
display: none;
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 9999;
}
.pi-deact-close-' . esc_attr($this->plugin_slug) . ' {
color: #000;
text-decoration: none;
font-size: 20px;
font-weight: bold;
}
.pi-deactivation-modal-' . esc_attr($this->plugin_slug) . ' {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 20px;
width: 400px;
max-width: 90%;
max-height: 90%;
overflow: auto;
box-shadow: 0 2px 10px rgba(0,0,0,0.3);
z-index: 10000;
}';
wp_add_inline_style('wp-admin', $css);
}
public function print_deactivation_modal() {
$nonce = wp_create_nonce('pi_deactivate_nonce_' . $this->plugin_slug);
?>
plugin_slug)) {
wp_die(esc_html__('Security check failed'));
}
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_die( esc_html__( 'You do not have permission to deactivate plugins.' ) );
}
$plugin_slug = sanitize_text_field(wp_unslash($_POST['plugin_slug'] ?? ''));
$message = sanitize_textarea_field(wp_unslash($_POST['message'] ?? ''));
$reason = sanitize_text_field(wp_unslash($_POST['reason_radio'] ?? ''));
// Combine reason and message, prioritizing reason if message is empty
if (!empty($reason) && !empty($message)) {
$message = $reason . ': ' . $message;
} elseif (!empty($reason)) {
$message = $reason;
}
$action_type = sanitize_text_field(wp_unslash($_POST['action_type'] ?? ''));
if (is_multisite() && is_plugin_active_for_network($this->plugin_path)) {
if (is_super_admin()) {
deactivate_plugins($this->plugin_path, false, true); // network-wide
} else {
wp_die(esc_html__('You do not have permission to deactivate a network plugin.'));
}
} else {
deactivate_plugins($this->plugin_path); // normal
}
if($action_type === 'submit'){
$this->send_activation_data('disable', wp_strip_all_tags($message));
}
// Redirect back to plugins page
wp_safe_redirect(admin_url('plugins.php'));
exit;
}
function getInstallationDate(){
$get_install_date = get_option($this->analytics_start_date);
if(empty($get_install_date) || !$this->validateDate($get_install_date)){
$now = current_time( "Y/m/d" );
add_option( $this->analytics_start_date, $now );
return $now;
}
return $get_install_date;
}
function validateDate($date, $format = 'Y/m/d'){
if ( empty($date) ) return false;
$d = \DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
}
new Pi_Efrs_Analytics(
'WooCommerce Advanced Flat Rate Shipping',
'advanced-free-flat-shipping-woocommerce/extended-flat-rate-shipping-woocommerce.php',
EXTENDED_FLAT_RATE_SHIPPING_WOOCOMMERCE_VERSION
);