From 8e7daf22d46fcb574ecadf4f161349d4cedf62a9 Mon Sep 17 00:00:00 2001 From: Nick Fenwick Date: Fri, 24 Nov 2023 16:56:42 +0700 Subject: [PATCH] Add NotifierManager::insertContent for easy content insertion. --- includes/classes/traits/NotifierManager.php | 77 +++++++++++++++++++ includes/functions/functions_general.php | 35 +++++++++ .../tpl_account_history_info_default.php | 11 ++- 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/includes/classes/traits/NotifierManager.php b/includes/classes/traits/NotifierManager.php index 2bb30e0737..6b7293a89f 100644 --- a/includes/classes/traits/NotifierManager.php +++ b/includes/classes/traits/NotifierManager.php @@ -113,6 +113,83 @@ public function notify( } } + /** + * Requires a define page, and calls regisistered Observers for content output. + * + * @param string $eventID + * @param mixed|array|null $param1 $param1 + * @param mixed ...$params + * @return void + */ + function insertContent(string $eventID, $param1 = [], &...$params) + { + global $l; + + // Output any 'define_' page, e.g. CONTENT_FOO will output define_foo.php + $define_page_name = strtolower(str_replace('CONTENT_', 'DEFINE_', $eventID)); + $this->outputDefinePage($define_page_name); + + $observers = $this->getRegisteredObservers(); + if (empty($observers)) { + return; + } + foreach ($observers as $key => $obs) { + // Skip observer if it's not registered for a CONTENT_ event. + if (!str_starts_with($obs['eventID'], 'CONTENT_')) { + continue; + } + + // If there is a mapping provided on the observer from event name to define page, use it and continue. + // This allows easy output of define pages with a different name to the CONTENT_* eventID with no extra coding. + // e.g. public array $contentDefinePageMap = [ + // 'CONTENT_SOMETHING' => 'define_example_content' + // ]; + if (!empty($obs['obs']->contentDefinePageMap) && array_key_exists($eventID, $obs['obs']->contentDefinePageMap)) { + $this->outputDefinePage($obs['obs']->contentDefinePageMap[$eventID]); + continue; + } + + // Notify the listening observer that this event has been triggered + $snake_case_method = strtolower($eventID); + if (method_exists($obs['obs'], $snake_case_method)) { + $obs['obs']->{$snake_case_method}($this, $eventID, $params); + } else { + // If no update handler method exists then trigger an error so the problem is logged + $className = (is_object($obs['obs'])) ? get_class($obs['obs']) : $obs['obs']; + trigger_error('WARNING: No update() method (or matching alternative) found in the ' . $className . ' class for event ' . $actualEventId, E_USER_WARNING); + } + } + } + + /** + * Write out a named define page, wrapping it in a
with extra + * classes and params defined by arguments. + * + * @param string $define_page_name The name of the define page, e.g. define_foo_bar.php + * @param array $classList Any classes to be added to the wrapper e.g. [ 'warning' ] + * @param string $params Any other params to be inserted into the
e.g. "id='foobar'" + * @return bool true if output was generated, else false (usually an error) + */ + protected function outputDefinePage (string $define_page_name, array $classList = [], string $params = null): bool { + $define_page = zen_get_define_page_content($define_page_name); + if (empty($define_page)) { + return false; + } + if (empty($classList)) { + $classList = []; + } + if (!empty($params)) { + $params = ' ' . $params; + } + $classList[] = 'define-page'; + // Add a class named after the define page for custom styling, e.g. define-contact-us + $classList[] = str_replace('_', '-', $define_page_name); + echo '
' . + $define_page . + '
'; + return true; + } + protected function logNotifier($eventID, $param1, $param2, $param3, $param4, $param5, $param6, $param7, $param8, $param9): void { if (!defined('NOTIFIER_TRACE') || empty(NOTIFIER_TRACE) || NOTIFIER_TRACE === 'false' || NOTIFIER_TRACE === 'Off') { diff --git a/includes/functions/functions_general.php b/includes/functions/functions_general.php index b0bd810e63..af53c1a2bf 100644 --- a/includes/functions/functions_general.php +++ b/includes/functions/functions_general.php @@ -208,3 +208,38 @@ function zen_get_buy_now_button($product_id, string $buy_now_link, $additional_l return $return_button; } + +/** + * Returns the string content of the named define page, processed either: + * - by an observer running its own processing/substitution/templating engine + * or + * - by the PHP engine (this is the classic default behaviour) + * + * @param string $define_page_name Name of the define page e.g. 'define_contact_us' + * @param array|null $context An object of extra data that may be used by the observer. + * @return string The final content from the define page. + */ +function zen_get_define_page_content(string $define_page_name, ?array $context = null) : string { + global $zco_notifier; + + $define_page = zen_get_file_directory(DIR_WS_LANGUAGES . $_SESSION['language'] . '/html_includes/', $define_page_name, false); + if (!file_exists($define_page)) { + trigger_error("Define Page file '$define_page' does not exist!"); + return ''; // no action + } + + // Give an observer a chance to process the define page + $processed_content = null; + $zco_notifier->notify('NOTIFY_ZEN_PROCESS_DEFINE_PAGE', $define_page, $processed_content, $context); + + if (!empty($processed_content)) { + return $processed_content; + } + + ob_start(); + require($define_page); + $output = ob_get_contents(); + ob_end_clean(); + + return $output; +} \ No newline at end of file diff --git a/includes/templates/template_default/templates/tpl_account_history_info_default.php b/includes/templates/template_default/templates/tpl_account_history_info_default.php index d47e7bed1f..991ca54e59 100644 --- a/includes/templates/template_default/templates/tpl_account_history_info_default.php +++ b/includes/templates/template_default/templates/tpl_account_history_info_default.php @@ -15,13 +15,13 @@

- insertContent('CONTENT_ACCOUNT_HISTORY_INFO_INTRO', $order); + $extra_headings = []; $zco_notifier->notify('NOTIFY_ACCOUNT_HISTORY_INFO_EXTRA_COLUMN_HEADING', $order, $extra_headings); ?> - @@ -105,6 +105,10 @@ +insertContent('CONTENT_ACCOUNT_HISTORY_INFO_POST_ORDER', $order); +?> + info['payment_method']; ?>
+insertContent('CONTENT_ACCOUNT_HISTORY_INFO_EXTRO', $order); +?>