-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd6316
commit 790b2d5
Showing
14 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Widget Updater REST API: WP_REST_Widget_Updater_Controller class | ||
* | ||
* @package gutenberg | ||
* @since 4.9.0 | ||
*/ | ||
|
||
/** | ||
* Controller which provides REST endpoint for updating a widget. | ||
* | ||
* @since 2.8.0 | ||
* | ||
* @see WP_REST_Controller | ||
*/ | ||
class WP_REST_Widget_Updater_Controller extends WP_REST_Controller { | ||
|
||
/** | ||
* Constructs the controller. | ||
* | ||
* @access public | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'widget-updater'; | ||
} | ||
|
||
/** | ||
* Registers the necessary REST API route. | ||
* | ||
* @access public | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base . '/', | ||
array( | ||
'args' => array( | ||
'name' => array( | ||
'description' => __( 'Unique registered name for the block.', 'gutenberg' ), | ||
'type' => 'string', | ||
), | ||
), | ||
array( | ||
'methods' => WP_REST_Server::EDITABLE, | ||
'callback' => array( $this, 'compute_new_widget' ), | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns the new widget instance and the form that represents it. | ||
* | ||
* @since 2.8.0 | ||
* @access public | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | ||
*/ | ||
public function compute_new_widget( $request ) { | ||
$json_request = $request->get_json_params(); | ||
if ( ! isset( $json_request['identifier'] ) ) { | ||
return; | ||
} | ||
$widget = $json_request['identifier']; | ||
global $wp_widget_factory; | ||
|
||
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { | ||
return; | ||
} | ||
|
||
$widget_obj = $wp_widget_factory->widgets[ $widget ]; | ||
if ( ! ( $widget_obj instanceof WP_Widget ) ) { | ||
return; | ||
} | ||
|
||
$instance = isset( $json_request['instance'] ) ? $json_request['instance'] : array(); | ||
|
||
$id_to_use = isset( $json_request['id_to_use'] ) ? $json_request['id_to_use'] : -1; | ||
|
||
$widget_obj->_set( $id_to_use ); | ||
ob_start(); | ||
|
||
if ( isset( $json_request['instance_changes'] ) ) { | ||
$instance = $widget_obj->update( $json_request['instance_changes'], $instance ); | ||
// TODO: apply required filters. | ||
} | ||
|
||
$widget_obj->form( $instance ); | ||
// TODO: apply required filters. | ||
|
||
$id_base = $widget_obj->id_base; | ||
$id = $widget_obj->id; | ||
$form = ob_get_clean(); | ||
|
||
return rest_ensure_response( | ||
array( | ||
'instance' => $instance, | ||
'form' => $form, | ||
'id_base' => $id_base, | ||
'id' => $id, | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/block-library/src/legacy-widget/WidgetEditDomManager.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { includes } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, createRef } from '@wordpress/element'; | ||
|
||
class WidgetEditDomManager extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.containerRef = createRef(); | ||
this.triggerWidgetEvent = this.triggerWidgetEvent.bind( this ); | ||
} | ||
|
||
componentDidMount() { | ||
this.triggerWidgetEvent( 'widget-added' ); | ||
} | ||
|
||
shouldComponentUpdate( nextProps ) { | ||
// We can not leverage react render otherwise we would destroy dom changes applied by the plugins. | ||
// We manually update the required dom node replicating what the widget screen and the customizer do. | ||
if ( nextProps.form !== this.props.form && this.containerRef.current ) { | ||
const widgetContent = this.containerRef.current.querySelector( '.widget-content' ); | ||
widgetContent.innerHTML = nextProps.form; | ||
this.triggerWidgetEvent( 'widget-updated' ); | ||
} | ||
return false; | ||
} | ||
|
||
render() { | ||
const { id, idBase, widgetNumber, form } = this.props; | ||
return ( | ||
<div className="widget open" ref={ this.containerRef }> | ||
<div className="widget-inside"> | ||
<form method="post"> | ||
<div className="widget-content" dangerouslySetInnerHTML={ { __html: form } }> | ||
</div> | ||
<input type="hidden" name="widget-id" className="widget-id" value={ id } /> | ||
<input type="hidden" name="id_base" className="id_base" value={ idBase } /> | ||
<input type="hidden" name="widget_number" className="widget_number" value={ widgetNumber } /> | ||
<input type="hidden" name="multi_number" className="multi_number" value="" /> | ||
<input type="hidden" name="add_new" className="add_new" value="" /> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
triggerWidgetEvent( event ) { | ||
window.$( window.document ).trigger( | ||
event, | ||
[ window.$( this.containerRef.current ) ] | ||
); | ||
} | ||
|
||
retrieveUpdatedInstance() { | ||
if ( this.containerRef.current ) { | ||
const { idBase, widgetNumber } = this.props; | ||
const form = this.containerRef.current.querySelector( 'form' ); | ||
const formData = new window.FormData( form ); | ||
const updatedInstance = {}; | ||
const keyPrefixLength = `widget-${ idBase }[${ widgetNumber }][`.length; | ||
const keySuffixLength = `]`.length; | ||
for ( const [ rawKey, value ] of formData ) { | ||
const keyParsed = rawKey.substring( keyPrefixLength, rawKey.length - keySuffixLength ); | ||
// This fields are added to the form because the widget JavaScript code may use this values. | ||
// They are not relevant for the update mechanism. | ||
if ( includes( | ||
[ 'widget-id', 'id_base', 'widget_number', 'multi_number', 'add_new' ], | ||
keyParsed, | ||
) ) { | ||
continue; | ||
} | ||
updatedInstance[ keyParsed ] = value; | ||
} | ||
return updatedInstance; | ||
} | ||
} | ||
} | ||
|
||
export default WidgetEditDomManager; | ||
|
Oops, something went wrong.