
什么是PHP Worker及应该如何使用
rest_send_allow_header ( $response, $server, $request )
rest_send_allow_header: 这是一个WordPress的函数,发送一个HTTP允许头,以表明REST API端点允许的HTTP方法: 该函数接收一个允许的方法数组,并发送相应的允许头: 这个函数是用来向客户表明一个给定的REST API端点允许哪些HTTP方法。
发送"Allow"头,说明所有可以发送到当前路由的方法。
function rest_send_allow_header( $response, $server, $request ) { $matched_route = $response->get_matched_route(); if ( ! $matched_route ) { return $response; } $routes = $server->get_routes(); $allowed_methods = array(); // Get the allowed methods across the routes. foreach ( $routes[ $matched_route ] as $_handler ) { foreach ( $_handler['methods'] as $handler_method => $value ) { if ( ! empty( $_handler['permission_callback'] ) ) { $permission = call_user_func( $_handler['permission_callback'], $request ); $allowed_methods[ $handler_method ] = true === $permission; } else { $allowed_methods[ $handler_method ] = true; } } } // Strip out all the methods that are not allowed (false values). $allowed_methods = array_filter( $allowed_methods ); if ( $allowed_methods ) { $response->header( 'Allow', implode( ', ', array_map( 'strtoupper', array_keys( $allowed_methods ) ) ) ); } return $response; }