<?php

namespace Webien\Setup;

class WCAG
{
    static function init()
    {
        $use = get_field('webien_jump_to_content_wcag', 'option');
        if(filter_var($use, FILTER_VALIDATE_BOOLEAN) || !isset($use)){
            add_action('wp_body_open', [__CLASS__, 'jump_to_content'], 1);
        }

    }

    /**
     * Render a `jump to content` button at start of body tag
     * to make it be the first element tabbable navigation focus on
     */
    public static function jump_to_content()
    {
        if(did_action('elementor/loaded')){
            $button = \Elementor\Plugin::instance()->elements_manager->create_element_instance(
                [
                    'elType' => 'widget',
                    'widgetType' => 'button',
                    'id' => 'jump-to-content',
                    'settings' => [
                        'text' => __('Till innehåll', 'webien'),
                        'link' => [
                            'url' => '#content'
                        ],
                    ],
                ],
                []
            );
            $button->add_render_attribute('wrapper', 'class', 'jump-to-content-button');
            $button->add_render_attribute('button', 'title', __('Till innehåll', 'webien'));
            $button->add_render_attribute('button', 'class', 'elementor-button-link elementor-button');
            $button->add_render_attribute('button', 'aria-label', __('Till innehåll', 'webien'));
            $button->print_element();
        }
    }
}