| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | public static function registerShortcodesScript() |
| 27 | 27 | { |
| 28 | 28 | /** |
| 29 | 29 | * Make sure Gutenberg is up and running to avoid |
| 30 | - * any Fatal Error, as the register_block_type() | |
| 30 | + * any fatal errors, as the register_block_type() | |
| 31 | 31 | * function may be not available on old instances. |
| 32 | 32 | */ |
| 33 | 33 | if (!function_exists('register_block_type')) |
| 34 | 34 | { |
| @@ -34,14 +34,23 @@ | ||
| 34 | 34 | { |
| 35 | 35 | return false; |
| 36 | 36 | } |
| 37 | 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 | + | |
| 38 | 46 | // register the script that contains all the JS functions used |
| 39 | 47 | // to implement a new block for Gutenberg editor |
| 40 | 48 | wp_register_script( |
| 41 | 49 | 'vikbooking-gutenberg-shortcodes', |
| 42 | 50 | VIKBOOKING_ADMIN_ASSETS_URI . 'js/gutenberg-shortcodes.js', |
| 43 | - array('wp-blocks', 'wp-element', 'wp-i18n') | |
| 51 | + ['wp-blocks', 'wp-element', 'wp-i18n', 'jquery'], | |
| 52 | + VIKBOOKING_SOFTWARE_VERSION | |
| 44 | 53 | ); |
| 45 | 54 | |
| 46 | 55 | // register the style that contains all the CSS rules used |
| 47 | 56 | // to stylize the blocks for Gutenberg editor |
| @@ -47,23 +56,112 @@ | ||
| 47 | 56 | // to stylize the blocks for Gutenberg editor |
| 48 | 57 | wp_register_style( |
| 49 | 58 | 'vikbooking-gutenberg-shortcodes', |
| 50 | 59 | VIKBOOKING_ADMIN_ASSETS_URI . 'css/gutenberg-shortcodes.css', |
| 51 | - array() | |
| 60 | + [], | |
| 61 | + VIKBOOKING_SOFTWARE_VERSION | |
| 52 | 62 | ); |
| 53 | 63 | |
| 54 | 64 | // create a new block type, which must provide the script and the |
| 55 | 65 | // 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 | - )); | |
| 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']); | |
| 60 | 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 | + { | |
| 61 | 159 | // get shortcode model |
| 62 | 160 | $model = JModel::getInstance('vikbooking', 'shortcodes', 'admin'); |
| 63 | 161 | |
| 64 | 162 | // obtain a categorized shortcodes list |
| 65 | - $shortcodes = array(); | |
| 163 | + $shortcodes = []; | |
| 66 | 164 | |
| 67 | 165 | foreach ($model->all() as $s) |
| 68 | 166 | { |
| 69 | 167 | $title = JText::translate($s->title); |
| @@ -69,14 +167,113 @@ | ||
| 69 | 167 | $title = JText::translate($s->title); |
| 70 | 168 | |
| 71 | 169 | if (!isset($shortcodes[$title])) |
| 72 | 170 | { |
| 73 | - $shortcodes[$title] = array(); | |
| 171 | + $shortcodes[$title] = []; | |
| 74 | 172 | } |
| 75 | 173 | |
| 76 | 174 | $shortcodes[$title][] = $s; |
| 77 | 175 | } |
| 78 | 176 | |
| 79 | - // register script to access JSON object | |
| 80 | - JFactory::getDocument()->addScriptDeclaration("var VIKBOOKING_SHORTCODES_BLOCK = " . json_encode($shortcodes) . ";"); | |
| 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; | |
| 81 | 278 | } |
| 82 | 279 | } |