| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package VikWP - Libraries |
| 4 | 4 | * @subpackage adapter.module |
| 5 | 5 | * @author E4J s.r.l. |
| 6 | - * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | 8 | * @link https://vikwp.com |
| 9 | 9 | */ |
| 10 | 10 | |
| @@ -170,8 +170,46 @@ | ||
| 170 | 170 | $result->params = reset($settings); |
| 171 | 171 | |
| 172 | 172 | // JSON encode params for Joomla compatibility |
| 173 | 173 | $result->params = json_encode($result->params); |
| 174 | + } | |
| 175 | + } | |
| 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 | + } | |
| 174 | 212 | } |
| 175 | 213 | } |
| 176 | 214 | |
| 177 | 215 | return $result; |