shop changes

This commit is contained in:
2025-06-25 16:04:19 -07:00
parent 8d33ce2dc7
commit b0635919b1
30 changed files with 550 additions and 144 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace ofc;
use ReflectionClass;
class ActionsLoader
{
public static function load()
{
$actions_dir = get_template_directory() . '/actions';
if (!is_dir($actions_dir)) {
return;
}
foreach (scandir($actions_dir) as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) !== 'php') {
continue;
}
$file_path = $actions_dir . '/' . $file;
require_once $file_path;
// Guess class name from file (assuming PSR-4 mapping matches)
// You can hardcode or use reflection if needed
$class_name = self::getClassFromFile($file);
if (!class_exists($class_name)) {
continue;
}
$reflection = new ReflectionClass($class_name);
if (!$reflection->isInstantiable()) {
continue;
}
$required_properties = ['hookName', 'priority'];
foreach ($required_properties as $prop) {
if (!$reflection->hasProperty($prop)) {
continue 2;
}
}
if (!$reflection->hasMethod('callback')) {
continue;
}
$instance = new $class_name();
if ($instance->wrapHookInInit()) {
add_action("init", function () use ($instance) {
add_action($instance->getHookName(), [$instance, 'callback'], $instance->getPriority());
}, $instance->getPriority());
continue;
}
add_action($instance->getHookName(), [$instance, 'callback'], $instance->getPriority());
}
}
private static function getClassFromFile($file)
{
$class_base = pathinfo($file, PATHINFO_FILENAME);
return 'Actions\\' . $class_base;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace ofc;
abstract class RadAction implements RadActionInterface
{
protected string $hookName;
protected int $priority = 99;
protected bool $wrapInit = false;
public function getHookName(): string
{
return $this->hookName;
}
public function getPriority(): int
{
return $this->priority;
}
public function wrapHookInInit(): bool
{
return $this->wrapInit;
}
abstract public function callback();
}

View File

@@ -0,0 +1,10 @@
<?php
namespace ofc;
interface RadActionInterface
{
public function getHookName(): string;
public function getPriority(): int;
public function callback();
}

View File

@@ -1097,10 +1097,37 @@ class Site
return $output;
}
private function getTermChildren($term, $fields = [])
{
$args = [
'taxonomy' => $term->taxonomy,
'parent' => $term->term_id,
'hide_empty' => false,
'suppress_filter' => true,
];
$results = get_terms($args);
if ($fields == []) {
return $results;
}
$output = [];
foreach ($results as $term) {
$append = $this->getFieldsForTerm($fields, $term);
$output[] = $append;
}
return $output;
}
private function getFieldsForTerm(array $fields, $term)
{
$output = [];
foreach ($fields as $key) {
if (str_starts_with($key, "children")) {
$fields = explode(",", str_replace("children.", "", $key));
$output["children"] = site()->getTermChildren($term, $fields);
continue;
}
if ($key === "id" || $key === "ID" || $key === "term_id") {
$output[$key] = $term->term_id;
continue;
@@ -1131,7 +1158,14 @@ class Site
$output[$key] = get_field($key, $term->taxonomy . "_" . $term->term_id);
continue;
}
// try to get the property straight off the object?
$output[$key] = $term->$key;
// still nothing? check meta
if (is_null($output[$key])) {
$output[$key] = get_term_meta($term->term_id, $key, true);
}
}
return $output;
}
@@ -1498,23 +1532,30 @@ class Site
private function processActions()
{
ActionsLoader::load();
if (!isset($this->config["actions"]) || !is_array($this->config["actions"])) {
return;
}
foreach ($this->config["actions"] as $hookName => $callback) {
if (is_array($callback) && array_is_list($callback)) {
$priority = $callback[1];
$callback = $callback[0];
foreach ($this->config["actions"] as $hookName => $action) {
if (is_array($action) && array_is_list($action) && count($action) === 2) {
if (!is_numeric($action[1])) {
// invalid priority
continue;
}
$priority = $action[1];
$callback = $action[0];
add_action($hookName, $callback, $priority);
continue;
}
if (is_array($callback) && isset($callback["hook"]) && isset($callback["callback"])) {
add_action($callback["hook"], $callback["callback"], $callback["priority"] ?? 99);
if (is_array($action) && isset($action["hook"]) && isset($action["callback"])) {
add_action($action["hook"], $action["callback"], $action["priority"] ?? 99);
continue;
}
add_action($hookName, $callback, 99);
add_action($hookName, $action, 99);
}
}