Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
register_music_player_widget
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
Music_Player_Widget
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 6
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 hide_widget_in_block_editor
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 widget_scripts
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 widget
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 update
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 form
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
1<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3/**
4 * A multi-song music player widget
5 */
6class Music_Player_Widget extends WP_Widget {
7
8    /**
9     * Constructor
10     *
11     * @return void
12     **/
13    public function __construct() {
14        $widget_ops = array(
15            'classname'   => 'music-player',
16            'description' => __( 'A multi-song music player', 'wpcomsh' ),
17        );
18        parent::__construct( 'music-player', __( 'Music Player', 'wpcomsh' ), $widget_ops );
19        add_action( 'admin_enqueue_scripts', array( $this, 'widget_scripts' ) );
20        add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
21    }
22
23    /**
24     * Remove the widget from the Legacy Widget block
25     *
26     * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
27     * @return array $widget_types New list of widgets that will be removed.
28     */
29    public function hide_widget_in_block_editor( $widget_types ) {
30        $widget_types[] = 'music-player';
31        return $widget_types;
32    }
33
34    /**
35     * Enqueue media and scripts.
36     */
37    public function widget_scripts() {
38        wp_enqueue_media();
39        wp_enqueue_script( 'music-player', plugins_url( 'music-player/music-player.js', __FILE__ ), array( 'jquery', 'thickbox' ), '1', true );
40    }
41
42    /**
43     * Outputs the HTML for this widget.
44     *
45     * @param array $args An array of standard parameters for widgets in this theme.
46     * @param array $instance An array of settings for this widget instance.
47     *
48     * @return void Echoes its output.
49     **/
50    public function widget( $args, $instance ) {
51        echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
52
53        if ( ! empty( $instance['title'] ) ) {
54            echo $args['before_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
55            $title = apply_filters( 'widget_title', $instance['title'] );
56            echo esc_html( $title );
57            echo $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
58        }
59
60        if ( isset( $instance['shortcode'] ) ) {
61            $shortcode = wp_kses( $instance['shortcode'], array() );
62            echo do_shortcode( $shortcode );
63        }
64        echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
65
66        /** This action is documented in jetpack/modules/widgets/gravatar-profile.php */
67        do_action( 'jetpack_stats_extra', 'widget_view', 'music-player' );
68    }
69
70    /**
71     * Deals with the settings when they are saved by the admin. Here is
72     * where any validation should be dealt with.
73     *
74     * @param array $new_instance An array of new settings as submitted by the admin.
75     * @param array $old_instance An array of the previous settings.
76     *
77     * @return array The validated and (if necessary) amended settings
78     **/
79    public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
80        $updated_instance = array(
81            'title'     => wp_strip_all_tags( $new_instance['title'] ),
82            'shortcode' => wp_strip_all_tags( $new_instance['shortcode'] ),
83        );
84        return $updated_instance;
85    }
86
87    /**
88     * Displays the form for this widget on the Widgets page of the WP Admin area.
89     *
90     * @param array $instance An array of the current settings for this widget.
91     *
92     * @return never Echoes its output
93     **/
94    public function form( $instance ) {
95        $instance = wp_parse_args(
96            (array) $instance,
97            array(
98                'title'     => '',
99                'shortcode' => '',
100            )
101        );
102        printf(
103            '<p><label>%s <input type="text" value="%s" name="%s" id="%s" /></label></p>',
104            esc_html__( 'Title:', 'wpcomsh' ),
105            esc_attr( $instance['title'] ),
106            esc_attr( $this->get_field_name( 'title' ) ),
107            esc_attr( $this->get_field_id( 'title' ) )
108        );
109
110        printf(
111            '<p><a class="music-player-edit" data-widget_id="%s" href="#">%s</a></p>',
112            esc_attr( $this->get_field_id( 'shortcode' ) ),
113            esc_html__( 'Choose songs', 'wpcomsh' )
114        );
115
116        printf(
117            '<p><label>%s <input class="widefat" name="%s" id="%s" value="%s" /></label>',
118            esc_html__( 'Music Player', 'wpcomsh' ),
119            esc_attr( $this->get_field_name( 'shortcode' ) ),
120            esc_attr( $this->get_field_id( 'shortcode' ) ),
121            esc_attr( $instance['shortcode'] )
122        );
123    }
124}
125
126/**
127 * Register the widget.
128 */
129function register_music_player_widget() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed
130    register_widget( 'Music_Player_Widget' );
131}
132add_action( 'widgets_init', 'register_music_player_widget' );