# searchpro/1.9.7/inc/class-scriptSeqManipulate.php

BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS &amp; JavaScript, version 1.9.7. 101 lines.

- Page: https://pluginprobe.com/plugins/searchpro/1.9.7/code/inc/class-scriptSeqManipulate.php
- Raw: https://pluginprobe.com/plugins/searchpro/1.9.7/raw/inc/class-scriptSeqManipulate.php
- Modified: 2024-08-31T18:32:00+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/searchpro/1.9.7/code/inc/class-scriptSeqManipulate.php#L10-L20`.

```php
<?php

class scriptSeqManipulate {
    public $buffer = null;

    function setBuffer($buffer) {
        $this->buffer = $buffer;
    } 

    function appendAfter($scriptMatch, $match) {
        $mainScript = $this->getScript($scriptMatch);
        $html = str_get_html($this->buffer);
        $scriptTags = $html->find('script');
        $matchFound = false;

        foreach ($scriptTags as $script) {
            if (strpos($script->outertext, $match) !== false) {
                $script->outertext = $script->outertext . $mainScript;
                $matchFound = true;
                break;
            }
        }


        $this->buffer = $html->save();
        $html->clear();
        unset($html);

        // if ($matchFound) {
        //     $this->removeScript($scriptMatch);
        // }
    }

    function appendBefore($scriptMatch, $match) {
        $mainScript = $this->getScript($scriptMatch);
        $html = str_get_html($this->buffer);
        $scriptTags = $html->find('script');
        $matchFound = false;

        foreach ($scriptTags as $script) {
            if (strpos($script->outertext, $match) !== false) {
                $script->outertext = $mainScript . $script->outertext;
                $matchFound = true;
                break;
            }
        }
        
        
        $this->buffer = $html->save();
        $html->clear();
        unset($html);

        // if ($matchFound) {
        //     $this->removeScript($scriptMatch);
        // }
    }

    function getScript($scriptMatch) {
        $html = str_get_html($this->buffer);
        $scriptTags = $html->find('script');
        $found_tag = null;

        foreach ($scriptTags as $script) {
            if (strpos($script->outertext, $scriptMatch) !== false) {
                $found_tag = $script->outertext;
                $script->outertext = '';
                break;
            }
        }

        $this->buffer = $html->save();
        $html->clear();
        unset($html);

        return $found_tag;
    }

    function removeScript($match) {
        $html = str_get_html($this->buffer);
        $scriptTags = $html->find('script');

        foreach ($scriptTags as $script) {
            if (strpos($script->outertext, $match) !== false) {
                $script->outertext = '';
                break;
            }
        }

        $this->buffer = $html->save();
        $html->clear();
        unset($html);
    }

    function getBuffer() {
        return $this->buffer;
    }

    function clean() {
        $this->buffer = null;
    }
}
```
