| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class responsible for adding & saving extra settings in the Charitable admin. |
| 4 |
* |
| 5 |
* @package Charitable Square/Classes/Charitable_Square_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.8.7 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Square_Admin' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Square_Admin |
| 21 |
* |
| 22 |
* @since 1.1.0 |
| 23 |
*/ |
| 24 |
class Charitable_Square_Admin { |
| 25 |
|
| 26 |
/** |
| 27 |
* Square Apps URL. |
| 28 |
* |
| 29 |
* @since 1.8.7 |
| 30 |
*/ |
| 31 |
public const SQUARE_APPS_URL = 'https://developer.squareup.com/apps'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Single instance of this class. |
| 35 |
* |
| 36 |
* @since 1.1.0 |
| 37 |
* |
| 38 |
* @var Charitable_Square_Admin |
| 39 |
*/ |
| 40 |
private static $instance = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* Create class object. Private constructor. |
| 44 |
* |
| 45 |
* @since 1.1.0 |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
|
| 49 |
/** |
| 50 |
* When saving Square settings, check for webhook if secret key has changed (when you aren't using Square Connect AM) |
| 51 |
*/ |
| 52 |
add_filter( 'charitable_save_settings', array( $this, 'save_square_settings' ), 10, 3 ); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* When connecting Square Connect, check for webhook if secret key has changed. |
| 58 |
*/ |
| 59 |
// add_action( 'wpcharitable_square_account_connected', array( $this, 'update_webhook_upon_connection' ), 10, 1 ); |
| 60 |
|
| 61 |
add_filter( 'charitable_admin_strings', array( $this, 'javascript_strings' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Enqueue scripts and styles. |
| 66 |
* |
| 67 |
* @since 1.8.7 |
| 68 |
*/ |
| 69 |
public function enqueue_scripts_styles() { |
| 70 |
|
| 71 |
if ( ! charitable_is_admin_screen( 'settings' ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$assets_dir = charitable()->get_path( 'directory', false ) . 'assets/'; |
| 76 |
$min = charitable_get_min_suffix(); |
| 77 |
$version = charitable()->get_version(); |
| 78 |
$dependencies = array(); |
| 79 |
|
| 80 |
wp_register_script( |
| 81 |
'charitable-admin-square-settings', |
| 82 |
$assets_dir . 'js/integrations/square/square-settings.js', |
| 83 |
$dependencies, |
| 84 |
$version, |
| 85 |
false |
| 86 |
); |
| 87 |
|
| 88 |
wp_enqueue_script( 'charitable-admin-square-settings' ); |
| 89 |
|
| 90 |
wp_enqueue_style( |
| 91 |
'charitable-admin-square', |
| 92 |
$assets_dir . 'css/admin/charitable-admin-square' . $min . '.css', |
| 93 |
array(), |
| 94 |
$version |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Create and return the class object. |
| 100 |
* |
| 101 |
* @since 1.1.0 |
| 102 |
* |
| 103 |
* @return Charitable_Square_Admin |
| 104 |
*/ |
| 105 |
public static function get_instance() { |
| 106 |
if ( is_null( self::$instance ) ) { |
| 107 |
self::$instance = new Charitable_Square_Admin(); |
| 108 |
} |
| 109 |
|
| 110 |
return self::$instance; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* When Square settings are saved, maybe run background processes to set hidden settings. |
| 116 |
* |
| 117 |
* @since 1.8.7 |
| 118 |
* |
| 119 |
* @param array $values The submitted values. |
| 120 |
* @param array $new_values The new settings. |
| 121 |
* @param array $old_values The previous settings. |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function save_square_settings( $values, $new_values, $old_values ) { |
| 125 |
|
| 126 |
if ( charitable_is_debug( 'webhook' ) ) { |
| 127 |
error_log( 'save_square_settings'); // phpcs:ignore |
| 128 |
error_log( print_r( $values, true ) ); // phpcs:ignore |
| 129 |
error_log( print_r( $new_values, true ) ); // phpcs:ignore |
| 130 |
error_log( print_r( $old_values, true ) ); // phpcs:ignore |
| 131 |
} |
| 132 |
|
| 133 |
/* Bail early if this is not the Square settings page. */ |
| 134 |
if ( ! array_key_exists( 'gateways_square', $values ) ) { |
| 135 |
return $values; |
| 136 |
} |
| 137 |
|
| 138 |
/* Bail early if Square is not an active gateway */ |
| 139 |
if ( isset( $values['active_gateways'] ) && ! array_key_exists( 'gateways_square', $values['active_gateways'] ) ) { |
| 140 |
return $values; |
| 141 |
} |
| 142 |
|
| 143 |
return $values; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Localize needed strings. |
| 148 |
* |
| 149 |
* @since 1.8.7 |
| 150 |
* |
| 151 |
* @param array $strings JS strings. |
| 152 |
* |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public function javascript_strings( $strings ): array { |
| 156 |
|
| 157 |
$strings = (array) $strings; |
| 158 |
|
| 159 |
$strings['square'] = array( |
| 160 |
'mode_update' => wp_kses( |
| 161 |
__( |
| 162 |
'<p>Switching sandbox/production modes requires Square account reconnection.</p><p>Press the <em>"Connect with Square"</em> button after saving the settings to reconnect.</p>', |
| 163 |
'charitable' |
| 164 |
), |
| 165 |
array( |
| 166 |
'p' => array(), |
| 167 |
'em' => array(), |
| 168 |
) |
| 169 |
), |
| 170 |
'refresh_error' => esc_html__( 'Something went wrong while performing a refresh tokens request.', 'charitable' ), |
| 171 |
'webhook_create_title' => esc_html__( 'Personal Access Token', 'charitable' ), |
| 172 |
'webhook_create_description' => sprintf( |
| 173 |
wp_kses( /* translators: %s - the Square developer dashboard URL. */ |
| 174 |
__( '<p>To receive events, create a webhook route by providing your Personal Access Token, which you can find after registering an app on the <a href="%1$s" target="_blank">Square Developer Dashboard</a>. You can also set it up manually in the Advanced section.</p><p>See <a href="%2$s" target="_blank">our documentation</a> for details.</p>', 'charitable' ), |
| 175 |
array( |
| 176 |
'a' => array( |
| 177 |
'href' => array(), |
| 178 |
'target' => array(), |
| 179 |
), |
| 180 |
'p' => array(), |
| 181 |
) |
| 182 |
), |
| 183 |
esc_url( self::SQUARE_APPS_URL ), |
| 184 |
esc_url( charitable_utm_link( 'https://wpcharitable.com/docs/setting-up-square-webhooks/', 'Settings - Payments', 'Square Webhooks Documentation Modal' ) ) |
| 185 |
), |
| 186 |
|
| 187 |
'webhook_token_placeholder' => esc_html__( 'Personal Access Token', 'charitable' ), |
| 188 |
'token_is_required' => esc_html__( 'Personal Access Token is required to proceed.', 'charitable' ), |
| 189 |
'webhook_urls' => array( |
| 190 |
'rest' => '', // Helpers::get_webhook_url_for_rest(), |
| 191 |
'curl' => '', // Helpers::get_webhook_url_for_curl(), |
| 192 |
), |
| 193 |
); |
| 194 |
|
| 195 |
return $strings; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
endif; |
| 200 |
|