Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implementing blade engine correctly #96

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion modularity-form-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
* Author: Kristoffer Svanmark, Sebastian Thulin
*/

use ModularityFormBuilder\Blade\Blade;
use ComponentLibrary\Init as ComponentLibraryInit;

define('FORM_BUILDER_MODULE_PATH', plugin_dir_path(__FILE__));
define('FORM_BUILDER_MODULE_URL', plugins_url('', __FILE__));
define('FORM_BUILDER_MODULE_TEMPLATE_PATH', FORM_BUILDER_MODULE_PATH . 'templates/');
define('FORM_BUILDER_MODULE_VIEW_PATH', FORM_BUILDER_MODULE_PATH . 'source/php/Module/views');

//Load lang
load_plugin_textdomain('modularity-form-builder', false, plugin_basename(dirname(__FILE__)) . '/languages');
Expand All @@ -19,6 +23,8 @@
require_once FORM_BUILDER_MODULE_PATH . 'vendor/autoload.php';
}

$bladeInstance = new Blade(new ComponentLibraryInit([]));

// Acf auto import and export
add_action('plugins_loaded', function () {
$acfExportManager = new \AcfExportManager\AcfExportManager();
Expand All @@ -32,5 +38,5 @@

// Start application
if (function_exists('get_field')) {
new ModularityFormBuilder\App();
new ModularityFormBuilder\App($bladeInstance);
}
6 changes: 5 additions & 1 deletion source/php/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace ModularityFormBuilder;

use ModularityFormBuilder\Blade\Blade;

class App
{
public $postType = 'mod-form';

public function __construct()
public function __construct(private Blade $bladeInstance)
{
new Submission();
new Options();
Expand Down Expand Up @@ -41,6 +43,7 @@ public function registerPostTypes()
{
// Default form submission post type
new Entity\PostType(
$this->bladeInstance,
'form-submissions',
__('Form submission', 'modularity-form-builder'),
__('Form submissions', 'modularity-form-builder')
Expand Down Expand Up @@ -77,6 +80,7 @@ public function registerPostTypes()
unset($postTypeArr['cap']);

new Entity\PostType(
$this->bladeInstance,
$postTypeArr['name'],
$postTypeArr['labels']['singular_name'] ?? $postTypeArr['label'],
$postTypeArr['label'],
Expand Down
44 changes: 44 additions & 0 deletions source/php/Blade/Blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace ModularityFormBuilder\Blade;

use ComponentLibrary\Init as ComponentLibraryInit;
use HelsingborgStad\BladeService\BladeServiceInterface;

class Blade
{
private BladeServiceInterface $bladeEngine;

public function __construct(private ComponentLibraryInit $componentLibrary)
{
$this->bladeEngine = $this->componentLibrary->getEngine();
}

public function render($view, $data = [], $compress = true, $viewPaths = [FORM_BUILDER_MODULE_VIEW_PATH])
{
$markup = '';
$data = array_merge($data, array('errorMessage' => false));

try {
$markup = $this->bladeEngine->makeView($view, $data, [], $viewPaths)->render();
} catch (\Throwable $e) {
$this->bladeEngine->errorHandler($e)->print();
}

if ($compress == true) {
$replacements = array(
["~<!--(.*?)-->~s", ""],
["/\r|\n/", ""],
["!\s+!", " "]
);

foreach ($replacements as $replacement) {
$markup = preg_replace($replacement[0], $replacement[1], $markup);
}

return $markup;
}

return $markup;
}
}
50 changes: 21 additions & 29 deletions source/php/Entity/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ModularityFormBuilder\Entity;

use ModularityFormBuilder\Blade\Blade;
use ModularityFormBuilder\Helper\NestedFields;

class PostType
Expand All @@ -11,7 +12,7 @@ class PostType
public $namePlural;
public $args;

public function __construct($postTypeSlug, $nameSingular, $namePlural, $args = array())
public function __construct(private Blade $bladeInstance, $postTypeSlug, $nameSingular, $namePlural, $args = array())
{
$this->postTypeSlug = $postTypeSlug;
$this->nameSingular = $nameSingular;
Expand Down Expand Up @@ -295,10 +296,8 @@ public function formdataDisplay()
if (!$this->isGrantedUser($data['module_id'])) {
//Error message
$this->renderBlade(
'unauthorized.blade.php',
array(
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
),
'unauthorized',
'/admin',
array(
'title' => __("Access denied", 'modularity-form-builder'),
'message' => __("You don't have the sufficient permissions to view this post.", 'modularity-form-builder'),
Expand Down Expand Up @@ -328,10 +327,8 @@ public function formdataDisplay()
if (is_admin() && isset($fields['editable_back_end']) && $fields['editable_back_end'] == true) {
//Editable
$this->renderBlade(
'form-edit.blade.php',
array(
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views'
),
'form-edit',
'',
$data
);
} elseif (self::editableFrontend($post)) {
Expand All @@ -348,28 +345,22 @@ public function formdataDisplay()

//Static
$this->renderBlade(
'form-data.blade.php',
array(
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
),
'form-data',
'/admin',
$data
);

//Editable
$this->renderBlade(
'form-edit-front.blade.php',
array(
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views'
),
'form-edit-front',
"",
$data
);
} else {
//Static
$this->renderBlade(
'form-data.blade.php',
array(
FORM_BUILDER_MODULE_PATH . 'source/php/Module/views/admin'
),
'form-data',
'/admin',
$data
);
}
Expand Down Expand Up @@ -529,15 +520,16 @@ private static function findMatchingNestedIndataArrayValue(&$nestedIndataArray,
* @param array $path Array with file paths
* @param array $data Template data
*/
public function renderBlade($fileName, $path, $data = array())
public function renderBlade(string $viewName, string $path = '', $data = array())
{
add_filter('Municipio/blade/view_paths', array($this, 'addViewPaths'), 2, 1);
$template = new \Municipio\template();
$view = \Municipio\Helper\Template::locateTemplate($fileName, $path);
$view = $template->cleanViewPath($fileName);
if (function_exists('render_blade_view')) {
echo render_blade_view($view, $data);
}
$html = $this->bladeInstance->render(
$viewName,
$data,
true,
[FORM_BUILDER_MODULE_VIEW_PATH . $path]
);

echo $html;
}

/**
Expand Down