From 965eb125b24f4b0f2b6d6de7d98a6ba1a7fde670 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 11 Nov 2024 15:31:56 -0800 Subject: [PATCH 1/4] (REF) Iframe Router - Convert private getHandler() to public getLayout() --- ext/iframe/Civi/Iframe/Router.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/iframe/Civi/Iframe/Router.php b/ext/iframe/Civi/Iframe/Router.php index 5b839c6b377d..ce54257a4571 100644 --- a/ext/iframe/Civi/Iframe/Router.php +++ b/ext/iframe/Civi/Iframe/Router.php @@ -17,7 +17,7 @@ public function invoke(array $params) { $config = \CRM_Core_Config::singleton(); $_GET[$config->userFrameworkURLVar] = $params['route']; - $handler = $this->getHandler(); + $handler = [$this, 'invoke' . ucfirst($this->getLayout())]; $handler($params); } @@ -46,12 +46,16 @@ public function isAllowedRoute(string $route): bool { return FALSE; } - protected function getHandler(): callable { + /** + * @return string + * 'basic' or 'raw' or 'cms' + */ + public function getLayout(): string { $setting = \Civi::settings()->get('iframe_layout'); if ($setting === 'auto') { $setting = 'basic'; } - return [$this, 'invoke' . ucfirst($setting)]; + return $setting; } protected function invokeRaw(array $params): void { From 976aa01e4f47d402a23597f2c6c119edd1511119 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 11 Nov 2024 15:32:56 -0800 Subject: [PATCH 2/4] (REF) Iframe Router - Allow UF-integration to replace 'echo' for showing standard pages --- ext/iframe/Civi/Iframe/Router.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/iframe/Civi/Iframe/Router.php b/ext/iframe/Civi/Iframe/Router.php index ce54257a4571..262172875723 100644 --- a/ext/iframe/Civi/Iframe/Router.php +++ b/ext/iframe/Civi/Iframe/Router.php @@ -9,6 +9,16 @@ */ class Router extends AutoService { + /** + * @param array $params + * Some mix of: + * - route: string, eg "civicrm/event/info" + * - printPage: function(string), Print an exact web page response + * - drupalKernel: The HTTP kernel handling the iframe request in D8/9/10/11 + * - drupalRequest: The HTTP object representing the iframe request in D8/9/10/11 + * @return void + * @throws \CRM_Core_Exception + */ public function invoke(array $params) { if (!$this->isAllowedRoute($params['route'])) { throw new \CRM_Core_Exception("Route not available for embedding."); @@ -68,7 +78,9 @@ protected function invokeRaw(array $params): void { if (empty($pageContent) && !empty($printedContent)) { $pageContent = $printedContent; } - echo $pageContent; + + $printPage = $params['printPage'] ?? 'print'; + $printPage($pageContent); } /** @@ -94,12 +106,15 @@ protected function invokeBasic(array $params): void { $htmlHeader = \CRM_Core_Region::instance('html-header')->render(''); $locale = \CRM_Core_I18n::getLocale(); - echo \CRM_Core_Smarty::singleton()->fetchWith('iframe-basic-page.tpl', [ + $fullPage = \CRM_Core_Smarty::singleton()->fetchWith('iframe-basic-page.tpl', [ 'lang' => substr($locale, 0, 2), 'dir' => \CRM_Core_I18n::isLanguageRTL($locale) ? 'rtl' : 'ltr', 'head' => $htmlHeader, 'body' => $pageContent, ]); + + $printPage = $params['printPage'] ?? 'print'; + $printPage($fullPage); } protected function invokeCms(array $params):void { From df8c6bcf33268f0882475eb457b6826aa31e6bbc Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 11 Nov 2024 15:34:11 -0800 Subject: [PATCH 3/4] Iframe Routing - Updates for WordPress --- ext/iframe/Civi/Iframe/Iframe.php | 7 +++++++ ext/iframe/Civi/Iframe/Router.php | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/ext/iframe/Civi/Iframe/Iframe.php b/ext/iframe/Civi/Iframe/Iframe.php index 1bddcd6e7575..9534faf27610 100644 --- a/ext/iframe/Civi/Iframe/Iframe.php +++ b/ext/iframe/Civi/Iframe/Iframe.php @@ -34,6 +34,13 @@ public function getTemplate(): string { * @throws \CRM_Core_Exception */ public function onRenderUrl(Url $url, ?string &$result) { + if (CIVICRM_UF === 'WordPress') { + $result = \Civi::url('frontend://', 'a') + ->merge($url, ['path', 'query', 'fragment', 'fragmentQuery', 'flags']) + ->addQuery('_cvwpif=1'); + return; + } + $result = \Civi::url('[civicrm.iframe]', 'a')->merge($url, ['path', 'query', 'fragment', 'fragmentQuery', 'flags']); } diff --git a/ext/iframe/Civi/Iframe/Router.php b/ext/iframe/Civi/Iframe/Router.php index 262172875723..edf0d48bf6a7 100644 --- a/ext/iframe/Civi/Iframe/Router.php +++ b/ext/iframe/Civi/Iframe/Router.php @@ -137,6 +137,11 @@ protected function invokeCms(array $params):void { $kernel->terminate($request, $response); break; + case 'WordPress': + // N.B. There are sufficient events in WP API to enforce IFRAME invariants. + // @see \CiviCRM_For_WordPress::activate_iframe() + throw new \LogicException("In Civi-WP, IFRAMEs with CMS page-chrome shuld use standard invoker."); + default: throw new \CRM_Core_Exception("Unimplemented: invokeCms(" . CIVICRM_UF . ")"); } From 6166bf9e83c499703176ca173e5879823023dcd9 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 11 Nov 2024 17:01:08 -0800 Subject: [PATCH 4/4] Iframe Script Manager - Don't need to run on WP --- ext/iframe/Civi/Iframe/ScriptManager.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/iframe/Civi/Iframe/ScriptManager.php b/ext/iframe/Civi/Iframe/ScriptManager.php index 301c4dcd2219..a46ef78f67d8 100644 --- a/ext/iframe/Civi/Iframe/ScriptManager.php +++ b/ext/iframe/Civi/Iframe/ScriptManager.php @@ -32,6 +32,11 @@ public function hook_civicrm_check(&$messages, $statusNames = [], $includeDisabl return; } + if (CIVICRM_UF === 'WordPress') { + // WP doesn't require installing a separate `/iframe.php`. Instead, it uses `?_cvwpif=1`. + return; + } + $path = $this->getPath(); $template = $this->iframe->getTemplate();