HEX
Server: LiteSpeed
System: Linux ragasnapis.serveriai.lt 5.14.0-687.26.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Jul 14 16:32:02 EDT 2026 x86_64
User: odxqitalt (1170)
PHP: 8.3.19
Disabled: link, symlink, exec, passthru, proc_close, proc_get_status, proc_open, shell_exec, system, popen, pclose, dl, show_source, highlight_file, pcntl_exec, ini_restore, ini_alter, chgrp
Upload Files
File: /home/odxqitalt/public_html/wp-content/plugins/extendify/app/QuickEdit/Schemas/Button.php
<?php

namespace Extendify\QuickEdit\Schemas;

defined('ABSPATH') || die('No direct access.');

class Button implements Schema
{
    public function fields(): array
    {
        return [
            [
                'key'     => 'text',
                'control' => 'text',
                'label'   => __('Button text', 'extendify-local'),
            ],
            [
                'key'     => 'url',
                'control' => 'link',
                'label'   => __('Link URL', 'extendify-local'),
            ],
        ];
    }

    public function apply(array $block, string $fieldKey, $value): array
    {
        $existing = (string) ($block['innerHTML'] ?? '');

        switch ($fieldKey) {
            case 'text':
                $text = is_string($value) ? $value : '';
                $escaped = wp_kses_post($text);
                // Replace inner text while preserving the anchor's attributes.
                // Callback (not a replacement string) so user text containing
                // $1 / \1 / $0 is spliced literally, not parsed as a backref.
                $newInner = preg_replace_callback(
                    '/(<a\b[^>]*>)(.*?)(<\/a>)/is',
                    static fn ($m) => $m[1] . $escaped . $m[3],
                    $existing,
                    1
                );
                $block['innerHTML']    = $newInner ?: $existing;
                $block['innerContent'] = [$block['innerHTML']];
                return $block;

            case 'url':
                $url = is_string($value) ? esc_url_raw($value) : '';
                $tp = new \WP_HTML_Tag_Processor($existing);
                if ($tp->next_tag('a')) {
                    if ($url === '') {
                        $tp->remove_attribute('href');
                    } else {
                        $tp->set_attribute('href', $url);
                    }
                    $existing = $tp->get_updated_html();
                }
                $block['innerHTML']    = $existing;
                $block['innerContent'] = [$existing];
                // The canonical link target is the inner anchor's href, not a block attribute.
                return $block;
        }
        return $block;
    }
}