Skip to content

Commit

Permalink
Ensure child classes are not also queried and add a basic cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mlewis-everley committed Dec 6, 2024
1 parent 0606643 commit cf9a4a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ Initial official release of module, including:

# 2.1.6

* Fix how required fields are called in validation
* Fix how required fields are called in validation

# 2.1.7

* Fix performance issues and add some basic caching
30 changes: 28 additions & 2 deletions src/GoogleShoppingFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\ORM\ArrayList;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DataObject;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Config\Config;
use SilverStripe\Versioned\Versioned;
use SilverCommerce\CatalogueAdmin\Model\CatalogueProduct;
Expand Down Expand Up @@ -58,6 +59,10 @@ class GoogleShoppingFeed
*/
private static $dataobjects = array();

/**
* Cache any generated item lists for performances sake
*/
private static $items_cache = [];

/**
* Checks whether the given class name is already registered or not.
Expand Down Expand Up @@ -97,20 +102,39 @@ public static function getItems()
$output = ArrayList::create();
$search_filter = Config::inst()->get(__CLASS__, 'use_show_in_search');
$disabled_filter = Config::inst()->get(__CLASS__, 'use_disabled');
$filter = [];
$classes = [];
$all_classes = ClassInfo::subclassesFor(DataObject::class);

unset($all_classes[strtolower(DataObject::class)]);

// build a master list of classes to query
foreach ($all_classes as $class) {
if ($class::has_extension(Extension::class, null, true)) {
$classes[] = $class;
}
}

unset($class);
$redundant = [];

// ensure only top level class is used
foreach ($classes as $top_class) {
foreach ($classes as $class) {
if ($class !== $top_class && is_a($class, $top_class, true)) {
$redundant[] = $class;
}
}
}

$classes = array_diff($classes, $redundant);

// todo migrate to extension hook or DI point for other modules to
foreach ($classes as $class) {
if (isset(self::$items_cache[$class])) {
$output->merge(self::$items_cache[$class]);
continue;
}

if ($class == SiteTree::class) {
$search_filter = ($search_filter) ? "\"ShowInSearch\" = 1" : "";
$instances = Versioned::get_by_stage('SiteTree', 'Live', $search_filter);
Expand All @@ -124,7 +148,9 @@ public static function getItems()
$instances = DataList::create($class);
}

if ($instances) {
if ($instances->exists()) {
self::$items_cache[$class] = $instances;

foreach ($instances as $obj) {
if ($obj->canIncludeInGoogleShoppingFeed()) {
$output->push($obj);
Expand Down

0 comments on commit cf9a4a3

Please sign in to comment.