| @@ -57,8 +57,18 @@ | ||
| 57 | 57 | */ |
| 58 | 58 | protected $registered = false; |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | + * An incremental counter to make sure the used ID is always unique, | |
| 62 | + * since Gutenberg seems to always use the number ID for the widgets | |
| 63 | + * published under the same page. | |
| 64 | + * | |
| 65 | + * @var int | |
| 66 | + * @since 10.1.59 | |
| 67 | + */ | |
| 68 | + protected static $incrementalCounter = 0; | |
| 69 | + | |
| 70 | + /** | |
| 61 | 71 | * Class constructor. |
| 62 | 72 | * |
| 63 | 73 | * @param string $path The widget absolute path. |
| 64 | 74 | * |
| @@ -147,8 +157,24 @@ | ||
| 147 | 157 | { |
| 148 | 158 | return; |
| 149 | 159 | } |
| 150 | 160 | |
| 161 | + /** | |
| 162 | + * If we are under a block preview, prefer a different layout. | |
| 163 | + * | |
| 164 | + * @since 10.1.71 | |
| 165 | + */ | |
| 166 | + if ($this->isBlockPreview()) { | |
| 167 | + // check whether the block preview layout is supported by this widget | |
| 168 | + $blockPreviewLayout = ABSPATH . '/wp-content/plugins/' . $this->_option . '/libraries/wordpress/fse/modules/' . $this->_id . '.php'; | |
| 169 | + | |
| 170 | + if (JFile::exists($blockPreviewLayout)) | |
| 171 | + { | |
| 172 | + // block preview layout supported, use it in place of the default one | |
| 173 | + $layout = JPath::clean($blockPreviewLayout); | |
| 174 | + } | |
| 175 | + } | |
| 176 | + | |
| 151 | 177 | // include system.js file to support JoomlaCore |
| 152 | 178 | JHtml::fetch('system.js'); |
| 153 | 179 | |
| 154 | 180 | /** |
| @@ -183,9 +209,9 @@ | ||
| 183 | 209 | * |
| 184 | 210 | * @since 10.1.30 |
| 185 | 211 | */ |
| 186 | 212 | $module = new stdClass; |
| 187 | - $module->id = $this->number; | |
| 213 | + $module->id = ++static::$incrementalCounter; | |
| 188 | 214 | |
| 189 | 215 | /** |
| 190 | 216 | * Plugins can manipulate the configuration of the widget at runtime. |
| 191 | 217 | * Fires before dispatching the widget in the front-end. |
| @@ -873,9 +899,9 @@ | ||
| 873 | 899 | register_block_type($this->_option . '/' . $block_id, array_merge($data, [ |
| 874 | 900 | 'render_callback' => function($config) { |
| 875 | 901 | // prepare widget arguments |
| 876 | 902 | $args = [ |
| 877 | - 'before_widget' => '<div class="widget widget_' . $this->_id . '" id="' . $this->_id . '_' . $this->number . '">', | |
| 903 | + 'before_widget' => '<div class="widget widget_' . $this->_id . '" id="' . $this->_id . '_' . (++static::$incrementalCounter) . '">', | |
| 878 | 904 | 'before_title' => '<h3 class="widget-title">', |
| 879 | 905 | 'after_title' => '</h3>', |
| 880 | 906 | 'after_widget' => '</div>', |
| 881 | 907 | ]; |
| @@ -1050,6 +1076,36 @@ | ||
| 1050 | 1076 | { |
| 1051 | 1077 | $this->extractHtmlElements($node, $tag); |
| 1052 | 1078 | } |
| 1053 | 1079 | } |
| 1080 | + } | |
| 1081 | + | |
| 1082 | + /** | |
| 1083 | + * Checks whether the block is displayed by a block preview. | |
| 1084 | + * | |
| 1085 | + * @return bool | |
| 1086 | + * | |
| 1087 | + * @since 1.10.71 | |
| 1088 | + */ | |
| 1089 | + protected function isBlockPreview() | |
| 1090 | + { | |
| 1091 | + $app = JFactory::getApplication(); | |
| 1092 | + | |
| 1093 | + // check whether we are displaying the widget on the block preview | |
| 1094 | + $restPrefix = trailingslashit(rest_get_url_prefix()); | |
| 1095 | + $isPreview = strpos($app->input->server->getString('REQUEST_URI', ''), $restPrefix) !== false | |
| 1096 | + || JUri::getInstance($app->input->server->getString('REQUEST_URI', ''))->hasVar('rest_route') | |
| 1097 | + || strpos($app->input->server->getString('REQUEST_URI', ''), '/wp-admin/') !== false; | |
| 1098 | + | |
| 1099 | + /** | |
| 1100 | + * This hook can be used to determine at runtime whether we should display | |
| 1101 | + * a preview of the widget for a block editor. | |
| 1102 | + * | |
| 1103 | + * @param bool $isPreview Whether the preview layout should be preferred. | |
| 1104 | + * | |
| 1105 | + * @since 1.10.71 | |
| 1106 | + */ | |
| 1107 | + $isPreview = apply_filters('vikwp_widget_block_preview', $isPreview); | |
| 1108 | + | |
| 1109 | + return $isPreview; | |
| 1054 | 1110 | } |
| 1055 | 1111 | } |