PluginProbe
Gutenberg / 10.9.1
Gutenberg v10.9.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / widgets.php

widgets.php in Gutenberg 10.9.1, at lib/widgets.php

167 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions used in making widgets interopable with block editors.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Checks if a screen containing the block editor is being loaded.
10 *
11 * @return boolean True if a screen containing the block editor is being loaded.
12 */
13 function gutenberg_is_block_editor() {
14 _deprecated_function(
15 'gutenberg_is_block_editor',
16 '10.8',
17 'WP_Screen::is_block_editor'
18 );
19
20 // If get_current_screen does not exist, we are neither in the standard block editor for posts, or the widget block editor.
21 // We can safely return false.
22 if ( ! function_exists( 'get_current_screen' ) ) {
23 return false;
24 }
25 $screen = get_current_screen();
26 return ! empty( $screen ) &&
27 (
28 $screen->is_block_editor() ||
29 'appearance_page_gutenberg-widgets' === $screen->id ||
30 ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $screen->id ) )
31 );
32 }
33
34 /**
35 * Whether or not to use the block editor to manage widgets. Defaults to true
36 * unless a theme has removed support for widgets-block-editor or a plugin has
37 * filtered the return value of this function.
38 *
39 * @return boolean Whether or not to use the block editor to manage widgets.
40 */
41 function gutenberg_use_widgets_block_editor() {
42 /**
43 * Filters whether or not to use the block editor to manage widgets.
44 *
45 * @param boolean $use_widgets_block_editor Whether or not to use the block editor to manage widgets.
46 */
47 return apply_filters(
48 'gutenberg_use_widgets_block_editor',
49 get_theme_support( 'widgets-block-editor' )
50 );
51 }
52
53 /**
54 * Returns the settings required by legacy widgets blocks.
55 *
56 * @return array Legacy widget settings.
57 */
58 function gutenberg_get_legacy_widget_settings() {
59 global $wp_widget_factory;
60
61 $settings = array();
62
63 $widget_types_to_hide_from_legacy_widget_block = apply_filters(
64 'widget_types_to_hide_from_legacy_widget_block',
65 array(
66 'pages',
67 'calendar',
68 'archives',
69 'media_audio',
70 'media_image',
71 'media_gallery',
72 'media_video',
73 'search',
74 'text',
75 'categories',
76 'recent-posts',
77 'recent-comments',
78 'rss',
79 'tag_cloud',
80 'custom_html',
81 'block',
82 )
83 );
84
85 $settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block;
86
87 return $settings;
88 }
89
90 /**
91 * Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag.
92 *
93 * @param array $arg Dynamic sidebar params.
94 * @return array Updated dynamic sidebar params.
95 */
96 function gutenberg_override_sidebar_params_for_block_widget( $arg ) {
97 if ( 'Block' === $arg[0]['widget_name'] ) {
98 $arg[0]['before_form'] = '';
99 $arg[0]['before_widget_content'] = '<div class="widget-content">';
100 $arg[0]['after_widget_content'] = '</div><form class="block-widget-form">';
101 $arg[0]['after_form'] = '</form>';
102 }
103
104 return $arg;
105 }
106
107 /**
108 * Registers the WP_Widget_Block widget.
109 */
110 function gutenberg_register_block_widget() {
111 global $pagenow;
112
113 register_widget( 'WP_Widget_Block' );
114
115 // By default every widget on widgets.php is wrapped with a <form>. This
116 // means that you can sometimes end up with invalid HTML, e.g. when one of
117 // the widgets is a Search block. To fix the problem, let's add a filter
118 // that moves the form below the actual widget content.
119 if ( 'widgets.php' === $pagenow ) {
120 add_filter(
121 'dynamic_sidebar_params',
122 'gutenberg_override_sidebar_params_for_block_widget'
123 );
124 }
125 }
126
127 add_action( 'widgets_init', 'gutenberg_register_block_widget' );
128
129 /**
130 * Sets show_instance_in_rest to true on all of the core WP_Widget subclasses.
131 * When merged to Core, this property should be added to WP_Widget and set to
132 * true on each WP_Widget subclass.
133 */
134 function gutenberg_set_show_instance_in_rest_on_core_widgets() {
135 global $wp_widget_factory;
136
137 $core_widgets = array(
138 'WP_Widget_Pages',
139 'WP_Widget_Calendar',
140 'WP_Widget_Archives',
141 'WP_Widget_Media_Audio',
142 'WP_Widget_Media_Image',
143 'WP_Widget_Media_Gallery',
144 'WP_Widget_Media_Video',
145 'WP_Widget_Meta',
146 'WP_Widget_Search',
147 'WP_Widget_Text',
148 'WP_Widget_Categories',
149 'WP_Widget_Recent_Posts',
150 'WP_Widget_Recent_Comments',
151 'WP_Widget_RSS',
152 'WP_Widget_Tag_Cloud',
153 'WP_Nav_Menu_Widget',
154 'WP_Widget_Custom_HTML',
155 );
156
157 foreach ( $core_widgets as $widget ) {
158 if ( isset( $wp_widget_factory->widgets[ $widget ] ) ) {
159 $wp_widget_factory->widgets[ $widget ]->show_instance_in_rest = true;
160 }
161 }
162 }
163
164 if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) {
165 add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' );
166 }
167