Completed
Push — remove/google-plus-widget ( 98e3ec )
by
unknown
06:26
created

widgets.php ➔ jetpack_widgets_purge_widget_option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Extra Sidebar Widgets
4
 * Module Description: Add images, Twitter streams, and more to your sidebar.
5
 * Sort Order: 4
6
 * First Introduced: 1.2
7
 * Requires Connection: No
8
 * Auto Activate: Yes
9
 * Module Tags: Social, Appearance
10
 * Feature: Appearance
11
 * Additional Search Queries: widget, widgets, facebook, gallery, twitter, gravatar, image, rss
12
 */
13
14
function jetpack_load_widgets() {
15
	$widgets_include = array();
16
17
	foreach ( Jetpack::glob_php( dirname( __FILE__ ) . '/widgets' ) as $file ) {
18
		$widgets_include[] = $file;
19
	}
20
	/**
21
	 * Modify which Jetpack Widgets to register.
22
	 *
23
	 * @module widgets
24
	 *
25
	 * @since 2.2.1
26
	 *
27
	 * @param array $widgets_include An array of widgets to be registered.
28
	 */
29
	$widgets_include = apply_filters( 'jetpack_widgets_to_include', $widgets_include );
30
31
	foreach( $widgets_include as $include ) {
32
		include_once $include;
33
	}
34
35
	include_once dirname( __FILE__ ) . '/widgets/migrate-to-core/image-widget.php';
36
	include_once dirname( __FILE__ ) . '/widgets/migrate-to-core/gallery-widget.php';
37
}
38
39
add_action( 'jetpack_modules_loaded', 'jetpack_widgets_loaded' );
40
41
function jetpack_widgets_loaded() {
42
	Jetpack::enable_module_configurable( __FILE__ );
43
	Jetpack::module_configuration_load( __FILE__, 'jetpack_widgets_configuration_load' );
44
	add_filter( 'jetpack_module_configuration_url_widgets', 'jetpack_widgets_configuration_url' );
45
}
46
47
function jetpack_widgets_configuration_load() {
48
	wp_safe_redirect( admin_url( 'widgets.php' ) );
49
	exit;
50
}
51
52
/**
53
 * Overrides default configuration url
54
 *
55
 * @uses admin_url
56
 * @return string module settings URL
57
 */
58
function jetpack_widgets_configuration_url() {
59
	return admin_url( 'customize.php?autofocus[panel]=widgets' );
60
}
61
62
jetpack_load_widgets();
63
64
/**
65
 * Enqueue utilities to work with widgets in Customizer.
66
 *
67
 * @since 4.4.0
68
 */
69
function jetpack_widgets_customizer_assets_preview() {
70
	wp_enqueue_script( 'jetpack-customizer-widget-utils', plugins_url( '/widgets/customizer-utils.js', __FILE__ ), array( 'customize-base' ) );
71
}
72
add_action( 'customize_preview_init', 'jetpack_widgets_customizer_assets_preview' );
73
74
/**
75
 * Enqueue styles to stylize widgets in Customizer.
76
 *
77
 * @since 4.4.0
78
 */
79
function jetpack_widgets_customizer_assets_controls() {
80
	wp_enqueue_style( 'jetpack-customizer-widget-controls', plugins_url( '/widgets/customizer-controls.css', __FILE__ ), array( 'customize-widgets' ) );
81
}
82
add_action( 'customize_controls_enqueue_scripts', 'jetpack_widgets_customizer_assets_controls' );
83
84
function jetpack_widgets_remove_old_widgets() {
85
	$old_widgets = array(
86
		'googleplus-badge',
87
	);
88
89
	foreach ( $old_widgets as $old_widget ) {
90
		jetpack_widgets_remove_widget( $old_widget );
91
		jetpack_widgets_purge_widget_option( $old_widget );
92
	}
93
}
94
95
function jetpack_widgets_remove_widget( $widget_base ) {
96
	$post_global = $_POST;
97
98
	$i = 0;
99
	while ( $sidebar_id = is_active_widget( false, false, $widget_base, false ) ) {
100
		$i++;
101
		if ( 100 < $i ) {
102
			break;
103
		}
104
105
		$sidebars_widgets = wp_get_sidebars_widgets();
106
		$sidebar = $sidebars_widgets[$sidebar_id];
107
108
		foreach ( $sidebar as $i => $widget_id ) {
109
			if ( _get_widget_id_base( $widget_id ) !== $widget_base ) {
110
				continue;
111
			}
112
113
			unset( $sidebar[$i] );
114
115
			// Is this important?
116
			$_POST = array( 'sidebar' => $sidebar_id, 'widget-' . $widget_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1' );
117
118
			do_action( 'delete_widget', $widget_id, $sidebar_id, $widget_base );
119
		}
120
121
		$sidebars_widgets[$sidebar_id] = array_values( $sidebar );
122
123
		wp_set_sidebars_widgets( $sidebars_widgets );
124
	}
125
126
	$_POST = $post_global;
127
}
128
129
function jetpack_widgets_purge_widget_option( $widget_base ) {
130
	delete_option( "widget_{$widget_base}" );
131
}
132
133
add_action( 'updating_jetpack_version', 'jetpack_widgets_remove_old_widgets' );
134