Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FlusherDock1 committed Aug 28, 2019
1 parent 0e65a88 commit e2b199b
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 124 deletions.
11 changes: 7 additions & 4 deletions behaviors/Gutenbergable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class Gutenbergable extends ModelBehavior
public function __construct($model)
{
parent::__construct($model);
$model->morphOne['content'] = [Content::class, 'name' => 'contentable'];
$model->morphOne['content'] = [ Content::class,
'name' => 'contentable',
'delete' => true
];
}

/**
* Returns the rendered HTML from the Content object
* @return String
Expand Down Expand Up @@ -47,10 +50,10 @@ public function getRenderedContent()
/**
* Sets the content object using the raw editor content
* @param String $content
* @param String $save - Calls .save() on the Content object if true
* @param boolean $save - Calls .save() on the Content object if true
*/
public function setContent($content, $save = false)
{
{
if (!$this->model->content) { $this->createContent(); }

$this->model->content->setContent($content);
Expand Down
79 changes: 39 additions & 40 deletions classes/Extenders.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
use ReaZzon\Gutenberg\Helpers\BlockHelper;
use ReaZzon\Gutenberg\Helpers\EmbedHelper;

class Extenders
class Extenders
{

public static function RainLabBlog()
{
if (Settings::get('integration_blog', false) &&
PluginManager::instance()->hasPlugin('RainLab.Blog'))
{
if (Settings::get('integration_blog', false) &&
PluginManager::instance()->hasPlugin('RainLab.Blog')) {

Event::listen('backend.form.extendFields', function ($widget) {

Expand All @@ -29,21 +28,21 @@ public static function RainLabBlog()

// Finding content field and changing it's type regardless whatever it already is.
foreach ($widget->getFields() as $field) {
if ($field->fieldName === 'content'){
$field->config['type'] = 'gutenberg';
$field->config['widget'] = 'ReaZzon\Gutenberg\FormWidgets\Gutenberg';
if ($field->fieldName === 'content') {
$field->config['type'] = 'gutenberg';
$field->config['widget'] = 'ReaZzon\Gutenberg\FormWidgets\Gutenberg';
$field->config['minheight'] = 500;
}
}
});

// Repalcing original content_html attribute.
\RainLab\Blog\Models\Post::extend(function($model) {
$model->bindEvent('model.getAttribute', function($attribute, $value) use ($model){
\RainLab\Blog\Models\Post::extend(function ($model) {
$model->bindEvent('model.getAttribute', function ($attribute, $value) use ($model) {
if ($attribute == 'content_html') {
return "<div class='gutenberg__content wp-embed-responsive'>".
return "<div class='gutenberg__content wp-embed-responsive'>" .
BlockHelper::renderBlocks(EmbedHelper::renderEmbeds($value))
."</div>";
. "</div>";
}
});
});
Expand All @@ -52,9 +51,8 @@ public static function RainLabBlog()

public static function LovataGoodNews()
{
if (Settings::get('integration_good_news', false) &&
PluginManager::instance()->hasPlugin('Lovata.GoodNews'))
{
if (Settings::get('integration_good_news', false) &&
PluginManager::instance()->hasPlugin('Lovata.GoodNews')) {

Event::listen('backend.form.extendFields', function ($widget) {

Expand All @@ -70,53 +68,54 @@ public static function LovataGoodNews()

// Finding content field and changing it's type regardless whatever it already is.
foreach ($widget->getFields() as $field) {
if ($field->fieldName === 'content'){
$field->config['type'] = 'gutenberg';
$field->config['widget'] = 'ReaZzon\Gutenberg\FormWidgets\Gutenberg';
if ($field->fieldName === 'content') {
$field->config['type'] = 'gutenberg';
$field->config['widget'] = 'ReaZzon\Gutenberg\FormWidgets\Gutenberg';
$field->config['minheight'] = 500;
}
}
});

// Repalcing original content attribute.
\Lovata\GoodNews\Classes\Item\ArticleItem::extend(function($elementItem) {
$elementItem->addDynamicMethod('getContentAttribute', function() use ($elementItem){
return "<div class='gutenberg__content wp-embed-responsive'>".
\Lovata\GoodNews\Classes\Item\ArticleItem::extend(function ($elementItem) {
$elementItem->addDynamicMethod('getContentAttribute', function () use ($elementItem) {
return "<div class='gutenberg__content wp-embed-responsive'>" .
BlockHelper::renderBlocks(EmbedHelper::renderEmbeds($elementItem->getAttribute('content')))
."</div>";
. "</div>";
});
});
}
}

/**
* Static pages currently in work.
*
*
*
* DONE:
* - Gutenberg.js working with content from static files.
* - Full work on master tab
*
* TODO:
*
* TODO:
* - Reload of formwidget everytime tab is changed. Gutenberg.js can't have mulitple instances at one page.
*
*
*/
public static function StaticPages()
{
Event::listen('backend.form.extendFields', function ($widget) {
if (!$widget->getController() instanceof \RainLab\Pages\Controllers\Index ||
!$widget->model instanceof \RainLab\Pages\Classes\Page) {
return;
}

// Registering gutenberg formwidget
$widget->addSecondaryTabFields([
'markup' => [
'tab' => 'rainlab.pages::lang.editor.content',
'type' => 'gutenberg',
'stretch' => 'true'
]
]);
});
// Event::listen('backend.form.extendFields', function ($widget) {
// if (!$widget->getController() instanceof \RainLab\Pages\Controllers\Index ||
// !$widget->model instanceof \RainLab\Pages\Classes\Page) {
// return;
// }
//
// // Registering gutenberg formwidget
// $widget->addSecondaryTabFields([
// 'markup' => [
// 'tab' => 'rainlab.pages::lang.editor.content',
// 'type' => 'gutenberg',
// 'stretch' => 'true'
// ]
// ]);
// });
}


Expand Down
9 changes: 4 additions & 5 deletions events/ContentCreated.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php namespace ReaZzon\Gutenberg\Events;

use Illuminate\Queue\SerializesModels;

use ReaZzon\Gutenberg\Models\Content;
use Illuminate\Queue\SerializesModels;

class ContentCreated
{
use SerializesModels;

public $content;

/**
* Create a new event instance
*
* @param ReaZzon\Gutenberg\Models\Content $content
*
* @param Content $content
* @return void
*/
public function __construct(Content $content)
Expand Down
7 changes: 3 additions & 4 deletions events/ContentRendered.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php namespace ReaZzon\Gutenberg\Events;

use Illuminate\Queue\SerializesModels;

use ReaZzon\Gutenberg\Models\Content;
use Illuminate\Queue\SerializesModels;

class ContentRendered
{
Expand All @@ -12,8 +11,8 @@ class ContentRendered

/**
* Create a new event instance
*
* @param ReaZzon\Gutenberg\Models\Content $content
*
* @param Content $content
* @return void
*/
public function __construct(Content $content)
Expand Down
17 changes: 8 additions & 9 deletions helpers/BlockHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ class BlockHelper
{
/**
* Renders any blocks in the HTML (recursively)
* @param String $html
* @param $html
* @return string|string[]|null
*/
public static function renderBlocks($html)
{
$regex = '/<!-- wp:block {"ref":(\d*)} \/-->/';
$result = preg_replace_callback($regex, function ($matches) {
return preg_replace_callback('/<!-- wp:block {"ref":(\d*)} \/-->/', function ($matches) {
return self::renderBlock($matches[1]);
}, $html);
return $result;
}

/**
* Returns the HTML of the Block with $id
* @param Int $id
* @param $id
* @return string
*/
private static function renderBlock($id)
{
$block = Block::find($id);
if ($block) {
if ($block = Block::find($id)) {
return $block->render();
} else {
return 'Block not found';
}

return 'Block not found';
}
}
10 changes: 5 additions & 5 deletions helpers/EmbedHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ class EmbedHelper

/**
* Renders any embeds in the HTML
* @param String $html
* @return String - The HTML containing all embed code
* @param string $html
* @return string - The HTML containing all embed code
*/
public static function renderEmbeds($html)
{
$regex = '/<!-- wp:core-embed\/.*?-->\s*?<figure class="wp-block-embed.*?".*?<div class="wp-block-embed__wrapper">\s*?(.*?)\s*?<\/div><\/figure>/';
$result = preg_replace_callback($regex, function ($matches) {
return preg_replace_callback($regex, function ($matches) {
$embed = self::createEmbed($matches[1]);
$url = preg_replace('/\//', '\/', preg_quote($matches[1]));
return preg_replace("/>\s*?$url\s*?</", ">$embed->code<", $matches[0]);
}, $html);
return $result;
}

/**
* Creates an embed from a URL
* @param String $url
* @param $url
* @return \Embed\Adapters\Adapter
*/
public static function createEmbed($url)
{
Expand Down
38 changes: 0 additions & 38 deletions helpers/SlugHelper.php

This file was deleted.

23 changes: 12 additions & 11 deletions models/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use Model;
use ReaZzon\Gutenberg\Helpers\BlockHelper;
use ReaZzon\Gutenberg\Helpers\EmbedHelper;
use ReaZzon\Gutenberg\Helpers\SlugHelper;

/**
* Block Model
Expand All @@ -16,21 +15,24 @@ class Block extends Model
public $table = 'reazzon_gutenberg_blocks';

/**
* Add array attributes that do not have a corresponding column in your database
* @var array Add array attributes that do not have a corresponding column in your database
*/
protected $appends = ['content', 'title'];
protected $appends = [
'content',
'title'
];

/**
* Updates slug according to title
*/
public function updateSlug()
{
$this->slug = SlugHelper::slugify($this->raw_title);
$this->slug = str_slug($this->raw_title, "-");
}

/**
* Returns the rendered content of the block
* @return String - The completely rendered content
* @return string - The completely rendered content
*/
public function render()
{
Expand All @@ -39,17 +41,16 @@ public function render()

/**
* Renders the content of the Block object
* @return String
* @return string
*/
public function renderRaw()
{
$this->rendered_content = EmbedHelper::renderEmbeds($this->raw_content);
return $this->rendered_content;
return EmbedHelper::renderEmbeds($this->raw_content);
}

/**
* Sets the raw content and performs some initial rendering
* @param String $html
* @param string $content
*/
public function setContent($content)
{
Expand All @@ -59,7 +60,7 @@ public function setContent($content)

/**
* Returns a content object similar to WordPress
* @return Array
* @return array
*/
public function getContentAttribute()
{
Expand All @@ -71,7 +72,7 @@ public function getContentAttribute()

/**
* Returns a content object similar to WordPress
* @return Array
* @return array
*/
public function getTitleAttribute()
{
Expand Down
Loading

0 comments on commit e2b199b

Please sign in to comment.