Restores the ability to edit WYSIWYG zones which are not directly in the displayed document, disabled in Foncierpedia request #5335
Requested in Foncierpedia request #5851
Depends on parser_context_for_WYSIWYG.diff
Index: MERN/lib/wiki/wikilib.php
===================================================================
--- MERN.orig/lib/wiki/wikilib.php
+++ MERN/lib/wiki/wikilib.php
@@ -512,6 +512,9 @@ class WikiLib extends TikiLib
 
 		$wiki_cache = ($prefs['feature_wiki_icache'] == 'y' && !is_null($info['wiki_cache'])) ? $info['wiki_cache'] : $prefs['wiki_cache'];
 
+		require_once 'lib/core/Entity/WikiPage.php'; // autoload seems broken for Entity classes.
+		$entity = new Entity_WikiPage($page);
+		$field = new WikiParser_Context_Field(new WikiParser_Process_Display(), $entity);
 		if ($wiki_cache > 0 && empty($_REQUEST['offset']) && empty($_REQUEST['itemId']) && (empty($user) || $prefs['wiki_cache'] == 0) ) {
 			$cache_info = $this->get_cache_info($page);
 			if (!empty($cache_info['cache_timestamp']) && $cache_info['cache_timestamp'] + $wiki_cache >= $this->now) {
@@ -533,7 +536,7 @@ class WikiLib extends TikiLib
 				$jsFile1 = $headerlib->getJsFilesWithScriptTags();
 				$js1 = $headerlib->getJs();
 				$info['outputType'] = $tikilib->getOne ("SELECT `outputType` FROM `tiki_output` WHERE `entityId` = ? AND `objectType` = ? AND `version` = ?", array($info['pageName'], 'wikiPage', $info['version']));
-				$content = (new WikiLibOutput($info, $info['data'],$parse_options))->parsedValue;
+				$content = (new WikiLibOutput($info, $info['data'],$parse_options, $field))->parsedValue;
 
 				// get any JS added to headerlib during parse_data and add to the bottom of the data to cache
 				$jsFile2 = $headerlib->getJsFilesWithScriptTags();
@@ -548,7 +551,7 @@ class WikiLib extends TikiLib
 				$this->update_cache($page, $content . $jsFile . $js);
 			}
 		} else {
-            $content = (new WikiLibOutput($info, $info['data'], $parse_options, $info['version']))->parsedValue;
+            $content = (new WikiLibOutput($info, $info['data'], $parse_options, $field))->parsedValue;
 		}
 
 		return $content;
@@ -1879,7 +1882,7 @@ class WikiLibOutput
     private static $wikiLingo;
     private static $wikiLingoScripts;
 
-    public function __construct($info, $originalValue, $options = array())
+    public function __construct($info, $originalValue, $options = array(), $context = null)
     {
         $tikilib = TikiLib::lib('tiki');
         $prefslib = TikiLib::lib('prefs');
@@ -1944,7 +1947,11 @@ class WikiLibOutput
                 }
             }
         } else {
-            $this->parsedValue = $tikilib->parse_data($this->originalValue, $this->options = $options);
+        	if (is_null($context)) {
+        		$context = new WikiParser_Context_Unknown();
+			}
+			$content = new WikiParser_Parsable($this->originalValue, $context);
+            $this->parsedValue = $content->parse($this->options = $options);
         }
     }
 }
Index: MERN/lib/ckeditor_tiki/tiki-ckeditor.js
===================================================================
--- MERN.orig/lib/ckeditor_tiki/tiki-ckeditor.js
+++ MERN/lib/ckeditor_tiki/tiki-ckeditor.js
@@ -149,13 +149,13 @@ function checkWysiwygInlineEditingDirty(
 /**
  * Process divs set up by wikiplugin_wysiwyg
  */
-
-$.fn.wysiwygPlugin = function (execution, page, ckoption) {
+// When there are inclusions, numberOfCallInEntity may differ from globalExecution.
+$.fn.wysiwygPlugin = function (globalExecution, numberOfCallInEntity, page, ckoption) {
 
 	$(this).each(function () {
 		var $this = $(this);
 		var $edit_button = $("<button />")
-			.addClass("btn btn-primary edit_button_wp_wysiwyg_" + execution)
+			.addClass("btn btn-primary edit_button_wp_wysiwyg_" + globalExecution)
 			.text(tr("Edit"))
 			.button()
 			.mouseover(function () {
@@ -190,7 +190,7 @@ $.fn.wysiwygPlugin = function (execution
 						$("#outerToc-static").hide();
 
 						var ok = true;
-						$(".wp_wysiwyg:not(#wp_wysiwyg_" + execution + ")").each(function () {
+						$(".wp_wysiwyg:not(#wp_wysiwyg_" + globalExecution + ")").each(function () {
 							if (CKEDITOR.instances[$(this).attr("id")]) {
 								if (CKEDITOR.instances[$(this).attr("id")].checkDirty()) {
 									if (confirm(tr("You have unsaved changes in this WYSIWYG section.\nDo you want to save your changes?"))) {
@@ -213,25 +213,25 @@ $.fn.wysiwygPlugin = function (execution
 							// close others
 							var editor = event.editor;
 
-							if (editor.element.getId() != "wp_wysiwyg_" + execution) {
+							if (editor.element.getId() != "wp_wysiwyg_" + globalExecution) {
 								return;
 							}
 							var editorSelector = "#cke_" + editor.element.getId();
 
-							$(".button_wp_wysiwyg_" + execution).remove();
+							$(".button_wp_wysiwyg_" + globalExecution).remove();
 
 							$(editorSelector).after(
 									$("<button />")
-										.addClass("button_wp_wysiwyg_" + execution)
+										.addClass("button_wp_wysiwyg_" + globalExecution)
 										.text(tr("Cancel"))
 										.button()
 										.click(function () {
-											$(".button_wp_wysiwyg_" + execution).remove();
+											$(".button_wp_wysiwyg_" + globalExecution).remove();
 											editor.destroy();
 										})
 								).after(
 									$("<button />")
-										.addClass("button_wp_wysiwyg_" + execution)
+										.addClass("button_wp_wysiwyg_" + globalExecution)
 										.text(tr("Save"))
 										.button()
 										.click(function (event) {
@@ -254,7 +254,7 @@ $.fn.wysiwygPlugin = function (execution
 												page: page,
 												type: "wysiwyg",
 												message: "Modified by WYSIWYG Plugin",
-												index: execution,
+												index: numberOfCallInEntity,
 												content: data,
 												params: params
 											};
Index: MERN/lib/wiki-plugins/wikiplugin_wysiwyg.php
===================================================================
--- MERN.orig/lib/wiki-plugins/wikiplugin_wysiwyg.php
+++ MERN/lib/wiki-plugins/wikiplugin_wysiwyg.php
@@ -70,11 +70,9 @@ function wikiplugin_wysiwyg($data, $para
 	global $tiki_p_edit, $page, $prefs, $user;
 	static $execution = 0;
 
-	global $wikiplugin_included_page;
-	if (!empty($wikiplugin_included_page)) {
-		$sourcepage = $wikiplugin_included_page;
-	} else {
-		$sourcepage = $page;
+	$entity = $context->getEntity();
+	if (isset($entity)) {
+		$sourcepage = $entity->name;
 	}
 
 	if ($params['use_html'] !== 'y') {
@@ -87,7 +85,9 @@ function wikiplugin_wysiwyg($data, $para
 	$content = new WikiParser_Parsable($data, $subContext, $context);
 	$visibleHTML = $content->parse(array('is_html' => $is_html));
 	
-	if (TikiLib::lib('tiki')->user_has_perm_on_object( $user, $sourcepage, 'wiki page', 'tiki_p_edit')) {
+	if (isset($entity) && TikiLib::lib('tiki')->user_has_perm_on_object( $user, $sourcepage, 'wiki page', 'tiki_p_edit')) {
+		$strictContext = $context->getContext()->getRoot('ignore');
+		$strictContext->WYSIWYGPluginCalls = $strictContext->WYSIWYGPluginCalls + 1;
 		$class = "wp_wysiwyg";
 		$exec_key = $class . '_' . ++ $execution;
 		$style = " style='min-width:{$params['width']};min-height:{$params['height']}'";
@@ -114,12 +114,10 @@ function wikiplugin_wysiwyg($data, $para
 		$editableSource = TikiLib::lib('edit')->parseToWysiwyg( $data, true, $is_html, array('page' => $sourcepage) ); // Not suitable for display. Les plugins tels que DIV à l'intérieur du plugin ne sont pas analysés ou s'affichent comme vides (bogue #6551).
 		$visibleHTML = "<div id='$exec_key' class='{$class}'$style data-initial='$namespace' data-html='{$params['use_html']}' data-source='" . smarty_modifier_escape($editableSource, 'attr') . "'>" . $visibleHTML . '</div>';
 
-		$js = '$("#' . $exec_key . '").wysiwygPlugin("' . $execution . '", "' . $sourcepage . '", ' . $ckoption . ');';
-		if (empty($wikiplugin_included_page)) { // Contourner bogue #6476 en attendant Tiki 18
-			TikiLib::lib('header')
-				->add_jsfile('lib/ckeditor_tiki/tiki-ckeditor.js')
-				->add_jq_onready($js);
-		}
+		$js = '$("#' . $exec_key . '").wysiwygPlugin("' . $execution . '", ' . $strictContext->WYSIWYGPluginCalls . ', "' . $sourcepage . '", ' . $ckoption . ');';
+		TikiLib::lib('header')
+			->add_jsfile('lib/ckeditor_tiki/tiki-ckeditor.js')
+			->add_jq_onready($js);
 	}
 	return $visibleHTML;
 
