| 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 errors, 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 declaring the reusable functions for Gutenberg |
| 39 |
wp_register_script( |
| 40 |
'vikbooking-gutenberg-tools', |
| 41 |
VIKBOOKING_ADMIN_ASSETS_URI . 'js/gutenberg-tools.js', |
| 42 |
['wp-blocks', 'wp-element', 'wp-i18n'], |
| 43 |
VIKBOOKING_SOFTWARE_VERSION |
| 44 |
); |
| 45 |
|
| 46 |
// register the script that contains all the JS functions used |
| 47 |
// to implement a new block for Gutenberg editor |
| 48 |
wp_register_script( |
| 49 |
'vikbooking-gutenberg-shortcodes', |
| 50 |
VIKBOOKING_ADMIN_ASSETS_URI . 'js/gutenberg-shortcodes.js', |
| 51 |
['wp-blocks', 'wp-element', 'wp-i18n', 'jquery'], |
| 52 |
VIKBOOKING_SOFTWARE_VERSION |
| 53 |
); |
| 54 |
|
| 55 |
// register the style that contains all the CSS rules used |
| 56 |
// to stylize the blocks for Gutenberg editor |
| 57 |
wp_register_style( |
| 58 |
'vikbooking-gutenberg-shortcodes', |
| 59 |
VIKBOOKING_ADMIN_ASSETS_URI . 'css/gutenberg-shortcodes.css', |
| 60 |
[], |
| 61 |
VIKBOOKING_SOFTWARE_VERSION |
| 62 |
); |
| 63 |
|
| 64 |
// create a new block type, which must provide the script and the |
| 65 |
// style we defined in the previous piece of code (script/style ID) |
| 66 |
register_block_type('vikbooking/gutenberg-shortcodes', [ |
| 67 |
'editor_script_handles' => [ |
| 68 |
'vikbooking-gutenberg-tools', |
| 69 |
'vikbooking-gutenberg-shortcodes', |
| 70 |
], |
| 71 |
'editor_style_handles' => [ |
| 72 |
'vikbooking-gutenberg-shortcodes', |
| 73 |
], |
| 74 |
'render_callback' => function($config, $content) { |
| 75 |
if (!empty($config['shortcode'])) |
| 76 |
{ |
| 77 |
$html = do_shortcode($config['shortcode']); |
| 78 |
|
| 79 |
ob_start(); |
| 80 |
wp_print_styles(); |
| 81 |
$html .= ob_get_contents(); |
| 82 |
ob_end_clean(); |
| 83 |
|
| 84 |
return $html; |
| 85 |
} |
| 86 |
|
| 87 |
return $content; |
| 88 |
}, |
| 89 |
'attributes' => [ |
| 90 |
'toggler' => [ |
| 91 |
'type' => 'boolean', |
| 92 |
'default' => 0, |
| 93 |
], |
| 94 |
'shortcode' => [ |
| 95 |
'type' => 'string', |
| 96 |
'default' => '', |
| 97 |
], |
| 98 |
], |
| 99 |
]); |
| 100 |
|
| 101 |
// pass the block configuration to the script previously loaded |
| 102 |
wp_localize_script( |
| 103 |
'vikbooking-gutenberg-shortcodes', |
| 104 |
'VIKBOOKING_SHORTCODES_BLOCK', |
| 105 |
static::getConfig() |
| 106 |
); |
| 107 |
|
| 108 |
// force WordPress to load the translations from VikBooking |
| 109 |
wp_set_script_translations('vikbooking-gutenberg-shortcodes', 'vikbooking'); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Creates the configuration object to use for the block. |
| 114 |
* |
| 115 |
* @return array |
| 116 |
* |
| 117 |
* @since 1.6.7 |
| 118 |
*/ |
| 119 |
protected static function getConfig() |
| 120 |
{ |
| 121 |
// do not elaborate the configuration for the gutenberg blocks in case we are in the |
| 122 |
// front-end or in case the plugin in use starts with "com_vik" |
| 123 |
if (!is_admin() || preg_match("/^com_vik/", JFactory::getApplication()->input->get('option', ''))) |
| 124 |
{ |
| 125 |
return []; |
| 126 |
} |
| 127 |
|
| 128 |
$languages = []; |
| 129 |
|
| 130 |
foreach (JLanguage::getKnownLanguages() as $tag => $lang) |
| 131 |
{ |
| 132 |
$languages[] = [ |
| 133 |
'tag' => $tag, |
| 134 |
'name' => $lang['nativeName'], |
| 135 |
]; |
| 136 |
} |
| 137 |
|
| 138 |
$uri = VBOFactory::getPlatform()->getUri(); |
| 139 |
$ajaxUrl = $uri->ajax('admin-ajax.php?action=vikbooking&task=shortcode.save'); |
| 140 |
$ajaxUrl = $uri->addCSRF($ajaxUrl); |
| 141 |
|
| 142 |
return [ |
| 143 |
'shortcodes' => static::getShortcodes(), |
| 144 |
'views' => static::getPages(), |
| 145 |
'languages' => $languages, |
| 146 |
'ajaxurl' => $ajaxUrl, |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Returns the list containing all the configured shortcodes. |
| 152 |
* |
| 153 |
* @return object[] |
| 154 |
* |
| 155 |
* @since 1.6.7 |
| 156 |
*/ |
| 157 |
protected static function getShortcodes() |
| 158 |
{ |
| 159 |
// get shortcode model |
| 160 |
$model = JModel::getInstance('vikbooking', 'shortcodes', 'admin'); |
| 161 |
|
| 162 |
// obtain a categorized shortcodes list |
| 163 |
$shortcodes = []; |
| 164 |
|
| 165 |
foreach ($model->all() as $s) |
| 166 |
{ |
| 167 |
$title = JText::translate($s->title); |
| 168 |
|
| 169 |
if (!isset($shortcodes[$title])) |
| 170 |
{ |
| 171 |
$shortcodes[$title] = []; |
| 172 |
} |
| 173 |
|
| 174 |
$shortcodes[$title][] = $s; |
| 175 |
} |
| 176 |
|
| 177 |
return $shortcodes; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the list containing all the supported site pages that can be |
| 182 |
* rendered through a shortcode. |
| 183 |
* |
| 184 |
* @return object[] |
| 185 |
* |
| 186 |
* @since 1.6.7 |
| 187 |
*/ |
| 188 |
protected static function getPages() |
| 189 |
{ |
| 190 |
$views = []; |
| 191 |
|
| 192 |
// get all the views that contain a default.xml file |
| 193 |
// [0] : base path |
| 194 |
// [1] : query |
| 195 |
// [2] : true for recursive search |
| 196 |
// [3] : true to return full paths |
| 197 |
$files = JFolder::files(VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'views', 'default.xml', true, true); |
| 198 |
|
| 199 |
foreach ($files as $file) |
| 200 |
{ |
| 201 |
// retrieve the view ID from the path: /views/[ID]/tmpl/default.xml |
| 202 |
if (preg_match("/[\/\\\\]views[\/\\\\](.*?)[\/\\\\]tmpl[\/\\\\]default\.xml$/i", $file, $matches)) |
| 203 |
{ |
| 204 |
$id = $matches[1]; |
| 205 |
// load the XML form |
| 206 |
$form = JForm::getInstance('vikbooking' . $id, $file); |
| 207 |
|
| 208 |
$fields = []; |
| 209 |
|
| 210 |
foreach ($form->getFields() as $field) |
| 211 |
{ |
| 212 |
try |
| 213 |
{ |
| 214 |
// get form field |
| 215 |
$field = JFormField::getInstance($field); |
| 216 |
} |
| 217 |
catch (Throwable $error) |
| 218 |
{ |
| 219 |
// prevent a fatal error in case of outdated adapter |
| 220 |
JLoader::import('adapter.form.fields.text'); |
| 221 |
$field = new JFormFieldText($field); |
| 222 |
} |
| 223 |
|
| 224 |
// obtain field layout data |
| 225 |
$displayData = array_merge( |
| 226 |
[ |
| 227 |
'type' => $field->type, |
| 228 |
'layout' => $field->layoutId, |
| 229 |
'label' => JText::translate($field->label ?? ''), |
| 230 |
'description' => strip_tags(JText::translate($field->description ?? '')), |
| 231 |
'showon' => $field->showon, |
| 232 |
], |
| 233 |
$field->getLayoutData() |
| 234 |
); |
| 235 |
|
| 236 |
// normalize options structure |
| 237 |
if (isset($displayData['options']) && is_array($displayData['options'])) |
| 238 |
{ |
| 239 |
$options = []; |
| 240 |
|
| 241 |
foreach ($displayData['options'] as $value => $label) |
| 242 |
{ |
| 243 |
if (is_object($label) || is_array($label)) |
| 244 |
{ |
| 245 |
$label = (object) $label; |
| 246 |
|
| 247 |
$options[] = [ |
| 248 |
'label' => JText::translate($label->text), |
| 249 |
'value' => $label->value, |
| 250 |
]; |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
$options[] = [ |
| 255 |
'label' => JText::translate($label), |
| 256 |
'value' => $value, |
| 257 |
]; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
$displayData['options'] = $options; |
| 262 |
} |
| 263 |
|
| 264 |
$fields[] = $displayData; |
| 265 |
} |
| 266 |
|
| 267 |
// get the view title |
| 268 |
$views[] = [ |
| 269 |
'type' => $id, |
| 270 |
'name' => JText::translate((string) $form->getXml()->layout->attributes()->title), |
| 271 |
'desc' => JText::translate((string) $form->getXml()->layout->message), |
| 272 |
'fields' => $fields, |
| 273 |
]; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return $views; |
| 278 |
} |
| 279 |
} |
| 280 |
|