woocommerce-square
/
includes
/
Framework
/
PaymentGateway
/
Admin
/
Payment_Gateway_Admin_User_Handler.php
views
1 month ago
Payment_Gateway_Admin_Order.php
9 months ago
Payment_Gateway_Admin_Payment_Token_Editor.php
2 years ago
Payment_Gateway_Admin_User_Handler.php
1 year ago
Payment_Gateway_Admin_User_Handler.php
406 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Payment Gateway Framework |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 01 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework\PaymentGateway\Admin; |
| 22 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway; |
| 23 | use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway_Plugin; |
| 24 | |
| 25 | defined( 'ABSPATH' ) or exit; |
| 26 | |
| 27 | /** |
| 28 | * Handle the admin user profile settings. |
| 29 | * |
| 30 | * @since 3.0.0 |
| 31 | */ |
| 32 | class Payment_Gateway_Admin_User_Handler { |
| 33 | |
| 34 | /** @var \Payment_Gateway_Plugin the plugin instance **/ |
| 35 | protected $plugin; |
| 36 | |
| 37 | /** @var array the token editor for each gateway **/ |
| 38 | protected $token_editors = array(); |
| 39 | |
| 40 | /** |
| 41 | * Construct the user handler. |
| 42 | * |
| 43 | * @since 3.0.0 |
| 44 | * @param \Payment_Gateway_Plugin The plugin instance |
| 45 | */ |
| 46 | public function __construct( Payment_Gateway_Plugin $plugin ) { |
| 47 | |
| 48 | $this->plugin = $plugin; |
| 49 | |
| 50 | // Set up a token editor for each gateway |
| 51 | add_action( 'admin_init', array( $this, 'init_token_editors' ) ); |
| 52 | |
| 53 | // Add the settings section |
| 54 | add_action( 'show_user_profile', array( $this, 'add_profile_section' ) ); |
| 55 | add_action( 'edit_user_profile', array( $this, 'add_profile_section' ) ); |
| 56 | |
| 57 | // Save the settings |
| 58 | add_action( 'personal_options_update', array( $this, 'save_profile_fields' ) ); |
| 59 | add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) ); |
| 60 | |
| 61 | // Display the token editor markup inside the profile section |
| 62 | add_action( 'wc_payment_gateway_square_user_profile', array( $this, 'display_token_editors' ) ); |
| 63 | |
| 64 | // Display the customer ID field markup inside the profile section |
| 65 | add_action( 'wc_payment_gateway_square_user_profile', array( $this, 'display_customer_id_fields' ) ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Set up a token editor for each gateway. |
| 71 | * |
| 72 | * @since 3.0.0 |
| 73 | */ |
| 74 | public function init_token_editors() { |
| 75 | |
| 76 | foreach ( $this->get_tokenized_gateways() as $gateway ) { |
| 77 | |
| 78 | if ( ! $gateway->supports_token_editor() ) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | $this->token_editors[ $gateway->get_id() ] = $gateway->get_payment_tokens_handler()->get_token_editor(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /** |
| 88 | * Display the customer profile settings markup. |
| 89 | * |
| 90 | * @since 3.0.0 |
| 91 | * @param \WP_User $user The user object |
| 92 | */ |
| 93 | public function add_profile_section( $user ) { |
| 94 | |
| 95 | if ( ! $this->is_supported() || ! current_user_can( 'manage_woocommerce' ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $user_id = $user->ID; |
| 100 | $plugin_id = 'square'; |
| 101 | $section_title = $this->get_title(); |
| 102 | $section_description = $this->get_description(); |
| 103 | |
| 104 | include( $this->get_plugin()->get_payment_gateway_framework_path() . '/Admin/views/html-user-profile-section.php' ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Display the token editor markup. |
| 110 | * |
| 111 | * @since 3.0.0 |
| 112 | * @param \WP_User $user The user object |
| 113 | */ |
| 114 | public function display_token_editors( $user ) { |
| 115 | |
| 116 | foreach ( $this->get_token_editors() as $gateway_id => $editor ) { |
| 117 | |
| 118 | $gateway = $this->get_plugin()->get_gateway( $gateway_id ); |
| 119 | |
| 120 | // if the gateway supports a customer ID but none is saved, don't display the token tables |
| 121 | if ( $gateway && $gateway->supports_customer_id() && ! $gateway->get_customer_id( $user->ID, array( 'autocreate' => false ) ) ) { |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | $editor->display( $user->ID ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Display the customer ID field(s). |
| 132 | * |
| 133 | * @since 3.0.0 |
| 134 | * @param \WP_User $user the user object |
| 135 | */ |
| 136 | public function display_customer_id_fields( $user ) { |
| 137 | |
| 138 | foreach( $this->get_customer_id_fields( $user->ID ) as $field ) { |
| 139 | |
| 140 | $label = $field['label']; |
| 141 | $name = $field['name']; |
| 142 | $value = $field['value']; |
| 143 | |
| 144 | include( $this->get_plugin()->get_payment_gateway_framework_path() . '/Admin/views/html-user-profile-field-customer-id.php' ); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Save the user profile section fields. |
| 151 | * |
| 152 | * @since 3.0.0 |
| 153 | * @param int $user_id the user ID |
| 154 | */ |
| 155 | public function save_profile_fields( $user_id ) { |
| 156 | |
| 157 | if ( ! $this->is_supported() || ! current_user_can( 'manage_woocommerce' ) ) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Save the token data from each token editor |
| 162 | $this->save_tokens( $user_id ); |
| 163 | |
| 164 | // Save the customer IDs |
| 165 | $this->save_customer_ids( $user_id ); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Save the token data from each token editor. |
| 171 | * |
| 172 | * @since 3.0.0 |
| 173 | * @param int $user_id the user ID |
| 174 | */ |
| 175 | protected function save_tokens( $user_id ) { |
| 176 | |
| 177 | foreach ( $this->get_token_editors() as $gateway_id => $editor ) { |
| 178 | $editor->save( $user_id ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Save the customer IDs. |
| 185 | * |
| 186 | * @since 3.0.0 |
| 187 | * @param int $user_id the user ID |
| 188 | */ |
| 189 | protected function save_customer_ids( $user_id ) { |
| 190 | |
| 191 | foreach ( $this->get_tokenized_gateways() as $gateway ) { |
| 192 | |
| 193 | if ( ! $gateway->supports_customer_id() ) { |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification is handled by WordPress |
| 198 | if ( isset( $_POST[ $gateway->get_customer_id_user_meta_name() ] ) ) { |
| 199 | // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verification is handled by WordPress |
| 200 | $gateway_customer_id = sanitize_text_field( wp_unslash( $_POST[ $gateway->get_customer_id_user_meta_name() ] ) ); |
| 201 | $gateway->update_customer_id( $user_id, trim( $gateway_customer_id ) ); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** Getter methods ******************************************************/ |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Get the token editor section title. |
| 212 | * |
| 213 | * @since 3.0.0 |
| 214 | * @return string |
| 215 | */ |
| 216 | protected function get_title() { |
| 217 | |
| 218 | $plugin_title = trim( str_replace( 'WooCommerce', '', $this->get_plugin()->get_plugin_name() ) ); |
| 219 | |
| 220 | /* translators: %s: plugin title */ |
| 221 | $title = sprintf( esc_html__( '%s Payment Tokens', 'woocommerce-square' ), $plugin_title ); |
| 222 | |
| 223 | /** |
| 224 | * Filter the admin token editor title. |
| 225 | * |
| 226 | * @since 3.0.0 |
| 227 | * @param string $title The section title |
| 228 | * @param \Payment_Gateway_Plugin $plugin The gateway plugin instance |
| 229 | */ |
| 230 | return apply_filters( 'wc_payment_gateway_admin_user_profile_title', $title, $this->get_plugin() ); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * Get the token editor section description. |
| 236 | * |
| 237 | * @since 3.0.0 |
| 238 | * @return string |
| 239 | */ |
| 240 | protected function get_description() { |
| 241 | |
| 242 | /** |
| 243 | * Filter the admin token editor description. |
| 244 | * |
| 245 | * @since 3.0.0 |
| 246 | * @param string $description The section description |
| 247 | * @param \Payment_Gateway_Plugin $plugin The gateway plugin instance |
| 248 | */ |
| 249 | return apply_filters( 'wc_payment_gateway_admin_user_profile_description', '', $this->get_plugin() ); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | /** |
| 254 | * Get the token editor objects. |
| 255 | * |
| 256 | * @since 3.0.0 |
| 257 | * @return array |
| 258 | */ |
| 259 | protected function get_token_editors() { |
| 260 | return $this->token_editors; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * Get the customer ID fields for the plugin's gateways. |
| 266 | * |
| 267 | * In most cases, this will be a single field unless the plugin has multiple gateways and they |
| 268 | * are set to different environments. |
| 269 | * |
| 270 | * @since 3.0.0 |
| 271 | * @param int $user_id the user ID |
| 272 | * @return array { |
| 273 | * The fields data |
| 274 | * |
| 275 | * @type string $label the field label |
| 276 | * @type string $name the input name |
| 277 | * @type string $value the input value |
| 278 | * } |
| 279 | */ |
| 280 | protected function get_customer_id_fields( $user_id ) { |
| 281 | |
| 282 | $unique_meta_key = ''; |
| 283 | |
| 284 | $fields = array(); |
| 285 | |
| 286 | foreach ( $this->get_tokenized_gateways() as $gateway ) { |
| 287 | |
| 288 | if ( ! $gateway->supports_customer_id() ) { |
| 289 | continue; |
| 290 | } |
| 291 | |
| 292 | $meta_key = $gateway->get_customer_id_user_meta_name(); |
| 293 | |
| 294 | // If a field with this meta key has already been set, skip this gateway |
| 295 | if ( $meta_key === $unique_meta_key ) { |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | $label = esc_html__( 'Customer ID', 'woocommerce-square' ); |
| 300 | |
| 301 | // If the plugin has multiple gateways configured for multiple environments, append the environment name to keep things straight |
| 302 | /* translators: %s: environment name */ |
| 303 | $label .= ( $this->has_multiple_environments() ) ? ' ' . sprintf( esc_html__( '(%s)', 'woocommerce-square' ), $gateway->get_environment_name() ) : ''; |
| 304 | |
| 305 | $fields[] = array( |
| 306 | 'label' => $label, |
| 307 | 'name' => $meta_key, |
| 308 | 'value' => $gateway->get_customer_id( $user_id, array( |
| 309 | 'autocreate' => false, |
| 310 | ) ), |
| 311 | ); |
| 312 | |
| 313 | $unique_meta_key = $meta_key; |
| 314 | } |
| 315 | |
| 316 | return $fields; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | /** |
| 321 | * Get the unique environments between the plugin's gateways. |
| 322 | * |
| 323 | * @since 3.0.0 |
| 324 | * @return array the environments in the format `$environment_id => $environment_name` |
| 325 | */ |
| 326 | protected function get_unique_environments() { |
| 327 | |
| 328 | $environments = array(); |
| 329 | |
| 330 | foreach ( $this->get_tokenized_gateways() as $gateway ) { |
| 331 | $environments[ $gateway->get_environment() ] = $gateway->get_environment_name(); |
| 332 | } |
| 333 | |
| 334 | $environments = array_unique( $environments ); |
| 335 | |
| 336 | return $environments; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * Get the gateways that support tokenization and are enabled. |
| 342 | * |
| 343 | * @since 3.0.0 |
| 344 | * @return Payment_Gateway[] |
| 345 | */ |
| 346 | protected function get_tokenized_gateways() { |
| 347 | |
| 348 | $gateways = array(); |
| 349 | |
| 350 | foreach ( $this->get_plugin()->get_gateways() as $gateway ) { |
| 351 | |
| 352 | if ( $gateway->is_enabled() && $gateway->supports_tokenization() && ( $gateway->supports_token_editor() || $gateway->supports_customer_id() ) ) { |
| 353 | $gateways[] = $gateway; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return $gateways; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /** Conditional methods ******************************************************/ |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * Determine if the user profile section is supported by at least one gateway. |
| 366 | * |
| 367 | * @since 3.0.0 |
| 368 | * @return bool |
| 369 | */ |
| 370 | protected function is_supported() { |
| 371 | |
| 372 | $gateways = $this->get_tokenized_gateways(); |
| 373 | |
| 374 | /** |
| 375 | * Filter whether the user profile section should be displayed for this gateway plugin. |
| 376 | * |
| 377 | * @since 3.0.0 |
| 378 | * @param bool $display |
| 379 | * @param \Payment_Gateway_Plugin $plugin the gateway plugin instance |
| 380 | */ |
| 381 | return apply_filters( 'wc_payment_gateway_square_display_user_profile', ! empty( $gateways ), $this->get_plugin() ); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | /** |
| 386 | * Determine if the plugin has varying environments between its gateways. |
| 387 | * |
| 388 | * @since 3.0.0 |
| 389 | * @return bool |
| 390 | */ |
| 391 | public function has_multiple_environments() { |
| 392 | return 1 < count( $this->get_unique_environments() ); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** |
| 397 | * Get the plugin instance. |
| 398 | * |
| 399 | * @since 3.0.0 |
| 400 | * @return \Payment_Gateway_Plugin the plugin instance |
| 401 | */ |
| 402 | protected function get_plugin() { |
| 403 | return $this->plugin; |
| 404 | } |
| 405 | } |
| 406 |