| 1 |
<?php |
| 2 |
/** |
| 3 |
* Start: Include for phase 2 |
| 4 |
* Widget Updater REST API: WP_REST_Widget_Forms class |
| 5 |
* |
| 6 |
* @package gutenberg |
| 7 |
* @since 5.2.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Controller which provides REST endpoint for updating a widget. |
| 12 |
* |
| 13 |
* @since 5.2.0 |
| 14 |
* |
| 15 |
* @see WP_REST_Controller |
| 16 |
*/ |
| 17 |
class WP_REST_Widget_Forms extends WP_REST_Controller { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructs the controller. |
| 21 |
* |
| 22 |
* @access public |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
$this->namespace = '__experimental'; |
| 26 |
$this->rest_base = 'widget-forms'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Registers the necessary REST API route. |
| 31 |
* |
| 32 |
* @access public |
| 33 |
*/ |
| 34 |
public function register_routes() { |
| 35 |
register_rest_route( |
| 36 |
$this->namespace, |
| 37 |
// Regex representing a PHP class extracted from http://php.net/manual/en/language.oop5.basic.php. |
| 38 |
'/' . $this->rest_base . '/(?P<widget_class>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/', |
| 39 |
array( |
| 40 |
'args' => array( |
| 41 |
'widget_class' => array( |
| 42 |
'description' => __( 'Class name of the widget.', 'gutenberg' ), |
| 43 |
'type' => 'string', |
| 44 |
'required' => true, |
| 45 |
'validate_callback' => array( $this, 'is_valid_widget' ), |
| 46 |
), |
| 47 |
'instance' => array( |
| 48 |
'description' => __( 'Current widget instance', 'gutenberg' ), |
| 49 |
'type' => 'object', |
| 50 |
'default' => array(), |
| 51 |
), |
| 52 |
'instance_changes' => array( |
| 53 |
'description' => __( 'Array of instance changes', 'gutenberg' ), |
| 54 |
'type' => 'object', |
| 55 |
'default' => array(), |
| 56 |
), |
| 57 |
), |
| 58 |
array( |
| 59 |
'methods' => WP_REST_Server::EDITABLE, |
| 60 |
'permission_callback' => array( $this, 'compute_new_widget_permissions_check' ), |
| 61 |
'callback' => array( $this, 'compute_new_widget' ), |
| 62 |
), |
| 63 |
) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Checks if the user has permissions to make the request. |
| 69 |
* |
| 70 |
* @since 5.2.0 |
| 71 |
* @access public |
| 72 |
* |
| 73 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 74 |
*/ |
| 75 |
public function compute_new_widget_permissions_check() { |
| 76 |
// Verify if the current user has edit_theme_options capability. |
| 77 |
// This capability is required to access the widgets screen. |
| 78 |
if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 79 |
return new WP_Error( |
| 80 |
'widgets_cannot_access', |
| 81 |
__( 'Sorry, you are not allowed to access widgets on this site.', 'gutenberg' ), |
| 82 |
array( |
| 83 |
'status' => rest_authorization_required_code(), |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Checks if the widget being referenced is valid. |
| 92 |
* |
| 93 |
* @since 5.2.0 |
| 94 |
* @param string $widget_class Name of the class the widget references. |
| 95 |
* |
| 96 |
* @return boolean| True if the widget being referenced exists and false otherwise. |
| 97 |
*/ |
| 98 |
private function is_valid_widget( $widget_class ) { |
| 99 |
global $wp_widget_factory; |
| 100 |
if ( ! $widget_class ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
return isset( $wp_widget_factory->widgets[ $widget_class ] ) && |
| 104 |
( $wp_widget_factory->widgets[ $widget_class ] instanceof WP_Widget ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns the new widget instance and the form that represents it. |
| 109 |
* |
| 110 |
* @since 5.7.0 |
| 111 |
* @access public |
| 112 |
* |
| 113 |
* @param WP_REST_Request $request Full details about the request. |
| 114 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 115 |
*/ |
| 116 |
public function compute_new_widget( $request ) { |
| 117 |
$widget_class = $request->get_param( 'widget_class' ); |
| 118 |
$instance = $request->get_param( 'instance' ); |
| 119 |
$instance_changes = $request->get_param( 'instance_changes' ); |
| 120 |
|
| 121 |
global $wp_widget_factory; |
| 122 |
$widget_obj = $wp_widget_factory->widgets[ $widget_class ]; |
| 123 |
|
| 124 |
$widget_obj->_set( -1 ); |
| 125 |
ob_start(); |
| 126 |
|
| 127 |
if ( ! empty( $instance_changes ) ) { |
| 128 |
$old_instance = $instance; |
| 129 |
$instance = $widget_obj->update( $instance_changes, $old_instance ); |
| 130 |
|
| 131 |
/** |
| 132 |
* Filters a widget's settings before saving. |
| 133 |
* |
| 134 |
* Returning false will effectively short-circuit the widget's ability |
| 135 |
* to update settings. The old setting will be returned. |
| 136 |
* |
| 137 |
* @since 5.2.0 |
| 138 |
* |
| 139 |
* @param array $instance The current widget instance's settings. |
| 140 |
* @param array $instance_changes Array of new widget settings. |
| 141 |
* @param array $old_instance Array of old widget settings. |
| 142 |
* @param WP_Widget $widget_ob The widget instance. |
| 143 |
*/ |
| 144 |
$instance = apply_filters( 'widget_update_callback', $instance, $instance_changes, $old_instance, $widget_obj ); |
| 145 |
if ( false === $instance ) { |
| 146 |
$instance = $old_instance; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$instance = apply_filters( 'widget_form_callback', $instance, $widget_obj ); |
| 151 |
|
| 152 |
$return = null; |
| 153 |
if ( false !== $instance ) { |
| 154 |
$return = $widget_obj->form( $instance ); |
| 155 |
|
| 156 |
/** |
| 157 |
* Fires at the end of the widget control form. |
| 158 |
* |
| 159 |
* Use this hook to add extra fields to the widget form. The hook |
| 160 |
* is only fired if the value passed to the 'widget_form_callback' |
| 161 |
* hook is not false. |
| 162 |
* |
| 163 |
* Note: If the widget has no form, the text echoed from the default |
| 164 |
* form method can be hidden using CSS. |
| 165 |
* |
| 166 |
* @since 5.2.0 |
| 167 |
* |
| 168 |
* @param WP_Widget $widget_obj The widget instance (passed by reference). |
| 169 |
* @param null $return Return null if new fields are added. |
| 170 |
* @param array $instance An array of the widget's settings. |
| 171 |
*/ |
| 172 |
do_action_ref_array( 'in_widget_form', array( &$widget_obj, &$return, $instance ) ); |
| 173 |
} |
| 174 |
$form = ob_get_clean(); |
| 175 |
|
| 176 |
return rest_ensure_response( |
| 177 |
array( |
| 178 |
'instance' => $instance, |
| 179 |
'form' => $form, |
| 180 |
) |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
/** |
| 185 |
* End: Include for phase 2 |
| 186 |
*/ |
| 187 |
|