ArrayUtil.php
7 months ago
BlocksUtil.php
5 months ago
COTMigrationUtil.php
1 year ago
DatabaseUtil.php
1 year ago
FilesystemUtil.php
3 months ago
HtmlSanitizer.php
2 years ago
LegacyRestApiStub.php
4 weeks ago
PluginInstaller.php
1 year ago
ProductUtil.php
9 months ago
Types.php
1 year ago
URL.php
1 year ago
URLException.php
4 years ago
Users.php
4 months ago
WebhookUtil.php
4 weeks ago
WebhookUtil.php
137 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WebhookUtil class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Utilities; |
| 7 | |
| 8 | /** |
| 9 | * Class with utility methods for dealing with webhooks. |
| 10 | */ |
| 11 | class WebhookUtil { |
| 12 | |
| 13 | /** |
| 14 | * Creates a new instance of the class. |
| 15 | */ |
| 16 | public function __construct() { |
| 17 | add_action( 'deleted_user', array( $this, 'reassign_webhooks_to_new_user_id' ), 10, 2 ); |
| 18 | add_action( 'delete_user_form', array( $this, 'maybe_render_user_with_webhooks_warning' ), 10, 2 ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Whenever a user is deleted, re-assign their webhooks to the new user. |
| 23 | * |
| 24 | * If re-assignment isn't selected during deletion, assign the webhooks to user_id 0, |
| 25 | * so that an admin can edit and re-save them in order to get them to be assigned to a valid user. |
| 26 | * |
| 27 | * @param int $old_user_id ID of the deleted user. |
| 28 | * @param int|null $new_user_id ID of the user to reassign existing data to, or null if no re-assignment is requested. |
| 29 | * |
| 30 | * @return void |
| 31 | * @since 7.8.0 |
| 32 | * |
| 33 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 34 | */ |
| 35 | public function reassign_webhooks_to_new_user_id( int $old_user_id, ?int $new_user_id ): void { |
| 36 | $webhook_ids = $this->get_webhook_ids_for_user( $old_user_id ); |
| 37 | |
| 38 | foreach ( $webhook_ids as $webhook_id ) { |
| 39 | $webhook = new \WC_Webhook( $webhook_id ); |
| 40 | $webhook->set_user_id( $new_user_id ?? 0 ); |
| 41 | $webhook->save(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * When users are about to be deleted show an informative text if they have webhooks assigned. |
| 47 | * |
| 48 | * @param \WP_User $current_user The current logged in user. |
| 49 | * @param array $userids Array with the ids of the users that are about to be deleted. |
| 50 | * @return void |
| 51 | * @since 7.8.0 |
| 52 | * |
| 53 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 54 | */ |
| 55 | public function maybe_render_user_with_webhooks_warning( \WP_User $current_user, array $userids ): void { |
| 56 | global $wpdb; |
| 57 | |
| 58 | $at_least_one_user_with_webhooks = false; |
| 59 | |
| 60 | foreach ( $userids as $user_id ) { |
| 61 | $webhook_ids = $this->get_webhook_ids_for_user( $user_id ); |
| 62 | if ( empty( $webhook_ids ) ) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | $at_least_one_user_with_webhooks = true; |
| 67 | |
| 68 | $user_data = get_userdata( $user_id ); |
| 69 | $user_login = false === $user_data ? '' : $user_data->user_login; |
| 70 | $webhooks_count = count( $webhook_ids ); |
| 71 | |
| 72 | $text = sprintf( |
| 73 | /* translators: 1 = user id, 2 = user login, 3 = webhooks count */ |
| 74 | _nx( |
| 75 | 'User #%1$s %2$s has created %3$d WooCommerce webhook.', |
| 76 | 'User #%1$s %2$s has created %3$d WooCommerce webhooks.', |
| 77 | $webhooks_count, |
| 78 | 'user webhook count', |
| 79 | 'woocommerce' |
| 80 | ), |
| 81 | $user_id, |
| 82 | $user_login, |
| 83 | $webhooks_count |
| 84 | ); |
| 85 | |
| 86 | echo '<p>' . esc_html( $text ) . '</p>'; |
| 87 | } |
| 88 | |
| 89 | if ( ! $at_least_one_user_with_webhooks ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $webhooks_settings_url = esc_url_raw( admin_url( 'admin.php?page=wc-settings&tab=advanced§ion=webhooks' ) ); |
| 94 | |
| 95 | // This block of code is copied from WordPress' users.php. |
| 96 | // phpcs:disable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared |
| 97 | $users_have_content = (bool) apply_filters( 'users_have_additional_content', false, $userids ); |
| 98 | if ( ! $users_have_content ) { |
| 99 | if ( $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_author IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) { |
| 100 | $users_have_content = true; |
| 101 | } elseif ( $wpdb->get_var( "SELECT link_id FROM {$wpdb->links} WHERE link_owner IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) { |
| 102 | $users_have_content = true; |
| 103 | } |
| 104 | } |
| 105 | // phpcs:enable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared |
| 106 | |
| 107 | if ( $users_have_content ) { |
| 108 | $text = __( 'If the "Delete all content" option is selected, the affected WooCommerce webhooks will <b>not</b> be deleted and will be attributed to user id 0.<br/>', 'woocommerce' ); |
| 109 | } else { |
| 110 | $text = __( 'The affected WooCommerce webhooks will <b>not</b> be deleted and will be attributed to user id 0.<br/>', 'woocommerce' ); |
| 111 | } |
| 112 | |
| 113 | $text .= sprintf( |
| 114 | /* translators: 1 = url of the WooCommerce webhooks settings page */ |
| 115 | __( 'After that they can be reassigned to the logged-in user by going to the <a href="%1$s">WooCommerce webhooks settings page</a> and re-saving them.', 'woocommerce' ), |
| 116 | $webhooks_settings_url |
| 117 | ); |
| 118 | |
| 119 | echo '<p>' . wp_kses_post( $text ) . '</p>'; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the ids of the webhooks assigned to a given user. |
| 124 | * |
| 125 | * @param int $user_id User id. |
| 126 | * @return int[] Array of webhook ids. |
| 127 | */ |
| 128 | private function get_webhook_ids_for_user( int $user_id ): array { |
| 129 | $data_store = \WC_Data_Store::load( 'webhook' ); |
| 130 | return $data_store->search_webhooks( |
| 131 | array( |
| 132 | 'user_id' => $user_id, |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 |