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
8 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§ion=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 |