Modify template contents before return during ajax refresh

<?php
/** different hooks needed depending on whether this is a facetwp template or not
 ** in both cases note that the template rather than full $output is modified
 ** potentially for use with image optimization plugins (optimole) that need to modify the
 ** final html in PHP
 **/

/** modifies template for a non-facetwp template **/
add_action( 'facetwp_inject_template', function( $output ) {
    $output['template'] = str_replace( '.jpg', '.svg', $output['template'] );
    FWP()->request->output = $output;
    return; 
}, 10, 2 );

/** modifies template for a facetwp template,
 ** $output needs to be json decoded before modification
 ** and re encoded before returning
 **/
add_filter( 'facetwp_ajax_response', function( $output ) {
    $output = json_decode( $output );
    $output->template = str_replace( '.jpg', '.svg', $output->template );

    // Ignore invalid UTF-8 characters in PHP 7.2+
    if ( version_compare( phpversion(), '7.2', '<' ) ) {
        $output = json_encode( $output );
    }
    else {
        $output = json_encode( $output, JSON_INVALID_UTF8_IGNORE );
    }

    return $output;
}, 10 );