Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 2
CRAP
n/a
0 / 0
jetpack_mu_wpcom_unregister_retired_widgets
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
90
jetpack_mu_wpcom_hide_retired_widgets_from_legacy_block
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Widgets for WordPress.com
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * WP.com Widgets (in alphabetical order)
10 */
11require_once __DIR__ . '/class-jetpack-i-voted-widget.php';
12
13/**
14 * Some widgets are getting long in the tooth, hardly used, or just not very useful.
15 * Disable these widgets on sites where they're not active.
16 */
17define(
18    'JETPACK_MU_WPCOM_RETIRED_WIDGETS',
19    array(
20        'akismet_widget' => 'Akismet_Widget',
21        'author_grid'    => 'Widget_Authors_Grid',
22        'delicious'      => 'Delicious_Widget',
23        'eventbrite'     => 'Eventbrite_Widget',
24        'geo_widget'     => 'Geo_Widget',
25        'i_voted'        => 'Jetpack_I_Voted_Widget',
26        'links'          => 'WP_Widget_Links',
27        'meta'           => 'WP_Widget_Meta',
28        'pd_top_rated'   => 'PD_Top_Rated',
29        'top-clicks'     => 'Widget_Top_Clicks',
30        'music-player'   => 'Music_Player_Widget',
31    )
32);
33
34/**
35 * Unregister retired widgets that aren't currently active.
36 *
37 * @global array $retired_widgets List of retired widgets.
38 * @return void
39 */
40function jetpack_mu_wpcom_unregister_retired_widgets() {
41
42    if ( ( function_exists( 'wpcom_is_vip' ) && wpcom_is_vip() ) || ! is_admin() ) {
43        return;
44    }
45
46    // just in case this is in any way a saving operation
47    if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' === $_SERVER['REQUEST_METHOD'] ) {
48        return;
49    }
50
51    // let's only retire on the widgets page or in the Customizer
52    // since plugins/themes might use `the_widget()`
53    if ( ! in_array( $GLOBALS['pagenow'], array( 'widgets.php', 'customize.php' ), true ) ) {
54        return;
55    }
56
57    foreach ( JETPACK_MU_WPCOM_RETIRED_WIDGETS as $widget_id => $widget_class ) {
58        if ( is_active_widget( false, false, $widget_id ) ) {
59            continue;
60        }
61        unregister_widget( $widget_class );
62    }
63}
64add_action( 'widgets_init', 'jetpack_mu_wpcom_unregister_retired_widgets', 20 );
65
66/**
67 * Remove retired widgets from Legacy Widget block
68 *
69 * @see https://developer.wordpress.org/block-editor/how-to-guides/widgets/legacy-widget-block/
70 *
71 * @param array $widget_types Array of widget types to hide.
72 * @return array Modified array of widget types.
73 */
74function jetpack_mu_wpcom_hide_retired_widgets_from_legacy_block( $widget_types ) {
75    foreach ( JETPACK_MU_WPCOM_RETIRED_WIDGETS as $widget_id => $widget_class ) {
76        $widget_types[] = $widget_id;
77    }
78
79    return $widget_types;
80}
81add_filter( 'widget_types_to_hide_from_legacy_widget_block', 'jetpack_mu_wpcom_hide_retired_widgets_from_legacy_block' );