ActionScheduler_DBStore::get_group_ids
Get a group's ID based on its name/slug.
Метод класса: ActionScheduler_DBStore{}
Хуков нет.
Возвращает
int[]. The group IDs, if they exist or were successfully created. May be empty.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_group_ids( $slugs, $create_if_not_exists );
- $slugs(строка|массив) (обязательный)
- The string name of a group, or names for several groups.
- $create_if_not_exists(true|false)
- Whether to create the group if it does not already exist. Default, true - create the group.
По умолчанию:true
Код ActionScheduler_DBStore::get_group_ids() ActionScheduler DBStore::get group ids WC 11.3.0
protected function get_group_ids( $slugs, $create_if_not_exists = true ) {
$slugs = (array) $slugs;
if ( empty( $slugs ) ) {
return array();
}
/**
* Global.
*
* @var \wpdb $wpdb
*/
global $wpdb;
// For the primary path, resolve slugs using the WordPress cache first, then identify which slugs require a database lookup.
$group_ids = array();
$unresolved = array();
foreach ( $slugs as $slug ) {
$cached_group_id = wp_cache_get( $this->get_group_cache_key( (string) $slug ), self::GROUP_IDS_CACHE_GROUP );
if ( false !== $cached_group_id ) {
$group_ids[] = (int) $cached_group_id;
} else {
$unresolved[] = $slug;
}
}
// For the secondary or initialization path, bulk-resolve any remaining slugs from the database.
if ( ! empty( $unresolved ) ) {
$resolved = array();
// Bulk-fetch unresolved slugs from the database. It is acceptable not to use wp_cache_set_multiple to maintain code simplicity.
$placeholders = implode( ', ', array_fill( 0, count( $unresolved ), '%s' ) );
$rows = $wpdb->get_results(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
"SELECT group_id, slug FROM {$wpdb->actionscheduler_groups} WHERE slug IN ( $placeholders )",
...$unresolved
)
);
foreach ( $rows as $row ) {
$resolved[] = $row->slug;
wp_cache_set( $this->get_group_cache_key( $row->slug ), $row->group_id, self::GROUP_IDS_CACHE_GROUP, 10 * MINUTE_IN_SECONDS );
}
// Automatically create unresolved slugs if requested.
if ( $create_if_not_exists ) {
foreach ( array_diff( $unresolved, $resolved ) as $slug ) {
$this->create_group( $slug );
}
}
// Ensure stable sorting by aligning the IDs, which are currently sourced from various locations, to match the order of the provided slugs.
$group_ids = array_values(
array_filter(
array_map(
function ( $slug ) {
return (int) wp_cache_get( $this->get_group_cache_key( (string) $slug ), self::GROUP_IDS_CACHE_GROUP );
},
$slugs
)
)
);
}
return $group_ids;
}