Skip to content

Commit

Permalink
Bootstrap wp-admin before rendering widgets – they may rely on wp-adm…
Browse files Browse the repository at this point in the history
…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 <hello@hellofromtonya.com>

Co-authored-by: Tonya Mork <hello@hellofromtonya.com>
  • Loading branch information
adamziel and Tonya Mork authored Jul 15, 2021
1 parent 984d8c8 commit 3952888
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
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 @@ -421,6 +421,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

0 comments on commit 3952888

Please sign in to comment.