Summary
Snippet calls are also output with the search results output.
Step to reproduce
Add a PHP snippet to any page and search for it on Simple-Search.
Observed behavior
Image 1 = Image 1
Image 2 = Image 2
Expected behavior
In my view, Simple Search should either process the snippets OR not output them.
Environment
simplesearch-2.1.2-pl
modx-version 2.8.1
I can’t reproduce this.
Where do you see the tags? In the extract?
Can you give a specific example (page content, search result template)?
See linked images above. Yes, you can see it in the search results. A part of the snippet is displayed in the search results.
Content on the testsite “dev123” (You can also use another snippet.):
[[pdoField?
&id=[[*id]]
&field=longtitle
&top=2
]]
Output on the dev123 Site > longtitle from the page, level top 2 > it works fine
Snippet-Call on the Search-Result-Site:
[[!SimpleSearchForm]]
[[!SimpleSearch]]
Output on simple search:
1. [dev123]
&field=
longtitle &top=
2`
Another test
Content on the testsite:
[[!If?
&subject=`[[#2.pagetitle]]`
&operator=`EQ`
&operand=`Kontakt`
&then=`Jup, its the site Kontakt`
&else=`Nope, it's not`
]]
Output on the site:
https://www.visions.ch/dev123/
Search-Result:
https://www.visions.ch/test.html?search=dev123&id=476
I can see that it doesn’t work on your website. Unfortunately when I test it, it works as expected and the tags are deleted.
The tags are deleted here in the code
}
/**
* Sanitize a string
*
* @param string $text The text to sanitize
* @return string The sanitized text
*/
public function sanitize($text) {
$text = strip_tags($text);
$text = preg_replace('/(\[\[\+.*?\]\])/i', '', $text);
return $this->modx->stripTags($text);
}
/**
* Create an extract from the passed text parameter
*
* @param string $text The text that the extract will be created from.
* @param int $length The length of the extract to be generated.
* @param string $search The search term to center the extract around.
and then here again
$extract = preg_replace("#\[\[(.*?)\]\]#si", '', $extract);
$extract = str_replace(array('[[',']]'), '', $extract);
No idea, why it doesn’t work on your site.
Yes, this is also included in the code for simplesearch. Exactly the same as yours.
I don’t know why it doesn’t work for me.
In the meantime, I have also tested it on another site, the same problem, but other server requirements, other MODX version.
So what I found out: The double [[ characters generate the problem.
That doesn’t work:
[[!If?
&subject=`[[#2.pagetitle]]`
&operator=`EQ`
&operand=`Kontakt`
&then=`Jup, its the site Kontakt`
&else=`Nope, it's not`
]]
That works fine:
[[!If?
&subject=`15`
&operator=`EQ`
&operand=`Kontakt`
&then=`Jup, its the site Kontakt`
&else=`Nope, it's not`
]]
Solution:
Old:
$text = preg_replace('/(\[\[\+.*?\]\])/i', '', $text);
New:
$text = preg_replace("/\[[^][]*+(?:(?R)[^][]*)*+]/i", '', $text);
Glad you found a solution.
But I mistakenly linked the wrong line in the code.
It’s actually the following line ($this->modx->stripTags($text);
) that removes the MODX tags (including nested tags).
As this is a function of the MODX core code, I’m suprised that it doesn’t work.
If you create a snippet with this code:
<?php
$text = "text before[[!If? &subject=`[[#2.pagetitle]]` &operator=`EQ` &operand=`Kontakt` &then=`Jup, its the site Kontakt` &else=`Nope, it's not`]]text after";
return $modx->stripTags($text);
Does it remove the MODX tags correctly?