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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Utilities / WebhookUtil.php
woocommerce / src / Internal / Utilities Last commit date
ArrayUtil.php 6 months ago BlocksUtil.php 4 months ago COTMigrationUtil.php 1 year ago DatabaseUtil.php 1 year ago FilesystemUtil.php 6 months ago HtmlSanitizer.php 2 years ago LegacyRestApiStub.php 1 year ago PluginInstaller.php 1 year ago ProductUtil.php 7 months ago Types.php 1 year ago URL.php 1 year ago URLException.php 4 years ago Users.php 3 months ago WebhookUtil.php 1 year ago
WebhookUtil.php
164 lines
1 <?php
2 /**
3 * WebhookUtil class file.
4 */
5
6 namespace Automattic\WooCommerce\Internal\Utilities;
7
8 use WC_Cache_Helper;
9
10 /**
11 * Class with utility methods for dealing with webhooks.
12 */
13 class WebhookUtil {
14
15 /**
16 * Creates a new instance of the class.
17 */
18 public function __construct() {
19 add_action( 'deleted_user', array( $this, 'reassign_webhooks_to_new_user_id' ), 10, 2 );
20 add_action( 'delete_user_form', array( $this, 'maybe_render_user_with_webhooks_warning' ), 10, 2 );
21 }
22
23 /**
24 * Whenever a user is deleted, re-assign their webhooks to the new user.
25 *
26 * If re-assignment isn't selected during deletion, assign the webhooks to user_id 0,
27 * so that an admin can edit and re-save them in order to get them to be assigned to a valid user.
28 *
29 * @param int $old_user_id ID of the deleted user.
30 * @param int|null $new_user_id ID of the user to reassign existing data to, or null if no re-assignment is requested.
31 *
32 * @return void
33 * @since 7.8.0
34 *
35 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
36 */
37 public function reassign_webhooks_to_new_user_id( int $old_user_id, ?int $new_user_id ): void {
38 $webhook_ids = $this->get_webhook_ids_for_user( $old_user_id );
39
40 foreach ( $webhook_ids as $webhook_id ) {
41 $webhook = new \WC_Webhook( $webhook_id );
42 $webhook->set_user_id( $new_user_id ?? 0 );
43 $webhook->save();
44 }
45 }
46
47 /**
48 * When users are about to be deleted show an informative text if they have webhooks assigned.
49 *
50 * @param \WP_User $current_user The current logged in user.
51 * @param array $userids Array with the ids of the users that are about to be deleted.
52 * @return void
53 * @since 7.8.0
54 *
55 * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
56 */
57 public function maybe_render_user_with_webhooks_warning( \WP_User $current_user, array $userids ): void {
58 global $wpdb;
59
60 $at_least_one_user_with_webhooks = false;
61
62 foreach ( $userids as $user_id ) {
63 $webhook_ids = $this->get_webhook_ids_for_user( $user_id );
64 if ( empty( $webhook_ids ) ) {
65 continue;
66 }
67
68 $at_least_one_user_with_webhooks = true;
69
70 $user_data = get_userdata( $user_id );
71 $user_login = false === $user_data ? '' : $user_data->user_login;
72 $webhooks_count = count( $webhook_ids );
73
74 $text = sprintf(
75 /* translators: 1 = user id, 2 = user login, 3 = webhooks count */
76 _nx(
77 'User #%1$s %2$s has created %3$d WooCommerce webhook.',
78 'User #%1$s %2$s has created %3$d WooCommerce webhooks.',
79 $webhooks_count,
80 'user webhook count',
81 'woocommerce'
82 ),
83 $user_id,
84 $user_login,
85 $webhooks_count
86 );
87
88 echo '<p>' . esc_html( $text ) . '</p>';
89 }
90
91 if ( ! $at_least_one_user_with_webhooks ) {
92 return;
93 }
94
95 $webhooks_settings_url = esc_url_raw( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks' ) );
96
97 // This block of code is copied from WordPress' users.php.
98 // phpcs:disable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared
99 $users_have_content = (bool) apply_filters( 'users_have_additional_content', false, $userids );
100 if ( ! $users_have_content ) {
101 if ( $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_author IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
102 $users_have_content = true;
103 } elseif ( $wpdb->get_var( "SELECT link_id FROM {$wpdb->links} WHERE link_owner IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
104 $users_have_content = true;
105 }
106 }
107 // phpcs:enable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared
108
109 if ( $users_have_content ) {
110 $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' );
111 } else {
112 $text = __( 'The affected WooCommerce webhooks will <b>not</b> be deleted and will be attributed to user id 0.<br/>', 'woocommerce' );
113 }
114
115 $text .= sprintf(
116 /* translators: 1 = url of the WooCommerce webhooks settings page */
117 __( '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' ),
118 $webhooks_settings_url
119 );
120
121 echo '<p>' . wp_kses_post( $text ) . '</p>';
122 }
123
124 /**
125 * Get the ids of the webhooks assigned to a given user.
126 *
127 * @param int $user_id User id.
128 * @return int[] Array of webhook ids.
129 */
130 private function get_webhook_ids_for_user( int $user_id ): array {
131 $data_store = \WC_Data_Store::load( 'webhook' );
132 return $data_store->search_webhooks(
133 array(
134 'user_id' => $user_id,
135 )
136 );
137 }
138
139 /**
140 * Gets the count of webhooks that are configured to use the Legacy REST API to compose their payloads.
141 *
142 * @param bool $clear_cache If true, the previously cached value of the count will be discarded if it exists.
143 *
144 * @return int
145 */
146 public function get_legacy_webhooks_count( bool $clear_cache = false ): int {
147 global $wpdb;
148
149 $cache_key = WC_Cache_Helper::get_cache_prefix( 'webhooks' ) . 'legacy_count';
150 if ( $clear_cache ) {
151 wp_cache_delete( $cache_key, 'webhooks' );
152 }
153
154 $count = wp_cache_get( $cache_key, 'webhooks' );
155
156 if ( false === $count ) {
157 $count = absint( $wpdb->get_var( "SELECT count( webhook_id ) FROM {$wpdb->prefix}wc_webhooks WHERE `api_version` < 1;" ) );
158 wp_cache_add( $cache_key, $count, 'webhooks' );
159 }
160
161 return $count;
162 }
163 }
164