Cannot destroy active lambda function, nested output buffer wordpress scss compiler

60 Views Asked by At

I am using wordpress and using the the action 'template_redirect' to start an output buffer of the html content before sending to the browser.

i have imported a library the converts SCSS to css code in php https://scssphp.github.io/scssphp

here is an example of the code

$compiler = new ScssPhp\ScssPhp\Compiler();

add_action( 'template_redirect', 'replace_scss', -100 );
function replace_scss(){
    ob_start( function( $buffer ) {
        $scss = 'body { p { color: red; } }';
            $css = $compiler->compile($scss);
            if(!empty($css) && is_string($css)) return preg_replace('scss_compiled', $css, $buffer );
            else return $buffer;
    }); 
}

edit:// the following function above when working correctly output

body p { color: red; }

however i am getting the following error.

PHP Fatal error:  Cannot destroy active lambda function in scss_compiler\src\Formatter.php on line 287

i assume its because in formatter.php it mentions ob_start; and you cant start ob_start when its already in a callback.

im wondering is there any other way to get the content of the html, make the changes then return to the browser?

1

There are 1 best solutions below

0
Kling93 On

i solved my own answer.

//from the formatter.php in scss php
public function format(OutputBlock $block, SourceMapGenerator $sourceMapGenerator = null)
  {
      $this->sourceMapGenerator = null;

      if ($sourceMapGenerator) {
          $this->currentLine        = 1;
          $this->currentColumn      = 0;
          $this->sourceMapGenerator = $sourceMapGenerator;
      }

      $this->testEmptyChildren($block);

      //ob_start();

      $this->block($block);

      $out = ob_get_contents();

      return $out;
  }

I commented out ob_start(); and replaced $out = ob_get_clean(); with $out = ob_get_contents();

as for the code

add_action( 'template_redirect', 'scss_compiler', -100 );
function scss_compiler(){
    ob_start('scss');
    ob_end_flush();
}

function scss($buffer){
    $scss = 'body { p { color: red; } }';
    $compiler = new ScssPhp\ScssPhp\Compiler();
    $css = $compiler->compileString($scss)->getCss();
    error_log($css);
    return $buffer;
}