Automattic\WooCommerce\Internal\DataStores\StockNotifications
StockNotificationsDataStore::query │ public │ WC 1.0
Query the stock notifications.
Method of the class: StockNotificationsDataStore{}
No Hooks.
Returns
Array|Array|Int. An array of notifications or the number of notifications.
Usage
$StockNotificationsDataStore = new StockNotificationsDataStore();
$StockNotificationsDataStore->query( $args );
- $args(array) (required)
- The arguments.
StockNotificationsDataStore::query() StockNotificationsDataStore::query code
WC 10.3.6
public function query( array $args ) {
global $wpdb;
$args = wp_parse_args(
$args,
array(
'status' => '',
'product_id' => array(),
'user_id' => 0,
'user_email' => '',
'last_attempt_limit' => 0,
'start_date' => 0,
'end_date' => 0,
'limit' => -1,
'offset' => 0,
'order_by' => array( 'id' => 'ASC' ),
'return' => 'ids', // i.e. 'count', 'ids', 'objects'.
)
);
$table = $this->get_table_name();
$select = 'id';
if ( 'count' === $args['return'] ) {
$select = 'COUNT(id)';
} elseif ( 'objects' === $args['return'] ) {
$select = '*';
}
// WHERE clauses.
$where = array();
$where_values = array();
if ( $args['status'] ) {
$where[] = 'status = %s';
$where_values[] = esc_sql( $args['status'] );
}
if ( ! empty( $args['product_id'] ) ) {
$product_ids = array_map( 'absint', (array) $args['product_id'] );
$where[] = 'product_id IN (' . implode( ',', array_fill( 0, count( $product_ids ), '%d' ) ) . ')';
$where_values = array_merge( $where_values, $product_ids );
}
if ( $args['user_id'] ) {
$where[] = 'user_id = %d';
$where_values[] = absint( $args['user_id'] );
}
if ( $args['user_email'] ) {
$where[] = 'user_email = %s';
$where_values[] = esc_sql( $args['user_email'] );
}
if ( $args['last_attempt_limit'] > 0 ) {
$where[] = '(date_last_attempt_gmt < %s OR date_last_attempt_gmt IS NULL)';
$where_values[] = gmdate( 'Y-m-d H:i:s', $args['last_attempt_limit'] );
}
if ( $args['start_date'] ) {
$where[] = 'date_created_gmt >= %s';
$where_values[] = esc_sql( $args['start_date'] );
}
if ( $args['end_date'] ) {
$where[] = 'date_created_gmt < %s';
$where_values[] = esc_sql( $args['end_date'] );
}
// ORDER BY clauses.
$order_by = '';
$order_by_clauses = array();
if ( $args['order_by'] && is_array( $args['order_by'] ) ) {
foreach ( $args['order_by'] as $what => $how ) {
$order_by_clauses[] = $table . '.' . esc_sql( strval( $what ) ) . ' ' . esc_sql( strval( $how ) );
}
}
// Assemble the query.
$where = implode( ' AND ', $where );
$where = $where ? ' WHERE ' . $where : '';
$order_by = ! empty( $order_by_clauses ) ? ' ORDER BY ' . implode( ', ', $order_by_clauses ) : '';
$limit = $args['limit'] > 0 ? ' LIMIT ' . absint( $args['limit'] ) : '';
$offset = $args['offset'] > 0 ? ' OFFSET ' . absint( $args['offset'] ) : '';
$sql = "SELECT $select FROM $table $where $order_by $limit $offset";
// Prepare the query.
$prepared_sql = empty( $where_values ) ? $sql : $wpdb->prepare( $sql, $where_values ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
// Execute the query.
if ( 'count' === $args['return'] ) {
return (int) $wpdb->get_var( $prepared_sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
$results = $wpdb->get_results( $prepared_sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( empty( $results ) || ! is_array( $results ) ) {
return array();
}
if ( 'objects' === $args['return'] ) {
return array_map(
function ( $result ) {
return new Notification( $result );
},
$results
);
}
return array_map(
function ( $result ) {
return absint( $result['id'] );
},
$results
);
}