Skip to content

Commit

Permalink
Initial functionality complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
alwaysblank committed Mar 14, 2018
0 parents commit 3a1215a
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
.idea/
30 changes: 30 additions & 0 deletions README.md
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.
18 changes: 18 additions & 0 deletions composer.json
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"
}
}
69 changes: 69 additions & 0 deletions composer.lock

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

85 changes: 85 additions & 0 deletions src/functions.php
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;
}

0 comments on commit 3a1215a

Please sign in to comment.