<?php
namespace Indeed\Ihc;
/*
in order to view all hooks and filters you can go to : http://{yourdomain}/wp-admin/admin.php?page=ihc_manage&tab=hooks
*/
class SearchFiltersAndHooks
{
    private $files                    = [];
    private $hooks                    = [];
    private $pluginName               = '';
    private $nameShouldContain        = [];

    public function __construct()
    {

    }

    public function setNameShouldContain( $input='' )
    {
        if ( is_array( $input ) ){
            $this->nameShouldContain = $input;
        } else {
            $this->nameShouldContain[] = $input;
        }
        return $this;
    }

    public function setPluginName( $input='' )
    {
        $this->pluginName = $input;
        return $this;
    }

    public function SearchFiles( $path='' )
    {
        if ( file_exists($path) && is_dir($path) ){
            $result = scandir($path);
            $files = array_diff($result, array('.', '..'));
            if ( count( $files ) > 0){
                foreach ( $files as $file ){
                    if ( is_file( "$path/$file" ) ){
                        $this->files[] = "$path/$file";
                    } else if( is_dir( "$path/$file" ) ){
                        $this->SearchFiles( "$path/$file" );
                    }
                }
            }
        }
    }

    public function getFiles()
    {
        return $this->files;
    }

    public function getResults()
    {
        if ( !$this->files ){
            return [];
        }
        foreach ( $this->files as $filePath ){
            $this->searchIntoFile( $filePath );
        }
        return $this->hooks;
    }

    public function searchIntoFile( $file='' )
    {
        $contents = file_get_contents( $file );
        $pluginFileName = $file;
        if ( $this->pluginName ){
            $pluginFileName = explode( $this->pluginName, $file );
            $pluginFileName = isset( $pluginFileName[1] ) ? $pluginFileName[1] : $file;
            $pluginFileName = str_replace( '//', '/', $pluginFileName );
        }

        $this->searchForHooksIntoString( $contents, 'filter', $file, $pluginFileName );
        $this->searchForHooksIntoString( $contents, 'action', $file, $pluginFileName );

        unset( $contents );
    }

    private function searchForHooksIntoString( &$string='', $type='', $file=null, $pluginFileName='' )
    {
        if ( $type == 'filter' ){
            $search = 'apply_filters(';
        } else {
            $search = 'do_action(';
        }

        $pattern = preg_quote( $search, '/' );
        $pattern = "/^.*$pattern.*\$/m";
        if ( preg_match_all( $pattern, $string, $matches ) ){
            foreach ( $matches[0] as $match ){
                $temporary = explode( $search, $match );
                if ( empty( $temporary[1] ) ){
                    continue;
                }
                $temporary[1] = trim( $temporary[1] );
                if ( stripos( $temporary[1], "'" ) === 0 ){
                    $temporaryName = explode( "'", $temporary[1] );
                } else {
                    $temporaryName = explode( '"', $temporary[1] );
                }
                $name = isset( $temporaryName[1] ) ? $temporaryName[1] : '';
                if ( !$this->checkName( $name ) ){
                    continue;
                }
                $description = $this->getDescription( $file, $match );
                $this->hooks[] = [
                          'file'              => $pluginFileName,
                          'name'              => $name,
                          'description'       => $description,
                          'type'              => $type,
                ];
            }
        }
    }

    private function checkName( $name='' )
    {
        if ( !$this->nameShouldContain ){
            return true;
        }
        foreach ( $this->nameShouldContain as $nameSubstring ){
            if ( stripos( $name, $nameSubstring ) !== false ){
                return true;
            }
        }
        return false;
    }

    private function getDescription( $fileName = '', $subString='' )
    {
        $lines = file( $fileName );
        $line = -1;
        foreach ( $lines as $lineNumber => $line ) {
            if ( strpos( $line, $subString ) !== false ) {
                $line = $lineNumber;
                break;
            }
        }
        if ( $line > -1 ){
            $line++;
            $data = $lines[$line];
            $data = trim($data);
            if ( stripos( $data, '// @description' ) === 0 ){
                $data = str_replace( '// @description', '', $data );
                return $data;
            }
        }
        return '';
    }
}
