| 1 |
<?php |
| 2 |
/* Load support for Vipps MobilePay Recurring payments, but only if it has been activated before, or if WooCommerce Subscriptions is loaded. |
| 3 |
Don't load it if the Vipps Mobilepay Recurring plugin is still loaded. Run activation and deactivation hooks if neccessary. */ |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
|
| 10 |
// We will not do activation or deactivation if this is ajax, rest or cron. |
| 11 |
function woo_vipps_ajax_cron_or_rest () { |
| 12 |
if (defined("DOING_AJAX") && DOING_AJAX) return true; |
| 13 |
if (function_exists('wp_is_json_request') && wp_is_json_request()) return true; |
| 14 |
if (function_exists('wp_doing_cron') && wp_doing_cron()) return true; |
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
/* If the standalone plugin has its "deactivate" method called, then we will need to re-activate it. IOK 2024-12-04 */ |
| 19 |
add_action( 'deactivate_plugin', function($plugin, $network_deactivating ) { |
| 20 |
if (basename($plugin) == 'woo-vipps-recurring.php') { |
| 21 |
delete_option('woo_vipps_recurring_payments_activation'); |
| 22 |
} |
| 23 |
},10,2); |
| 24 |
|
| 25 |
|
| 26 |
// Load very early in plugins_loaded, since the Recurring feature will add events on precedence 10 itself. |
| 27 |
add_action('plugins_loaded', function () { |
| 28 |
$woo_exists = function_exists('WC'); |
| 29 |
if (!$woo_exists) return false; |
| 30 |
|
| 31 |
$vipps_recurring_exists = defined('WC_VIPPS_RECURRING_VERSION'); |
| 32 |
/* If the standalone plugin has been loaded, note that it has been activated, but do nothing else */ |
| 33 |
if ($vipps_recurring_exists) { |
| 34 |
update_option('woo_vipps_recurring_payments_activation', WC_VIPPS_RECURRING_VERSION); |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
/* We will load support now if either the plugin has been activated, or if subscriptions exist */ |
| 39 |
/* Plugins like HF Subscriptions also creates a WC_Subscriptions class, so we should use WC_Subscriptions_Plugin */ |
| 40 |
$subscriptions_exist = class_exists( 'WC_Subscriptions_Plugin' ); |
| 41 |
|
| 42 |
$previously_activated = get_option('woo_vipps_recurring_payments_activation'); |
| 43 |
if (!$subscriptions_exist && !$previously_activated) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
// It should now be safe to load the recurring support. |
| 48 |
require_once(dirname(__FILE__) . "/recurring.php"); |
| 49 |
|
| 50 |
// We need to do the old plugin de/activation logic for the standalone plugin here: |
| 51 |
// If we are here and we have not been previously activated, we should call the "activate" hook and note that we have been activated. |
| 52 |
// If we are here and we were previously activated, but subscriptions are not active, we should call the *deactivate* hook and reset the database setting. |
| 53 |
// This must be done fairly late, so we are going to use "wp_loaded". IOK 2024-12-04 |
| 54 |
if (!$previously_activated) { |
| 55 |
if (!woo_vipps_ajax_cron_or_rest ()) { |
| 56 |
add_action('wp_loaded', function () { |
| 57 |
WC_Vipps_Recurring::get_instance()->activate(); |
| 58 |
update_option('woo_vipps_recurring_payments_activation', WC_VIPPS_RECURRING_VERSION); |
| 59 |
}); |
| 60 |
} |
| 61 |
} else if (!$subscriptions_exist) { |
| 62 |
if (!woo_vipps_ajax_cron_or_rest ()) { |
| 63 |
add_action('wp_loaded', function () { |
| 64 |
WC_Vipps_Recurring::get_instance()->deactivate(); |
| 65 |
delete_option('woo_vipps_recurring_payments_activation'); |
| 66 |
}); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
}, 1); |
| 73 |
|
| 74 |
/* Mark the Vipps MobilePay Recurring-plugin as needing update, so that we can inform that it is no longer required IOK 2024-12-20 */ |
| 75 |
add_filter( 'all_plugins', function ( $plugins ) { |
| 76 |
foreach ( $plugins as $plugin=> $plugin_data ) { |
| 77 |
if (basename($plugin) == 'woo-vipps-recurring.php') { |
| 78 |
// Necessary to properly display notice within row. |
| 79 |
$plugins[$plugin]['update'] = 1; |
| 80 |
} |
| 81 |
} |
| 82 |
return $plugins; |
| 83 |
} |
| 84 |
); |
| 85 |
add_action('after_plugin_row', function ($plugin) { |
| 86 |
global $wp_list_table; |
| 87 |
if (!$wp_list_table) return; |
| 88 |
if (basename($plugin) != 'woo-vipps-recurring.php') return; |
| 89 |
$columns_count = $wp_list_table->get_column_count(); |
| 90 |
|
| 91 |
$notice = ""; |
| 92 |
if (is_plugin_active($plugin)) { |
| 93 |
$notice = sprintf(__( 'This plugin can now be <strong>deactivated</strong> because its functionality is now included in <strong>%1$s</strong>. After deactivation, it can not be activated again while that plugin is active, but exactly the same functionality will be provided.', 'woo-vipps' ), __("Pay with Vipps and MobilePay for WooCommerce", 'woo-vipps')); |
| 94 |
} else { |
| 95 |
$notice = sprintf(__( 'This plugin can no longer be activated because its functionality is now included in <strong>%1$s</strong>. It is recommended to <strong>delete</strong> it.', 'woo-vipps' ), __("Pay with Vipps and MobilePay for WooCommerce", 'woo-vipps')); |
| 96 |
} |
| 97 |
echo '<tr class="plugin-update-tr"><td colspan="' . esc_attr( $columns_count ) . '" class="plugin-update"><div class="update-message notice inline notice-error notice-alt"><p>' . wp_kses_post( $notice ) . '</p></div></td></tr>'; |
| 98 |
} |
| 99 |
); |
| 100 |
|