Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
sangnguyenplus committed Aug 7, 2024
1 parent 9e48c87 commit f4fb8c3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 115 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<p align="center">
<img src="https://github.com/botble/my-style/blob/main/screenshot.png" />
<img src="https://github.com/baoboine/botble-my-style/blob/main/screenshot.png" alt="screenshot" />
</p>

# Botble My Style

This is a plugin for Botble CMS so you have to purchase Botble CMS first to use this plugin.

## Screenshot

![Screenshot 1](./art/screenshot.png)

## Install

- For developers:
Expand Down
Binary file added art/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions helpers/constants.php

This file was deleted.

5 changes: 3 additions & 2 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"provider": "Botble\\MyStyle\\Providers\\MyStyleServiceProvider",
"author": "Bao Boi",
"url": "https://github.com/baoboine/botble-my-style",
"version": "1.0.3",
"description": "Custom your article styles"
"version": "1.1.0",
"description": "Custom your article styles",
"minimum_core_version": "7.1.0"
}
4 changes: 2 additions & 2 deletions resources/views/css-editor.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@if($isWriteable)
{{ Form::textarea('my_custom_css', $css, ['class' => 'form-control', 'rows' => 3, 'id' => 'custom_css']) }}
@if ($isWriteable)
{{ Form::codeEditor('my_custom_css', $css, ['class' => 'form-control', 'rows' => 3, 'id' => 'custom_css']) }}
<input type="hidden" name="has-my-style" value="1"/>
@push('header')
<style>
Expand Down
24 changes: 24 additions & 0 deletions src/Facades/MyStyleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Botble\MyStyle\Facades;

use Botble\MyStyle\MyStyleHelper as BaseMyStyleHelper;
use Illuminate\Support\Facades\Facade;

/**
* @method static \Botble\MyStyle\MyStyleHelper registerModule($model)
* @method static array supportedModels()
* @method static bool isSupportedModel(string $model)
* @method static \Botble\MyStyle\MyStyleHelper unregisterModule(string $model)
* @method static \Botble\MyStyle\MyStyleHelper setConfig(array $config)
* @method static mixed config(string|null $key = null, $default = null)
*
* @see \Botble\MyStyle\MyStyleHelper
*/
class MyStyleHelper extends Facade
{
protected static function getFacadeAccessor(): string
{
return BaseMyStyleHelper::class;
}
}
35 changes: 2 additions & 33 deletions src/MyStyleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@

class MyStyleHelper
{
private array $options;

/**
* array
*/
private $options;

/**
* MyStyleHelper constructor.
*/
public function __construct()
{
$this->options = config('plugins.my-style.config', []);
}

/**
* @param string | array $model
* @return $this
*/
public function registerModule($model): self
{
if (!is_array($model)) {
if (! is_array($model)) {
$model = [$model];
}

Expand All @@ -39,27 +28,16 @@ public function registerModule($model): self
return $this;
}

/**
* @return array
*/
public function supportedModels(): array
{
return config('plugins.my-style.config.supported', []);
}

/**
* @param string $model
* @return bool
*/
public function isSupportedModel(string $model): bool
{
return in_array($model, $this->supportedModels());
}

/**
* @param string $model
* @return $this
*/
public function unregisterModule(string $model): self
{
$supported = $this->supportedModels();
Expand All @@ -75,10 +53,6 @@ public function unregisterModule(string $model): self
return $this;
}

/**
* @param array $config
* @return $this
*/
public function setConfig(array $config): self
{
$options = array_merge($this->options, $config);
Expand All @@ -90,11 +64,6 @@ public function setConfig(array $config): self
return $this;
}

/**
* @param string|null $key
* @param mixed $default
* @return mixed
*/
public function config(?string $key = null, $default = null)
{
$options = $this->options;
Expand Down
89 changes: 24 additions & 65 deletions src/Providers/HookServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

namespace Botble\MyStyle\Providers;

use Assets;
use BaseHelper;
use Botble\Base\Facades\BaseHelper;
use Botble\Base\Facades\MetaBox;
use Botble\Base\Models\BaseModel;
use File;
use Botble\MyStyle\Facades\MyStyleHelper;
use Botble\Theme\Facades\Theme;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use MetaBox;
use MyStyleHelper;
use Theme;

class HookServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
add_action(BASE_ACTION_META_BOXES, [$this, 'addMyStyleField'], 50, 2);
add_action(BASE_ACTION_AFTER_CREATE_CONTENT, [$this, 'saveFieldsInFormScreen'], 75, 3);
Expand All @@ -26,15 +25,12 @@ public function boot()
add_action(BASE_ACTION_PUBLIC_RENDER_SINGLE, [$this, 'embedMyStyles'], 1001, 2);
}

/**
* @param $screen
* @param Request $request
* @param BaseModel $object
*/
public function deleteFields($screen, $request, $object)
public function deleteFields(string $screen, Request $request, BaseModel $object): void
{
if (MyStyleHelper::isSupportedModel(get_class($object)) &&
Auth::user()->hasPermission('my-style.root')) {
if (
MyStyleHelper::isSupportedModel(get_class($object))
&& Auth::user()->hasPermission('my-style.root')
) {
$fileName = $this->fileName($object);
$file = $this->file($fileName);

Expand All @@ -44,14 +40,12 @@ public function deleteFields($screen, $request, $object)
}
}

/**
* @param $context
* @param $object
*/
public function addMyStyleField($context, $object)
public function addMyStyleField(string $context, BaseModel $object): void
{
if (MyStyleHelper::isSupportedModel(get_class($object)) &&
Auth::user()->hasPermission('my-style.root')) {
if (
MyStyleHelper::isSupportedModel(get_class($object))
&& Auth::user()->hasPermission('my-style.root')
) {
MetaBox::addMetaBox(
'my_style',
__('My CSS'),
Expand All @@ -63,32 +57,14 @@ public function addMyStyleField($context, $object)
}
}

/**
* @param BaseModel $object
* @return string
*/
public function renderCustomCssField($object): string
public function renderCustomCssField(BaseModel $object): string
{
$fileName = $this->fileName($object);
$path = $this->file();
$isWriteable = File::isWritable($path);
$css = '';

if ($isWriteable) {
Assets::addStylesDirectly([
'vendor/core/core/base/libraries/codemirror/lib/codemirror.css',
'vendor/core/core/base/libraries/codemirror/addon/hint/show-hint.css',
'vendor/core/packages/theme/css/custom-css.css',
])
->addScriptsDirectly([
'vendor/core/core/base/libraries/codemirror/lib/codemirror.js',
'vendor/core/core/base/libraries/codemirror/lib/css.js',
'vendor/core/core/base/libraries/codemirror/addon/hint/show-hint.js',
'vendor/core/core/base/libraries/codemirror/addon/hint/anyword-hint.js',
'vendor/core/core/base/libraries/codemirror/addon/hint/css-hint.js',
'vendor/core/packages/theme/js/custom-css.js',
]);

if ($fileName) {
$file = $this->file($fileName);

Expand All @@ -101,17 +77,12 @@ public function renderCustomCssField($object): string
return view('plugins/my-style::css-editor', compact('css', 'isWriteable', 'path'))->render();
}

/**
* @param $type
* @param Request $request
* @param $object
*/
public function saveFieldsInFormScreen($type, Request $request, $object)
public function saveFieldsInFormScreen(string $type, Request $request, BaseModel $object): void
{
if (
MyStyleHelper::isSupportedModel(get_class($object)) &&
Auth::user()->hasPermission('my-style.root') &&
$request->has('has-my-style')
MyStyleHelper::isSupportedModel($object::class)
&& Auth::user()->hasPermission('my-style.root')
&& $request->has('has-my-style')
) {
$fileName = $this->fileName($object);
$css = strip_tags($request->input('my_custom_css', ''));
Expand All @@ -125,11 +96,7 @@ public function saveFieldsInFormScreen($type, Request $request, $object)
}
}

/**
* @param string $screen
* @param $object
*/
public function embedMyStyles($screen, $object)
public function embedMyStyles(string $screen, BaseModel $object): void
{
if (MyStyleHelper::isSupportedModel(get_class($object))) {
$fileName = $this->fileName($object);
Expand All @@ -139,26 +106,18 @@ public function embedMyStyles($screen, $object)
Theme::asset()
->container('after_header')
->usePath()
->add($fileName . '-my-style', 'css/' . $fileName . '.css', [], [], filectime($file));
->add($fileName . '-my-style', 'css/' . $fileName . '.css', [], [], (string) filectime($file));
}
}
}

/**
* @param string $slug
* @return string
*/
protected function file(string $slug = ''): string
{
$path = Theme::path() . '/css';

return !empty($slug) ? public_path($path . '/' . $slug . '.css') : $path;
return ! empty($slug) ? public_path($path . '/' . $slug . '.css') : $path;
}

/**
* @param BaseModel $object
* @return string
*/
protected function fileName(BaseModel $object): string
{
return md5(get_class($object) . '-' . $object->id);
Expand Down
13 changes: 6 additions & 7 deletions src/Providers/MyStyleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@

namespace Botble\MyStyle\Providers;

use Botble\Base\Supports\Helper;
use Botble\Base\Traits\LoadAndPublishDataTrait;
use Botble\MyStyle\Facades\MyStyleHelperFacade;
use Botble\MyStyle\Facades\MyStyleHelper;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

class MyStyleServiceProvider extends ServiceProvider
{
use LoadAndPublishDataTrait;

public function register()
public function register(): void
{
Helper::autoload(__DIR__ . '/../../helpers');
AliasLoader::getInstance()->alias('MyStyleHelper', MyStyleHelperFacade::class);
AliasLoader::getInstance()->alias('MyStyleHelper', MyStyleHelper::class);
}

public function boot()
public function boot(): void
{
$this->setNamespace('plugins/my-style')
$this
->setNamespace('plugins/my-style')
->loadAndPublishConfigurations(['permissions', 'config'])
->loadAndPublishViews();

Expand Down

0 comments on commit f4fb8c3

Please sign in to comment.