Skip to content

Commit

Permalink
Merge pull request #61 from TycheSoftwares/deactivation-changes
Browse files Browse the repository at this point in the history
deactivation changes and version updated
  • Loading branch information
gaminikhil authored Oct 22, 2024
2 parents 8fcc337 + 542a905 commit 5d93ee4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 43 deletions.
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 All @@ -42,6 +49,13 @@ class Tyche_Plugin_Deactivation {
*/
private $plugin_name = '';

/**
* Plugin Locale.
*
* @var string $plugin_locale
*/
private $plugin_locale = '';

/**
* Plugin Short Name.
*
Expand Down Expand Up @@ -93,15 +107,16 @@ public function init_vars( $options ) {
return false;
}

if ( ! isset( $options['plugin_name'] ) || ! isset( $options['plugin_base'] ) || ! isset( $options['script_file'] ) || ! isset( $options['plugin_short_name'] ) || ! isset( $options['version'] ) ) {
if ( ! isset( $options['plugin_name'] ) || ! isset( $options['plugin_base'] ) || ! isset( $options['script_file'] ) || ! isset( $options['plugin_short_name'] ) || ! isset( $options['version'] ) || ! isset( $options['plugin_locale'] ) ) {
return false;
}

$this->plugin_name = $options['plugin_name'];
$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 All @@ -117,7 +132,7 @@ public function init_vars( $options ) {
public function plugin_action_links( $links ) {

if ( isset( $links['deactivate'] ) ) {
$links['deactivate'] .= '<i class="' . $this->plugin_short_name . ' ts-slug" data-slug="' . $this->plugin_base . '"></i>';
$links['deactivate'] .= '<i class="' . $this->plugin_short_name . ' ts-slug" data-slug="' . $this->plugin_base . '" data-plugin="' . $this->plugin_name . '"></i>';
}

return $links;
Expand All @@ -129,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 All @@ -161,9 +180,10 @@ public function enqueue_scripts() {
'tyche_plugin_deactivation_' . $this->plugin_short_name,
'tyche_plugin_deactivation_' . $this->plugin_short_name . '_js',
array(
'deactivation_data' => $data,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tyche_plugin_deactivation_submit_action' ),
'deactivation_data' => $data,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tyche_plugin_deactivation_submit_action' ),
'deactivation_req_msg' => __( 'Please select a reason for deactivation!', $this->plugin_locale ),
)
);

Expand All @@ -177,8 +197,14 @@ public function enqueue_scripts() {
*/
public function tyche_plugin_deactivation_submit_action() {

if ( ! wp_verify_nonce( $_POST['nonce'], 'tyche_plugin_deactivation_submit_action' ) || ! isset( $_POST['reason_id'] ) || ! isset( $_POST['reason_text'] ) ) { // phpcs:ignore
if ( ! wp_verify_nonce( $_POST['nonce'], 'tyche_plugin_deactivation_submit_action' ) || ! isset( $_POST['reason_id'] ) || ! isset( $_POST['reason_text'] ) || ! isset( $_POST['plugin_short_name'] ) || ! isset( $_POST['plugin_name'] ) ) { // phpcs:ignore
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(
Expand All @@ -193,10 +219,11 @@ public function tyche_plugin_deactivation_submit_action() {
'body' => wp_json_encode(
array(
'action' => 'plugin-deactivation',
'plugin_slug' => $this->plugin_short_name,
'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' ) ),
'plugin_name' => $this->plugin_name,
'plugin_name' => isset( $_POST['plugin_name'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_name'] ) ) : '',
'reason_id' => isset( $_POST['reason_id'] ) ? sanitize_text_field( wp_unslash( $_POST['reason_id'] ) ) : '',
'reason_text' => isset( $_POST['reason_text'] ) ? sanitize_text_field( wp_unslash( $_POST['reason_text'] ) ) : '',
'reason_info' => isset( $_POST['reason_info'] ) ? sanitize_text_field( wp_unslash( $_POST['reason_info'] ) ) : '',
Expand Down
25 changes: 18 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.2', // version of tyche constructor.
version: '1.2', // version of tyche constructor.
constructor: tyche,
extend: function() {
let target = arguments[ 0 ] || {},
Expand Down Expand Up @@ -43,6 +43,7 @@ var tyche = {
}
}
},
wcap: {}, // Abandoned Cart Pro for WooCommerce.
orddd: {}, // Order Delivery Date Pro for WooCommerce.
bkap: {}, // Booking & Appointment Plugin for WooCommerce.
ac: {}, // Abandoned Cart Pro for WooCommerce.
Expand Down Expand Up @@ -134,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 @@ -156,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 @@ -173,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 @@ -182,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 @@ -209,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 @@ -253,4 +264,4 @@ var tyche = {
}
}

tyche.init();
tyche.init();
49 changes: 27 additions & 22 deletions woocommerce-call-for-price.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,29 +151,34 @@ private function includes() {
}
require_once 'includes/class-wc-call-for-price.php';
$cfp_plugin_url = plugins_url() . '/woocommerce-call-for-price';

// plugin deactivation.
require_once 'includes/component/plugin-deactivation/class-tyche-plugin-deactivation.php';
new Tyche_Plugin_Deactivation(
array(
'plugin_name' => 'Call for Price for WooCommerce',
'plugin_base' => 'woocommerce-call-for-price/woocommerce-call-for-price.php',
'script_file' => $cfp_plugin_url . '/includes/js/plugin-deactivation.js',
'plugin_short_name' => 'cfp_lite',
'version' => $this->version,
'plugin_locale' => 'woocommerce-call-for-price',
)
);
require_once 'includes/class-cfp-lite-data-tracking.php';
require_once 'includes/component/plugin-tracking/class-tyche-plugin-tracking.php';
new Tyche_Plugin_Tracking(
array(
'plugin_name' => 'Call for Price for WooCommerce',
'plugin_locale' => 'woocommerce-call-for-price',
'plugin_short_name' => 'cfp_lite',
'version' => $this->version,
'blog_link' => 'https://www.tychesoftwares.com/docs/woocommerce-call-for-price/call-for-price-usage-tracking/',
)
);
if ( is_admin() ) {
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 'includes/component/plugin-deactivation/class-tyche-plugin-deactivation.php';
new Tyche_Plugin_Deactivation(
array(
'plugin_name' => 'Call for Price for WooCommerce',
'plugin_base' => 'woocommerce-call-for-price/woocommerce-call-for-price.php',
'script_file' => $cfp_plugin_url . '/includes/js/plugin-deactivation.js',
'plugin_short_name' => 'cfp_lite',
'version' => $this->version,
'plugin_locale' => 'woocommerce-call-for-price',
)
);
}
require_once 'includes/class-cfp-lite-data-tracking.php';
require_once 'includes/component/plugin-tracking/class-tyche-plugin-tracking.php';
new Tyche_Plugin_Tracking(
array(
'plugin_name' => 'Call for Price for WooCommerce',
'plugin_locale' => 'woocommerce-call-for-price',
'plugin_short_name' => 'cfp_lite',
'version' => $this->version,
'blog_link' => 'https://www.tychesoftwares.com/docs/woocommerce-call-for-price/call-for-price-usage-tracking/',
)
);
}
}

/**
Expand Down

0 comments on commit 5d93ee4

Please sign in to comment.