本文整理汇总了PHP中WP_REST_Request::get_json_params方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::get_json_params方法的具体用法?PHP WP_REST_Request::get_json_params怎么用?PHP WP_REST_Request::get_json_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::get_json_params方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: update_items
/**
* Update all Shipping Zone Locations.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function update_items($request)
{
$zone = $this->get_zone($request['id']);
if (is_wp_error($zone)) {
return $zone;
}
$raw_locations = $request->get_json_params();
$locations = array();
foreach ((array) $raw_locations as $raw_location) {
if (empty($raw_location['code']) || empty($raw_location['type'])) {
continue;
}
$locations[] = $raw_location;
}
$zone->set_locations($locations);
$zone->save();
return $this->get_items($request);
}
示例2: jumpstart_toggle
/**
* Toggles activation or deactivation of the JumpStart
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
* }
*
* @return bool|WP_Error True if toggling Jumpstart succeeded. Otherwise, a WP_Error instance with the corresponding error.
*/
public static function jumpstart_toggle($data)
{
$param = $data->get_json_params();
if ($param['active']) {
return self::jumpstart_activate($data);
} else {
return self::jumpstart_deactivate($data);
}
}
示例3: add_post_meta_fields
/**
* Add post meta fields.
*
* @param WP_Post $post
* @param WP_REST_Request $request
* @return bool|WP_Error
*/
protected function add_post_meta_fields($post, $request)
{
$data = $request->get_json_params();
$defaults = array('discount_type' => 'fixed_cart', 'amount' => 0, 'individual_use' => false, 'product_ids' => array(), 'exclude_product_ids' => array(), 'usage_limit' => '', 'usage_limit_per_user' => '', 'limit_usage_to_x_items' => '', 'usage_count' => '', 'expiry_date' => '', 'free_shipping' => false, 'product_categories' => array(), 'excluded_product_categories' => array(), 'exclude_sale_items' => false, 'minimum_amount' => '', 'maximum_amount' => '', 'email_restrictions' => array(), 'description' => '');
$data = wp_parse_args($data, $defaults);
// Set coupon meta.
update_post_meta($post->ID, 'discount_type', $data['discount_type']);
update_post_meta($post->ID, 'coupon_amount', wc_format_decimal($data['amount']));
update_post_meta($post->ID, 'individual_use', true === $data['individual_use'] ? 'yes' : 'no');
update_post_meta($post->ID, 'product_ids', implode(',', array_filter(array_map('intval', $data['product_ids']))));
update_post_meta($post->ID, 'exclude_product_ids', implode(',', array_filter(array_map('intval', $data['exclude_product_ids']))));
update_post_meta($post->ID, 'usage_limit', absint($data['usage_limit']));
update_post_meta($post->ID, 'usage_limit_per_user', absint($data['usage_limit_per_user']));
update_post_meta($post->ID, 'limit_usage_to_x_items', absint($data['limit_usage_to_x_items']));
update_post_meta($post->ID, 'usage_count', absint($data['usage_count']));
update_post_meta($post->ID, 'expiry_date', $this->get_coupon_expiry_date(wc_clean($data['expiry_date'])));
update_post_meta($post->ID, 'free_shipping', true === $data['free_shipping'] ? 'yes' : 'no');
update_post_meta($post->ID, 'product_categories', array_filter(array_map('intval', $data['product_categories'])));
update_post_meta($post->ID, 'exclude_product_categories', array_filter(array_map('intval', $data['excluded_product_categories'])));
update_post_meta($post->ID, 'exclude_sale_items', true === $data['exclude_sale_items'] ? 'yes' : 'no');
update_post_meta($post->ID, 'minimum_amount', wc_format_decimal($data['minimum_amount']));
update_post_meta($post->ID, 'maximum_amount', wc_format_decimal($data['maximum_amount']));
update_post_meta($post->ID, 'customer_email', array_filter(array_map('sanitize_email', $data['email_restrictions'])));
return true;
}
示例4: update_data
/**
* If it's a valid Jetpack module and configuration parameters have been sent, update it.
*
* @since 4.3.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
*
* @type string $slug Module slug.
* }
*
* @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error.
*/
public function update_data($data)
{
// If it's null, we're trying to update many module options from different modules.
if (is_null($data['slug'])) {
// Value admitted by Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list that will make it return all module options.
// It will not be passed. It's just checked in this method to pass that method a string or array.
$data['slug'] = 'any';
} else {
if (!Jetpack::is_module($data['slug'])) {
return new WP_Error('not_found', esc_html__('The requested Jetpack module was not found.', 'jetpack'), array('status' => 404));
}
if (!Jetpack::is_module_active($data['slug'])) {
return new WP_Error('inactive', esc_html__('The requested Jetpack module is inactive.', 'jetpack'), array('status' => 409));
}
}
// Get parameters to update the module.
$params = $data->get_json_params();
// Exit if no parameters were passed.
if (!is_array($params)) {
return new WP_Error('missing_options', esc_html__('Missing options.', 'jetpack'), array('status' => 404));
}
// Get available module options.
$options = Jetpack_Core_Json_Api_Endpoints::get_updateable_data_list('any' === $data['slug'] ? $params : $data['slug']);
// Prepare to toggle module if needed
$toggle_module = new Jetpack_Core_API_Module_Toggle_Endpoint(new Jetpack_IXR_Client());
// Options that are invalid or failed to update.
$invalid = array_keys(array_diff_key($params, $options));
$not_updated = array();
// Remove invalid options
$params = array_intersect_key($params, $options);
// Used if response is successful. The message can be overwritten and additional data can be added here.
$response = array('code' => 'success', 'message' => esc_html__('The requested Jetpack data updates were successful.', 'jetpack'));
// If there are modules to activate, activate them first so they're ready when their options are set.
foreach ($params as $option => $value) {
if ('modules' === $options[$option]['jp_group']) {
// Used if there was an error. Can be overwritten with specific error messages.
$error = '';
// Set to true if the module toggling was successful.
$updated = false;
// Check if user can toggle the module.
if ($toggle_module->can_request()) {
// Activate or deactivate the module according to the value passed.
$toggle_result = $value ? $toggle_module->activate_module($option) : $toggle_module->deactivate_module($option);
if (is_wp_error($toggle_result)) {
$error = $toggle_result->get_error_message();
} else {
$updated = true;
}
} else {
$error = Jetpack_Core_Json_Api_Endpoints::$user_permissions_error_msg;
}
// The module was not toggled.
if (!$updated) {
$not_updated[$option] = $error;
}
// Remove module from list so we don't go through it again.
unset($params[$option]);
}
}
foreach ($params as $option => $value) {
// Used if there was an error. Can be overwritten with specific error messages.
$error = '';
// Set to true if the option update was successful.
$updated = false;
// Get option attributes, including the group it belongs to.
$option_attrs = $options[$option];
// If this is a module option and the related module isn't active for any reason, continue with the next one.
if ('settings' !== $option_attrs['jp_group']) {
if (!Jetpack::is_module($option_attrs['jp_group'])) {
$not_updated[$option] = esc_html__('The requested Jetpack module was not found.', 'jetpack');
continue;
}
if (!Jetpack::is_module_active($option_attrs['jp_group'])) {
$not_updated[$option] = esc_html__('The requested Jetpack module is inactive.', 'jetpack');
continue;
}
}
// Properly cast value based on its type defined in endpoint accepted args.
$value = Jetpack_Core_Json_Api_Endpoints::cast_value($value, $option_attrs);
switch ($option) {
case 'monitor_receive_notifications':
$monitor = new Jetpack_Monitor();
// If we got true as response, consider it done.
$updated = true === $monitor->update_option_receive_jetpack_monitor_notification($value);
break;
case 'post_by_email_address':
if ('create' == $value) {
//.........这里部分代码省略.........
示例5: update_module
/**
* If it's a valid Jetpack module and configuration parameters have been sent, update it.
*
* @since 4.1.0
*
* @param WP_REST_Request $data {
* Array of parameters received by request.
*
* @type string $slug Module slug.
* }
*
* @return bool|WP_Error True if module was updated. Otherwise, a WP_Error instance with the corresponding error.
*/
public static function update_module($data)
{
if (!Jetpack::is_module($data['slug'])) {
return new WP_Error('not_found', esc_html__('The requested Jetpack module was not found.', 'jetpack'), array('status' => 404));
}
if (!Jetpack::is_module_active($data['slug'])) {
return new WP_Error('inactive', esc_html__('The requested Jetpack module is inactive.', 'jetpack'), array('status' => 409));
}
// Get parameters to update the module.
$param = $data->get_json_params();
// Exit if no parameters were passed.
if (!is_array($param)) {
return new WP_Error('missing_option', esc_html__('Missing option.', 'jetpack'), array('status' => 404));
}
// Get option name and value.
$option = key($param);
$value = current($param);
// Get available module options.
$options = self::get_module_available_options();
// If option is invalid, don't go any further.
if (!in_array($option, array_keys($options))) {
return new WP_Error('invalid_param', esc_html(sprintf(__('The option %s is invalid for this module.', 'jetpack'), $option)), array('status' => 404));
}
// Used if response is successful. The message can be overwritten and additional data can be added here.
$response = array('code' => 'success', 'message' => esc_html__('The requested Jetpack module was updated.', 'jetpack'));
// Used if there was an error. Can be overwritten with specific error messages.
/* Translators: the variable is a module option name. */
$error = sprintf(__('The option %s was not updated.', 'jetpack'), $option);
// Set to true if the option update was successful.
$updated = false;
// Properly cast value based on its type defined in endpoint accepted args.
$value = self::cast_value($value, $options[$option]);
switch ($option) {
case 'monitor_receive_notifications':
$monitor = new Jetpack_Monitor();
// If we got true as response, consider it done.
$updated = true === $monitor->update_option_receive_jetpack_monitor_notification($value);
break;
case 'post_by_email_address':
if ('create' == $value) {
$result = self::_process_post_by_email('jetpack.createPostByEmailAddress', esc_html__('Unable to create the Post by Email address. Please try again later.', 'jetpack'));
} elseif ('regenerate' == $value) {
$result = self::_process_post_by_email('jetpack.regeneratePostByEmailAddress', esc_html__('Unable to regenerate the Post by Email address. Please try again later.', 'jetpack'));
} elseif ('delete' == $value) {
$result = self::_process_post_by_email('jetpack.deletePostByEmailAddress', esc_html__('Unable to delete the Post by Email address. Please try again later.', 'jetpack'));
} else {
$result = false;
}
// If we got an email address (create or regenerate) or 1 (delete), consider it done.
if (preg_match('/[a-z0-9]+@post.wordpress.com/', $result)) {
$response[$option] = $result;
$updated = true;
} elseif (1 == $result) {
$updated = true;
} elseif (is_array($result) && isset($result['message'])) {
$error = $result['message'];
}
break;
case 'jetpack_protect_key':
$protect = Jetpack_Protect_Module::instance();
if ('create' == $value) {
$result = $protect->get_protect_key();
} else {
$result = false;
}
// If we got one of Protect keys, consider it done.
if (preg_match('/[a-z0-9]{40,}/i', $result)) {
$response[$option] = $result;
$updated = true;
}
break;
case 'jetpack_protect_global_whitelist':
$updated = jetpack_protect_save_whitelist(explode(PHP_EOL, str_replace(' ', '', $value)));
if (is_wp_error($updated)) {
$error = $updated->get_error_message();
}
break;
case 'show_headline':
case 'show_thumbnails':
$grouped_options = $grouped_options_current = Jetpack_Options::get_option('relatedposts');
$grouped_options[$option] = $value;
// If option value was the same, consider it done.
$updated = $grouped_options_current != $grouped_options ? Jetpack_Options::update_option('relatedposts', $grouped_options) : true;
break;
case 'google':
case 'bing':
case 'pinterest':
//.........这里部分代码省略.........
示例6: set_configuration
/**
* Used by endpoint to store changes
*
* @param WP_REST_Request $request Request from the REST API.
*
* @return array List of feedback per option if saving succeeded.
*/
public function set_configuration(WP_REST_Request $request)
{
return $this->storage->store($request->get_json_params());
}