diff --git a/class-RegisterCSS.php b/class-RegisterCSS.php index e39ca81..c18eb4e 100644 --- a/class-RegisterCSS.php +++ b/class-RegisterCSS.php @@ -1,85 +1,98 @@ . class CSS { public $url; public $enqueue = false; public $enqueued = false; public function __construct($url) { $this->url = $url; } } class RegisterCSS { /** * Stylesheets */ private $css = []; + /** + * Get the singleton instance + * + * @return self + */ + public function instance() { + static $me = false; + if( ! $me ) { + $me = new self(); + } + return $me; + } + /** * Register a new style name. * * @param string $name CSS name, like: "jquery-ui". * @param string $url CSS url, like "http://example.org/styles/jquery-ui.css". * @param boolean $private_use True: include it with $this->enqueueAll(); false: you have to include it manually. Use CSS::PRIVATE_USE instead of true for semantic reasons. */ public function register($css_uid, $url) { $this->css[$css_uid] = new CSS($url); } /** * Enqueue a previous registered CSS name. * * @param string $css_uid CSS name. */ public function enqueue($css_uid) { if(!isset($this->css[$css_uid])) { DEBUG && error( sprintf( __("Il foglio di stile %s non può essere incorporato perchè non è stato ancora registrato."), "" . esc_html($css_uid) . "" ) ); } else { $this->css[$css_uid]->enqueue = true; return true; } return false; } /** * Enqueue all registered style that are not registered as private use. */ public function printAll($force = false) { foreach($this->css as $css_uid=>$css) { if($css->enqueue && !$css->enqueued) { self::link($css->url, $css_uid); $this->css[$css_uid]->enqueued = true; } } } /** * Print the CSS link tag. */ public static function link($url, $css_uid) { if( CACHE_BUSTER ) { $url .= false === strpos($url, '?') ? '?' : '&'; $url .= CACHE_BUSTER; } echo "\n\t"; } } diff --git a/class-RegisterJS.php b/class-RegisterJS.php index f34fdf4..212592d 100644 --- a/class-RegisterJS.php +++ b/class-RegisterJS.php @@ -1,117 +1,130 @@ . /** * Classes useful to enqueue JS libraries. */ class JS { const HEADER = true; const FOOTER = false; public $url; public $position = self::HEADER; public $enqueue = false; public function __construct($url) { $this->url = $url; } /** * @param boolean $in_head {JS::HEADER, JS::FOOTER} */ public function enqueue($position = self::HEADER) { $this->position = self::filterPosition($position); $this->enqueue = true; } public static function filterPosition($position) { if( $position === null ) { return self::HEADER; } return $position; } } class RegisterJS { /** * Script names. */ private $javascript = []; private $generated = [false, false]; + /** + * Get the singleton instance + * + * @return self + */ + public function instance() { + static $me = false; + if( ! $me ) { + $me = new self(); + } + return $me; + } + /** * Register a new script name. * * @param string $name JS name, like: "jquery". * @param string $url Script url, like "http://example.org/lib/jquery.js". */ public function register($javascript_uid, $url) { if(isset($this->javascript[$javascript_uid])) { $this->javascript[$javascript_uid]->url = $url; } else { $this->javascript[$javascript_uid] = new JS($url); } } /** * Enqueue a previous registered JS name. * * @param string $name JS name. * @param bool $in_head Place it in the head of the page or not. */ public function enqueue($javascript_uid, $position = null) { $position = JS::filterPosition($position); if(isset($this->javascript[$javascript_uid])) { $this->javascript[$javascript_uid]->enqueue($position); return true; } else { DEBUG && error( sprintf( __("La libreria JavaScript %s non può essere incorporata poichè non è ancora stata registrata."), "" . esc_html($javascript_uid) . "" ) ); return false; } } public function printAll($position) { $cache_burster = CACHE_BUSTER; foreach($this->javascript as $javascript_uid=>$javascript) { if($javascript->enqueue && $javascript->position === $position) { echo "\n"; if($position === JS::HEADER) { echo "\t"; } $url = $javascript->url; if( CACHE_BUSTER ) { $url .= false === strpos($url, '?') ? '?' : '&'; $url .= CACHE_BUSTER; } echo ""; if(DEBUG) { echo ""; } } } $this->generated[(int) $position] = true; } public function isGenerated($position) { return $this->generated[(int) $position]; } } diff --git a/class-RegisterLanguage.php b/class-RegisterLanguage.php index 8e91cf5..e96f2ea 100644 --- a/class-RegisterLanguage.php +++ b/class-RegisterLanguage.php @@ -1,332 +1,332 @@ . /** * Handle languages using standard GNU Gettext functionalities * * @use error_die(), esc_html() */ class RegisterLanguage { private $i = -1; private $languages = []; /** * Associative map of alias=>index of $this->languages */ private $aliases = []; private $gettextDomain; private $gettextDirectory; private $gettextDefaultEncode; /** * Use native GNU Gettext (lot of quicker but more unreliable) * * @var bool */ private $native; /** * Latest language applied */ private $latest = null; /** * Default language (don't apply GNU Gettext in this case). * * Index of self#languages[] */ private $default = null; /** * Get the singleton instance * * @return self */ public function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Constructor * * @param string $domain GNU Gettext domain * @param string $directory GNU Gettext directory * @param string $default_encode GNU Gettext default language encode */ public function __construct( $domain = null, $directory = null, $default_encode = null, $native = null ) { if( ! $domain ) { $domain = GETTEXT_DOMAIN; } if( ! $directory ) { $directory = GETTEXT_DIRECTORY; } if( ! $default_encode ) { $default_encode = GETTEXT_DEFAULT_ENCODE; } if( null === $native ) { $native = defined( 'GETTEXT_NATIVE' ) ? GETTEXT_NATIVE : false; } $this->gettextDomain = $domain; $this->gettextDirectory = $directory; $this->gettextDefaultEncode = $default_encode; $this->native = $native; } /** * Register a language * * @param $code string GNU Gettext code language * @param $aliases array You can specify language aliases (in lower case) * @param $encode string Override the default GNU Gettext language encode * @param $iso string * @param $human string e.g. 'English' */ public function registerLanguage( $code, $aliases = [], $encode = null, $iso = null, $human = null, $humanL10n = null ) { if( ! $encode ) { $encode = $this->gettextDefaultEncode; } if( ! $encode ) { DEBUG && error( sprintf( ___("Non hai specificato una codifica per la lingua '%s' e non ce n'è una predefinita. Impostala con la costante %s."), esc_html($code), 'GETTEXT_DEFAULT_ENCODE' ) ); return false; } $this->languages[ ++$this->i ] = new BozPHPLanguage( $code, $encode, $iso, $human, $humanL10n ); // Yes, the language code it's an alias to itself. That's a lazy hack! $this->aliases[ self::normalize($code) ] = $this->i; // Each alias is associated to it's language code force_array( $aliases ); foreach( $aliases as $alias ) { $this->aliases[ $alias ] = $this->i; } } /** * Set the default language. * * A default language allow less high-level pain. * * @param string $default */ public function setDefaultLanguage( $default ) { $code = self::normalize($default); if( ! isset( $this->aliases[ $code ] ) ) { error_die( sprintf( ___("Default language '%s' have to be registered previously"), esc_html($default) ) ); } $this->default = $this->languages[ $this->aliases[ $code ] ]; } /** * Get a Language object from a language alias. * * @param string $language_alias Language alias, null for browser preferences * @return false|object Language object or false if it isn't registered */ public function getLanguage( $language_alias = null ) { if( $language_alias ) { $language_alias = self::normalize( $language_alias ); if( isset( $this->aliases[ $language_alias ] ) ) { return $this->languages[ $this->aliases[ $language_alias ] ]; } } else { return $this->getLanguageFromHTTP(); } return false; } /** * Is a native GETTEXT implementation? * * @return bool */ public function isNative() { return $this->native; } /** * Set the GNU Gettext environment from a language alias or from browser preferences. * * @param string $language_alias Language alias, null for browser preferences * @return mixed */ public function applyLanguage( $language_alias = null ) { $language = $this->getLanguage( $language_alias ); if( ! $language ) { return false; } $this->latest = $language; self::GNUGettextEnvironment( $language->getCode(), $language->getEncode(), $this->gettextDomain, $this->gettextDirectory, $this->isNative() ); return $language->getCode(); } /** * @return BozPHPLanguage|null */ function getLatestLanguageApplied() { return $this->latest ? $this->latest : $this->default; } /** * Determine browser language in preference order, respecting RFC. * * @see http://stackoverflow.com/a/11161193 */ function getLanguageFromHTTP() { if( ! isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { return false; } // Create an associative array of priority=>language preg_match_all( '/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ], $langs ); if( 0 === count( $langs[ 1 ] ) ) { return false; } $langs = array_combine( $langs[ 1 ], $langs[ 4 ] ); // To respect RFC if no priority is specified (q=;) foreach( $langs as $lang => $val ){ $langs[ $lang ] = ( '' === $val ) ? 1.0 : (float) $val; } // Order by priority arsort( $langs, SORT_NUMERIC ); // Search and use a known language foreach( $langs as $lang => $val ) { $language = $this->getLanguage( $lang ); if( false !== $language ) { // Found a language that match browser preferences! return $language; } } return false; } /** * Get all the registered languages * * @return array */ public function getAll() { return $this->languages; } /** * Fill the GNU Gettext Environment * * @param $native bool Use or not the native implementation */ public static function GNUGettextEnvironment( $code, $encode, $domain, $directory, $native = false ) { if( $native ) { putenv( "LANG=$code.$encode" ); $ok = setlocale( LC_MESSAGES, "$code.$encode" ); bindtextdomain( $domain, $directory ); textdomain( $domain ); bind_textdomain_codeset( $domain, $encode ); } else { - $loader = MoLoader::getInstance(); + $loader = MoLoader::instance(); $loader->setlocale( "$code.$encode" ); $loader->textdomain( $domain ); $loader->bindtextdomain( $domain, $directory ); } } /** * Normalize a language code for internal use * * @param $code string * @return string */ private static function normalize( $code ) { return strtolower( $code ); } } class BozPHPLanguage { private $code; private $encode; private $iso; private $human; private $humanL10n; public function __construct( $code, $encode, $iso, $human, $humanL10n ) { $this->code = $code; $this->encode = $encode; $this->iso = $iso; $this->human = $human; $this->humanL10n = $humanL10n; } public function getCode() { return $this->code; } public function getEncode() { return $this->encode; } public function getISO() { if($this->iso === null) { $this->iso = self::guessISO($this->iso); } return $this->iso; } public function getHuman() { return $this->human; } public function getHumanL10n() { return $this->humanL10n; } /** * Gets 'it' from 'it_IT', 'it_IT.utf8' etc. * * Can be called statically */ public function guessISO( $code = null, $fallback = 'en' ) { if($code === null) { $code = $this->code; } $p = strpos( $code, '_' ); if( false === $p ) { DEBUG && error( sprintf( ___("Can't guess the ISO language from language code '%s'. Falling on default '%s'."), esc_html($code), $fallback ) ); return $fallback; } return substr($code, 0, $p); } }