Plugin Directory

Changeset 2675218


Ignore:
Timestamp:
02/08/2022 06:05:33 PM (4 years ago)
Author:
Supertext
Message:

Sync with GitHub

Location:
polylang-supertext/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • polylang-supertext/trunk/plugin.php

    r2652708 r2675218  
    77Domain Path: /resources/languages
    88Author: Supertext AG
    9 Version: 4.08
     9Version: 4.09
    1010Author URI: http://www.supertext.ch
    1111License: GPLv2 or later
    1212*/
    1313
    14 define('SUPERTEXT_PLUGIN_VERSION', '4.08');
     14define('SUPERTEXT_PLUGIN_VERSION', '4.09');
    1515define('SUPERTEXT_PLUGIN_REVISION', 46);
    1616define('SUPERTEXT_BASE_PATH', __DIR__);
  • polylang-supertext/trunk/readme.txt

    r2652708 r2675218  
    110110== Changelog ==
    111111
     112= 4.09 =
     113* Add basic support for ACF Gutenberg blocks that contain text in the data object.
     114
    112115= 4.08 =
    113116* Suppress warnings generated by DOMDocument::loadHTML()
  • polylang-supertext/trunk/src/Supertext/TextAccessors/AcfTextAccessor.php

    r2611600 r2675218  
    1212{
    1313  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_[^_]+)_(.*)/';
    1417
    1518  /**
     
    2932  {
    3033    $translatableFields = parent::getTranslatableFields($postId);
    31 
    32     return array_merge(array(array(
     34    $structuralData = array(
    3335      'title' => __('Copy structural meta data', 'supertext'),
    3436      '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);
    3750  }
    3851
     
    5063    $fields = get_fields($post->ID);
    5164
    52     return $this->getMetaData($post->ID, "", $fields);
     65    return $this->getMetaData($post->ID, '', $fields);
    5366  }
    5467
     
    6275      update_post_meta($post->ID, $key, $value);
    6376    }
     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);
    64122  }
    65123
     
    184242    return $metaData;
    185243  }
     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  }
    186318}
Note: See TracChangeset for help on using the changeset viewer.