From 4add45375069db424ea7194d74978f69f2395047 Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 2 Feb 2019 00:32:57 +0300 Subject: [PATCH] Flextype Core: Shortcodes - Refactoring - Shortcodes moved into folder /shortcodes/ - Method addDefaultShortcodes() fixed according to Scrutinizer tests. --- flextype/Shortcodes.php | 100 ++------------------- flextype/shortcodes/ImagesShortcode.php | 61 +++++++++++++ flextype/shortcodes/ImagesUrlShortcode.php | 54 +++++++++++ flextype/shortcodes/SiteUrlShortcode.php | 22 +++++ flextype/shortcodes/SnippetsShortcode.php | 22 +++++ 5 files changed, 166 insertions(+), 93 deletions(-) create mode 100644 flextype/shortcodes/ImagesShortcode.php create mode 100644 flextype/shortcodes/ImagesUrlShortcode.php create mode 100644 flextype/shortcodes/SiteUrlShortcode.php create mode 100644 flextype/shortcodes/SnippetsShortcode.php diff --git a/flextype/Shortcodes.php b/flextype/Shortcodes.php index 11ad824f..49319b71 100644 --- a/flextype/Shortcodes.php +++ b/flextype/Shortcodes.php @@ -12,8 +12,8 @@ namespace Flextype; -use Flextype\Component\Http\Http; use Flextype\Component\Event\Event; +use Flextype\Component\Filesystem\Filesystem; use Thunder\Shortcode\ShortcodeFacade; use Thunder\Shortcode\Shortcode\ShortcodeInterface; @@ -114,99 +114,13 @@ class Shortcodes { */ private static function addDefaultShortcodes() : void { - // Shortcode: [site_url] - Shortcodes::shortcode()->addHandler('site_url', function() { - return Http::getBaseUrl(); - }); + // Get Default Shortocdes List + $shortcodes_list = Filesystem::listContents(ROOT_DIR . '/flextype/shortcodes'); - // Snippets - // Shortcode: [snippets get=snippet-name] - Shortcodes::shortcode()->addHandler('snippets', function(ShortcodeInterface $s) { - return Snippets::get($s->getParameter('get')); - }); - - // Images - // Shortcode: [images path="home/image.jpg"] - // Result: Display image - Shortcodes::shortcode()->addHandler('images', function(ShortcodeInterface $s) { - $params = []; - $attributes = []; - - // API - // http://glide.thephpleague.com/1.0/api/quick-reference/ - ($s->getParameter('or')) and $params['or'] = $s->getParameter('or'); - ($s->getParameter('flip')) and $params['flip'] = $s->getParameter('flip'); - ($s->getParameter('crop')) and $params['crop'] = $s->getParameter('crop'); - ($s->getParameter('w')) and $params['w'] = $s->getParameter('w'); - ($s->getParameter('h')) and $params['h'] = $s->getParameter('h'); - ($s->getParameter('fit')) and $params['fit'] = $s->getParameter('fit'); - ($s->getParameter('dpr')) and $params['dpr'] = $s->getParameter('dpr'); - ($s->getParameter('bri')) and $params['bri'] = $s->getParameter('bri'); - ($s->getParameter('con')) and $params['con'] = $s->getParameter('con'); - ($s->getParameter('gam')) and $params['gam'] = $s->getParameter('gam'); - ($s->getParameter('sharp')) and $params['sharp'] = $s->getParameter('sharp'); - ($s->getParameter('blur')) and $params['blur'] = $s->getParameter('blur'); - ($s->getParameter('pixel')) and $params['pixel'] = $s->getParameter('pixel'); - ($s->getParameter('filt')) and $params['filt'] = $s->getParameter('filt'); - ($s->getParameter('mark')) and $params['mark'] = $s->getParameter('mark'); - ($s->getParameter('markw')) and $params['markw'] = $s->getParameter('markw'); - ($s->getParameter('markh')) and $params['markh'] = $s->getParameter('markh'); - ($s->getParameter('markx')) and $params['markx'] = $s->getParameter('markx'); - ($s->getParameter('marky')) and $params['marky'] = $s->getParameter('marky'); - ($s->getParameter('markpad')) and $params['markpad'] = $s->getParameter('markpad'); - ($s->getParameter('markpos')) and $params['markpos'] = $s->getParameter('markpos'); - ($s->getParameter('markalpha')) and $params['markalpha'] = $s->getParameter('markalpha'); - ($s->getParameter('bg')) and $params['bg'] = $s->getParameter('bg'); - ($s->getParameter('border')) and $params['border'] = $s->getParameter('border'); - ($s->getParameter('q')) and $params['q'] = $s->getParameter('q'); - ($s->getParameter('fm')) and $params['fm'] = $s->getParameter('fm'); - - ($s->getParameter('width')) and $attributes['width'] = $s->getParameter('width'); - ($s->getParameter('height')) and $attributes['height'] = $s->getParameter('height'); - ($s->getParameter('class')) and $attributes['class'] = $s->getParameter('class'); - ($s->getParameter('id')) and $attributes['id'] = $s->getParameter('id'); - ($s->getParameter('alt')) and $attributes['alt'] = $s->getParameter('alt'); - - return Images::getImage($s->getParameter('path'), $params, $attributes); - }); - - // Images - // Shortcode: [images_url path="home/image.jpg"] - // Result: Display image url - Shortcodes::shortcode()->addHandler('images_url', function(ShortcodeInterface $s) { - $params = []; - - // API - // http://glide.thephpleague.com/1.0/api/quick-reference/ - ($s->getParameter('or')) and $params['or'] = $s->getParameter('or'); - ($s->getParameter('flip')) and $params['flip'] = $s->getParameter('flip'); - ($s->getParameter('crop')) and $params['crop'] = $s->getParameter('crop'); - ($s->getParameter('w')) and $params['w'] = $s->getParameter('w'); - ($s->getParameter('h')) and $params['h'] = $s->getParameter('h'); - ($s->getParameter('fit')) and $params['fit'] = $s->getParameter('fit'); - ($s->getParameter('dpr')) and $params['dpr'] = $s->getParameter('dpr'); - ($s->getParameter('bri')) and $params['bri'] = $s->getParameter('bri'); - ($s->getParameter('con')) and $params['con'] = $s->getParameter('con'); - ($s->getParameter('gam')) and $params['gam'] = $s->getParameter('gam'); - ($s->getParameter('sharp')) and $params['sharp'] = $s->getParameter('sharp'); - ($s->getParameter('blur')) and $params['blur'] = $s->getParameter('blur'); - ($s->getParameter('pixel')) and $params['pixel'] = $s->getParameter('pixel'); - ($s->getParameter('filt')) and $params['filt'] = $s->getParameter('filt'); - ($s->getParameter('mark')) and $params['mark'] = $s->getParameter('mark'); - ($s->getParameter('markw')) and $params['markw'] = $s->getParameter('markw'); - ($s->getParameter('markh')) and $params['markh'] = $s->getParameter('markh'); - ($s->getParameter('markx')) and $params['markx'] = $s->getParameter('markx'); - ($s->getParameter('marky')) and $params['marky'] = $s->getParameter('marky'); - ($s->getParameter('markpad')) and $params['markpad'] = $s->getParameter('markpad'); - ($s->getParameter('markpos')) and $params['markpos'] = $s->getParameter('markpos'); - ($s->getParameter('markalpha')) and $params['markalpha'] = $s->getParameter('markalpha'); - ($s->getParameter('bg')) and $params['bg'] = $s->getParameter('bg'); - ($s->getParameter('border')) and $params['border'] = $s->getParameter('border'); - ($s->getParameter('q')) and $params['q'] = $s->getParameter('q'); - ($s->getParameter('fm')) and $params['fm'] = $s->getParameter('fm'); - - return Images::getImageUrl($s->getParameter('path'), $params); - }); + // Include default shortcodes + foreach ($shortcodes_list as $shortcode) { + include_once $shortcode['path']; + } } /** diff --git a/flextype/shortcodes/ImagesShortcode.php b/flextype/shortcodes/ImagesShortcode.php new file mode 100644 index 00000000..041372a7 --- /dev/null +++ b/flextype/shortcodes/ImagesShortcode.php @@ -0,0 +1,61 @@ + + * @link http://flextype.org + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +use Thunder\Shortcode\ShortcodeFacade; +use Thunder\Shortcode\Shortcode\ShortcodeInterface; + +// Images +// Shortcode: [images path="home/image.jpg"] +// Result: Display image +Shortcodes::shortcode()->addHandler('images', function(ShortcodeInterface $s) { + $params = []; + $attributes = []; + + // API + // http://glide.thephpleague.com/1.0/api/quick-reference/ + ($s->getParameter('or')) and $params['or'] = $s->getParameter('or'); + ($s->getParameter('flip')) and $params['flip'] = $s->getParameter('flip'); + ($s->getParameter('crop')) and $params['crop'] = $s->getParameter('crop'); + ($s->getParameter('w')) and $params['w'] = $s->getParameter('w'); + ($s->getParameter('h')) and $params['h'] = $s->getParameter('h'); + ($s->getParameter('fit')) and $params['fit'] = $s->getParameter('fit'); + ($s->getParameter('dpr')) and $params['dpr'] = $s->getParameter('dpr'); + ($s->getParameter('bri')) and $params['bri'] = $s->getParameter('bri'); + ($s->getParameter('con')) and $params['con'] = $s->getParameter('con'); + ($s->getParameter('gam')) and $params['gam'] = $s->getParameter('gam'); + ($s->getParameter('sharp')) and $params['sharp'] = $s->getParameter('sharp'); + ($s->getParameter('blur')) and $params['blur'] = $s->getParameter('blur'); + ($s->getParameter('pixel')) and $params['pixel'] = $s->getParameter('pixel'); + ($s->getParameter('filt')) and $params['filt'] = $s->getParameter('filt'); + ($s->getParameter('mark')) and $params['mark'] = $s->getParameter('mark'); + ($s->getParameter('markw')) and $params['markw'] = $s->getParameter('markw'); + ($s->getParameter('markh')) and $params['markh'] = $s->getParameter('markh'); + ($s->getParameter('markx')) and $params['markx'] = $s->getParameter('markx'); + ($s->getParameter('marky')) and $params['marky'] = $s->getParameter('marky'); + ($s->getParameter('markpad')) and $params['markpad'] = $s->getParameter('markpad'); + ($s->getParameter('markpos')) and $params['markpos'] = $s->getParameter('markpos'); + ($s->getParameter('markalpha')) and $params['markalpha'] = $s->getParameter('markalpha'); + ($s->getParameter('bg')) and $params['bg'] = $s->getParameter('bg'); + ($s->getParameter('border')) and $params['border'] = $s->getParameter('border'); + ($s->getParameter('q')) and $params['q'] = $s->getParameter('q'); + ($s->getParameter('fm')) and $params['fm'] = $s->getParameter('fm'); + + ($s->getParameter('width')) and $attributes['width'] = $s->getParameter('width'); + ($s->getParameter('height')) and $attributes['height'] = $s->getParameter('height'); + ($s->getParameter('class')) and $attributes['class'] = $s->getParameter('class'); + ($s->getParameter('id')) and $attributes['id'] = $s->getParameter('id'); + ($s->getParameter('alt')) and $attributes['alt'] = $s->getParameter('alt'); + + return Images::getImage($s->getParameter('path'), $params, $attributes); +}); diff --git a/flextype/shortcodes/ImagesUrlShortcode.php b/flextype/shortcodes/ImagesUrlShortcode.php new file mode 100644 index 00000000..f22fc54d --- /dev/null +++ b/flextype/shortcodes/ImagesUrlShortcode.php @@ -0,0 +1,54 @@ + + * @link http://flextype.org + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +use Thunder\Shortcode\ShortcodeFacade; +use Thunder\Shortcode\Shortcode\ShortcodeInterface; + +// Images +// Shortcode: [images_url path="home/image.jpg"] +// Result: Display image url +Shortcodes::shortcode()->addHandler('images_url', function(ShortcodeInterface $s) { + $params = []; + + // API + // http://glide.thephpleague.com/1.0/api/quick-reference/ + ($s->getParameter('or')) and $params['or'] = $s->getParameter('or'); + ($s->getParameter('flip')) and $params['flip'] = $s->getParameter('flip'); + ($s->getParameter('crop')) and $params['crop'] = $s->getParameter('crop'); + ($s->getParameter('w')) and $params['w'] = $s->getParameter('w'); + ($s->getParameter('h')) and $params['h'] = $s->getParameter('h'); + ($s->getParameter('fit')) and $params['fit'] = $s->getParameter('fit'); + ($s->getParameter('dpr')) and $params['dpr'] = $s->getParameter('dpr'); + ($s->getParameter('bri')) and $params['bri'] = $s->getParameter('bri'); + ($s->getParameter('con')) and $params['con'] = $s->getParameter('con'); + ($s->getParameter('gam')) and $params['gam'] = $s->getParameter('gam'); + ($s->getParameter('sharp')) and $params['sharp'] = $s->getParameter('sharp'); + ($s->getParameter('blur')) and $params['blur'] = $s->getParameter('blur'); + ($s->getParameter('pixel')) and $params['pixel'] = $s->getParameter('pixel'); + ($s->getParameter('filt')) and $params['filt'] = $s->getParameter('filt'); + ($s->getParameter('mark')) and $params['mark'] = $s->getParameter('mark'); + ($s->getParameter('markw')) and $params['markw'] = $s->getParameter('markw'); + ($s->getParameter('markh')) and $params['markh'] = $s->getParameter('markh'); + ($s->getParameter('markx')) and $params['markx'] = $s->getParameter('markx'); + ($s->getParameter('marky')) and $params['marky'] = $s->getParameter('marky'); + ($s->getParameter('markpad')) and $params['markpad'] = $s->getParameter('markpad'); + ($s->getParameter('markpos')) and $params['markpos'] = $s->getParameter('markpos'); + ($s->getParameter('markalpha')) and $params['markalpha'] = $s->getParameter('markalpha'); + ($s->getParameter('bg')) and $params['bg'] = $s->getParameter('bg'); + ($s->getParameter('border')) and $params['border'] = $s->getParameter('border'); + ($s->getParameter('q')) and $params['q'] = $s->getParameter('q'); + ($s->getParameter('fm')) and $params['fm'] = $s->getParameter('fm'); + + return Images::getImageUrl($s->getParameter('path'), $params); +}); diff --git a/flextype/shortcodes/SiteUrlShortcode.php b/flextype/shortcodes/SiteUrlShortcode.php new file mode 100644 index 00000000..bc143c99 --- /dev/null +++ b/flextype/shortcodes/SiteUrlShortcode.php @@ -0,0 +1,22 @@ + + * @link http://flextype.org + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +use Flextype\Component\Http\Http; +use Thunder\Shortcode\ShortcodeFacade; +use Thunder\Shortcode\Shortcode\ShortcodeInterface; + +// Shortcode: [site_url] +Shortcodes::shortcode()->addHandler('site_url', function() { + return Http::getBaseUrl(); +}); diff --git a/flextype/shortcodes/SnippetsShortcode.php b/flextype/shortcodes/SnippetsShortcode.php new file mode 100644 index 00000000..20f440b2 --- /dev/null +++ b/flextype/shortcodes/SnippetsShortcode.php @@ -0,0 +1,22 @@ + + * @link http://flextype.org + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flextype; + +use Thunder\Shortcode\ShortcodeFacade; +use Thunder\Shortcode\Shortcode\ShortcodeInterface; + +// Snippets +// Shortcode: [snippets get=snippet-name] +Shortcodes::shortcode()->addHandler('snippets', function(ShortcodeInterface $s) { + return Snippets::get($s->getParameter('get')); +});