Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal/Utilitie...
File: WebhookUtil.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WebhookUtil class file.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Internal\Utilities;
[5] Fix | Delete
[6] Fix | Delete
use WC_Cache_Helper;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Class with utility methods for dealing with webhooks.
[10] Fix | Delete
*/
[11] Fix | Delete
class WebhookUtil {
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Creates a new instance of the class.
[15] Fix | Delete
*/
[16] Fix | Delete
public function __construct() {
[17] Fix | Delete
add_action( 'deleted_user', array( $this, 'reassign_webhooks_to_new_user_id' ), 10, 2 );
[18] Fix | Delete
add_action( 'delete_user_form', array( $this, 'maybe_render_user_with_webhooks_warning' ), 10, 2 );
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Whenever a user is deleted, re-assign their webhooks to the new user.
[23] Fix | Delete
*
[24] Fix | Delete
* If re-assignment isn't selected during deletion, assign the webhooks to user_id 0,
[25] Fix | Delete
* so that an admin can edit and re-save them in order to get them to be assigned to a valid user.
[26] Fix | Delete
*
[27] Fix | Delete
* @param int $old_user_id ID of the deleted user.
[28] Fix | Delete
* @param int|null $new_user_id ID of the user to reassign existing data to, or null if no re-assignment is requested.
[29] Fix | Delete
*
[30] Fix | Delete
* @return void
[31] Fix | Delete
* @since 7.8.0
[32] Fix | Delete
*
[33] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[34] Fix | Delete
*/
[35] Fix | Delete
public function reassign_webhooks_to_new_user_id( int $old_user_id, ?int $new_user_id ): void {
[36] Fix | Delete
$webhook_ids = $this->get_webhook_ids_for_user( $old_user_id );
[37] Fix | Delete
[38] Fix | Delete
foreach ( $webhook_ids as $webhook_id ) {
[39] Fix | Delete
$webhook = new \WC_Webhook( $webhook_id );
[40] Fix | Delete
$webhook->set_user_id( $new_user_id ?? 0 );
[41] Fix | Delete
$webhook->save();
[42] Fix | Delete
}
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
/**
[46] Fix | Delete
* When users are about to be deleted show an informative text if they have webhooks assigned.
[47] Fix | Delete
*
[48] Fix | Delete
* @param \WP_User $current_user The current logged in user.
[49] Fix | Delete
* @param array $userids Array with the ids of the users that are about to be deleted.
[50] Fix | Delete
* @return void
[51] Fix | Delete
* @since 7.8.0
[52] Fix | Delete
*
[53] Fix | Delete
* @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed.
[54] Fix | Delete
*/
[55] Fix | Delete
public function maybe_render_user_with_webhooks_warning( \WP_User $current_user, array $userids ): void {
[56] Fix | Delete
global $wpdb;
[57] Fix | Delete
[58] Fix | Delete
$at_least_one_user_with_webhooks = false;
[59] Fix | Delete
[60] Fix | Delete
foreach ( $userids as $user_id ) {
[61] Fix | Delete
$webhook_ids = $this->get_webhook_ids_for_user( $user_id );
[62] Fix | Delete
if ( empty( $webhook_ids ) ) {
[63] Fix | Delete
continue;
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
$at_least_one_user_with_webhooks = true;
[67] Fix | Delete
[68] Fix | Delete
$user_data = get_userdata( $user_id );
[69] Fix | Delete
$user_login = false === $user_data ? '' : $user_data->user_login;
[70] Fix | Delete
$webhooks_count = count( $webhook_ids );
[71] Fix | Delete
[72] Fix | Delete
$text = sprintf(
[73] Fix | Delete
/* translators: 1 = user id, 2 = user login, 3 = webhooks count */
[74] Fix | Delete
_nx(
[75] Fix | Delete
'User #%1$s %2$s has created %3$d WooCommerce webhook.',
[76] Fix | Delete
'User #%1$s %2$s has created %3$d WooCommerce webhooks.',
[77] Fix | Delete
$webhooks_count,
[78] Fix | Delete
'user webhook count',
[79] Fix | Delete
'woocommerce'
[80] Fix | Delete
),
[81] Fix | Delete
$user_id,
[82] Fix | Delete
$user_login,
[83] Fix | Delete
$webhooks_count
[84] Fix | Delete
);
[85] Fix | Delete
[86] Fix | Delete
echo '<p>' . esc_html( $text ) . '</p>';
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
if ( ! $at_least_one_user_with_webhooks ) {
[90] Fix | Delete
return;
[91] Fix | Delete
}
[92] Fix | Delete
[93] Fix | Delete
$webhooks_settings_url = esc_url_raw( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks' ) );
[94] Fix | Delete
[95] Fix | Delete
// This block of code is copied from WordPress' users.php.
[96] Fix | Delete
// phpcs:disable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared
[97] Fix | Delete
$users_have_content = (bool) apply_filters( 'users_have_additional_content', false, $userids );
[98] Fix | Delete
if ( ! $users_have_content ) {
[99] Fix | Delete
if ( $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_author IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
[100] Fix | Delete
$users_have_content = true;
[101] Fix | Delete
} elseif ( $wpdb->get_var( "SELECT link_id FROM {$wpdb->links} WHERE link_owner IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
[102] Fix | Delete
$users_have_content = true;
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
// phpcs:enable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared
[106] Fix | Delete
[107] Fix | Delete
if ( $users_have_content ) {
[108] Fix | Delete
$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] Fix | Delete
} else {
[110] Fix | Delete
$text = __( 'The affected WooCommerce webhooks will <b>not</b> be deleted and will be attributed to user id 0.<br/>', 'woocommerce' );
[111] Fix | Delete
}
[112] Fix | Delete
[113] Fix | Delete
$text .= sprintf(
[114] Fix | Delete
/* translators: 1 = url of the WooCommerce webhooks settings page */
[115] Fix | Delete
__( '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] Fix | Delete
$webhooks_settings_url
[117] Fix | Delete
);
[118] Fix | Delete
[119] Fix | Delete
echo '<p>' . wp_kses_post( $text ) . '</p>';
[120] Fix | Delete
}
[121] Fix | Delete
[122] Fix | Delete
/**
[123] Fix | Delete
* Get the ids of the webhooks assigned to a given user.
[124] Fix | Delete
*
[125] Fix | Delete
* @param int $user_id User id.
[126] Fix | Delete
* @return int[] Array of webhook ids.
[127] Fix | Delete
*/
[128] Fix | Delete
private function get_webhook_ids_for_user( int $user_id ): array {
[129] Fix | Delete
$data_store = \WC_Data_Store::load( 'webhook' );
[130] Fix | Delete
return $data_store->search_webhooks(
[131] Fix | Delete
array(
[132] Fix | Delete
'user_id' => $user_id,
[133] Fix | Delete
)
[134] Fix | Delete
);
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/**
[138] Fix | Delete
* Gets the count of webhooks that are configured to use the Legacy REST API to compose their payloads.
[139] Fix | Delete
*
[140] Fix | Delete
* @param bool $clear_cache If true, the previously cached value of the count will be discarded if it exists.
[141] Fix | Delete
*
[142] Fix | Delete
* @return int
[143] Fix | Delete
*/
[144] Fix | Delete
public function get_legacy_webhooks_count( bool $clear_cache = false ): int {
[145] Fix | Delete
global $wpdb;
[146] Fix | Delete
[147] Fix | Delete
$cache_key = WC_Cache_Helper::get_cache_prefix( 'webhooks' ) . 'legacy_count';
[148] Fix | Delete
if ( $clear_cache ) {
[149] Fix | Delete
wp_cache_delete( $cache_key, 'webhooks' );
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
$count = wp_cache_get( $cache_key, 'webhooks' );
[153] Fix | Delete
[154] Fix | Delete
if ( false === $count ) {
[155] Fix | Delete
$count = absint( $wpdb->get_var( "SELECT count( webhook_id ) FROM {$wpdb->prefix}wc_webhooks WHERE `api_version` < 1;" ) );
[156] Fix | Delete
wp_cache_add( $cache_key, $count, 'webhooks' );
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
return $count;
[160] Fix | Delete
}
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function