Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bootstrap wp-admin before rendering widgets – they may rely on wp-adm…
…in functions being loaded. (#33454)

* Bootstrap wp-admin before rendering widgets – they may rely on wp-admin functions being loaded.

* Bootstrap wp-admin function also in /wp/v2/widgets endpoint

* Add a dot at the end of the comment to satisfy linter requirements

* Move the require_once statement to encode_form_data to increase the size of the safety net it provides.

* Update lib/class-wp-rest-widget-types-controller.php

Co-authored-by: Tonya Mork <[email protected]>

Co-authored-by: Tonya Mork <[email protected]>
  • Loading branch information
2 people authored and youknowriad committed Jul 15, 2021
commit 9d7017f6b7c8145284141e3dbbc4154c3428c52f
3 changes: 3 additions & 0 deletions lib/class-wp-rest-widget-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ public function encode_form_data( $request ) {
);
}

// Third-party widgets may rely on wp-admin functions. So let's load them before working with the widget object.
require_once ABSPATH . 'wp-admin/includes/admin.php';

// Set the widget's number so that the id attributes in the HTML that we
// return are predictable.
if ( isset( $request['number'] ) && is_numeric( $request['number'] ) ) {
Expand Down
6 changes: 6 additions & 0 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,16 @@ public function prepare_item_for_response( $item, $request ) {
rest_is_field_included( 'rendered', $fields ) &&
'wp_inactive_widgets' !== $sidebar_id
) {
// Some third-party widgets rely on wp-admin functions.
require_once ABSPATH . 'wp-admin/includes/admin.php';

$prepared['rendered'] = trim( wp_render_widget( $widget_id, $sidebar_id ) );
}

if ( rest_is_field_included( 'rendered_form', $fields ) ) {
// Some third-party widgets rely on wp-admin functions.
require_once ABSPATH . 'wp-admin/includes/admin.php';

$rendered_form = wp_render_widget_control( $widget_id );
if ( ! is_null( $rendered_form ) ) {
$prepared['rendered_form'] = trim( $rendered_form );
Expand Down