Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyc committed Jul 25, 2022
1 parent 5c3bd2e commit 8626283
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 202 deletions.
72 changes: 0 additions & 72 deletions RoboFile.php

This file was deleted.

19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@
],
"require": {
"php": ">=5.6.0",
"composer/installers": "~1.0",
"yahnis-elsts/plugin-update-checker": "^4.4",
"generoi/wp-genero-common": "^0.1.2",
"brick/varexporter": "^0.2.1"
"brick/varexporter": "^0.3"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.5.1",
"consolidation/robo": "~1.3.0",
"generoi/robo-genero": "dev-master"
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^1.8",
"szepeviktor/phpstan-wordpress": "^1.1"
},
"autoload": {
"psr-4": {
"GeneroWP\\Gutenberg\\Templates\\": "src/"
}
},
"files": [
"src/api.php"
]
},
"scripts": {
"test": [
"vendor/bin/phpcs"
"phpcs",
"phpstan"
]
},
"archive" : {
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<!-- Ignore Composer dependencies -->
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>languages/</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern>

<!-- Show colors in console -->
<arg value="-colors"/>
Expand Down
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon

parameters:
level: 6
inferPrivatePropertyTypeFromConstructor: true
paths:
- src/

bootstrapFiles:
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
- tests/stubs.php

111 changes: 1 addition & 110 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
License: MIT License
License URI: http://opensource.org/licenses/MIT
*/
namespace GeneroWP\Gutenberg\Templates;

use Puc_v4_Factory;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use GeneroWP\Common\Singleton;
use GeneroWP\Common\Assets;
use GeneroWP\Gutenberg\Templates\Plugin;

if (!defined('ABSPATH')) {
exit;
Expand All @@ -26,106 +19,4 @@
require_once $composer;
}


class Plugin
{
use Singleton;
use Assets;

public $version = '1.0.0';
public $plugin_name = 'wp-gutenberg-templates';
public $plugin_path;
public $plugin_url;
public $github_url = 'https://github.com/generoi/wp-gutenberg-templates';

public function __construct()
{
require_once __DIR__ . '/api.php';

$this->plugin_path = plugin_dir_path(__FILE__);
$this->plugin_url = plugin_dir_url(__FILE__);

Puc_v4_Factory::buildUpdateChecker($this->github_url, __FILE__, $this->plugin_name);

add_action('plugins_loaded', [$this, 'init']);
}

public function init()
{
add_filter('theme_templates', [$this, 'templates'], 100, 4);
add_filter('block_editor_settings', [$this, 'blockEditorSettings'], 10, 2);
add_action('rest_api_init', [$this, 'restApiEndpoints']);
add_action('enqueue_block_editor_assets', [$this, 'blockEditorAssets']);
add_action('init', [$this, 'loadTextdomain']);
add_action('debug_bar_panels', [$this, 'debugBar']);
}

public function templates($templates, $theme, $post, $post_type)
{
foreach (\get_gutenberg_templates($post_type) as $template => $args) {
$name = $args['name'];
$file = $args['template_file'];
$templates[$file] = $name;
}
return $templates;
}

public function blockEditorSettings($settings, $post)
{
$templateName = get_page_template_slug($post);
if ($template = get_gutenberg_template_by_file($templateName)) {
$settings['template'] = $template['template'];
$settings['templateLock'] = $template['template_lock'];
}
return $settings;
}

public function restApiEndpoints()
{
register_rest_route('gutenberg-templates/v1', '/template', [
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'getTemplate'],
'args' => [
'template' => ['required' => true, 'validate_callback' => [$this, 'validateTemplateFile']],
],
]);
}

public function validateTemplateFile(string $template)
{
return !!preg_match('/^[a-zA-Z0-9-_\/\.]+\.php$/', $template);
}

public function getTemplate(WP_REST_Request $request)
{
$template = \get_gutenberg_template_by_file($request['template']);
if (!$template) {
return new \WP_Error('no_template', 'Invalid template', ['status' => 404]);
}
return $template;
}

public function debugBar($panels)
{
// Cannot use namespaces and therefore no autoloading.
require_once __DIR__ . '/src/DebugBar.php';
$panels[] = new \GutenbergTemplates_DebugBar();
return $panels;
}

public function blockEditorAssets()
{
$this->enqueueScript("{$this->plugin_name}/js", 'dist/index.js', ['wp-editor', 'wp-data', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-api-request']);
wp_set_script_translations("{$this->plugin_name}/js", $this->plugin_name);
}

public function loadTextdomain()
{
// WP Performance Pack
include __DIR__ . '/languages/javascript.php';

load_plugin_textdomain($this->plugin_name, false, dirname(plugin_basename(__FILE__)) . '/languages');
}
}

Plugin::getInstance();
28 changes: 21 additions & 7 deletions src/DebugBar.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
<?php
// phpcs:ignoreFile PSR1.Classes.ClassDeclaration.MissingNamespace

use Brick\VarExporter\VarExporter;

class GutenbergTemplates_DebugBar extends Debug_Bar_Panel
{
public function init()
protected ?WP_Post $post;

public function init(): void
{
$this->post = $this->postObject();
$this->title('Block Template');
}

public function prerender()
public function prerender(): void
{
$this->set_visible($this->post ? true : false);
}

public function render()
public function render(): void
{
$output = VarExporter::export($this->blocks(), VarExporter::INLINE_NUMERIC_SCALAR_ARRAY);
$output = apply_filters('wp-gutenberg-templates/debugbar/export', $output);
$output = str_replace(['<', '>'], ['&lt;', '&gt;'], $output);
echo "<pre>$output</pre>";
}

protected function postObject()
protected function postObject(): ?WP_Post
{
$post_id = null;

Expand All @@ -43,10 +46,13 @@ protected function postObject()
return $post_id ? get_post($post_id) : null;
}

protected function blocks()
/**
* @return array<mixed>
*/
protected function blocks(): array
{
if (!$this->post || !$this->post->post_content) {
return null;
return [];
}

$blocks = parse_blocks($this->post->post_content);
Expand All @@ -57,6 +63,10 @@ protected function blocks()
return $blocks;
}

/**
* @param array<string,mixed> $block
* @return ?array<mixed>
*/
protected function buildBlock($block)
{
if (!$block['blockName']) {
Expand Down Expand Up @@ -86,7 +96,11 @@ protected function buildBlock($block)
return $value;
}

protected function buildAttrs($block)
/**
* @param array<string,mixed> $block
* @return array<string,mixed>
*/
protected function buildAttrs(array $block): array
{
$attrs = $block['attrs'];

Expand Down
Loading

0 comments on commit 8626283

Please sign in to comment.