settings->get(); foreach ( $settings->post_types() as $type ) { register_rest_field( $type, self::FIELD, array( 'get_callback' => array( $this, 'get_value' ), 'update_callback' => array( $this, 'update_value' ), 'schema' => array( 'type' => 'string', 'description' => __( 'Language code', 'blocklingo' ), 'enum' => array_keys( $settings->languages() ), 'context' => array( 'view', 'edit' ), ), ) ); } } /** * @param array $post Prepared post data. */ public function get_value( array $post ): string { return $this->posts->language_of( (int) $post['id'] ); } public function update_value( mixed $value, mixed $post ): bool|WP_Error { $post_id = $post instanceof \WP_Post ? $post->ID : 0; $lang = (string) $value; if ( $this->posts->language_of( $post_id ) === $lang && $this->translations->find( $post_id ) ) { return true; } if ( ! $this->settings->get()->is_language( $lang ) ) { return new WP_Error( 'blocklingo_bad_lang', __( 'Unknown language.', 'blocklingo' ), array( 'status' => 400 ) ); } $result = $this->translations->assign( $post_id, $lang ); if ( $result instanceof WP_Error ) { $result->add_data( array( 'status' => 400 ) ); } return $result; } }