Make WordPress Core

Ticket #63922: fontsize-bug-replication.php

File fontsize-bug-replication.php, 10.7 KB (added by yashjawale, 6 months ago)

Test plugin that tries to reproduce bug

Line 
1<?php
2/**
3 * Plugin Name: FontSizePicker Unit Bug Replication
4 * Description: Replicates WordPress Core Ticket #63922 - FontSizePicker incorrectly measures units for selecting elements
5 * Version: 1.0.0
6 * Author: Bug Replication
7 * Requires at least: 6.0
8 * Tested up to: 6.8
9 * License: GPL v2 or later
10 */
11
12// Prevent direct access
13if (!defined('ABSPATH')) {
14    exit;
15}
16
17class FontSizePickerBugReplication {
18   
19    public function __construct() {
20        add_action('init', array($this, 'register_block'));
21        add_action('enqueue_block_editor_assets', array($this, 'enqueue_block_assets'));
22    }
23   
24    public function register_block() {
25        register_block_type('fontsize-bug/test-block', array(
26            'editor_script' => 'fontsize-bug-block',
27            'attributes' => array(
28                'titleFontSize' => array(
29                    'type' => 'string',
30                    'default' => '16'
31                ),
32                'titleFontUnit' => array(
33                    'type' => 'string', 
34                    'default' => 'px'
35                ),
36                'content' => array(
37                    'type' => 'string',
38                    'default' => 'Test text with FontSizePicker bug'
39                )
40            ),
41            'render_callback' => array($this, 'render_block')
42        ));
43    }
44   
45    public function enqueue_block_assets() {
46        wp_enqueue_script(
47            'fontsize-bug-block',
48            plugin_dir_url(__FILE__) . 'block.js',
49            array('wp-blocks', 'wp-components', 'wp-element', 'wp-editor', 'wp-block-editor'),
50            '1.0.0',
51            true
52        );
53       
54        // Inline the JavaScript since this is a single file plugin
55        $this->inline_block_script();
56    }
57   
58    private function inline_block_script() {
59        $script = "
60        (function() {
61            const { registerBlockType } = wp.blocks;
62            const { InspectorControls, RichText } = wp.blockEditor;
63            const { PanelBody, FontSizePicker } = wp.components;
64            const { Fragment, createElement: el } = wp.element;
65           
66            registerBlockType('fontsize-bug/test-block', {
67                title: 'FontSize Bug Test',
68                icon: 'text',
69                category: 'text',
70                attributes: {
71                    titleFontSize: {
72                        type: 'string',
73                        default: '16'
74                    },
75                    titleFontUnit: {
76                        type: 'string',
77                        default: 'px'
78                    },
79                    content: {
80                        type: 'string',
81                        default: 'Test text with FontSizePicker bug'
82                    }
83                },
84               
85                edit: function(props) {
86                    const { attributes, setAttributes } = props;
87                    const { titleFontSize, titleFontUnit, content } = attributes;
88                   
89                    // Font sizes that demonstrate the bug
90                    // Using numeric values that will be converted to rem internally
91                    const fontSizes = [
92                        {
93                            name: 'Small',
94                            size: 12,
95                            slug: 'small'
96                        },
97                        {
98                            name: 'Normal',
99                            size: 16,
100                            slug: 'normal'
101                        },
102                        {
103                            name: 'Medium',
104                            size: 20,
105                            slug: 'medium'
106                        },
107                        {
108                            name: 'Large',
109                            size: 24,
110                            slug: 'large'
111                        },
112                        {
113                            name: 'Big',
114                            size: 32,
115                            slug: 'big'
116                        }
117                    ];
118                   
119                    return el(Fragment, {},
120                        el(InspectorControls, {},
121                            el(PanelBody, { title: 'Typography Settings', initialOpen: true },
122                                el('div', { style: { marginBottom: '20px' } },
123                                    el('strong', {}, 'Font Size (Bug Demonstration)'),
124                                    el('br'),
125                                    el('small', {}, 'Switch between presets (S/M/L/XL) then to custom - notice unit inconsistency')
126                                ),
127                                el(FontSizePicker, {
128                                    __next40pxDefaultSize: true,
129                                    fontSizes: fontSizes,
130                                    value: titleFontSize ? parseFloat(titleFontSize) : undefined,
131                                    onChange: function(newSize) {
132                                        console.log('FontSizePicker onChange:', newSize, typeof newSize);
133                                       
134                                        if (newSize === undefined) {
135                                            setAttributes({
136                                                titleFontSize: '16',
137                                                titleFontUnit: 'px'
138                                            });
139                                            return;
140                                        }
141                                       
142                                        // This is where the bug manifests:
143                                        // We set unit as 'px' but the value might be in rem
144                                        setAttributes({
145                                            titleFontSize: String(newSize),
146                                            titleFontUnit: 'px'  // Always setting as px but actual value conversion varies
147                                        });
148                                    },
149                                    withSlider: true
150                                }),
151                                el('div', { style: { marginTop: '15px', padding: '10px', background: '#f0f0f0', borderRadius: '4px' } },
152                                    el('strong', {}, 'Debug Info:'),
153                                    el('br'),
154                                    el('span', {}, 'Stored Font Size: ' + titleFontSize + ' ' + titleFontUnit),
155                                    el('br'),
156                                    el('span', {}, 'Actual Computed Style: Check inspector'),
157                                    el('br'),
158                                    el('small', { style: { color: '#d63638' } },
159                                        'BUG: When switching from presets to custom, units may show as px but be calculated as rem'
160                                    )
161                                )
162                            )
163                        ),
164                        el('div', {
165                            className: 'fontsize-bug-test-block',
166                            style: {
167                                fontSize: titleFontSize + titleFontUnit,
168                                border: '2px dashed #ccc',
169                                padding: '20px',
170                                margin: '10px 0'
171                            }
172                        },
173                            el(RichText, {
174                                tagName: 'p',
175                                placeholder: 'Enter test text...',
176                                value: content,
177                                onChange: function(newContent) {
178                                    setAttributes({ content: newContent });
179                                },
180                                style: {
181                                    fontSize: 'inherit',
182                                    margin: 0
183                                }
184                            }),
185                            el('div', {
186                                style: {
187                                    fontSize: '12px',
188                                    color: '#666',
189                                    marginTop: '10px',
190                                    fontFamily: 'monospace'
191                                }
192                            },
193                                'Applied: font-size: ' + titleFontSize + titleFontUnit
194                            )
195                        )
196                    );
197                },
198               
199                save: function(props) {
200                    const { attributes } = props;
201                    const { titleFontSize, titleFontUnit, content } = attributes;
202                   
203                    return el('div', {
204                        className: 'fontsize-bug-test-block',
205                        style: { fontSize: titleFontSize + titleFontUnit }
206                    }, content);
207                }
208            });
209        })();
210        ";
211       
212        wp_add_inline_script('fontsize-bug-block', $script);
213    }
214   
215    public function render_block($attributes) {
216        $font_size = isset($attributes['titleFontSize']) ? $attributes['titleFontSize'] : '16';
217        $font_unit = isset($attributes['titleFontUnit']) ? $attributes['titleFontUnit'] : 'px';
218        $content = isset($attributes['content']) ? $attributes['content'] : 'Test text';
219       
220        return sprintf(
221            '<div class="fontsize-bug-test-block" style="font-size: %s%s; border: 1px solid #ddd; padding: 15px;">%s</div>',
222            esc_attr($font_size),
223            esc_attr($font_unit), 
224            wp_kses_post($content)
225        );
226    }
227}
228
229// Initialize the plugin
230new FontSizePickerBugReplication();
231
232// Add some CSS for better visualization
233add_action('wp_head', function() {
234    echo '<style>
235        .fontsize-bug-test-block {
236            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
237        }
238    </style>';
239});
240
241// Add admin notice explaining the bug
242add_action('admin_notices', function() {
243    $screen = get_current_screen();
244    if ($screen && ($screen->id === 'post' || $screen->id === 'page')) {
245        echo '<div class="notice notice-info">
246            <p><strong>FontSizePicker Bug Replication Active</strong></p>
247            <p>This plugin replicates WordPress Core Ticket #63922. To see the bug:</p>
248            <ol>
249                <li>Add the "FontSize Bug Test" block</li>
250                <li>In the sidebar, switch between font size presets (S/M/L/XL)</li>
251                <li>Then switch to custom size input</li>
252                <li>Notice the unit inconsistency - it shows "px" but may calculate as "rem"</li>
253                <li>Check browser inspector to see actual computed font-size</li>
254            </ol>
255        </div>';
256    }
257});
258
259?>