Skip to content

Commit f387b2b

Browse files
committed
Update PicoFilePrefixes to Pico 2.0+
1 parent e7d6aef commit f387b2b

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

PicoFilePrefixes.php

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
<?php
2+
/**
3+
* This file is part of Pico. It's copyrighted by the contributors recorded
4+
* in the version control history of the file, available from the following
5+
* original location:
6+
*
7+
* <https://github.com/PhrozenByte/pico-file-prefixes/blob/master/PicoFilePrefixes.php>
8+
*
9+
* SPDX-License-Identifier: MIT
10+
* License-Filename: LICENSE
11+
*/
212

313
/**
414
* Pico file prefixes plugin - drop file prefixes from page URLs
@@ -15,22 +25,31 @@
1525
*/
1626
class PicoFilePrefixes extends AbstractPicoPlugin
1727
{
28+
/**
29+
* API version used by this plugin
30+
*
31+
* @var int
32+
*/
33+
const API_VERSION = 2;
34+
1835
/**
1936
* Regex pattern matching directory paths with prefixed files
37+
*
2038
* @var string
2139
*/
2240
protected $filePathRegex = '';
2341

2442
/**
2543
* List of pages whose URL has been altered
44+
*
2645
* @var array
2746
*/
2847
protected $prefixPages = array();
2948

3049
/**
31-
* Prepare the plugin's configuration and prepare the file path regex
50+
* Prepares the plugin's configuration and the file path regex
3251
*
33-
* @see DummyPlugin::onRequestFile()
52+
* @see DummyPlugin::onConfigLoaded()
3453
*/
3554
public function onConfigLoaded(array &$config)
3655
{
@@ -54,11 +73,11 @@ public function onConfigLoaded(array &$config)
5473
}
5574

5675
// prepare file path regex
57-
$this->filePathRegex = '#^(';
76+
$this->filePathRegex = '#^';
5877
if (!empty($config['PicoFilePrefixes']['recursiveDirs'])) {
5978
if (in_array('.', $config['PicoFilePrefixes']['recursiveDirs'])) {
6079
// enable plugin for any directory
61-
$this->filePathRegex = '#^(.+)$#';
80+
$this->filePathRegex = '#^.+$#';
6281
return;
6382
}
6483

@@ -75,13 +94,13 @@ public function onConfigLoaded(array &$config)
7594
return preg_quote($dir, '#');
7695
}, $config['PicoFilePrefixes']['dirs']));
7796
}
78-
$this->filePathRegex .= ')$#';
97+
$this->filePathRegex .= '$#';
7998
}
8099

81100
/**
82-
* Rewrite shortened URLs to their matching file on the filesystem
101+
* Rewrites shortened URLs to their matching file on the filesystem
83102
*
84-
* @see DummyPlugin::onRequestFile()
103+
* @see DummyPlugin::onRequestFile()
85104
*/
86105
public function onRequestFile(&$file)
87106
{
@@ -107,16 +126,12 @@ public function onRequestFile(&$file)
107126
}
108127

109128
/**
110-
* Alter URLs of prefixed files
129+
* Alters URLs of prefixed files
111130
*
112-
* @see DummyPlugin::onPagesLoaded()
131+
* @see DummyPlugin::onPagesLoaded()
113132
*/
114-
public function onPagesLoaded(
115-
array &$pages,
116-
array &$currentPage = null,
117-
array &$previousPage = null,
118-
array &$nextPage = null
119-
) {
133+
public function onPagesLoaded(array &$pages)
134+
{
120135
foreach ($pages as &$pageData) {
121136
$filePath = dirname($pageData['id']);
122137
if (preg_match($this->filePathRegex, $filePath)) {

README.md

+33-11
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,50 @@ Pico is a stupidly simple, blazing fast, flat file CMS. See http://picocms.org/
1010
Install
1111
-------
1212

13-
Just [download the latest release](https://github.com/PhrozenByte/pico-file-prefixes/releases/latest) and upload the `PicoFilePrefixes.php` file to the `plugins` directory of your Pico installation (e.g. `/var/www/html/pico/plugins/`). The plugin is also available on [Packagist.org](https://packagist.org/packages/phrozenbyte/pico-file-prefixes) and may be included in other projects via `composer require phrozenbyte/pico-file-prefixes`. The plugin requires Pico 1.0+
13+
You can either install `PicoFilePrefixes` using [Composer](https://getcomposer.org/), or using a single PHP plugin file. We recommend you to use Composer whenever possible, because it allows you to keep the plugin up-to-date way more easily.
14+
15+
If you use a Composer-based installation of Pico and want to either remove or install `PicoFilePrefixes`, simply open a shell on your server and navigate to Pico's install directory (e.g. `/var/www/html/pico/`). Run `composer remove phrozenbyte/pico-file-prefixes` to remove `PicoFilePrefixes`, or run `composer require phrozenbyte/pico-file-prefixes` (via [Packagist.org](https://packagist.org/packages/phrozenbyte/pico-file-prefixes)) to install `PicoFilePrefixes`.
16+
17+
If you really want to install `PicoFilePrefixes` using a single PHP plugin file, [download the latest release](https://github.com/PhrozenByte/pico-file-prefixes/releases/latest) and upload the `PicoFilePrefixes.php` file to the `plugins` directory of your Pico installation (e.g. `/var/www/html/pico/plugins/`).
18+
19+
`PicoFilePrefixes` requires Pico 2.0+
1420

1521
Config
1622
------
1723

18-
The plugin recursively drops file prefixes of all files in the `content/blog/` directory by default. You can specify other directories by altering the `$config['PicoFilePrefixes']['recursiveDirs']` and/or `$config['PicoFilePrefixes']['dirs']` config variables in your `config/config.php`. The former parses all files of a directory recursively (i.e. including all its subfolders), whereas the latter parses just files in this particular directory. The default configuration looks like the following:
24+
The plugin recursively drops file prefixes of all files in the `content/blog/` directory by default. You can specify other directories by altering the `PicoFilePrefixes.recursiveDirs` and/or `PicoFilePrefixes.dirs` config variables (both expect YAML lists) in your `config/config.php`. The former parses all files of a directory recursively (i.e. including all its subfolders), whereas the latter parses just files in this particular directory. The default configuration looks like the following:
1925

20-
```php
21-
$config['PicoFilePrefixes']['recursiveDirs'] = array('blog');
22-
$config['PicoFilePrefixes']['dirs'] = array();
26+
```yaml
27+
PicoFilePrefixes:
28+
recursiveDirs:
29+
- blog
30+
dirs: []
2331
```
2432
2533
If you want to additionally enable the plugin for the `content/showcase/` directory, try the following configuration:
2634

27-
```php
28-
$config['PicoFilePrefixes']['recursiveDirs'] = array('blog', 'showcase');
29-
$config['PicoFilePrefixes']['dirs'] = array();
35+
```yaml
36+
PicoFilePrefixes:
37+
recursiveDirs:
38+
- blog
39+
- showcase
40+
dirs: []
3041
```
3142

3243
If you want to enable the plugin for any folder, try the following:
3344

34-
```php
35-
$config['PicoFilePrefixes']['recursiveDirs'] = array('.');
36-
$config['PicoFilePrefixes']['dirs'] = array();
45+
```yaml
46+
PicoFilePrefixes:
47+
recursiveDirs:
48+
- .
49+
dirs: []
50+
```
51+
52+
To enable the plugin for pages in the `content/misc/` directory only (i.e. not including subfolders like `content/misc/sub/`), try the following:
53+
54+
```yaml
55+
PicoFilePrefixes:
56+
recursiveDirs: []
57+
dirs:
58+
- misc
3759
```

0 commit comments

Comments
 (0)