← Back
Editing: class-rest-response.php
<?php namespace Integromat; defined( 'ABSPATH' ) || die( 'No direct access allowed' ); class Rest_Response { /** * Includes users Custom fields into the REST API response payload */ public static function include_custom_fields() { // rest_api_init runs before the API user exists, so only the token is checked here. if ( ! self::has_valid_api_key() ) { return; } $content_types = array( 'post', 'comment', 'user', 'term' ); $args = array( 'show_in_rest' => true, ); $post_types = get_post_types( $args ); foreach ( $content_types as $object_type ) { $options = get_option( 'integromat_api_options_' . $object_type ); if ( empty( $options ) ) { continue; } foreach ( $options as $option => $val ) { $meta_key = str_replace( IWC_FIELD_PREFIX, '', $option ); if ( Meta_Object::is_blocked_meta_key( $meta_key, $object_type ) ) { continue; } $types = ( 'post' === $object_type ) ? $post_types : array( $object_type ); foreach ( $types as $type ) { register_rest_field( $type, $meta_key, array( 'get_callback' => function ( $object, $field_name, $request ) use ( $object_type ) { return self::get_custom_field_value( $object, $field_name, $object_type, $request ); }, ) ); } } } } /** * Permission result per route and method. The token does not change mid-request; the route can, via embed or batch. * * @var array */ private static $may_read_custom_meta = array(); /** * Read one opted-in meta value for a Make API request. * * A null return is written onto the REST object, so a refused read keeps a core field that shares the name. * * @param array $object * @param string $field_name * @param string $object_type post, user, comment, or term. * @param object $request REST request. Route and method come from here. * @return mixed|null */ private static function get_custom_field_value( $object, $field_name, $object_type, $request ) { $keep = ( is_array( $object ) && array_key_exists( $field_name, $object ) ) ? $object[ $field_name ] : null; if ( Meta_Object::is_blocked_meta_key( $field_name, $object_type ) ) { return $keep; } if ( ! self::request_may_read_custom_meta( $request ) ) { return $keep; } if ( empty( $object['id'] ) ) { return $keep; } if ( 'post' === $object_type ) { $post_meta_keys = get_post_meta( $object['id'] ); // see if the field to register belongs to the post type. if ( ! isset( $post_meta_keys[ $field_name ] ) ) { return $keep; } } return get_metadata( $object_type, $object['id'], $field_name, true ); } /** * Token check only. Safe to call from rest_api_init, before the API user is set. * * @return bool */ private static function has_valid_api_key() { if ( ! isset( $_SERVER['HTTP_IWC_API_KEY'] ) || empty( $_SERVER['HTTP_IWC_API_KEY'] ) ) { return false; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Token is hashed before comparison in is_valid() $token = $_SERVER['HTTP_IWC_API_KEY']; return is_string( $token ) && strlen( $token ) === Api_Token::API_TOKEN_LENGTH && Api_Token::is_valid( $token ); } /** * Custom meta is readable only when this request carries a valid Make API key * and the API permission check for the current route passes. * * @param object $request REST request. * @return bool */ private static function request_may_read_custom_meta( $request ) { $endpoint = ''; $method = 'GET'; if ( is_object( $request ) && method_exists( $request, 'get_route' ) ) { $endpoint = (string) $request->get_route(); if ( method_exists( $request, 'get_method' ) ) { $method = (string) $request->get_method(); } } $cache_key = $method . ' ' . $endpoint; if ( array_key_exists( $cache_key, self::$may_read_custom_meta ) ) { return self::$may_read_custom_meta[ $cache_key ]; } if ( ! self::has_valid_api_key() ) { self::$may_read_custom_meta[ $cache_key ] = false; return false; } self::$may_read_custom_meta[ $cache_key ] = (bool) Api_Permissions::check_permission( $endpoint, $method, false ); return self::$may_read_custom_meta[ $cache_key ]; } /** * @param int $status_code * @param string $error_message * @param string $error_code * @param array $headers Optional headers to include */ public static function render_error( $status_code, $error_message, $error_code, $headers = array(), $details = array() ) { http_response_code( $status_code ); header( 'Content-type: application/json' ); $error = array( "code" => $error_code, "message" => $error_message, "data" => array_merge( array ( "status" => $status_code ), $details ), ); foreach ( $headers as $name => $value ) { $clean_name = preg_replace( '/[^\w-]/', '', $name ); $clean_value = preg_replace( '/[\r\n]/', '', $value ); if ( ! empty( $clean_name ) && ! empty( $clean_value ) ) { header( "$clean_name: $clean_value" ); } } echo wp_json_encode( $error ); exit; } }
Save File
Cancel