Automattic\WooCommerce\Internal\Utilities

WebhookUtil::maybe_render_user_with_webhooks_warning()privateWC 7.8.0

When users are about to be deleted show an informative text if they have webhooks assigned.

Метод класса: WebhookUtil{}

Хуки из метода

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->maybe_render_user_with_webhooks_warning( $current_user, $userids ): void;
$current_user(\WP_User) (обязательный)
The current logged in user.
$userids(массив) (обязательный)
Array with the ids of the users that are about to be deleted.

Список изменений

С версии 7.8.0 Введена.

Код WebhookUtil::maybe_render_user_with_webhooks_warning() WC 9.3.3

private function maybe_render_user_with_webhooks_warning( \WP_User $current_user, array $userids ): void {
	global $wpdb;

	$at_least_one_user_with_webhooks = false;

	foreach ( $userids as $user_id ) {
		$webhook_ids = $this->get_webhook_ids_for_user( $user_id );
		if ( empty( $webhook_ids ) ) {
			continue;
		}

		$at_least_one_user_with_webhooks = true;

		$user_data      = get_userdata( $user_id );
		$user_login     = false === $user_data ? '' : $user_data->user_login;
		$webhooks_count = count( $webhook_ids );

		$text = sprintf(
			/* translators: 1 = user id, 2 = user login, 3 = webhooks count */
			_nx(
				'User #%1$s %2$s has created %3$d WooCommerce webhook.',
				'User #%1$s %2$s has created %3$d WooCommerce webhooks.',
				$webhooks_count,
				'user webhook count',
				'woocommerce'
			),
			$user_id,
			$user_login,
			$webhooks_count
		);

		echo '<p>' . esc_html( $text ) . '</p>';
	}

	if ( ! $at_least_one_user_with_webhooks ) {
		return;
	}

	$webhooks_settings_url = esc_url_raw( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks' ) );

	// This block of code is copied from WordPress' users.php.
	// phpcs:disable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared
	$users_have_content = (bool) apply_filters( 'users_have_additional_content', false, $userids );
	if ( ! $users_have_content ) {
		if ( $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_author IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
			$users_have_content = true;
		} elseif ( $wpdb->get_var( "SELECT link_id FROM {$wpdb->links} WHERE link_owner IN( " . implode( ',', $userids ) . ' ) LIMIT 1' ) ) {
			$users_have_content = true;
		}
	}
	// phpcs:enable WooCommerce.Commenting.CommentHooks, WordPress.DB.PreparedSQL.NotPrepared

	if ( $users_have_content ) {
		$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' );
	} else {
		$text = __( 'The affected WooCommerce webhooks will <b>not</b> be deleted and will be attributed to user id 0.<br/>', 'woocommerce' );
	}

	$text .= sprintf(
		/* translators: 1 = url of the WooCommerce webhooks settings page */
		__( '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' ),
		$webhooks_settings_url
	);

	echo '<p>' . wp_kses_post( $text ) . '</p>';
}