Plugin Directory

Changeset 3289551


Ignore:
Timestamp:
05/08/2025 06:06:49 AM (10 months ago)
Author:
weather25
Message:

update

Location:
free-weather/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • free-weather/trunk/free-weather.php

    r3288609 r3289551  
    538538}
    539539
    540 
    541 function weather_enqueue_block_editor_assets()
    542 {
    543     wp_enqueue_script(
     540// 1. Register your block (with proper handles)
     541function weather_register_block() {
     542    wp_register_script(
    544543        'weather-block-editor',
    545544        plugins_url('public/js/weather-block-editor.js', __FILE__),
    546         array('wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'),
     545        ['wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'],
    547546        filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-block-editor.js'),
    548547        true
    549548    );
    550549
     550    wp_register_style(
     551        'weather-block-editor-style',
     552        plugins_url('public/css/weather-block-editor.css', __FILE__),
     553        [],
     554        filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-block-editor.css')
     555    );
     556
     557    wp_register_style(
     558        'weather-public-style',
     559        plugins_url('public/css/weather-public.css', __FILE__),
     560        [],
     561        filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-public.css')
     562    );
     563
     564    register_block_type('weather/widget', [
     565        'editor_script'   => 'weather-block-editor',
     566        'editor_style'    => 'weather-block-editor-style',
     567        'style'           => 'weather-public-style', // Shared on frontend
     568        'render_callback' => 'weather_render_widget',
     569    ]);
     570}
     571add_action('init', 'weather_register_block');
     572// 2. Load weather-public.js on frontend AND iframe preview
     573function weather_enqueue_block_assets() {
    551574    wp_enqueue_script(
    552575        'weather-widget-runtime',
    553576        plugins_url('public/js/weather-public.js', __FILE__),
    554         array('jquery'),
     577        ['jquery'],
    555578        filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-public.js'),
    556579        true
    557580    );
    558 
    559     // If you have localizations to pass to your script
    560     wp_localize_script('weather-block-editor', 'weatherLocalize', array(
     581}
     582add_action('enqueue_block_assets', 'weather_enqueue_block_assets');
     583// 3. Optional: Load localized data for block editor only
     584function weather_enqueue_block_editor_localization() {
     585    wp_localize_script('weather-block-editor', 'weatherLocalize', [
    561586        'some_data' => 'Some value',
    562         // Add other data here
    563     ));
    564 
    565     wp_enqueue_style(
    566         'weather-style',
    567         plugins_url('public/css/weather-public.css', __FILE__),
    568         array(),
    569         filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-public.css')
    570     );
    571 }
    572 
    573 add_action('enqueue_block_editor_assets', 'weather_enqueue_block_editor_assets');
    574 
    575 
    576 function weather_register_block()
    577 {
    578     // Register the block editor script
    579     wp_register_script(
    580         'weather-block-editor',
    581         plugins_url('public/js/weather-block-editor.js', __FILE__),
    582         array('wp-blocks', 'wp-element', 'wp-components', 'wp-i18n', 'wp-editor'),
    583         filemtime(plugin_dir_path(__FILE__) . 'public/js/weather-block-editor.js')
    584     );
    585 
    586     // Enqueue the block editor style
    587     wp_enqueue_style(
    588         'weather-block-editor-style',
    589         plugins_url('public/css/weather-block-editor.css', __FILE__), // Path to your custom CSS file
    590         array(),
    591         filemtime(plugin_dir_path(__FILE__) . 'public/css/weather-block-editor.css')
    592     );
    593 
    594     // Register the block with a render callback for server-side rendering
    595     register_block_type('weather/widget', array(
    596         'editor_script' => 'weather-block-editor',
    597         'render_callback' => 'weather_render_widget', // Specify the render callback function
    598     ));
    599 }
     587    ]);
     588}
     589add_action('enqueue_block_editor_assets', 'weather_enqueue_block_editor_localization');
    600590
    601591// The render callback function to generate the block's HTML based on attributes
  • free-weather/trunk/public/css/weather-block-editor.css

    r3288609 r3289551  
    3838    font-weight: normal;
    3939    color: #555;
    40     margin-bottom: 8px;
     40    margin: 0 0 8px 0;
    4141}
    4242.wp-block[data-type="weather/widget"] .components-panel__body-title,
  • free-weather/trunk/public/js/weather-block-editor.js

    r3288609 r3289551  
    1515        props.setAttributes(obj);
    1616        setTimeout(() => {
    17             // fire in parent (just in case)
    18             window.dispatchEvent(new Event('weather-widget-refresh'));
    19 
    20             // fire in every Gutenberg preview iframe
    21             document.querySelectorAll('iframe').forEach(frame => {
    22                 try {
    23                     frame.contentWindow.dispatchEvent(new Event('weather-widget-refresh'));
    24                 } catch (e) {
    25                     /* cross‑origin or inaccessible frame — skip */
    26                 }
    27             });
    28         }, 2000);
     17            refresh_widget();
     18        }, 300);
     19    }
     20
     21    function refresh_widget(){
     22        // fire in parent (just in case)
     23        window.dispatchEvent(new Event('weather-widget-refresh'));
     24
     25        // fire in every Gutenberg preview iframe
     26        document.querySelectorAll('iframe').forEach(frame => {
     27            try {
     28                frame.contentWindow.dispatchEvent(new Event('weather-widget-refresh'));
     29            } catch (e) {
     30                /* cross‑origin or inaccessible frame — skip */
     31            }
     32        });
    2933    }
    3034
     
    5054            backgroundColor: {
    5155                type: 'string',
    52                 default: '#becffb',
     56                default: 'linear-gradient(to left,#d3dfff,#fbfcff)',
    5357            },
    5458            widgetWidth: {
     
    6569            },
    6670            showSunrise: {
    67                 type: 'boolean',
    68                 default: false,
     71                type: 'string',
     72                default: '',
    6973            },
    7074            showWind: {
    71                 type: 'boolean',
    72                 default: false,
     75                type: 'string',
     76                default: '',
    7377            },
    7478            language: {
     
    7781            },
    7882            showCurrent: {
    79                 type: 'boolean',
    80                 default: true,
     83                type: 'string',
     84                default: 'on',
    8185            },
    8286        },
    8387
    8488        edit: function(props) {
     89            setTimeout(() => {
     90                refresh_widget();
     91            }, 300);
     92            setTimeout(() => {
     93                refresh_widget();
     94            }, 2500);
    8595            return el(
    8696                'div', {},
     
    136146                            checked: props.attributes.showCurrent,
    137147                            onChange: function(newValue) {
    138                                 props.setAttributes({ showCurrent: newValue });
    139                                 setAttrAndRefresh(props, { showCurrent: newValue })
     148                                props.setAttributes({ showCurrent:  newValue ? 'on' : '' });
     149                                setAttrAndRefresh(props, { showCurrent:  newValue ? 'on' : '' })
    140150                            }
    141151                        }),
     
    218228                    )
    219229                ),
    220                 // ... Your block preview or other components here
     230                el('div', {
     231                        className: 'preview-weather-widget weather-widget weather_widget_wrap',
     232                        'data-text-color': props.attributes.textColor,
     233                        'data-background': props.attributes.backgroundColor,
     234                        'data-width': props.attributes.widgetWidth,
     235                        'data-days': props.attributes.days,
     236                        'data-sunrise': props.attributes.showSunrise,
     237                        'data-wind': props.attributes.showWind,
     238                        'data-current': props.attributes.showCurrent,
     239                        'data-language': props.attributes.language,
     240                        'data-city': props.attributes.city,
     241                        'data-country': props.attributes.country,
     242                        style: {
     243                            backgroundColor: props.attributes.backgroundColor,
     244                            color: props.attributes.textColor,
     245                            padding: '10px',
     246                            marginTop: '20px',
     247                            border: '1px dashed #ccc'
     248                        }
     249                    },
     250                    el('div', {
     251                        className: 'weather_widget_placeholder'
     252                    }),
     253                    el('div', {
     254                            style: {
     255                                fontSize: '14px',
     256                                textAlign: 'center',
     257                                paddingTop: '6px',
     258                                paddingBottom: '4px',
     259                                background: 'rgba(0,0,0,0.03)'
     260                            }
     261                        },
     262                        'Data from ',
     263                        el('a', {
     264                            target: '_blank',
     265                            href: 'https://www.weather25.com'
     266                        }, 'weather25.com')
     267                    )
     268                )
    221269            );
    222270        },
  • free-weather/trunk/public/js/weather-public.js

    r3288823 r3289551  
    5252     * ----------------------------------------------------------- */
    5353    function initWidget($w) {
    54         const city      = $w.data('city');
    55         const country   = $w.data('country');
    56         const language  = $w.data('language') || 'english';
    57         const days      = parseInt($w.data('days'), 10) || 0;
    58         const widthPref = $w.data('width') === 'tight' ? 'tight' : 'maxwidth';
    59         const showCur   = $w.data('current') === 'on';
    60         const showWind  = $w.data('wind')    === 'on';
    61         const showSun   = $w.data('sunrise') === 'on';
    62         const bg        = $w.data('background') || 'linear-gradient(to left,#d3dfff,#fbfcff)';
    63         const fg        = $w.data('text-color') || 'black';
     54        const city      = $w.attr('data-city');
     55        const country   = $w.attr('data-country');
     56        const language  = $w.attr('data-language') || 'english';
     57        const days      = parseInt($w.attr('data-days'), 10) || 0;
     58        const widthPref = $w.attr('data-width') === 'tight' ? 'tight' : 'maxwidth';
     59        const showCur   = $w.attr('data-current') === 'on';
     60        const showWind  = $w.attr('data-wind')    === 'on';
     61        const showSun   = $w.attr('data-sunrise') === 'on';
     62        const bg        = $w.attr('data-background') || 'linear-gradient(to left,#d3dfff,#fbfcff)';
     63        const fg        = $w.attr('data-text-color') || 'black';
    6464        const place     = city.replace(' ', '_') + ',' + country;
    6565
     
    7373        $w.attr('data-render-hash', hash);
    7474
     75        $w.removeClass('tight maxwidth').addClass(widthPref);
     76
    7577        $.getJSON(
    7678            'https://www.weatherwp.com/api/common/publicWeatherForLocation.php',
     
    8082        function draw(r) {
    8183            const wrap = $('<div>', {
    82                 class : `main_wrap ${widthPref}`,
     84                class : `main_wrap`,
    8385                css   : { background: bg, color: fg }
    8486            });
     
    157159            link.append(wrap);
    158160
    159             $w.find('.weather_widget_placeholder').replaceWith(link);
     161            $w.find('.weather_widget_placeholder')
     162                .empty()
     163                .append(link);
    160164        }
    161165    }
     
    166170    function refresh() {
    167171        $('.weather-widget').each(function () { initWidget($(this)); });
     172        $('.preview-weather-widget.weather-widget').each(function () {
     173            initWidget($(this));
     174        });
     175        document.querySelectorAll('iframe').forEach(frame => {
     176            try {
     177                const doc = frame.contentDocument || frame.contentWindow.document;
     178                if (!doc) return;
     179
     180                $(doc).find('.preview-weather-widget.weather-widget').each(function () {
     181                    initWidget($(this));
     182                });
     183            } catch (e) {
     184                // Cross-origin or inaccessible iframe — safely skip
     185                // console.warn('Skipping iframe due to CORS or sandbox:', e);
     186            }
     187        });
    168188    }
    169189    window.refreshMeteoWidgets = refresh;
Note: See TracChangeset for help on using the changeset viewer.