| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\QuickEdit\Services; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No direct access.'); |
| 6 |
|
| 7 |
use Extendify\Config; |
| 8 |
|
| 9 |
// core/media-text renders its image as a child <figure>, but the image can |
| 10 |
// live purely in the markup with no mediaUrl / mediaId block attrs — e.g. |
| 11 |
// blocks imported via Extendify carry only the <img> tag. The agent's |
| 12 |
// TagBlocks never marks the media side as its own selectable target, so tag |
| 13 |
// the media <figure> directly (when it actually holds an <img>) so the |
| 14 |
// front-end selector can resolve a click on the image to a media-text image |
| 15 |
// edit. Video media is out of scope, so gate on mediaType image. |
| 16 |
class MediaTextTagger |
| 17 |
{ |
| 18 |
// phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound -- 7.0 floor: no const visibility |
| 19 |
const ATTR = 'data-extendify-quick-edit-mediatext-media'; |
| 20 |
|
| 21 |
public static function init() |
| 22 |
{ |
| 23 |
add_filter('render_block', [self::class, 'tag'], 11, 2); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @param string|mixed $html |
| 28 |
* @param array $block |
| 29 |
*/ |
| 30 |
public static function tag($html, $block) |
| 31 |
{ |
| 32 |
if (is_admin() || !is_string($html) || $html === '') { |
| 33 |
return $html; |
| 34 |
} |
| 35 |
if (($block['blockName'] ?? '') !== 'core/media-text') { |
| 36 |
return $html; |
| 37 |
} |
| 38 |
if (!is_user_logged_in() || !current_user_can(Config::$requiredCapability)) { |
| 39 |
return $html; |
| 40 |
} |
| 41 |
if (($block['attrs']['mediaType'] ?? 'image') !== 'image') { |
| 42 |
return $html; |
| 43 |
} |
| 44 |
|
| 45 |
$tp = new \WP_HTML_Tag_Processor($html); |
| 46 |
while ($tp->next_tag('figure')) { |
| 47 |
if (!$tp->has_class('wp-block-media-text__media')) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
if ($tp->get_attribute(self::ATTR) !== null) { |
| 51 |
return $html; |
| 52 |
} |
| 53 |
// Only tag a figure that actually holds an image: the first tag |
| 54 |
// inside the media figure is the <img> for image-type blocks. This |
| 55 |
// skips placeholder figures with no media set. |
| 56 |
$tp->set_bookmark('media'); |
| 57 |
if (!$tp->next_tag() || $tp->get_tag() !== 'IMG') { |
| 58 |
return $html; |
| 59 |
} |
| 60 |
$tp->seek('media'); |
| 61 |
$tp->set_attribute(self::ATTR, '1'); |
| 62 |
return $tp->get_updated_html(); |
| 63 |
} |
| 64 |
|
| 65 |
return $html; |
| 66 |
} |
| 67 |
} |
| 68 |
|