| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking - Libraries |
| 4 |
* @subpackage system |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2018 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Class used to provide support for Gutenberg editor. |
| 16 |
* |
| 17 |
* @since 1.0.17 |
| 18 |
*/ |
| 19 |
class VikBookingGutenberg |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Attaches the necessary scripts to handle the shortcode event. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public static function registerShortcodesScript() |
| 27 |
{ |
| 28 |
/** |
| 29 |
* Make sure Gutenberg is up and running to avoid |
| 30 |
* any Fatal Error, as the register_block_type() |
| 31 |
* function may be not available on old instances. |
| 32 |
*/ |
| 33 |
if (!function_exists('register_block_type')) |
| 34 |
{ |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
// register the script that contains all the JS functions used |
| 39 |
// to implement a new block for Gutenberg editor |
| 40 |
wp_register_script( |
| 41 |
'vikbooking-gutenberg-shortcodes', |
| 42 |
VIKBOOKING_ADMIN_ASSETS_URI . 'js/gutenberg-shortcodes.js', |
| 43 |
array('wp-blocks', 'wp-element', 'wp-i18n') |
| 44 |
); |
| 45 |
|
| 46 |
// register the style that contains all the CSS rules used |
| 47 |
// to stylize the blocks for Gutenberg editor |
| 48 |
wp_register_style( |
| 49 |
'vikbooking-gutenberg-shortcodes', |
| 50 |
VIKBOOKING_ADMIN_ASSETS_URI . 'css/gutenberg-shortcodes.css', |
| 51 |
array() |
| 52 |
); |
| 53 |
|
| 54 |
// create a new block type, which must provide the script and the |
| 55 |
// style we defined in the previous piece of code (script/style ID) |
| 56 |
register_block_type('vikbooking/gutenberg-shortcodes', array( |
| 57 |
'editor_script' => 'vikbooking-gutenberg-shortcodes', |
| 58 |
'editor_style' => 'vikbooking-gutenberg-shortcodes', |
| 59 |
)); |
| 60 |
|
| 61 |
// get shortcode model |
| 62 |
$model = JModel::getInstance('vikbooking', 'shortcodes', 'admin'); |
| 63 |
|
| 64 |
// obtain a categorized shortcodes list |
| 65 |
$shortcodes = array(); |
| 66 |
|
| 67 |
foreach ($model->all() as $s) |
| 68 |
{ |
| 69 |
$title = JText::translate($s->title); |
| 70 |
|
| 71 |
if (!isset($shortcodes[$title])) |
| 72 |
{ |
| 73 |
$shortcodes[$title] = array(); |
| 74 |
} |
| 75 |
|
| 76 |
$shortcodes[$title][] = $s; |
| 77 |
} |
| 78 |
|
| 79 |
// register script to access JSON object |
| 80 |
JFactory::getDocument()->addScriptDeclaration("var VIKBOOKING_SHORTCODES_BLOCK = " . json_encode($shortcodes) . ";"); |
| 81 |
} |
| 82 |
} |
| 83 |
|