Make WordPress Core

Ticket #43003: HTML5required.php

File HTML5required.php, 4.2 KB (added by Andy Schmidt, 8 years ago)

Plugin with two versions of Widget - one works, one fails.

Line 
1<?php
2declare(strict_types=1);
3namespace HTML5required;
4
5// Reject attempt to launch this script stand-alone.
6if ( !defined( '\ABSPATH' ) ) { http_response_code( 403 ); exit( 'This script must not be launched stand-alone!' ); }
7
8/**
9 * HTML5 "required" Bug Widget.
10 *
11 * @author      Andy Schmidt <Andy_Schmidt@HM-Software.com>
12 * @copyright   2017 H&M Systems Software, Inc.
13 *
14 * @wordpress-plugin
15 * Plugin Name: HTML5required
16 * Description: Reproduces bug with HTML5 "required" attribute in Widget form. Tested with WordPress 4.9.1 on PHP 7.1.12.
17 * Version:     1.00.00
18 * Author:      H&M Systems Software, Inc.
19 */
20
21error_reporting( E_ALL ); 
22ini_set( 'display_errors', '1' );
23
24\add_action( 'widgets_init', function(): void { \register_widget( __NAMESPACE__.'\Widget_HTML5works' ); } );
25\add_action( 'widgets_init', function(): void { \register_widget( __NAMESPACE__.'\Widget_HTML5fails' ); } );
26
27return;
28
29class Widget_HTML5works extends \WP_Widget 
30{
31        public const BASE_ID            = 'html_does_work';             // Unique, lowercase base ID within WordPress.
32        public const REQUIRED_ATTR      = '';                                   // NO "required" attribute in form();
33       
34        /*
35         * Initializes the instance variables.
36         */
37        public function __construct() 
38        {
39                parent::__construct(    // Initialize instance variables.
40                        static::BASE_ID, 
41                        static::BASE_ID,                                                        // Name for the widget displayed on the configuration page.
42                        [       //  Widget options for wp_register_sidebar_widget().
43                                'description'   => 'Reproduces bug with HTML5 required attribute in Widget form.',
44                                'classname'             => static::BASE_ID,
45                        ]       
46                );
47        }
48       
49        /**
50         * Front-end display of widget.
51         *
52         * @param array $args                           Display arguments, including the opening/closing HTML elements.
53         * @param array $instance                       Saved settings for this instance of the widget.
54         *
55         * @see WP_Widget::widget()
56         */
57        public function widget( $args, $instance )
58        {
59                /*
60                 * Opening HTML Elements for the Container.
61                 */
62                echo $args[ 'before_widget' ];
63                if ( !empty( $title ) ) {
64                        echo $args[ 'before_title' ] . \apply_filters( 'widget_title', $instance[ 'title' ] ) . $args[ 'after_title' ];
65                }
66               
67                /*
68                 * Display Content.
69                 */
70                echo 'Hello World';
71               
72                /*
73                 * Closing HTML Elements for the Container.
74                 */
75                echo $args[ 'after_widget' ];
76        }
77       
78        /**
79         * Back-end widget form.
80         *
81         * Outputs the settings update form. The default width of the fully expanded
82         * control form is 250 and changing it is discouraged.
83         *
84         * @param array $old_settings           Current settings for this instance.
85         *
86         * @return string                                       'noform' will hide the "save" button.
87         *
88         * @see WP_Widget::form()
89         */
90        public function form( $old_settings )
91        {
92                ?>
93<p>
94        <label for="<?php echo \esc_attr( $this->get_field_id( 'title' ) ); ?>">Title:</label>
95        <input class="widefat" title="Optional title used for this instance." type="text" id="<?php echo \esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo \esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo \esc_attr( $old_settings[ 'title' ] ?? '' ); ?>">
96</p>
97<p>
98        <label for="<?php echo \esc_attr( $this->get_field_id( 'addtlfld' ) ); ?>">Text:</label>
99        <input class="widefat" title="Additional text fails if required input." type="text" <?php echo static::REQUIRED_ATTR; ?>id="<?php echo \esc_attr( $this->get_field_id( 'addtlfld' ) ); ?>" name="<?php echo \esc_attr( $this->get_field_name( 'addtlfld' ) ); ?>" value="<?php echo \esc_attr( $old_settings[ 'addtlfld' ] ?? '' ); ?>">
100</p>
101
102        <?php
103                return ''; 
104        }
105       
106        /**
107         * Sanitize and widget form values before saving.
108         *
109         * @param array $input                          Values gathered by settings update form.
110         * @param array $old_settings           Previously saved instance settings from database.
111         *
112         * @return array|bool                           Sanitized instance settings to be saved. FALSE = Skip saving.
113         *
114         * @see WP_Widget::update()
115         */
116        public function update( $input, $old_settings )
117        {
118                // processes widget options to be saved
119                return $input;
120        }
121}
122
123class Widget_HTML5fails extends Widget_HTML5works
124{
125        public const BASE_ID            = 'html_will_fail';                     // Unique, lowercase base ID within WordPress.
126        public const REQUIRED_ATTR      = ' required="required" ';
127}
128
129
130/*EOF*/