Changeset 2675218
- Timestamp:
- 02/08/2022 06:05:33 PM (4 years ago)
- Location:
- polylang-supertext/trunk
- Files:
-
- 3 edited
-
plugin.php (modified) (1 diff)
-
readme.txt (modified) (1 diff)
-
src/Supertext/TextAccessors/AcfTextAccessor.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
polylang-supertext/trunk/plugin.php
r2652708 r2675218 7 7 Domain Path: /resources/languages 8 8 Author: Supertext AG 9 Version: 4.0 89 Version: 4.09 10 10 Author URI: http://www.supertext.ch 11 11 License: GPLv2 or later 12 12 */ 13 13 14 define('SUPERTEXT_PLUGIN_VERSION', '4.0 8');14 define('SUPERTEXT_PLUGIN_VERSION', '4.09'); 15 15 define('SUPERTEXT_PLUGIN_REVISION', 46); 16 16 define('SUPERTEXT_BASE_PATH', __DIR__); -
polylang-supertext/trunk/readme.txt
r2652708 r2675218 110 110 == Changelog == 111 111 112 = 4.09 = 113 * Add basic support for ACF Gutenberg blocks that contain text in the data object. 114 112 115 = 4.08 = 113 116 * Suppress warnings generated by DOMDocument::loadHTML() -
polylang-supertext/trunk/src/Supertext/TextAccessors/AcfTextAccessor.php
r2611600 r2675218 12 12 { 13 13 const META_KEY_DELIMITER = '_(\\d+_)?'; 14 const ACF_BLOCK_NAME_PREFIX = 'acf/'; 15 const ACF_BLOCK_ID_PREFIX = 'block_'; 16 const ACF_BLOCK_TEXT_ID_REGEX = '/(block_[^_]+)_(.*)/'; 14 17 15 18 /** … … 29 32 { 30 33 $translatableFields = parent::getTranslatableFields($postId); 31 32 return array_merge(array(array( 34 $structuralData = array( 33 35 'title' => __('Copy structural meta data', 'supertext'), 34 36 'name' => 'sttr-structural-meta-data', 35 'checkedPerDefault' => true 36 )), $translatableFields); 37 'checkedPerDefault' => false 38 ); 39 $additionalFields = array($structuralData); 40 41 if ($this->hasAcfBlocks(get_post($postId)->post_content)) { 42 array_push($additionalFields, array( 43 'title' => __('Blocks', 'supertext'), 44 'name' => 'sttr-blocks-meta-data', 45 'checkedPerDefault' => true 46 )); 47 } 48 49 return array_merge($additionalFields, $translatableFields); 37 50 } 38 51 … … 50 63 $fields = get_fields($post->ID); 51 64 52 return $this->getMetaData($post->ID, "", $fields);65 return $this->getMetaData($post->ID, '', $fields); 53 66 } 54 67 … … 62 75 update_post_meta($post->ID, $key, $value); 63 76 } 77 } 78 79 /** 80 * Handle special case when ACF blocks are being used. 81 * @param $post 82 * @param $selectedTranslatableFields 83 * @return array 84 */ 85 public function getTexts($post, $selectedTranslatableFields) 86 { 87 $texts = parent::getTexts($post, $selectedTranslatableFields); 88 89 if (!$this->hasAcfBlocks($post->post_content) || !isset($selectedTranslatableFields['sttr-blocks-meta-data'])) { 90 return $texts; 91 } 92 93 return $this->addAcfBlockTexts($post, $texts); 94 } 95 96 /** 97 * Handle special case when ACF blocks are being used. 98 * @param $post 99 * @param $texts 100 */ 101 public function setTexts($post, $texts) 102 { 103 if (!$this->hasAcfBlocks($post->post_content)) { 104 parent::setTexts($post, $texts); 105 return; 106 } 107 108 $metaTexts = array(); 109 $acfBlockTexts = array(); 110 111 foreach ($texts as $id => $text) { 112 if (strpos($id, self::ACF_BLOCK_ID_PREFIX) === 0) { 113 $acfBlockTexts[$id] = $text; 114 } else { 115 $metaTexts[$id] = $text; 116 } 117 } 118 119 $this->setAcfBlockTexts($post, $acfBlockTexts); 120 121 parent::setTexts($post, $metaTexts); 64 122 } 65 123 … … 184 242 return $metaData; 185 243 } 244 245 private function hasAcfBlocks($post_content) 246 { 247 $necessaryBlockFunctionsExist = function_exists('acf_register_block_type') && function_exists('parse_blocks') && function_exists('serialize_blocks') && function_exists('has_blocks'); 248 249 return $necessaryBlockFunctionsExist && has_blocks($post_content) && strpos($post_content, '<!-- wp:' . self::ACF_BLOCK_NAME_PREFIX) !== false; 250 } 251 252 private function addAcfBlockTexts($post, $texts) 253 { 254 $savedFieldDefinitions = $this->library->getSettingOption(Constant::SETTING_PLUGIN_CUSTOM_FIELDS); 255 256 if (!isset($savedFieldDefinitions[$this->pluginId])) { 257 return $texts; 258 } 259 260 $blocks = parse_blocks($post->post_content); 261 262 foreach ($blocks as $block) { 263 if (!$this->isAcfBlock($block)) { 264 continue; 265 } 266 267 $blockId = $block['attrs']['id']; 268 $data = $block['attrs']['data']; 269 $metaKeys = array_keys($data); 270 271 foreach ($savedFieldDefinitions[$this->pluginId] as $savedFieldDefinition) { 272 $metaKeyMatches = preg_grep('/^' . $savedFieldDefinition['meta_key_regex'] . '$/', $metaKeys); 273 274 foreach ($metaKeyMatches as $metaKeyMatch) { 275 $textKey = $blockId . '_' . $metaKeyMatch; 276 $texts[$textKey] = $this->textProcessor->replaceShortcodes($data[$metaKeyMatch]); 277 } 278 } 279 } 280 281 return $texts; 282 } 283 284 private function setAcfBlockTexts($post, $acfBlockTexts) 285 { 286 $blocks = parse_blocks($post->post_content); 287 288 foreach ($acfBlockTexts as $id => $text) { 289 preg_match(self::ACF_BLOCK_TEXT_ID_REGEX, $id, $idRegexMatches); 290 291 $blockId = $idRegexMatches[1]; 292 $metaKey = $idRegexMatches[2]; 293 $decodedContent = html_entity_decode($text, ENT_COMPAT | ENT_HTML401, 'UTF-8'); 294 $value = $this->textProcessor->replaceShortcodeNodes($decodedContent); 295 296 foreach ($blocks as &$block) { 297 if (!$this->isAcfBlock($block)) { 298 continue; 299 } 300 301 $currentBlockId = $block['attrs']['id']; 302 303 if ($currentBlockId !== $blockId) { 304 continue; 305 } 306 307 $block['attrs']['data'][$metaKey] = $value; 308 } 309 } 310 311 $post->post_content = serialize_blocks($blocks); 312 } 313 314 private function isAcfBlock($block) 315 { 316 return strpos($block['blockName'], self::ACF_BLOCK_NAME_PREFIX) === 0 && isset($block['attrs']) && isset($block['attrs']['id']) && isset($block['attrs']['data']); 317 } 186 318 }
Note: See TracChangeset
for help on using the changeset viewer.