/** * Returns the URL for a page given it's slug. * * @param string $page_slug * @return string */ function get_page_url($page_slug) { /* * Validate that the slug is for a $post_type==='page' * by getting it's post type. */ $post_type = get_post_type(get_page_by_path($page_slug)); if (false === $post_type) { if (WPLib::is_development()) { /** * @future Ensure the page is here and throw an error if not. */ } else { /** * @future MAYBE output a warning as an HTML comment? */ } } /* * If $post_type==='page' then take slug onto home URL, return null otherwise. */ return WPLib_Page::POST_TYPE === $post_type ? home_url($page_slug) : '#'; }
/** * Load all registered modules, by priority */ private static function _load_modules() { ksort(self::$_modules); self::$_modules = apply_filters('wplib_modules', self::$_modules); $called_class = get_called_class(); $module_classes = isset(self::$_module_classes[$called_class]) ? self::$_module_classes[$called_class] : array(); $abspath_regex = '#^' . preg_quote(ABSPATH) . '(.+)' . DIRECTORY_SEPARATOR . '.+\\.php$#'; foreach (self::$_modules as $priority) { foreach ($priority as $filepath) { if (WPLib::is_development() && !is_file($filepath)) { WPLib::trigger_error(sprintf(__("Required file not found: %s", 'wplib'), $filepath)); } /** * Set self::$_file_loading so 'shutdown' hook can report which file caused the load error. */ self::$_file_loading = $filepath; require_once $filepath; self::$_file_loading = false; $classes = get_declared_classes(); $module_classes[end($classes)] = $module_path = preg_replace($abspath_regex, '~/$1', $filepath); /** * Find all autoloading files defined by the above module. */ static::_find_autoload_files(); } } self::$_module_classes[$called_class] = $module_classes; self::$_modules = array(); }
/** * @param WPLib_Item_Base|WP_Post|WP_Term $item * @param array $args * * @return WPLib_Term_Base|WPLib_Post_Base */ static function make_new_item($item, $args = array()) { $class = get_called_class(); if (WPLib::get_constant('INSTANCE_CLASS', $class)) { if (self::class_declares_method($class, 'make_new_item')) { $item = $class::make_new_item($item, $args); } else { if (WPLib::is_development()) { $err_msg = __('Cannot make new item. Class %s does not have make_new_item method', 'wplib'); WPLib::trigger_error(sprintf($err_msg, $class), E_USER_ERROR); } } } else { if (WPLib::is_development()) { $err_msg = __('Cannot make new item. Class %s does not have INSTANCE_CLASS constant.', 'wplib'); WPLib::trigger_error(sprintf($err_msg, $class), E_USER_ERROR); } } return $item; }
/** * 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; }
/** * 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; }
/** * Returns array of class names $base_class children with positive values for $base_class::$contant_name. * * @param $base_class * * @param $constant_name * * @return string[] */ static function get_child_classes($base_class, $constant_name) { $cache_key = "classes[{$base_class}::{$constant_name}]"; if (!WPLib::is_development()) { $cache_key = md5($cache_key); } if (!($child_classes = self::cache_get($cache_key))) { WPLib::autoload_all_classes(); $child_classes = array(); foreach (self::site_classes() as $class_name) { do { if (!is_subclass_of($class_name, $base_class)) { continue; } if (is_null($constant_value = self::get_constant($constant_name, $class_name))) { continue; } $child_classes[$constant_value] = $class_name; } while (false); } self::cache_set($cache_key, $child_classes); } return $child_classes; }