| 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 |
$settings = array(); |
| 60 |
|
| 61 |
$widget_types_to_hide_from_legacy_widget_block = apply_filters( |
| 62 |
'widget_types_to_hide_from_legacy_widget_block', |
| 63 |
array( |
| 64 |
'pages', |
| 65 |
'calendar', |
| 66 |
'archives', |
| 67 |
'media_audio', |
| 68 |
'media_image', |
| 69 |
'media_gallery', |
| 70 |
'media_video', |
| 71 |
'search', |
| 72 |
'text', |
| 73 |
'categories', |
| 74 |
'recent-posts', |
| 75 |
'recent-comments', |
| 76 |
'rss', |
| 77 |
'tag_cloud', |
| 78 |
'custom_html', |
| 79 |
'block', |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
$settings['widgetTypesToHideFromLegacyWidgetBlock'] = $widget_types_to_hide_from_legacy_widget_block; |
| 84 |
|
| 85 |
return $settings; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Overrides dynamic_sidebar_params to make sure Blocks are not wrapped in <form> tag. |
| 90 |
* |
| 91 |
* @param array $arg Dynamic sidebar params. |
| 92 |
* @return array Updated dynamic sidebar params. |
| 93 |
*/ |
| 94 |
function gutenberg_override_sidebar_params_for_block_widget( $arg ) { |
| 95 |
if ( 'Block' === $arg[0]['widget_name'] ) { |
| 96 |
$arg[0]['before_form'] = ''; |
| 97 |
$arg[0]['before_widget_content'] = '<div class="widget-content">'; |
| 98 |
$arg[0]['after_widget_content'] = '</div><form class="block-widget-form">'; |
| 99 |
$arg[0]['after_form'] = '</form>'; |
| 100 |
} |
| 101 |
|
| 102 |
return $arg; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Registers the WP_Widget_Block widget. |
| 107 |
*/ |
| 108 |
function gutenberg_register_block_widget() { |
| 109 |
global $pagenow; |
| 110 |
|
| 111 |
register_widget( 'WP_Widget_Block' ); |
| 112 |
|
| 113 |
// By default every widget on widgets.php is wrapped with a <form>. This |
| 114 |
// means that you can sometimes end up with invalid HTML, e.g. when one of |
| 115 |
// the widgets is a Search block. To fix the problem, let's add a filter |
| 116 |
// that moves the form below the actual widget content. |
| 117 |
if ( 'widgets.php' === $pagenow ) { |
| 118 |
add_filter( |
| 119 |
'dynamic_sidebar_params', |
| 120 |
'gutenberg_override_sidebar_params_for_block_widget' |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
add_action( 'widgets_init', 'gutenberg_register_block_widget' ); |
| 126 |
|
| 127 |
/** |
| 128 |
* Sets show_instance_in_rest to true on all of the core WP_Widget subclasses. |
| 129 |
* When merged to Core, this property should be added to WP_Widget and set to |
| 130 |
* true on each WP_Widget subclass. |
| 131 |
*/ |
| 132 |
function gutenberg_set_show_instance_in_rest_on_core_widgets() { |
| 133 |
global $wp_widget_factory; |
| 134 |
|
| 135 |
$core_widgets = array( |
| 136 |
'WP_Widget_Pages', |
| 137 |
'WP_Widget_Calendar', |
| 138 |
'WP_Widget_Archives', |
| 139 |
'WP_Widget_Media_Audio', |
| 140 |
'WP_Widget_Media_Image', |
| 141 |
'WP_Widget_Media_Gallery', |
| 142 |
'WP_Widget_Media_Video', |
| 143 |
'WP_Widget_Meta', |
| 144 |
'WP_Widget_Search', |
| 145 |
'WP_Widget_Text', |
| 146 |
'WP_Widget_Categories', |
| 147 |
'WP_Widget_Recent_Posts', |
| 148 |
'WP_Widget_Recent_Comments', |
| 149 |
'WP_Widget_RSS', |
| 150 |
'WP_Widget_Tag_Cloud', |
| 151 |
'WP_Nav_Menu_Widget', |
| 152 |
'WP_Widget_Custom_HTML', |
| 153 |
); |
| 154 |
|
| 155 |
foreach ( $core_widgets as $widget ) { |
| 156 |
if ( isset( $wp_widget_factory->widgets[ $widget ] ) ) { |
| 157 |
$wp_widget_factory->widgets[ $widget ]->show_instance_in_rest = true; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! function_exists( 'wp_use_widgets_block_editor' ) ) { |
| 163 |
add_action( 'widgets_init', 'gutenberg_set_show_instance_in_rest_on_core_widgets' ); |
| 164 |
} |
| 165 |
|