当前位置: 首页>>代码示例>>PHP>>正文


PHP WP_REST_Request::get_attributes方法代码示例

本文整理汇总了PHP中WP_REST_Request::get_attributes方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::get_attributes方法的具体用法?PHP WP_REST_Request::get_attributes怎么用?PHP WP_REST_Request::get_attributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WP_REST_Request的用法示例。


在下文中一共展示了WP_REST_Request::get_attributes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: wc_rest_validate_reports_request_arg

/**
 * Validate reports request arguments.
 *
 * @since 2.6.0
 * @param mixed $value
 * @param WP_REST_Request $request
 * @param string $param
 * @return WP_Error|boolean
 */
function wc_rest_validate_reports_request_arg($value, $request, $param)
{
    $attributes = $request->get_attributes();
    if (!isset($attributes['args'][$param]) || !is_array($attributes['args'][$param])) {
        return true;
    }
    $args = $attributes['args'][$param];
    if ('string' === $args['type'] && !is_string($value)) {
        return new WP_Error('woocommerce_rest_invalid_param', sprintf(__('%1$s is not of type %2$s', 'woocommerce'), $param, 'string'));
    }
    if ('date' === $args['format']) {
        $regex = '#^\\d{4}-\\d{2}-\\d{2}$#';
        if (!preg_match($regex, $value, $matches)) {
            return new WP_Error('woocommerce_rest_invalid_date', __('The date you provided is invalid.', 'woocommerce'));
        }
    }
    return true;
}
开发者ID:tlovett1,项目名称:woocommerce,代码行数:27,代码来源:wc-rest-functions.php

示例2: return

 /**
  * Sanitize a request argument based on details registered to the route.
  *
  * @param  mixed            $value
  * @param  WP_REST_Request  $request
  * @param  string           $param
  * @return mixed
  */
 function rest_sanitize_request_arg($value, $request, $param)
 {
     $attributes = $request->get_attributes();
     if (!isset($attributes['args'][$param]) || !is_array($attributes['args'][$param])) {
         return $value;
     }
     $args = $attributes['args'][$param];
     if ('integer' === $args['type']) {
         return (int) $value;
     }
     if ('boolean' === $args['type']) {
         return rest_sanitize_boolean($value);
     }
     if (isset($args['format'])) {
         switch ($args['format']) {
             case 'date-time':
                 return sanitize_text_field($value);
             case 'email':
                 /*
                  * sanitize_email() validates, which would be unexpected
                  */
                 return sanitize_text_field($value);
             case 'uri':
                 return esc_url_raw($value);
             case 'ipv4':
                 return sanitize_text_field($value);
         }
     }
     return $value;
 }
开发者ID:woocommerce,项目名称:woocommerce,代码行数:38,代码来源:wp-rest-functions.php

示例3: validate_list_item

 /**
  * Validates that the parameter belongs to a list of admitted values.
  *
  * @since 4.3.0
  *
  * @param string $value Value to check.
  * @param WP_REST_Request $request
  * @param string $param Name of the parameter passed to endpoint holding $value.
  *
  * @return bool
  */
 public static function validate_list_item($value = '', $request, $param)
 {
     $attributes = $request->get_attributes();
     if (!isset($attributes['args'][$param]) || !is_array($attributes['args'][$param])) {
         return new WP_Error('invalid_param', sprintf(esc_html__('%s not recognized', 'jetpack'), $param));
     }
     $args = $attributes['args'][$param];
     if (!empty($args['enum'])) {
         // If it's an associative array, use the keys to check that the value is among those admitted.
         $enum = count(array_filter(array_keys($args['enum']), 'is_string')) > 0 ? array_keys($args['enum']) : $args['enum'];
         if (!in_array($value, $enum)) {
             return new WP_Error('invalid_param_value', sprintf(esc_html__('%1$s must be one of %2$s', 'jetpack'), $param, implode(', ', $enum)));
         }
     }
     return true;
 }
开发者ID:iamtakashi,项目名称:jetpack,代码行数:27,代码来源:class.core-rest-api-endpoints.php

示例4: rest_sanitize_request_arg

/**
 * Sanitize a request argument based on details registered to the route.
 *
 * @since 4.7.0
 *
 * @param  mixed            $value
 * @param  WP_REST_Request  $request
 * @param  string           $param
 * @return mixed
 */
function rest_sanitize_request_arg($value, $request, $param)
{
    $attributes = $request->get_attributes();
    if (!isset($attributes['args'][$param]) || !is_array($attributes['args'][$param])) {
        return $value;
    }
    $args = $attributes['args'][$param];
    return rest_sanitize_value_from_schema($value, $args, $param);
}
开发者ID:023yangbo,项目名称:WordPress,代码行数:19,代码来源:rest-api.php

示例5: sanitize_post_statuses

 /**
  * Sanitizes and validates the list of post statuses, including whether the
  * user can query private statuses.
  *
  * @since 4.7.0
  * @access public
  *
  * @param  string|array    $statuses  One or more post statuses.
  * @param  WP_REST_Request $request   Full details about the request.
  * @param  string          $parameter Additional parameter to pass to validation.
  * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
  */
 public function sanitize_post_statuses($statuses, $request, $parameter)
 {
     $statuses = wp_parse_slug_list($statuses);
     // The default status is different in WP_REST_Attachments_Controller
     $attributes = $request->get_attributes();
     $default_status = $attributes['args']['status']['default'];
     foreach ($statuses as $status) {
         if ($status === $default_status) {
             continue;
         }
         $post_type_obj = get_post_type_object($this->post_type);
         if (current_user_can($post_type_obj->cap->edit_posts)) {
             $result = rest_validate_request_arg($status, $request, $parameter);
             if (is_wp_error($result)) {
                 return $result;
             }
         } else {
             return new WP_Error('rest_forbidden_status', __('Status is forbidden.'), array('status' => rest_authorization_required_code()));
         }
     }
     return $statuses;
 }
开发者ID:aaronjorbin,项目名称:wordpress.codecov,代码行数:34,代码来源:class-wp-rest-posts-controller.php


注:本文中的WP_REST_Request::get_attributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。