Skip to content

Commit

Permalink
new filters
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Sep 7, 2019
1 parent 9bc680f commit 6a788be
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 64 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,31 @@ Do different stuff depending on the access level of the user:
@endguest
```

## Tweak settings with filters (optional)

Modify where cached views are stored:

```php
add_filter('cache_path_blockmodules', function ($cachePath) {
return '/cache/to/this/dir';
});
```

Change the base directory used to located view templates:

```php
add_filter('base_path_blockmodules', function ($basePath) {
return '/views/relative/from/this/dir';
});
```

Disable user functions (saves the database call):

```php
add_filter('disable_user_blockmodules', function () {
return true;
});
```

## Blade for everyone

Expand Down
8 changes: 3 additions & 5 deletions block-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
* @param string $directory
* @return void
*/
public function __invoke() : void
public function __invoke($pluginsDir) : void
{
$runtime = new Runtime(
realpath(__DIR__ . '/..')
);
$runtime = new Runtime(realpath($pluginsDir));
}
})();
})(__DIR__ . '/..');
2 changes: 1 addition & 1 deletion example-block/example-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'handle' => 'example/block',
'entry' => 'index.js',
'blade' => 'blade/render',
]);
]);

return $blocks;
});
22 changes: 16 additions & 6 deletions src/Modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Modules
public function __construct(Collection $blocks, Blade $view)
{
self::$blocks = $blocks;

self::$View = $view;
}

Expand All @@ -47,28 +48,37 @@ public function __construct(Collection $blocks, Blade $view)
*
* @return void
*/
public function registerTemplates() : void
public function registerViews() : void
{
self::$blocks->each(function ($block) {
if (is_null($block['blade'])) {
return;
}

register_block_type($block['handle'], [
'editor_script' => $block['entry'],
'editor_script' => $block['entry'],
'render_callback' => function ($attr, $content) use ($block) {
$data = [
'attr' => (object) $attr,
'content' => $content,
];

return self::$View->run(
$this->template($block['handle']),
['attr' => (object) $attr, 'content' => $content]
$this->view($block['handle']),
$data
);
},
]);
});
}

/**
* Returns template path
* Returns view
*
* @param string $blockName
* @return string
*/
public function template(string $blockName) : string
public function view(string $blockName) : string
{
$block = self::$blocks->where('handle', $blockName);

Expand Down
106 changes: 54 additions & 52 deletions src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,97 +47,99 @@ class Runtime
*/
public static $basePath;

/**
* Debugger
*
* @var bool
*/
public static $mode;

/**
* Cache path
*
* @var string
*/
public static $cachePath;


/**
* Class constructor
*
*/
public function __construct(string $basePath)
{
add_action('init', function () use ($basePath) {
$cachePath = wp_upload_dir('block_modules')['path'];

$blocks = Collection::make();

self::$cachePath = has_filter('cache_path_blockmodules')
? apply_filters('cache_path_blockmodules', $cachePath)
: $cachePath;

self::$basePath = has_filter('base_path_blockmodules')
? apply_filters('base_path_blockmodules', $basePath)
: $basePath;

self::$blocks = has_filter('register_blockmodules')
? apply_filters('register_blockmodules', $blocks)
: $blocks;

self::$Modules = new Modules(self::$blocks, new Blade(
self::$basePath = $this->filter('base_path_blockmodules', $basePath);
self::$cachePath = $this->filter('cache_path_blockmodules', wp_upload_dir('block_modules')['path']);
self::$blocks = $this->filter('register_blockmodules', Collection::make());

/**
* \eftec\bladeone\BladeOne
*/
self::$View = new Blade(
self::$basePath,
self::$cachePath,
Blade::MODE_AUTO
));

self::$Modules->setUser(\wp_get_current_user());
self::mode($this->filter('debug_blockmodules', Blade::MODE_AUTO))
);

self::$Modules->registerTemplates();
/**
* \TinyPixel\Modules\Modules
*/
self::$Modules = new Modules(self::$blocks, self::$View);

/**
* Enable @user, @guest, et al.
*/
if (!$this->filter('disable_user_blockmodules', false)) {
self::$Modules->setUser(\wp_get_current_user());
}

/**
* Register views with WordPress
*/
self::$Modules->registerViews();
});

/**
* Enqueue assets
*/
add_action('enqueue_block_editor_assets', function () {
self::enqueue(self::$blocks);
});
}

/**
* Enqueues blocks
* Return filter result or default
*
* @param \Illuminate\Support\Collection $blocks
* @return void
* @param string $filter
* @param mixed $default
* @return mixed
*/
private static function enqueue(Collection $blocks) : void
public function filter(string $filter, $default)
{
$blocks->each(function ($block) {
wp_enqueue_script(
$block['handle'],
self::asset($block),
self::deps($block['handle']),
...['', true],
);
});
if (!has_filter($filter)) {
return $default;
}

return apply_filters($filter, $default);
}

/**
* Returns plugin asset path
* Set blade engine debug mode
*
* @param string $block
* @param string $path
* @return \eftec\bladeone\BladeOne
*/
private static function asset(array $block) : string
private static function mode($mode)
{
return plugins_url("{$block['plugin']}") . "/dist/{$block['entry']}";
return $mode == 'debug' ? Blade::MODE_DEBUG : Blade::MODE_AUTO;
}

/**
* Returns dependencies depending on if
* script is specified as a block or plugin
* Enqueue block (editor scripts)
*
* @param string $handle
* @return array
* @param \Illuminate\Support\Collection $blocks
* @return void
*/
private static function deps(string $handle) : array
private static function enqueue(Collection $blocks) : void
{
return ($handle == 'block') ? self::$blockDeps : self::$blockDeps;
$blocks->each(function ($block) {
$assetUrl = plugins_url("{$block['plugin']}/dist/{$block['entry']}");
$dependencies = isset($block['extension']) ? self::$extensionDeps : self::$blockDeps;

wp_enqueue_script($block['handle'], $assetUrl, $dependencies, ...['', true]);
});
}
}

0 comments on commit 6a788be

Please sign in to comment.