diff --git a/flextype/Snippets.php b/flextype/Snippets.php index 6d1f5c06..d44df4d8 100644 --- a/flextype/Snippets.php +++ b/flextype/Snippets.php @@ -45,7 +45,56 @@ class Snippets $vars['fetch'] = $id; - return $this->_fetch_snippet($vars); + return $this->_display_snippet($vars); + } + + /** + * Fetch snippet + * + * @access public + * @param string $id Snippet id + * @return array|false The entry contents or false on failure. + */ + public function fetch(string $id) + { + $snippet_file = Fieldsets::_file_location($id); + + if (Filesystem::has($snippet_file)) { + if ($snippet_body = Filesystem::read($snippet_file)) { + return $snippet_body; + } else { + return false; + } + } else { + return false; + } + } + + /** + * Fetch Snippets + * + * @access public + * @return array + */ + public function fetchList() : array + { + $snippets = []; + + // Get snippets files + $_snippets = Filesystem::listContents($this->_dir_location()); + + // If there is any snippets file then go... + if (count($_snippets) > 0) { + foreach ($_snippets as $snippet) { + if ($snippet['type'] == 'file' && $snippet['extension'] == 'json') { + $snippet_content = JsonParser::decode(Filesystem::read($snippet['path'])); + $snippet[$snippet['basename']] = $snippet_content['title']; + } + } + } + + // return snippets + return $snippets; } /** diff --git a/flextype/shortcodes/SnippetsShortcode.php b/flextype/shortcodes/SnippetsShortcode.php index c3d065e3..e866d04a 100644 --- a/flextype/shortcodes/SnippetsShortcode.php +++ b/flextype/shortcodes/SnippetsShortcode.php @@ -15,7 +15,7 @@ namespace Flextype; use Thunder\Shortcode\ShortcodeFacade; use Thunder\Shortcode\Shortcode\ShortcodeInterface; -// Shortcode: [snippets fetch=snippet-name] +// Shortcode: [snippets id=snippet-name] $flextype['shortcodes']->addHandler('snippets', function(ShortcodeInterface $s) use ($flextype) { - return $flextype['shortcodes']->fetch($s->getParameter('fetch')); + return $flextype['shortcodes']->display($s->getParameter('id')); });