Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Products: Decouple taxonomy term generation #156

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions includes/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ abstract class Generator {
/**
* Caches term IDs.
*
* @deprecated
*
* @var array Array of IDs.
*/
protected static $term_ids;
Expand Down Expand Up @@ -100,12 +102,17 @@ protected static function validate_batch_amount( $amount ) {
/**
* Get random term ids.
*
* @deprecated Use Product::get_term_ids instead.
*
* @param int $limit Number of term IDs to get.
* @param string $taxonomy Taxonomy name.
* @param string $name Product name to extract terms from.
*
* @return array
*/
protected static function generate_term_ids( $limit, $taxonomy, $name = '' ) {
_deprecated_function( __METHOD__, '1.2.2', 'Product::get_term_ids' );

self::init_faker();

$term_ids = array();
Expand Down
54 changes: 49 additions & 5 deletions includes/Generator/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

namespace WC\SmoothGenerator\Generator;

use WC\SmoothGenerator\Util\RandomRuntimeCache;

/**
* Product data generator.
*/
class Product extends Generator {

/**
* Holds array of product IDs for generating relationships.
*
Expand Down Expand Up @@ -139,6 +140,9 @@ public static function batch( $amount, array $args = array() ) {
$product_ids[] = $product->get_id();
}

// In case multiple batches are being run in one request, refresh the cache data.
RandomRuntimeCache::reset();

return $product_ids;
}

Expand Down Expand Up @@ -310,8 +314,8 @@ protected static function generate_variable_product() {
'upsell_ids' => self::get_existing_product_ids(),
'cross_sell_ids' => self::get_existing_product_ids(),
'image_id' => self::get_image(),
'category_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_cat', $name ),
'tag_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_tag', $name ),
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
'gallery_image_ids' => $gallery,
'reviews_allowed' => self::$faker->boolean(),
'purchase_note' => self::$faker->boolean() ? self::$faker->text() : '',
Expand Down Expand Up @@ -405,8 +409,8 @@ protected static function generate_simple_product() {
'menu_order' => self::$faker->numberBetween( 0, 10000 ),
'virtual' => $is_virtual,
'downloadable' => false,
'category_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_cat', $name ),
'tag_ids' => self::generate_term_ids( self::$faker->numberBetween( 0, 5 ), 'product_tag', $name ),
'category_ids' => self::get_term_ids( 'product_cat', self::$faker->numberBetween( 0, 3 ) ),
'tag_ids' => self::get_term_ids( 'product_tag', self::$faker->numberBetween( 0, 5 ) ),
'shipping_class_id' => 0,
'image_id' => $image_id,
'gallery_image_ids' => $gallery,
Expand All @@ -415,6 +419,46 @@ protected static function generate_simple_product() {
return $product;
}

/**
* Get a number of random term IDs for a specific taxonomy.
*
* @param string $taxonomy The taxonomy to get terms for.
* @param int $limit The number of term IDs to get.
*
* @return array
*/
protected static function get_term_ids( $taxonomy, $limit ) {
if ( $limit <= 0 ) {
return array();
}

if ( ! RandomRuntimeCache::exists( $taxonomy ) ) {
$args = array(
'taxonomy' => $taxonomy,
'number' => 20,
'orderby' => 'count',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'ids',
);

if ( 'product_cat' === $taxonomy ) {
$uncategorized = get_term_by( 'slug', 'uncategorized', 'product_cat' );
if ( $uncategorized ) {
$args['exclude'] = $uncategorized->term_id;
}
}

$term_ids = get_terms( $args );

RandomRuntimeCache::set( $taxonomy, $term_ids );
}

RandomRuntimeCache::shuffle( $taxonomy );

return RandomRuntimeCache::get( $taxonomy, $limit );
}

/**
* Generate an image gallery.
*
Expand Down
170 changes: 170 additions & 0 deletions includes/Util/RandomRuntimeCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php
/**
* A runtime object cache for storing and randomly retrieving reusable data.
*
* @package SmoothGenerator\Util
*/

namespace WC\SmoothGenerator\Util;

/**
* Class RandomRuntimeCache.
*/
class RandomRuntimeCache {
/**
* Associative array for storing groups of cache items.
*
* @var array
*/
private static $cache = array();

/**
* Check if a specific cache group exists.
*
* @param string $group The specified cache group.
*
* @return bool
*/
public static function exists( string $group ): bool {
return array_key_exists( $group, self::$cache );
}

/**
* Get a number of items from a specific cache group.
*
* The retrieved items will be from the top of the group's array.
*
* @param string $group The specified cache group.
* @param int $limit Optional. Get up to this many items. Using 0 will return all the items in the group.
* Default 0.
*
* @return array
*/
public static function get( string $group, int $limit = 0 ): array {
$all_items = self::get_group( $group );

if ( $limit <= 0 || count( $all_items ) <= $limit ) {
return $all_items;
}

$items = array_slice( $all_items, 0, $limit );

return $items;
}

/**
* Remove a number of items from a specific cache group and return them.
*
* The items will be extracted from the top of the group's array.
*
* @param string $group The specified cache group.
* @param int $limit Optional. Extract up to this many items. Using 0 will return all the items in the group and
* delete it from the cache. Default 0.
*
* @return array
*/
public static function extract( string $group, int $limit = 0 ): array {
$all_items = self::get_group( $group );

if ( $limit <= 0 || count( $all_items ) <= $limit ) {
self::clear( $group );

return $all_items;
}

$items = array_slice( $all_items, 0, $limit );
$remaining_items = array_slice( $all_items, $limit );

self::set( $group, $remaining_items );

return $items;
}

/**
* Add items to a specific cache group.
*
* @param string $group The specified cache group.
* @param array $items The items to add to the group.
*
* @return void
*/
public static function add( string $group, array $items ): void {
$existing_items = self::get_group( $group );

self::set( $group, array_merge( $existing_items, $items ) );
}

/**
* Set a cache group to contain a specific set of items.
*
* @param string $group The specified cache group.
* @param array $items The items that will be in the group.
*
* @return void
*/
public static function set( string $group, array $items ): void {
self::$cache[ $group ] = $items;
}

/**
* Count the number of items in a specific cache group.
*
* @param string $group The specified cache group.
*
* @return int
*/
public static function count( string $group ): int {
$group = self::get_group( $group );

return count( $group );
}

/**
* Shuffle the order of the items in a specific cache group.
*
* @param string $group The specified cache group.
*
* @return void
*/
public static function shuffle( string $group ): void {
// Ensure group exists.
self::get_group( $group );

shuffle( self::$cache[ $group ] );
}

/**
* Delete a group from the cache.
*
* @param string $group The specified cache group.
*
* @return void
*/
public static function clear( string $group ): void {
unset( self::$cache[ $group ] );
}

/**
* Clear the entire cache.
*
* @return void
*/
public static function reset(): void {
self::$cache = array();
}

/**
* Get the items in a cache group, ensuring that the group exists in the cache.
*
* @param string $group The specified cache group.
*
* @return array
*/
private static function get_group( string $group ): array {
if ( ! self::exists( $group ) ) {
self::set( $group, array() );
}

return self::$cache[ $group ];
}
}