| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class responsible for adding & saving extra settings in the Charitable admin. |
| 4 |
* |
| 5 |
* @package Charitable Stripe/Classes/Charitable_Stripe_Admin |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2021-2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.1.0 |
| 10 |
* @version 1.3.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Stripe_Admin' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Stripe_Admin |
| 22 |
* |
| 23 |
* @since 1.1.0 |
| 24 |
*/ |
| 25 |
class Charitable_Stripe_Admin { |
| 26 |
|
| 27 |
/** |
| 28 |
* Single instance of this class. |
| 29 |
* |
| 30 |
* @since 1.1.0 |
| 31 |
* |
| 32 |
* @var Charitable_Stripe_Admin |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Create class object. Private constructor. |
| 38 |
* |
| 39 |
* @since 1.1.0 |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
|
| 43 |
/** |
| 44 |
* Add a direct link to the Extensions settings page from the plugin row. |
| 45 |
*/ |
| 46 |
if ( class_exists( 'Charitable' ) ) { |
| 47 |
add_filter( 'plugin_action_links_' . plugin_basename( charitable()->get_path() ), [ $this, 'add_plugin_action_links' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Add settings to the Privacy tab. |
| 52 |
*/ |
| 53 |
add_filter( 'charitable_settings_tab_fields_privacy', [ $this, 'add_stripe_privacy_settings' ] ); |
| 54 |
|
| 55 |
/** |
| 56 |
* When saving Stripe settings, check for webhook if secret key has changed (when you aren't using Stripe Connect AM) |
| 57 |
*/ |
| 58 |
add_filter( 'charitable_save_settings', [ $this, 'save_stripe_settings' ], 10, 3 ); |
| 59 |
|
| 60 |
/** |
| 61 |
* When connecting Stripe Connect, check for webhook if secret key has changed. |
| 62 |
*/ |
| 63 |
add_action( 'wpcharitable_stripe_account_connected', [ $this, 'update_webhook_upon_connection' ], 10, 1 ); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Create and return the class object. |
| 69 |
* |
| 70 |
* @since 1.1.0 |
| 71 |
* |
| 72 |
* @return Charitable_Stripe_Admin |
| 73 |
*/ |
| 74 |
public static function get_instance() { |
| 75 |
if ( is_null( self::$instance ) ) { |
| 76 |
self::$instance = new Charitable_Stripe_Admin(); |
| 77 |
} |
| 78 |
|
| 79 |
return self::$instance; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add links to activate |
| 84 |
* |
| 85 |
* @since 1.1.0 |
| 86 |
* |
| 87 |
* @param string[] $links Plugin action links. |
| 88 |
* @return string[] |
| 89 |
*/ |
| 90 |
public function add_plugin_action_links( $links ) { |
| 91 |
if ( Charitable_Gateways::get_instance()->is_active_gateway( 'stripe' ) ) { |
| 92 |
// $links[] = '<a href="' . admin_url( 'admin.php?page=charitable-settings&tab=gateways&group=gateways_stripe&default_gateway=true' ) . '">' . __( 'Settings', 'charitable-stripe' ) . '</a>'; |
| 93 |
} else { |
| 94 |
$activate_url = esc_url( |
| 95 |
add_query_arg( |
| 96 |
[ |
| 97 |
'charitable_action' => 'enable_gateway', |
| 98 |
'gateway_id' => 'stripe', |
| 99 |
'_nonce' => wp_create_nonce( 'gateway' ), |
| 100 |
], |
| 101 |
admin_url( 'admin.php?page=charitable-settings&tab=gateways' ) |
| 102 |
) |
| 103 |
); |
| 104 |
|
| 105 |
$links[] = '<a href="' . $activate_url . '">' . __( 'Activate Stripe Gateway', 'charitable-stripe' ) . '</a>'; |
| 106 |
} |
| 107 |
|
| 108 |
return $links; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Add extra settings to the Privacy tab. |
| 113 |
* |
| 114 |
* @since 1.3.0 |
| 115 |
* |
| 116 |
* @param array $settings The privacy settings. |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function add_stripe_privacy_settings( $settings ) { |
| 120 |
if ( array_key_exists( 'data_retention_fields', $settings ) ) { |
| 121 |
$settings['data_retention_fields']['options']['stripe'] = __( 'Stripe Data', 'charitable-stripe' ); |
| 122 |
} |
| 123 |
|
| 124 |
return $settings; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* When Stripe settings are saved, maybe run background processes to set hidden settings. |
| 129 |
* |
| 130 |
* @since 1.3.0 |
| 131 |
* |
| 132 |
* @param array $values The submitted values. |
| 133 |
* @param array $new_values The new settings. |
| 134 |
* @param array $old_values The previous settings. |
| 135 |
* @return array |
| 136 |
*/ |
| 137 |
public function save_stripe_settings( $values, $new_values, $old_values ) { |
| 138 |
|
| 139 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 140 |
error_log( 'save_stripe_settings'); |
| 141 |
error_log( print_r( $values, true ) ); |
| 142 |
error_log( print_r( $new_values, true ) ); |
| 143 |
error_log( print_r( $old_values, true ) ); |
| 144 |
} |
| 145 |
|
| 146 |
/* Bail early if this is not the Stripe settings page. */ |
| 147 |
if ( ! array_key_exists( 'gateways_stripe', $values ) ) { |
| 148 |
return $values; |
| 149 |
} |
| 150 |
|
| 151 |
/* Bail early if Stripe is not an active gateway */ |
| 152 |
if ( isset( $values['active_gateways'] ) && ! array_key_exists( 'gateways_stripe', $values['active_gateways'] ) ) { |
| 153 |
return $values; |
| 154 |
} |
| 155 |
|
| 156 |
/* Add webhooks unless we're on localhost. */ |
| 157 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 158 |
error_log( 'save_settings'); |
| 159 |
} |
| 160 |
|
| 161 |
// a reminder on the charitable_using_stripe_connect check: |
| 162 |
// the option gets written when the stripe connect in the core plugin (starting in v1.7.0) is connected in gateway settings in the admin. |
| 163 |
// the option is removed when, after the stripe connect is connected, the user clicks on the "disconnect" link is clicked in the settings. |
| 164 |
if ( function_exists( 'charitable_stripe_should_setup_webhooks' ) && charitable_stripe_should_setup_webhooks() && ! charitable_using_stripe_connect() ) { |
| 165 |
if ( defined( 'CHARITABLE_FORCE_WEBHOOKS_WITHOUT_STRIPE_CONNECT' ) && CHARITABLE_FORCE_WEBHOOKS_WITHOUT_STRIPE_CONNECT ) { |
| 166 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 167 |
error_log( 'made it 2'); |
| 168 |
error_log( print_r( $values, true ) ); |
| 169 |
} |
| 170 |
$values = $this->setup_webhooks( $values, $new_values, $old_values ); |
| 171 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 172 |
error_log( print_r( $values, true ) ); |
| 173 |
error_log( print_r( $new_values, true ) ); |
| 174 |
error_log( print_r( $old_values, true ) ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $values; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* When Stripe settings are saved, maybe run background processes to set hidden settings. |
| 184 |
* |
| 185 |
* @since 1.3.0 |
| 186 |
* |
| 187 |
* @param array $values The submitted values. |
| 188 |
* @param array $new_values The new settings. |
| 189 |
* @param array $old_values The previous settings. |
| 190 |
* @return array |
| 191 |
*/ |
| 192 |
public function update_webhook_upon_connection( $account_data ) { |
| 193 |
|
| 194 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 195 |
error_log( 'update_webhook_upon_connection 0'); |
| 196 |
error_log( print_r( charitable_stripe_should_setup_webhooks(), true ) ); |
| 197 |
error_log( print_r( charitable_using_stripe_connect(), true ) ); |
| 198 |
} |
| 199 |
|
| 200 |
// a reminder on the charitable_using_stripe_connect check: |
| 201 |
// the option gets written when the stripe connect in the core plugin (starting in v1.7.0) is connected in gateway settings in the admin. |
| 202 |
// the option is removed when, after the stripe connect is connected, the user clicks on the "disconnect" link is clicked in the settings. |
| 203 |
if ( function_exists( 'charitable_stripe_should_setup_webhooks' ) && function_exists( 'charitable_using_stripe_connect' ) && charitable_stripe_should_setup_webhooks() && charitable_using_stripe_connect() ) { |
| 204 |
// going to "simulate" a save settings so we can use re-use the setup_webhooks function |
| 205 |
$values = $new_values = $old_values = get_option( 'charitable_settings', array() ); |
| 206 |
|
| 207 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 208 |
error_log( 'update_webhook_upon_connection 3'); |
| 209 |
error_log( print_r( $account_data, true ) ); |
| 210 |
error_log( print_r( $values, true ) ); |
| 211 |
error_log( print_r( $new_values, true ) ); |
| 212 |
error_log( print_r( $old_values, true ) ); |
| 213 |
} |
| 214 |
$values = $this->setup_webhooks( $values, $new_values, $old_values ); |
| 215 |
|
| 216 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 217 |
error_log( print_r( $values, true ) ); |
| 218 |
} |
| 219 |
|
| 220 |
// update the settings |
| 221 |
update_option( 'charitable_settings', $values ); |
| 222 |
|
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Set up webhooks after settings are saved. |
| 228 |
* |
| 229 |
* @since 1.4.0 |
| 230 |
* |
| 231 |
* @param array $values The submitted values. |
| 232 |
* @param array $new_values The new settings. |
| 233 |
* @param array $old_values The previous settings. |
| 234 |
* @return array |
| 235 |
*/ |
| 236 |
private function setup_webhooks( $values, $new_values, $old_values ) { |
| 237 |
/* Check whether the stripe_update_hidden_settings upgrade has been completed. */ |
| 238 |
$upgrade_log = get_option( 'charitable_stripe_upgrade_log' ); |
| 239 |
$upgrade_done = is_array( $upgrade_log ) && array_key_exists( 'stripe_update_hidden_settings', $upgrade_log ); |
| 240 |
|
| 241 |
$old_settings = $old_values['gateways_stripe']; |
| 242 |
$new_settings = $values['gateways_stripe']; |
| 243 |
|
| 244 |
$setting_pairs = [ |
| 245 |
'test_secret_key' => true, |
| 246 |
'live_secret_key' => false, |
| 247 |
]; |
| 248 |
|
| 249 |
foreach ( $setting_pairs as $setting_key => $test_mode ) { |
| 250 |
|
| 251 |
$old = isset( $old_settings[ $setting_key ] ) ? trim( $old_settings[ $setting_key ] ) : false; |
| 252 |
$new = trim( $new_settings[ $setting_key ] ); |
| 253 |
|
| 254 |
/* The secret key is unchanged and the upgrade is done, so no need to do anything. */ |
| 255 |
if ( $old == $new && $upgrade_done ) { |
| 256 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 257 |
error_log( 'key unchanged' ); |
| 258 |
error_log( 'old:' ); |
| 259 |
error_log( print_r( $old, true ) ); |
| 260 |
error_log( 'new:' ); |
| 261 |
error_log( print_r( $new, true ) ); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 266 |
error_log( 'old:' ); |
| 267 |
error_log( print_r( $old, true ) ); |
| 268 |
error_log( 'new:' ); |
| 269 |
error_log( print_r( $new, true ) ); |
| 270 |
} |
| 271 |
|
| 272 |
/* If the secret key has changed, deactivate the previously stored webhook. */ |
| 273 |
if ( $old != $new ) { |
| 274 |
error_log( 'old != new' ); |
| 275 |
$webhook_api = new Charitable_Stripe_Webhook_API( $test_mode, $old ); |
| 276 |
$webhook_api->deactivate_webhook(); |
| 277 |
} |
| 278 |
|
| 279 |
/* If the new secret key is blank, set webhook_id to false. */ |
| 280 |
if ( '' == $new && isset( $webhook_api->setting_key ) ) { |
| 281 |
$values['gateways_stripe'][ $webhook_api->setting_key ] = false; |
| 282 |
continue; |
| 283 |
} |
| 284 |
|
| 285 |
/* Finally, if we're still here, add a webhook using the new secret key. */ |
| 286 |
$webhook_api = new Charitable_Stripe_Webhook_API( $test_mode, $new ); |
| 287 |
|
| 288 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 289 |
error_log( 'webhook_api here' ); |
| 290 |
error_log( print_r( $webhook_api, true ) ); |
| 291 |
} |
| 292 |
|
| 293 |
/* First, check if we have a webhook. */ |
| 294 |
$webhook = $webhook_api->get_webhook(); |
| 295 |
|
| 296 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 297 |
error_log( 'webhook here' ); |
| 298 |
error_log( print_r( $webhook, true ) ); |
| 299 |
} |
| 300 |
|
| 301 |
/* We don't have a webhook, so create one. */ |
| 302 |
if ( ! $webhook ) { |
| 303 |
error_log( 'add webhook We do not have a webhook, so create one.' ); |
| 304 |
$webhook_id = $webhook_api->add_webhook(); |
| 305 |
error_log( 'add webhook' ); |
| 306 |
} else { |
| 307 |
/* We have a webhook, but it needs to be updated. */ |
| 308 |
if ( $webhook_api->webhook_needs_update( $webhook ) ) { |
| 309 |
$webhook_api->update_webhook(); |
| 310 |
error_log( 'update webhook' ); |
| 311 |
} |
| 312 |
$webhook_id = $webhook->id; |
| 313 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 314 |
error_log( print_r( $webhook_id, true ) ); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 319 |
error_log( 'final testing data' ); |
| 320 |
error_log( print_r( $webhook_api->setting_key, true ) ); |
| 321 |
error_log( print_r( $webhook_id, true ) ); |
| 322 |
} |
| 323 |
|
| 324 |
$values['gateways_stripe'][ $webhook_api->setting_key ] = $webhook_id; |
| 325 |
|
| 326 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 327 |
error_log( 'values gateways_stripe updated' ); |
| 328 |
error_log( print_r( $values, true ) ); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/* Mark the upgrade as done. */ |
| 333 |
if ( ! $upgrade_done ) { |
| 334 |
if ( ! is_array( $upgrade_log ) ) { |
| 335 |
$upgrade_log = []; |
| 336 |
} |
| 337 |
|
| 338 |
$upgrade_log['stripe_update_hidden_settings'] = [ |
| 339 |
'time' => time(), |
| 340 |
'version' => charitable_stripe()->get_version(), |
| 341 |
]; |
| 342 |
|
| 343 |
update_option( 'charitable_stripe_upgrade_log', $upgrade_log ); |
| 344 |
} |
| 345 |
|
| 346 |
return $values; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
endif; |
| 351 |
|