/**
  * Triggered in the front end to learn the locale based on the type
  * of object the user is browsing. Prevents a user from seeing
  * a object in another locale than the one the object is supposed to be in.
  * @return \Strata\i18n\Locale
  */
 public function getByFrontContext()
 {
     $i18n = Strata::i18n();
     $defaultLocale = $i18n->getDefaultLocale();
     global $wp_query;
     // Prevents 'xyz was called incorrectly' messages.
     if (!is_null($wp_query) && !Strata::isCommandLineInterface() && !is_search() && !is_404()) {
         // By Post
         $postId = get_the_ID();
         if ($postId) {
             $suspectedLocale = $this->getLocaleByPostId($postId);
             if ($suspectedLocale && !$suspectedLocale->isDefault() && $i18n->shouldFallbackToDefaultLocale()) {
                 $defaultPost = $defaultLocale->getTranslatedPost($postId);
                 $localizedPost = $suspectedLocale->getTranslatedPost($postId);
                 if ($localizedPost && $defaultPost) {
                     if ((int) $defaultPost->ID !== (int) $localizedPost->ID) {
                         return $suspectedLocale;
                     }
                 }
             }
         }
         // By Taxonomy
         global $wp_query;
         if ($wp_query) {
             $taxonomy = $wp_query->queried_object;
             if (is_a($taxonomy, "WP_Term")) {
                 $suspectedLocale = $this->getLocaleByTaxonomyId($taxonomy->term_id, $taxonomy->taxonomy);
                 if ($suspectedLocale && !$suspectedLocale->isDefault() && $i18n->shouldFallbackToDefaultLocale()) {
                     $defaultPost = $defaultLocale->getTranslatedTerm($taxonomy->term_id, $taxonomy->taxonomy);
                     $localizedPost = $suspectedLocale->getTranslatedTerm($taxonomy->term_id, $taxonomy->taxonomy);
                     if ($localizedPost && $defaultPost) {
                         if ((int) $defaultPost->term_id !== (int) $localizedPost->term_id) {
                             return $suspectedLocale;
                         }
                     }
                 }
             }
         }
     }
     if (preg_match('#/(' . Utility::getLocaleUrlsRegex() . ')/#', $_SERVER['REQUEST_URI'], $matches)) {
         return Strata::i18n()->getLocaleByUrl($matches[1]);
     }
 }
Exemple #2
0
 /**
  * Outputs a stack trace based on the supplied options.
  *
  * ### Options
  *
  * - `depth` - The number of stack frames to return. Defaults to 50
  * - `start` - The stack frame to start generating a trace from. Defaults to 1
  *
  * @param array $options Format for outputting stack trace
  * @return mixed Formatted stack trace
  * @link https://github.com/cakephp/cakephp/blob/master/src/basics.php
  */
 function stackTrace(array $options = array())
 {
     if (!WP_DEBUG) {
         return;
     }
     $defaults = array('start' => 0);
     if (Strata::isBundledServer() || Strata::isCommandLineInterface()) {
         $defaults['output'] = Debugger::CONSOLE;
     }
     $options += $defaults;
     $options['start']++;
     $trace = Debugger::trace(null, $options);
     if (Strata::isBundledServer()) {
         Strata::app()->getLogger("StrataConsole")->debug("\n\n" . $trace . "\n\n");
     }
     if (Strata::isCommandLineInterface()) {
         echo "\n\n" . $trace . "\n\n";
     } else {
         echo "<div style=\"" . Debugger::HTML_STYLES . "\"><pre>" . $trace . "</pre></div>";
     }
 }
 private function shouldBeDebugging()
 {
     return $this->useStrataDebugger() && Strata::isDev() && !Strata::isCommandLineInterface();
 }