/** * Load 7 char abbreviated hash for commit from the system (file or exec). * * Look for a file RECENT_COMMIT if a Git post-commit hook exists and created it * otherwise call Git using shell_exec(). * * @param string $class_name * * @return null */ static function load_recent_commit($class_name) { $filepath = self::_get_recent_commit_file($class_name); $recent_commit = WPLib::is_found($filepath) ? trim(WPLib::get_contents($filepath)) : null; if (is_null($recent_commit) && WPLib::is_development()) { /** * Call `git log` via exec() */ $root_dir = self::_get_class_root_dir($class_name); $command = "cd {$root_dir} && git log -1 --oneline && cd -"; exec($command, $output, $return_value); if (0 === $return_value && isset($output[0])) { /** * If no git repo in dir, $return_value==127 and $output==array() * If no git on system, $return_value==128 and $output==array() * If good, first 7 chars of $output[0] has abbreviated hash for commit */ $recent_commit = substr($output[0], 0, 7); } } return $recent_commit; }
/** * @param string $template_slug * @param array|string $_template_vars * @param WPLib_Item_Base|object $item * * @note This is called via an instance as well as * If this becomes deprecated we can prefix with an '_' and then * use __call() and __callStatic() to allow it to be invoked. * @see http://stackoverflow.com/a/7983863/102699 */ static function the_template($template_slug, $_template_vars = array(), $item = null) { /* * Calculate the md5 value for caching this template filename */ if (!self::is_development()) { $_md5 = md5(serialize(array($template_slug, $_template_vars, get_class($item)))); } else { $_md5 = $template_slug . '[' . get_class($item) . '][' . serialize($_template_vars) . ']'; } if (!($template = self::cache_get($_cache_key = "template_file[{$_md5}]"))) { $template = new stdClass(); $template->filenames_tried = array(); $template->found = false; /** * Ensure $_template_vars is an array */ $template->vars = is_string($_template_vars) ? wp_parse_args($_template_vars) : $_template_vars; if (!is_array($template->vars)) { $template->vars = array(); } /* * Ensure filename does not have a leading slash ('/') but does have a trailing '.php' */ $_filename = preg_replace('#(.+)(\\.php)?$#', '$1.php', ltrim($template_slug, '/')); foreach (array('theme', 'module', 'app') as $template_type) { switch ($template_type) { case 'theme': $template->dir = get_stylesheet_directory(); $template->subdir = static::templates_subdir(); break; case 'module': $_app_class = !empty($template->vars['@app']) ? $template->vars['@app'] : self::app_class(); $_module_class = !empty($template->vars['@module']) ? self::get_module_class($template->vars['@module'], $_app_class) : get_class($item); $template->dir = self::get_module_dir($_module_class); $template->subdir = 'templates'; break; case 'app': /** * @note Not implemented yet. */ $_app_class = !empty($template->vars['@app']) ? $template->vars['@app'] : self::app_class(); $template->dir = call_user_func(array($_app_class, 'root_dir')); $template->subdir = 'templates'; break; } $template->filename = "{$template->dir}/{$template->subdir}/{$_filename}"; if (!WPLib::is_found($template->filename)) { $template->filenames_tried[$template_type] = $template->filename; } else { $template->found = true; $template->var_name = self::get_constant('VAR_NAME', get_class($item)); $template->comments = "<!--[TEMPLATE FILE: {$template->filename} -->"; break; } } self::cache_set($_cache_key, $template); } $template->add_comments = !self::doing_ajax() && !self::is_production(); if (!$template->found) { if ($template->add_comments) { /** * This can be used by theme developers with view source to see which templates failed. * * @note FOR CODE REVIEWERS: * * This is ONLY output of constant 'WPLIB_RUNMODE' is defined in wp-config.php. * In other words, this will NEVER run on your servers (unless you set WPLIB_RUNMODE.) */ echo "\n<!--[FAILED TEMPLATE FILE: {$template_slug}. Tried:\n"; foreach ($template->filenames_tried as $template_type => $template_filename) { echo "\n\t{$template_type}: {$template_filename}"; } echo "\n]-->"; } } else { if ($template->add_comments) { echo $template->comments; } /* * This use of extract() is to support templates in the same way * that WordPress supports templates with variables that are accessible * in the namespace. However some code sniffers constantly flag extract() * so it is easier to hide it than to have to constantly see it flagged. * * OTOH if you are using WPLib and you think we should do a direct call * to extract() here please add an issue so we can discuss the pros and * cons at https://github.com/wplib/wplib/issues */ $function = 'extract'; $function($template->vars, EXTR_PREFIX_SAME, '_'); if ($template->var_name) { /* * Assign the $item's preferred variable name in addition to '$item', i.e. '$brand' * This is a very controlled use of extract() i.e. we know what we are doing here. * * See a few lines above to explain ${'extract'} */ $function(array($template->var_name => $item)); } unset($_template_vars, $_filename, $_cache_key, $_md5, $_app_class, $_module_class); ob_start(); self::$_file_loading = $template->filename; require $template->filename; self::$_file_loading = false; if (!$template->add_comments) { echo ob_get_clean(); } else { /** * This can be used by theme developers with view source to see which templates failed. * * @note FOR CODE REVIEWERS: * * This is ONLY output if constant 'WPLIB_RUNMODE' is defined in wp-config.php. * In other words, this will NEVER run on your servers (unless you set WPLIB_RUNMODE.) */ echo $template->comments; echo ob_get_clean(); echo "\n<!--[END TEMPLATE FILE: {$template->filename} -->\n"; } } }
/** * Load 7 char abbreviated hash for commit from the system (file or exec). * * Look for a file RECENT_COMMIT if a Git post-commit hook exists and created it * otherwise call Git using shell_exec(). * * @param string $class_name * * @return null */ static function load_recent_commit($class_name) { $filepath = self::_get_recent_commit_file($class_name); $recent_commit = WPLib::is_found($filepath) ? trim(WPLib::get_contents($filepath)) : null; if (is_null($recent_commit) && WPLib::is_development()) { /** * Call `git log` via exec() */ $root_dir = call_user_func(array($class_name, 'root_dir')); do { $git_dir_found = false; if (is_dir("{$root_dir}/.git")) { $git_dir_found = true; break; } else { if (DIRECTORY_SEPARATOR === $root_dir) { /** * This is needed to work for WPLib Box if the App's repo is the project repo. */ if (is_dir($hail_mary_dir = '/vagrant/.git')) { $root_dir = $hail_mary_dir; $git_dir_found = true; } break; } } $new_root_dir = dirname($root_dir); if ($new_root_dir === $root_dir) { $new_root_dir = DIRECTORY_SEPARATOR; } $root_dir = $new_root_dir; } while (true); if ($git_dir_found) { $command = "cd {$root_dir} && git log -1 --oneline && cd -"; exec($command, $output, $return_value); if (0 === $return_value && isset($output[0])) { /** * If no git repo in dir, $return_value==127 and $output==array() * If no git on system, $return_value==128 and $output==array() * If good, first 7 chars of $output[0] has abbreviated hash for commit */ $recent_commit = substr($output[0], 0, 7); file_put_contents($filepath, $recent_commit); } } } return $recent_commit; }