WPLib::file_hash PHP Method

file_hash() static public method

Returns a file hash, but caches it in persistent cache
static public file_hash ( string $filepath ) : string
$filepath string
return string
    static function file_hash($filepath)
    {
        $subscript = self::is_development() ? $filepath : md5($filepath);
        if ($file_hash = WPLib::cache_get($cache_key = "file_hash[{$subscript}]")) {
            WPLib::cache_get($cache_key, md5_file($filepath));
        }
        return $file_hash;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Enqueue JS or CSS.
  *
  * Auto generate version.
  *
  * @param $handle
  * @param $src
  * @param array $deps
  * @param bool $in_footer
  *
  * @future https://github.com/wplib/wplib/issues/2
  * @see https://github.com/wplib/wplib/commit/8dc27c368e84f7ba6e1448753e1b1f082a60ac6d#commitcomment-11026274
  */
 function enqueue_external($handle, $src, $deps = array(), $in_footer = false)
 {
     preg_match('#\\.(js|css)$#i', $src, $file_type);
     if ('~' === $src[0] && '/' === $src[1]) {
         /**
          * Assume $src that start with ~/ are relative.
          */
         $src = preg_replace('#^(~/)#', '', $src);
     }
     if (!($absolute = preg_match('#^(/|https?://)#', $src))) {
         /**
          * If relative, add stylesheet URL and DIR to the $src and $filepath.
          */
         $src = $this->get_root_url($src);
         $filepath = $this->get_root_dir($src);
     }
     if (!WPLib::is_script_debug() && !$absolute) {
         /**
          * If script debug and not absolute URL
          * then prefix extensions 'js' and 'css' with 'min.'
          */
         $src = preg_replace('#\\.(js|css)$#i', '.min.$1', $src);
         $ver = rand(1, 1000000);
     } else {
         $ver = !empty($filepath) ? WPLib::file_hash($filepath) : false;
     }
     switch (strtolower($file_type[1])) {
         case 'js':
             wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
             break;
         case 'css':
             wp_enqueue_style($handle, $src, $deps, $ver, $in_footer);
             break;
     }
 }