<?php

function find_paths($top)
{
	$_path_cands[] = $top;
	$_path_cands = array_merge(
		$_path_cands,
		walk_dir($top, 1, $skip_files=TRUE, $skip_dirs=FALSE)
	);
	$_path_cands = array_unique($_path_cands);

	function csort($a,$b){
		return strlen($a)-strlen($b);
	}

	function match_exclude_list($path)
	{
				$exclude = Array("wp-content", "wp-admin", "wp-includes", "administrator", "cache", "components", "images", "includes", "language", "libraries", "media", "modules", "plugins", "templates");
				foreach ($exclude as $exclude_item)
				{
					if (strpos($path, $exclude_item) !== FALSE)
					{
						return FALSE;
					}
				}

				return TRUE;
	}

	$_path_cands = array_filter($_path_cands, "match_exclude_list");

	usort($_path_cands, 'csort'); # from shortest path to longest

	return $_path_cands;
}

function walk_dir($dir, $depth=0, $skip_files=FALSE, $skip_dirs=TRUE)
{
        if ($depth < 0)
        {
            return Array();
        }

        $result = Array();

        if (($h = opendir($dir)) === FALSE)
        {
            return $result;
        }

        while (($f = readdir($h)) !== FALSE)
        {
            if ($f == '.' || $f == '..')
            {
                continue;
            }

            $current_file = "$dir/$f";
            if (is_dir($current_file))
            {
                if (!$skip_dirs)
                {
                    $result[] = $current_file;
                }

                $result = array_merge($result, walk_dir($current_file, $depth - 1, $skip_files, $skip_dirs));
            }
            else
            {
                if (!$skip_files)
                {
                    $result[] = $current_file;
                }
            }
        }

        closedir($h);

        return $result;
}

var_dump(find_paths($argv[1]));
