@@ -705,6 +705,28 @@ public function filter_image_block_pre_render( $block, $source_block ) {
705705 return $ block ;
706706 }
707707
708+
709+ /**
710+ * Add filters for Rest API handling.
711+ */
712+ public function init_rest_filters () {
713+ // Gutenberg compatibility.
714+ add_filter ( 'rest_prepare_attachment ' , array ( $ this , 'filter_attachment_for_rest ' ) );
715+ $ types = get_post_types_by_support ( 'editor ' );
716+ $ filter = $ this ;
717+ array_map (
718+ function ( $ type ) use ( $ filter ) {
719+ $ post_type = get_post_type_object ( $ type );
720+ // Check if this is a rest supported type.
721+ if ( true === $ post_type ->show_in_rest ) {
722+ // Add filter only to rest supported types.
723+ add_filter ( 'rest_prepare_ ' . $ type , array ( $ filter , 'pre_filter_rest_content ' ), 10 , 3 );
724+ }
725+ },
726+ $ types
727+ );
728+ }
729+
708730 /**
709731 * Setup hooks for the filters.
710732 */
@@ -723,21 +745,8 @@ public function setup_hooks() {
723745 // Filter video codes.
724746 add_filter ( 'media_send_to_editor ' , array ( $ this , 'filter_video_embeds ' ), 10 , 3 );
725747
726- // Gutenberg compatibility.
727- add_filter ( 'rest_prepare_attachment ' , array ( $ this , 'filter_attachment_for_rest ' ) );
728- $ types = get_post_types_by_support ( 'editor ' );
729- $ filter = $ this ;
730- array_map (
731- function ( $ type ) use ( $ filter ) {
732- $ post_type = get_post_type_object ( $ type );
733- // Check if this is a rest supported type.
734- if ( true === $ post_type ->show_in_rest ) {
735- // Add filter only to rest supported types.
736- add_filter ( 'rest_prepare_ ' . $ type , array ( $ filter , 'pre_filter_rest_content ' ), 10 , 3 );
737- }
738- },
739- $ types
740- );
748+ // Enable Rest filters.
749+ add_action ( 'rest_api_init ' , array ( $ this , 'init_rest_filters ' ) );
741750
742751 // Remove editors to prevent users from manually editing images in WP.
743752 add_filter ( 'wp_image_editors ' , array ( $ this , 'disable_editors_maybe ' ) );
0 commit comments