WC_Webhook_Data_Store::search_webhooks()publicWC 1.0

Search webhooks.

Method of the class: WC_Webhook_Data_Store{}

No Hooks.

Return

Array|Object.

Usage

$WC_Webhook_Data_Store = new WC_Webhook_Data_Store();
$WC_Webhook_Data_Store->search_webhooks( $args );
$args(array) (required)
Search arguments.

WC_Webhook_Data_Store::search_webhooks() code WC 9.4.2

public function search_webhooks( $args ) {
	global $wpdb;

	$args = wp_parse_args(
		$args,
		array(
			'limit'    => 10,
			'offset'   => 0,
			'order'    => 'DESC',
			'orderby'  => 'id',
			'paginate' => false,
		)
	);

	// Map post statuses.
	$statuses = array(
		'publish' => 'active',
		'draft'   => 'paused',
		'pending' => 'disabled',
	);

	// Map orderby to support a few post keys.
	$orderby_mapping = array(
		'ID'            => 'webhook_id',
		'id'            => 'webhook_id',
		'name'          => 'name',
		'title'         => 'name',
		'post_title'    => 'name',
		'post_name'     => 'name',
		'date_created'  => 'date_created_gmt',
		'date'          => 'date_created_gmt',
		'post_date'     => 'date_created_gmt',
		'date_modified' => 'date_modified_gmt',
		'modified'      => 'date_modified_gmt',
		'post_modified' => 'date_modified_gmt',
	);
	$orderby         = isset( $orderby_mapping[ $args['orderby'] ] ) ? $orderby_mapping[ $args['orderby'] ] : 'webhook_id';
	$sort            = 'ASC' === strtoupper( $args['order'] ) ? 'ASC' : 'DESC';
	$order           = "ORDER BY {$orderby} {$sort}";
	$limit           = -1 < $args['limit'] ? $wpdb->prepare( 'LIMIT %d', $args['limit'] ) : '';
	$offset          = 0 < $args['offset'] ? $wpdb->prepare( 'OFFSET %d', $args['offset'] ) : '';
	$status          = ! empty( $args['status'] ) ? $wpdb->prepare( 'AND `status` = %s', isset( $statuses[ $args['status'] ] ) ? $statuses[ $args['status'] ] : $args['status'] ) : '';
	$search          = ! empty( $args['search'] ) ? $wpdb->prepare( 'AND `name` LIKE %s', '%' . $wpdb->esc_like( sanitize_text_field( $args['search'] ) ) . '%' ) : '';
	$include         = '';
	$exclude         = '';
	$date_created    = '';
	$date_modified   = '';
	$user_id         = '';
	$api_version     = '';

	if ( ! empty( $args['include'] ) ) {
		$args['include'] = implode( ',', wp_parse_id_list( $args['include'] ) );
		$include         = 'AND webhook_id IN (' . $args['include'] . ')';
	}

	if ( ! empty( $args['exclude'] ) ) {
		$args['exclude'] = implode( ',', wp_parse_id_list( $args['exclude'] ) );
		$exclude         = 'AND webhook_id NOT IN (' . $args['exclude'] . ')';
	}

	if ( ! empty( $args['user_id'] ) ) {
		$user_id = $wpdb->prepare( 'AND `user_id` = %d', absint( $args['user_id'] ) );
	}

	if ( ! empty( $args['after'] ) || ! empty( $args['before'] ) ) {
		$args['after']  = empty( $args['after'] ) ? '0000-00-00' : $args['after'];
		$args['before'] = empty( $args['before'] ) ? current_time( 'mysql', 1 ) : $args['before'];

		$date_created = "AND `date_created_gmt` BETWEEN STR_TO_DATE('" . esc_sql( $args['after'] ) . "', '%Y-%m-%d %H:%i:%s') and STR_TO_DATE('" . esc_sql( $args['before'] ) . "', '%Y-%m-%d %H:%i:%s')";
	}

	if ( ! empty( $args['modified_after'] ) || ! empty( $args['modified_before'] ) ) {
		$args['modified_after']  = empty( $args['modified_after'] ) ? '0000-00-00' : $args['modified_after'];
		$args['modified_before'] = empty( $args['modified_before'] ) ? current_time( 'mysql', 1 ) : $args['modified_before'];

		$date_modified = "AND `date_modified_gmt` BETWEEN STR_TO_DATE('" . esc_sql( $args['modified_after'] ) . "', '%Y-%m-%d %H:%i:%s') and STR_TO_DATE('" . esc_sql( $args['modified_before'] ) . "', '%Y-%m-%d %H:%i:%s')";
	}

	$api_version_value = $args['api_version'] ?? null;
	if ( is_numeric( $api_version_value ) ) {
		$api_version = 'AND `api_version`=' . esc_sql( $api_version_value );
	}

	// Check for cache.
	$cache_key   = WC_Cache_Helper::get_cache_prefix( 'webhooks' ) . 'search_webhooks' . md5( implode( ',', $args ) );
	$cache_value = wp_cache_get( $cache_key, 'webhook_search_results' );

	if ( $cache_value ) {
		return $cache_value;
	}

	if ( $args['paginate'] ) {
		$query = trim(
			"SELECT SQL_CALC_FOUND_ROWS webhook_id
			FROM {$wpdb->prefix}wc_webhooks
			WHERE 1=1
			{$status}
			{$search}
			{$include}
			{$exclude}
			{$date_created}
			{$date_modified}
			{$api_version}
			{$user_id}
			{$order}
			{$limit}
			{$offset}"
		);

		$webhook_ids  = wp_parse_id_list( $wpdb->get_col( $query ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$total        = (int) $wpdb->get_var( 'SELECT FOUND_ROWS();' );
		$return_value = (object) array(
			'webhooks'      => $webhook_ids,
			'total'         => $total,
			'max_num_pages' => $args['limit'] > 1 ? ceil( $total / $args['limit'] ) : 1,
		);
	} else {
		$query = trim(
			"SELECT webhook_id
			FROM {$wpdb->prefix}wc_webhooks
			WHERE 1=1
			{$status}
			{$search}
			{$include}
			{$exclude}
			{$date_created}
			{$date_modified}
			{$user_id}
			{$order}
			{$limit}
			{$offset}"
		);

		$webhook_ids  = wp_parse_id_list( $wpdb->get_col( $query ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$return_value = $webhook_ids;
	}

	wp_cache_set( $cache_key, $return_value, 'webhook_search_results' );

	return $return_value;
}