Skip to content

Commit

Permalink
Merge branch 'release/2.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 2, 2020
2 parents 8ed2a50 + 36463fd commit 7f89d9f
Show file tree
Hide file tree
Showing 14 changed files with 690 additions and 18 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# v2.2.1
## 12/02/2020

1. [](#new)
* Require Grav 1.6.0+
* Added autoloader
* Pass phpstan level 3 tests
1. [](#bugfix)
* Fixed `header_keys_ignored` configuration option not taking effect

# v2.2.0
## 09/14/2020

Expand Down Expand Up @@ -149,7 +159,7 @@
1. [](#improved)
* Added support for Grav's autoescape twig setting
1. [](#bugfix)
* Fix searching on `@self `[#53](https://github.com/getgrav/grav-plugin-simplesearch/pull/53)
* Fix searching on `@self `[#53](https://github.com/getgrav/grav-plugin-simplesearch/pull/53)

# v1.8.0
## 07/14/2016
Expand Down
7 changes: 6 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: SimpleSearch
version: 2.2.0
type: plugin
slug: simplesearch
version: 2.2.1
description: "Don't be fooled, the **SimpleSearch** plugin provides a **fast** and highly **configurable** way to search your content."
icon: search
author:
Expand All @@ -12,6 +14,9 @@ keywords: simplesearch, plugin, search, page, content, find
bugs: https://github.com/getgrav/grav-plugin-simplesearch/issues
license: MIT

dependencies:
- { name: grav, version: '>=1.6.0' }

form:
validation: strict
fields:
Expand Down
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "getgrav/grav-plugin-simplesearch",
"type": "grav-plugin",
"description": "Simple search plugin for Grav CMS",
"keywords": ["simplesearch", "search", "plugin"],
"homepage": "https://github.com/getgrav/grav-plugin-simplesearch",
"license": "MIT",
"authors": [
{
"name": "Team Grav",
"email": "[email protected]",
"homepage": "https://getgrav.org",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/getgrav/grav-plugin-simplesearch/issues",
"irc": "https://chat.getgrav.org",
"forum": "https://discourse.getgrav.org",
"docs": "https://github.com/getgrav/grav-plugin-simplesearch/blob/master/README.md"
},
"require": {
"php": ">=7.1.3"
},
"autoload": {
"classmap": [
"simplesearch.php"
]
},
"config": {
"platform": {
"php": "7.1.3"
}
}
}
22 changes: 22 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 42 additions & 16 deletions simplesearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

namespace Grav\Plugin;

use Grav\Common\Config\Config;
use Grav\Common\Data\Data;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Page\Types;
use Grav\Common\Plugin;
use Grav\Common\Taxonomy;
use Grav\Common\Uri;
use Grav\Common\Utils;
use RocketTheme\Toolbox\Event\Event;

class SimplesearchPlugin extends Plugin
Expand Down Expand Up @@ -44,6 +42,8 @@ public static function getSubscribedEvents()

/**
* Add page template types. (for Admin plugin)
*
* @return void
*/
public function onGetPageTemplates(Event $event)
{
Expand All @@ -55,6 +55,8 @@ public function onGetPageTemplates(Event $event)

/**
* Add current directory to twig lookup paths.
*
* @return void
*/
public function onTwigTemplatePaths()
{
Expand All @@ -63,6 +65,8 @@ public function onTwigTemplatePaths()

/**
* Enable search only if url matches to the configuration.
*
* @return void
*/
public function onPluginsInitialized()
{
Expand All @@ -79,6 +83,8 @@ public function onPluginsInitialized()

/**
* Build search results.
*
* @return void
*/
public function onPagesInitialized()
{
Expand Down Expand Up @@ -117,6 +123,7 @@ public function onPagesInitialized()
}

// Explode query into multiple strings. Drop empty values
// @phpstan-ignore-next-line
$this->query = array_filter(array_filter(explode(',', $query), 'trim'), 'strlen');

/** @var Taxonomy $taxonomy_map */
Expand All @@ -140,8 +147,8 @@ public function onPagesInitialized()
}
}

if (!$should_process || !$filters || $query === false || (count($filters) == 1 && !reset($filters))) {
/** @var \Grav\Common\Page\Pages $pages */
if (!$should_process || !$filters || $query === false || (count($filters) === 1 && !reset($filters))) {
/** @var Pages $pages */
$pages = $this->grav['pages'];
$this->collection = $pages->all();
} else {
Expand Down Expand Up @@ -179,12 +186,13 @@ public function onPagesInitialized()

//Drop unpublished pages, but do not drop unroutable pages right now to be able to search modular pages which are unroutable per se
$this->collection->published();
/** @var modular Pages $modularPageCollection */
/** @var Collection $modularPageCollection */
$modularPageCollection = $this->collection->copy();
//Get published modular pages
$modularPageCollection->modular();
foreach ($modularPageCollection as $cpage) {
if (!$cpage->parent()->published()) {
$parent = $cpage->parent();
if (!$parent || !$parent->published()) {
$modularPageCollection->remove($cpage);
}
}
Expand Down Expand Up @@ -262,6 +270,9 @@ public function onPagesInitialized()
/**
* Filter the pages, and return only the pages the user has access to.
* Implementation based on Login Plugin authorizePage() function.
*
* @param Collection $collection
* @return Collection
*/
public function checkForPermissions($collection)
{
Expand Down Expand Up @@ -310,9 +321,9 @@ public function checkForPermissions($collection)
}

/**
* @param $query
* @param string $query
* @param Page $page
* @param $taxonomies
* @param array|false $taxonomies
* @return bool
*/
private function notFound($query, $page, $taxonomies)
Expand All @@ -321,6 +332,7 @@ private function notFound($query, $page, $taxonomies)
$results = true;
$search_content = $this->config->get('plugins.simplesearch.search_content');

$result = null;
foreach ($searchable_types as $type => $enabled) {
if ($type === 'title' && $enabled) {
$result = $this->matchText(strip_tags($page->title()), $query) === false;
Expand All @@ -344,25 +356,30 @@ private function notFound($query, $page, $taxonomies)
}
$result = !$taxonomy_match;
} elseif ($type === 'content' && $enabled) {
if ($search_content == 'raw') {
if ($search_content === 'raw') {
$content = $page->rawMarkdown();
} else {
$content = $page->content();
}
$result = $this->matchText(strip_tags($content), $query) === false;
} elseif ($type == 'header' && $enabled) {
} elseif ($type === 'header' && $enabled) {
$header = (array) $page->header();
$content = $this->getArrayValues($header);
$result = $this->matchText(strip_tags($content), $query) === false;
}
$results = $results && $result;
$results = (bool)$result;
if ($results === false) {
break;
}
}
return $results;
}

/**
* @param string $haystack
* @param string $needle
* @return false|int
*/
private function matchText($haystack, $needle)
{
if ($this->config->get('plugins.simplesearch.ignore_accented_characters')) {
Expand All @@ -374,13 +391,15 @@ private function matchText($haystack, $needle)
}
setlocale(LC_ALL, '');
return $result;
} else {
return mb_stripos($haystack, $needle);
}

return mb_stripos($haystack, $needle);
}

/**
* Set needed variables to display the search results.
*
* @return void
*/
public function onTwigSiteVariables()
{
Expand All @@ -400,15 +419,22 @@ public function onTwigSiteVariables()
}
}

/**
* @param array $array
* @param array|null $ignore_keys
* @param int $level
* @return string
*/
protected function getArrayValues($array, $ignore_keys = null, $level = 0) {
$output = '';

if (is_null($ignore_keys)) {
$ignore_keys = $this->config()->header_keys_ignored;
$config = $this->config();
$ignore_keys = $config['header_keys_ignored'] ?? ['title', 'taxonomy','content', 'form', 'forms', 'media_order'];
}
foreach ($array as $key => $child) {

if ($level === 0 && in_array($key, $ignore_keys)) {
if ($level === 0 && in_array($key, $ignore_keys, true)) {
continue;
}

Expand Down
7 changes: 7 additions & 0 deletions vendor/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInitca0f44aa14891d4b4dfc9b834c0e0763::getLoader();
Loading

0 comments on commit 7f89d9f

Please sign in to comment.