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

(dev/core#5464) Iframe - Add support for hosting Civi IFRAMEs on WordPress #31460

Open
wants to merge 4 commits into
base: master
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 ext/iframe/Civi/Iframe/Iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

Expand Down
34 changes: 29 additions & 5 deletions ext/iframe/Civi/Iframe/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -17,7 +27,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);
}

Expand Down Expand Up @@ -46,12 +56,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 {
Expand All @@ -64,7 +78,9 @@ protected function invokeRaw(array $params): void {
if (empty($pageContent) && !empty($printedContent)) {
$pageContent = $printedContent;
}
echo $pageContent;

$printPage = $params['printPage'] ?? 'print';
$printPage($pageContent);
}

/**
Expand All @@ -90,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 {
Expand All @@ -118,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 . ")");
}
Expand Down
5 changes: 5 additions & 0 deletions ext/iframe/Civi/Iframe/ScriptManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down