-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3a1215a
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Template Plumbing | ||
|
||
This little tool gives you an easy way to organize your page (or custom post type) templates better. By "templates" I mean the templates you can select from the "Templates" dropdown in the editor. | ||
|
||
## Usage | ||
|
||
First, install the package: | ||
|
||
```bash | ||
composer require livy/plumbing-template | ||
``` | ||
|
||
Then call the tool like this, somewhere where it'll be run early (i.e. `functions.php`): | ||
|
||
```php | ||
Livy\Plumbing\Templates\register_template_directory('custom-templates'); | ||
``` | ||
|
||
By default, the plugin assumes you're changing the template for the `page` post type, but you can specify _any_ post type by passing a second parameter: | ||
|
||
```php | ||
// Define templates for the `event` post type: | ||
Livy\Plumbing\Templates\register_template_directory('event-templates', 'event'); | ||
``` | ||
|
||
## Limitations | ||
|
||
The tool looks in your template directory (i.e. the result of `get_template_directory()`) when looking for the directory you specify. If will silently fail if it can't find the directory, or if you try to do something clever (i.e. `../../../usr/bin`) for your template path. | ||
|
||
Currently this tool can only point to a single directory per post type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "livy/plumbing-templates", | ||
"description": "Put your page (or custom post type) templates in better directories. Works with Sage!", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ben Martinez-Bateman", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"files": ["src/functions.php"] | ||
}, | ||
"require": { | ||
"php": ">=7.0", | ||
"symfony/finder": "^4.0" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace Livy\Plumbing\Templates; | ||
|
||
use Symfony\Component\Finder\Finder; | ||
|
||
function register_template_directory(string $path, $post_type = 'page') | ||
{ | ||
add_filter("theme_{$post_type}_templates", function($templates) use ($path, $post_type) { | ||
return array_merge($templates, set_template_directory($path, $post_type)); | ||
}); | ||
} | ||
|
||
function set_template_directory(string $path, string $post_type = 'page') | ||
{ | ||
$checked_path = check_path_segment($path); | ||
$full_path = realpath(get_wordpress_template_directory($post_type) . DIRECTORY_SEPARATOR . $checked_path) ?: ''; | ||
|
||
// Make sure you're not trying to go somewhere weird | ||
if (0 !== strpos($full_path, get_wordpress_template_directory($post_type))) { | ||
return []; | ||
} | ||
|
||
$templates = []; | ||
|
||
if (is_dir($full_path)) { | ||
$inline_identifier = get_inline_identifier($post_type); | ||
$finder = new Finder(); | ||
$finder->files()->in($full_path)->name(get_file_extenstion($post_type)); | ||
foreach ($finder as $file) { | ||
|
||
// Set $name so it'll be sure to have a value. | ||
$name = false; | ||
|
||
// Look in each file to find the name; leave as soon as we have it. | ||
if ($opened_file = fopen($file->getRealPath(), 'r')) { | ||
while (($line = fgets($opened_file)) !== false) { | ||
if (strpos($line, $inline_identifier)) { | ||
$name = trim(str_replace($inline_identifier, '', $line)); | ||
break; | ||
} | ||
} | ||
} | ||
fclose($opened_file); | ||
|
||
// Only add to list if we have a viable $name. | ||
if ($name) { | ||
$templates[$checked_path . $file->getFilename()] = $name; | ||
} | ||
|
||
// Don't want to accidentally pass these to the next iteration. | ||
unset($name, $opened_file); | ||
} | ||
} | ||
|
||
return $templates; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
function get_wordpress_template_directory($post_type) | ||
{ | ||
return apply_filters('template-dir/theme-directory', get_template_directory(), $post_type); | ||
} | ||
|
||
function get_inline_identifier($post_type) | ||
{ | ||
return apply_filters('template-dir/inline-identifier', 'Template Name:', $post_type); | ||
} | ||
|
||
function get_file_extenstion($post_type) | ||
{ | ||
return apply_filters('template-dir/file-extension', '*.php', $post_type); | ||
} | ||
|
||
function check_path_segment(string $path) | ||
{ | ||
// Need a trailing slash. | ||
$trailingslashed = trailingslashit($path); | ||
|
||
// Don't want a leading slash. | ||
$frontchecked = ltrim($trailingslashed, '\\/'); | ||
return $frontchecked; | ||
} |