| @@ -173,7 +173,45 @@ | ||
| 173 | 173 | $result->params = json_encode($result->params); |
| 174 | 174 | } |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | + /** | |
| 178 | + * If we are not able to find a widget, then we should look into the | |
| 179 | + * posts and pages in search of a Gutenberg block and extract the | |
| 180 | + * settings from there. | |
| 181 | + * | |
| 182 | + * @since 10.1.52 | |
| 183 | + */ | |
| 184 | + if (!$result->id) | |
| 185 | + { | |
| 186 | + // split the module name in chunks: | |
| 187 | + // [MOD] | [COMPONENT] | ... | |
| 188 | + $block = explode('_', $name); | |
| 189 | + // construct gutenberg block identifier | |
| 190 | + $block = $block[1] . '/' . implode('-', array_slice($block, 2)) . '-widget-block'; | |
| 191 | + | |
| 192 | + // look for a post/page that contains the block name of the requested module | |
| 193 | + $query = new WP_Query([ | |
| 194 | + 's' => '<!-- wp:' . $block, | |
| 195 | + ]); | |
| 196 | + | |
| 197 | + // fetch all the matching posts, if any | |
| 198 | + foreach ($query->get_posts() as $post) | |
| 199 | + { | |
| 200 | + // use the same ID of the post | |
| 201 | + $result->id = $post->ID; | |
| 202 | + | |
| 203 | + // escape characters of the module block name for being safely used in a regex | |
| 204 | + $regex = preg_quote($block, '/'); | |
| 205 | + | |
| 206 | + // extract parameters from block name | |
| 207 | + if (preg_match("/<!-- wp:$regex\s+({(?:.*?)})(?:.*?)-->/", $post->post_content, $match)) | |
| 208 | + { | |
| 209 | + // keep the parameters encoded in JSON format for Joomla compatibility | |
| 210 | + $result->params = $match[1]; | |
| 211 | + } | |
| 212 | + } | |
| 213 | + } | |
| 214 | + | |
| 177 | 215 | return $result; |
| 178 | 216 | } |
| 179 | 217 | } |