From 381b7570c998f2c0eea81cd133ce8b370ff89a40 Mon Sep 17 00:00:00 2001
From: Thomas Steur <tsteur@users.noreply.github.com>
Date: Tue, 21 Nov 2017 15:59:33 +1300
Subject: [PATCH] let plugins decide whether to embed widgetized iframe empty
 or not (#12292)

---
 CHANGELOG.md                     |  1 +
 plugins/Dashboard/Dashboard.php  | 10 +++++++++-
 plugins/Widgetize/Controller.php | 27 ++++++++++++++++++++++++---
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5b39c1c346..de60a62dd6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ The Product Changelog at **[piwik.org/changelog](https://piwik.org/changelog)**
 
 ### New APIs
  * Themes can now customize the header text color using `@theme-color-header-text`
+ * New event `Widgetize.shouldEmbedIframeEmpty` added so plugins can optionally define the output of the widgetized HTML themselves
 
 ## Piwik 3.2.0
 
diff --git a/plugins/Dashboard/Dashboard.php b/plugins/Dashboard/Dashboard.php
index 7392ea119b..3caf0d13f5 100644
--- a/plugins/Dashboard/Dashboard.php
+++ b/plugins/Dashboard/Dashboard.php
@@ -30,10 +30,18 @@ class Dashboard extends \Piwik\Plugin
             'UsersManager.deleteUser'                => 'deleteDashboardLayout',
             'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
             'Widget.addWidgetConfigs'                => 'addWidgetConfigs',
-            'Category.addSubcategories'              => 'addSubcategories'
+            'Category.addSubcategories'              => 'addSubcategories',
+            'Widgetize.shouldEmbedIframeEmpty'       => 'shouldEmbedIframeEmpty'
         );
     }
 
+    public function shouldEmbedIframeEmpty(&$shouldEmbedEmpty, $controllerName, $actionName)
+    {
+        if ($controllerName == 'Dashboard' && $actionName == 'index') {
+            $shouldEmbedEmpty = true;
+        }
+    }
+
     public function addWidgetConfigs(&$widgets)
     {
         if (Piwik::isUserIsAnonymous()) {
diff --git a/plugins/Widgetize/Controller.php b/plugins/Widgetize/Controller.php
index cf6cb5114a..e639827915 100644
--- a/plugins/Widgetize/Controller.php
+++ b/plugins/Widgetize/Controller.php
@@ -8,9 +8,9 @@
  */
 namespace Piwik\Plugins\Widgetize;
 
-use Piwik\API\Request;
 use Piwik\Common;
 use Piwik\FrontController;
+use Piwik\Piwik;
 use Piwik\View;
 
 /**
@@ -27,7 +27,6 @@ class Controller extends \Piwik\Plugin\Controller
 
     public function iframe()
     {
-        Request::reloadAuthUsingTokenAuth();
         $this->init();
 
         $controllerName = Common::getRequestVar('moduleToWidgetize');
@@ -37,7 +36,29 @@ class Controller extends \Piwik\Plugin\Controller
             throw new \Exception("Widgetizing API requests is not supported for security reasons. Please change query parameter 'moduleToWidgetize'.");
         }
 
-        if ($controllerName == 'Dashboard' && $actionName == 'index') {
+        $shouldEmbedEmpty = false;
+
+        /**
+         * Triggered to detect whether a widgetized report should be wrapped in the widgetized HTML or whether only
+         * the rendered output of the controller/action should be printed. Set `$shouldEmbedEmpty` to `true` if
+         * your widget renders the full HTML itself.
+         *
+         * **Example**
+         *
+         *     public function embedIframeEmpty(&$shouldEmbedEmpty, $controllerName, $actionName)
+         *     {
+         *         if ($controllerName == 'Dashboard' && $actionName == 'index') {
+         *             $shouldEmbedEmpty = true;
+         *         }
+         *     }
+         *
+         * @param string &$shouldEmbedEmpty Defines whether the iframe should be embedded empty or wrapped within the widgetized html.
+         * @param string $controllerName    The name of the controller that will be executed.
+         * @param string $actionName        The name of the action within the controller that will be executed.
+         */
+        Piwik::postEvent('Widgetize.shouldEmbedIframeEmpty', array(&$shouldEmbedEmpty, $controllerName, $actionName));
+
+        if ($shouldEmbedEmpty) {
             $view = new View('@Widgetize/iframe_empty');
         } else {
             $view = new View('@Widgetize/iframe');
-- 
GitLab