Skip to content

Commit

Permalink
Merge pull request #250 from TycheSoftwares/deactivation-changes
Browse files Browse the repository at this point in the history
deactivation changes
  • Loading branch information
gaminikhil authored Oct 22, 2024
2 parents 07368af + 507e07b commit 277dc7e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
24 changes: 13 additions & 11 deletions includes/class-alg-wc-all-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ public function __construct() {
$pgbf_blog_post_link = 'https://www.tychesoftwares.com/docs/docs/payment-gateway-based-fees-and-discounts-for-woocommerce/payment-gateway-based-fees-and-discounts-usage-tracking/';
$pgbf_get_previous_version = get_option( 'alg_woocommerce_checkout_fees_version' );

require_once( "component/plugin-deactivation/class-tyche-plugin-deactivation.php" );
new Tyche_Plugin_Deactivation(
array(
'plugin_name' => $pgbf_plugin_name,
'plugin_base' => $pgbf_file_name,
'script_file' => $plugin_url . '/includes/js/plugin-deactivation.js',
'plugin_short_name' => $pgbf_plugin_prefix,
'version' => $pgbf_get_previous_version,
'plugin_locale' => $pgbf_locale,
)
);
if ( strpos( $_SERVER['REQUEST_URI'], 'plugins.php' ) !== false || strpos( $_SERVER['REQUEST_URI'], 'action=deactivate' ) !== false || ( strpos( $_SERVER['REQUEST_URI'], 'admin-ajax.php' ) !== false && isset( $_POST['action'] ) && $_POST['action'] === 'tyche_plugin_deactivation_submit_action' ) ) { //phpcs:ignore
require_once( "component/plugin-deactivation/class-tyche-plugin-deactivation.php" );
new Tyche_Plugin_Deactivation(
array(
'plugin_name' => $pgbf_plugin_name,
'plugin_base' => $pgbf_file_name,
'script_file' => $plugin_url . '/includes/js/plugin-deactivation.js',
'plugin_short_name' => $pgbf_plugin_prefix,
'version' => $pgbf_get_previous_version,
'plugin_locale' => $pgbf_locale,
)
);
}

require_once( "component/plugin-tracking/class-tyche-plugin-tracking.php" );
new Tyche_Plugin_Tracking(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Tyche Softwares
* @package TycheSoftwares/PluginDeactivation
* @category Classes
* @since 1.1
* @since 1.3
*/

defined( 'ABSPATH' ) || exit;
Expand All @@ -26,7 +26,14 @@ class Tyche_Plugin_Deactivation {
*
* @var string $version
*/
private $version = '';
private $version = '1.3';

/**
* Plugin Version.
*
* @var string $plugin_version
*/
private $plugin_version = '';

/**
* API Url.
Expand Down Expand Up @@ -108,7 +115,7 @@ public function init_vars( $options ) {
$this->plugin_base = $options['plugin_base'];
$this->script_file = $options['script_file'];
$this->plugin_short_name = $options['plugin_short_name'];
$this->version = $options['version'];
$this->plugin_version = $options['version'];
$this->plugin_locale = $options['plugin_locale'];

return true;
Expand Down Expand Up @@ -137,23 +144,27 @@ public function plugin_action_links( $links ) {
* @since 1.1
*/
public function enqueue_scripts() {
$current_screen = get_current_screen();
if ( isset( $current_screen->id ) && 'plugins' !== $current_screen->id ) {
return;
}

wp_enqueue_style(
'tyche_plugin_deactivation',
$this->api_url . '/assets/plugin-deactivation/css/style.css',
array(),
$this->version
$this->plugin_version
);

wp_register_script(
'tyche_plugin_deactivation_' . $this->plugin_short_name,
$this->script_file,
array( 'jquery', 'tyche' ),
$this->version,
$this->plugin_version,
true
);

$request = wp_remote_get( $this->api_url . '?action=fetch-deactivation-data&plugin=' . $this->plugin_short_name . '&language=' . apply_filters( 'tyche_plugin_deactivation_language', 'en' ) );
$request = wp_remote_get( $this->api_url . '?action=fetch-deactivation-data&plugin=' . $this->plugin_short_name . '&language=' . apply_filters( 'tyche_plugin_deactivation_language', 'en' ) . '&version=' . $this->version );

if ( is_wp_error( $request ) || 200 !== wp_remote_retrieve_response_code( $request ) ) {
return false; // In case the user is offline or something else that could have probably caused an error.
Expand Down Expand Up @@ -190,6 +201,12 @@ public function tyche_plugin_deactivation_submit_action() {
wp_send_json_error( 0 );
}

$reason_id = isset( $_POST['reason_id'] ) ? sanitize_text_field( wp_unslash( $_POST['reason_id'] ) ) : '';
if ( 0 === (int) $reason_id ) {
wp_send_json_error( 0 );
exit;
}

wp_safe_remote_post(
$this->api_url,
array(
Expand All @@ -202,6 +219,7 @@ public function tyche_plugin_deactivation_submit_action() {
'body' => wp_json_encode(
array(
'action' => 'plugin-deactivation',
'version' => $this->version,
'plugin_slug' => isset( $_POST['plugin_short_name'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_short_name'] ) ) : '',
'url' => home_url(),
'email' => apply_filters( 'ts_tracker_admin_email', get_option( 'admin_email' ) ),
Expand Down
24 changes: 17 additions & 7 deletions includes/js/tyche.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

var tyche = {
version: '1.1.3', // version of tyche constructor.
version: '1.2', // version of tyche constructor.
constructor: tyche,
extend: function() {
let target = arguments[ 0 ] || {},
Expand Down Expand Up @@ -135,6 +135,7 @@ var tyche = {

if ( btn_deactivate.length > 0 && modal.hasClass( 'no-confirmation-message' ) && !btn_deactivate.hasClass( 'allow-deactivate' ) ) {
btn_deactivate.addClass( 'allow-deactivate' );
modal.find( '.button-skip-deactivate' ).addClass( 'allow-deactivate' );
modal.find( '.ts-modal-panel' ).removeClass( 'active ' );
modal.find( '[data-panel-id="reasons"]' ).addClass( 'active' );
}
Expand All @@ -157,6 +158,12 @@ var tyche = {
} );
}

modal.on( 'click', '.button-skip-deactivate', function( e ) {
e.preventDefault();
jQuery( '.ts-modal-footer p' ).hide();
tyche.plugin_deactivation.events.button_submit( this, data, plugin, true );
} );

modal.on( 'click', '.button-deactivate', function( e ) {
e.preventDefault();
jQuery( '.ts-modal-footer p' ).hide();
Expand All @@ -174,7 +181,7 @@ var tyche = {
} );
},

button_submit: function( $this, $data, plugin ) {
button_submit: function( $this, $data, plugin, skip = false ) {

if ( jQuery( $this ).hasClass( 'disabled' ) || !jQuery( $this ).hasClass( 'allow-deactivate' ) ) {
return;
Expand All @@ -183,17 +190,19 @@ var tyche = {
let modal = jQuery( $this ).parents( `.${plugin}.ts-modal` ),
option = jQuery( 'input[type="radio"]:checked' ),
reason = option.parents( 'li:first' ),
response = reason.find( 'textarea, input[type="text"]' );
response = reason.find( 'textarea, input[type="text"]' ),
reason_id = skip ? 0 : option.val(),
reason_text = skip ? 'Deactivation Reason Skipped' : reason.text().trim();

if ( 0 === option.length ) {
if ( 0 === option.length && ! skip ) {
jQuery( '.ts-modal-footer p' ).css( 'display', 'inline-block' );
return;
}

let data = {
'action': 'tyche_plugin_deactivation_submit_action',
'reason_id': option.val(),
'reason_text': reason.text().trim(),
'reason_id': reason_id,
'reason_text': reason_text,
'reason_info': 0 !== response.length ? response.val().trim() : '',
'plugin_short_name': plugin,
'plugin_name': jQuery( `.${plugin}.ts-slug` ).attr( 'data-plugin' ),
Expand All @@ -210,6 +219,7 @@ var tyche = {
data,
beforeSend: function() {
modal.find( '.button-deactivate' ).addClass( 'disabled' );
modal.find( '.button-skip-deactivate' ).addClass( 'disabled' );
},
complete: function() {
window.location.href = href;
Expand Down Expand Up @@ -254,4 +264,4 @@ var tyche = {
}
}

tyche.init();
tyche.init();

0 comments on commit 277dc7e

Please sign in to comment.