Skip to content

Commit

Permalink
Assets from shortcode {{% assets %}} are now being cached with page…
Browse files Browse the repository at this point in the history
… contents
  • Loading branch information
Sommerregen committed Oct 9, 2015
1 parent bd4f767 commit c0f748f
Show file tree
Hide file tree
Showing 6 changed files with 308 additions and 20 deletions.
160 changes: 160 additions & 0 deletions classes/GravProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* This file is part of Grav Shortcodes plugin.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*/

namespace Grav\Plugin\Shortcodes;

use Grav\Common\Grav;
use Grav\Common\Page\Page;
use RocketTheme\Toolbox\Event\Event;

class GravProxy implements \ArrayAccess
{
protected $object;
protected $page;
protected $name = [];
protected $callable;

/**
* Constructor.
*
* @param object $object [description]
* @param Page $page [description]
* @param [type] $name [description]
*/
public function __construct($object, $page, $callable = null, $name = null)
{
$this->object = $object;
$this->page = $page;
$this->callable = $callable;

if ($name) {
$this->name = is_array($name) ? $name : [$name];
}

if ($callable && !is_callable($callable)) {
throw new \Exception(sprintf("Function '%s' must be callable.", $callable));
}
}

public function __call($method, array $arguments = [])
{
if ($callable = $this->callable) {
$result = $callable([// new Event([
'type' => '__call',
'object' => $this->object,
'page' => $this->page,
'name' => implode('.', $this->name),
'arguments' => [$method, $arguments]
]);
}

return is_null($result) ? call_user_func_array([$this->object, $method], $arguments) : $result;
}

/**
* ArrayAccess implementation
*/

/**
* Whether or not an offset exists.
*
* @param mixed $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function offsetExists($offset)
{
if ($callable = $this->callable) {
$result = $callable([ //new Event([
'type' => 'offsetExists',
'object' => $this->object,
'page' => $this->page,
'name' => implode('.', $this->name),
'arguments' => [$offset]
]);
}

return is_null($result) ? isset($this->object[$offset]) : $result;
}

/**
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
if (isset($this->object[$offset])) {
if ($callable = $this->callable) {
$result = $callable([ // new Event([
'type' => 'offsetGet',
'object' => $this->object,
'page' => $this->page,
'name' => implode('.', $this->name),
'arguments' => [$offset]
]);
}

if (is_null($result)) {
$name = array_merge($this->name, [$offset]);
$result = new static($this->object[$offset], $this->page, $this->callable, $name);
}

return $result;
}
}

/**
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet($offset, $value)
{
if ($callable = $this->callable) {
$result = $callable([ //new Event([
'type' => 'offsetSet',
'object' => $object,
'page' => $this->page,
'name' => implode('.', $this->name),
'arguments' => [$offset]
]);
}

if (is_null($result)) {
if (is_null($offset)) {
$this->object[] = $value;
} else {
$this->object[$offset] = $value;
}
}
}

/**
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
*/
public function offsetUnset($offset)
{
if ($callable = $this->callable) {
$result = $callable([// new Event([
'type' => 'offsetUnset',
'object' => $this->object,
'page' => $this->page,
'name' => implode('.', $this->name),
'arguments' => [$offset]
]);
}

if (is_null($result)) {
unset($this->object[$offset]);
}
}
}
10 changes: 3 additions & 7 deletions classes/Shortcodes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,8 @@ public function execute(Event $event)
$type = strtolower($options->get('type'));
$body = trim(strip_tags($event['body'], '<link><script>'));

if ($inline = $options->get('inline')) {
if ($type === 'css') {
$grav['assets']->addInlineCss($body);
} elseif ($type === 'js') {
$grav['assets']->addInlineJs($body);
}
if ($options->get('inline')) {
$shortcodes->addExtra('assets', 'addInline'. ucfirst($type), $body);
} else {
/* @var \Grav\Common\Page\Page $page */
$page = $event['page'];
Expand Down Expand Up @@ -123,7 +119,7 @@ public function execute(Event $event)
}
}

$grav['assets']->{$name}($url, $priority, $pipeline, $loading);
$shortcodes->addExtra('assets', $name, [$url, $priority, $pipeline, $loading]);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions classes/Shortcodes/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function execute(Event $event)
/* @var \Grav\Common\Page\Page $page */
$page = $event['page'];

/* @var \Grav\Plugin\Shortcodes\Shortcodes $shortcodes */
$shortcodes = $event['shortcodes'];

/* @var \Grav\Common\Data\Data $options */
$options = $event['options'];
$options->setDefaults($this->defaults);
Expand Down
82 changes: 82 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# German
de:
PLUGINS:
SHORTCODES:
GLOBAL_CONFIG: "Globale Einstellungen"
DEFAULT_CONFIG: "Standardeinstellungen für Shortcodes"
PLUGIN_STATUS: "Plugin Status"

OPTIONS: "Optionen"

ASSETS: "Assets"
ASSETS_ENABLED: "<code>{{% assets %}}</code> Shortcode aktivieren"
ASSETS_OPTIONS:
TYPE: "Assets-Typ"
CSS: "CSS"
JS: "JavaScript"
INLINE: "Assets als Inline-Element"
PRIORITY: "Assets Priorität"
PRIORITY_HELP: "Priorität des Assets, wenn sie zur Grav Pipeline hinzugefügt werden; größere Werte werden zuerst geladen"
PIPELINE: "Kombiniere Assets"
LOAD: "Ladeverhalten"
LOAD_HELP: 'Lade Assets asynchron "async" oder verzögert "defer"'
DEFAULT: "- Standard -"
ASYNC: "Async - asynchrones Laden von JavaScript"
DEFER: "Defer - verzögertes Laden von JavaScript"

COMMENT: "Comment"
COMMENT_ENABLED: "<code>{{% comment %}}</code> Shortcode aktivieren"

EMBED: "Embed"
EMBED_ENABLED: "<code>{{% embed %}}</code> Shortcode aktivieren"
EMBED_TEMPLATE: "Standard Seiten-Template"

MARKDOWN: "Markdown"
MARKDOWN_ENABLED: "<code>{{% markdown %}}</code> Shortcode aktivieren"

SUMMARY: "Summary"
SUMMARY_ENABLED: "<code>{{% summary %}}</code> Shortcode aktivieren"

TWIG: "Twig"
TWIG_ENABLED: "<code>{{% twig %}}</code> Shortcode aktivieren"

# English
en:
PLUGINS:
SHORTCODES:
GLOBAL_CONFIG: "Global plugin configurations"
DEFAULT_CONFIG: "Default values for Shortcodes configuration"
PLUGIN_STATUS: "Plugin status"
OPTIONS: "Options"

ASSETS: "Assets"
ASSETS_ENABLED: "Activate <code>{{% assets %}}</code> shortcode"
ASSETS_OPTIONS:
TYPE: "Default assets type"
CSS: "CSS"
JS: "JavaScript"
INLINE: "Inline assets by default"
PRIORITY: "Default assets priority"
PRIORITY_HELP: "Priority to add CSS or JS to Grav pipeline; bigger comes first"
PIPELINE: "Pipeline assets"
LOAD: "Load behavior"
LOAD_HELP: 'Load asset either asynchronously "async" or deferred "defer"'
DEFAULT: "- default -"
ASYNC: "Async - asynchronously load JavaScript"
DEFER: "Defer - deferred loading of JavaScript"

COMMENT: "Comment"
COMMENT_ENABLED: "Activate <code>{{% comment %}}</code> shortcode"

EMBED: "Embed"
EMBED_ENABLED: "Activate <code>{{% embed %}}</code> shortcode"
EMBED_TEMPLATE: "Default page template"

MARKDOWN: "Markdown"
MARKDOWN_ENABLED: "Activate <code>{{% markdown %}}</code> shortcode"

SUMMARY: "Summary"
SUMMARY_ENABLED: "Activate <code>{{% summary %}}</code> shortcode"

TWIG: "Twig"
TWIG_ENABLED: "Activate <code>{{% twig %}}</code> shortcode"
Loading

0 comments on commit c0f748f

Please sign in to comment.