public static function get_content_meta($slug, $folder = null, $raw = false, $parse = true) { $app = \Slim\Slim::getInstance(); $site_root = Config::getSiteRoot(); $content_root = Config::getContentRoot(); $content_type = Config::getContentType(); $file = $folder ? "{$content_root}/{$folder}/{$slug}.{$content_type}" : "{$content_root}/{$slug}.{$content_type}"; $file = Path::tidy($file); $meta_raw = File::exists($file) ? file_get_contents($file) : ''; if (Pattern::endsWith($meta_raw, "---")) { $meta_raw .= "\n"; # prevent parse failure } # Parse YAML Front Matter if (strpos($meta_raw, "---") === false) { $meta = self::loadYamlCached($meta_raw); if (is_array($meta)) { $meta = array_merge($meta, $app->config); } $meta['content'] = ""; if ($raw) { $meta['content_raw'] = ""; } } else { list($yaml, $content) = preg_split("/\n---/", $meta_raw, 2, PREG_SPLIT_NO_EMPTY); $meta = self::loadYamlCached($yaml); if ($raw) { $meta['content_raw'] = $content; } // Parse the content if necessary //$meta['content'] = $parse ? Content::parse($content, $meta) : $content; $meta['content'] = $content; } if (File::exists($file)) { $meta['last_modified'] = filemtime($file); } if (!$raw) { $meta['homepage'] = Config::getSiteRoot(); $meta['raw_url'] = Request::getResourceURI(); $meta['page_url'] = Request::getResourceURI(); # Is date formatted correctly? if (Config::getEntryTimestamps() && Slug::isDateTime($slug)) { $datetimestamp = Slug::getTimestamp($slug); $datestamp = Slug::getTimestamp($slug); $meta['datetimestamp'] = $datetimestamp; $meta['datestamp'] = $datestamp; $meta['date'] = Date::format(Config::getDateFormat(), $datestamp); $meta['time'] = Date::format(Config::getTimeFormat(), $datetimestamp); $meta['page_url'] = preg_replace(Pattern::DATETIME, '', $meta['page_url']); # clean url override } elseif (Slug::isDate($slug)) { $datestamp = Slug::getTimestamp($slug); $meta['datestamp'] = $datestamp; $meta['date'] = Date::format(Config::getDateFormat(), $datestamp); $meta['page_url'] = preg_replace(Pattern::DATE, '', $meta['page_url']); # clean url override } elseif (Slug::isNumeric($slug)) { $meta['numeric'] = Slug::getOrderNumber($slug); } $meta['permalink'] = Path::tidy(Config::getSiteURL() . '/' . $meta['page_url']); $taxonomy_slugify = isset($app->config['_taxonomy_slugify']) && $app->config['_taxonomy_slugify']; # Jam it all together, brother. # @todo: functionize/abstract this method for more flexibility and readability foreach ($meta as $key => $value) { if (!is_array($value) && Taxonomy::isTaxonomy($key)) { $value = array($value); $meta[$key] = $value; } if (is_array($value)) { $list = array(); $url_list = array(); $i = 1; $total_results = count($meta[$key]); foreach ($meta[$key] as $k => $v) { $url = null; if (Taxonomy::isTaxonomy($key) && !is_array($v)) { // DO NOT DO numerical regex replace on the actual taxonomy item $url = Path::tidy(strtolower($site_root . '/' . $folder . '/' . $key)); $url = preg_replace(Pattern::NUMERIC, '', $url); if ($taxonomy_slugify) { $url .= "/" . strtolower(Slug::make($v)); } else { $url .= "/" . strtolower($v); } $list[] = array('name' => $v, 'count' => $i, 'url' => $url, 'total_results' => $total_results, 'first' => $i == 1 ? true : false, 'last' => $i == $total_results ? true : false); $url_list[] = '<a href="' . $url . '">' . $v . '</a>'; } elseif (!is_array($v)) { $list[] = array('name' => $v, 'count' => $i, 'url' => $url, 'total_results' => $total_results, 'first' => $i == 1 ? true : false, 'last' => $i == $total_results ? true : false); } // account for known structure // - // name: something // url: http://example.com if (is_array($v) && isset($v['name']) && isset($v['url'])) { $url_list[] = '<a href="' . $v['url'] . '">' . $v['name'] . '</a>'; } $i++; } if (isset($url) || count($url_list)) { $meta[$key . '_url_list'] = implode(', ', $url_list); $meta[$key . '_spaced_url_list'] = join(" ", $url_list); $meta[$key . '_ordered_url_list'] = "<ol><li>" . join("</li><li>", $url_list) . "</li></ol>"; $meta[$key . '_unordered_url_list'] = "<ul><li>" . join("</li><li>", $url_list) . "</li></ul>"; $meta[$key . '_sentence_url_list'] = Helper::makeSentenceList($url_list); $meta[$key . '_ampersand_sentence_url_list'] = Helper::makeSentenceList($url_list, "&", false); } if (isset($meta[$key][0]) && !is_array($meta[$key][0])) { $meta[$key . '_list'] = implode(', ', $meta[$key]); $meta[$key . '_option_list'] = implode('|', $meta[$key]); $meta[$key . '_spaced_list'] = implode(' ', $meta[$key]); $meta[$key . '_ordered_list'] = "<ol><li>" . join("</li><li>", $meta[$key]) . "</li></ol>"; $meta[$key . '_unordered_list'] = "<ul><li>" . join("</li><li>", $meta[$key]) . "</li></ul>"; $meta[$key . '_sentence_list'] = Helper::makeSentenceList($meta[$key]); $meta[$key . '_ampersand_sentence_list'] = Helper::makeSentenceList($meta[$key], "&", false); $meta[$key] = $list; } } } } return $meta; }
/** * Supplements the content in the set * * @param array $context Context for supplementing * @return void */ public function supplement($context = array()) { $hash = Debug::markStart('content', 'supplementing'); if ($this->supplemented) { return; } $this->supplemented = true; $context = Helper::ensureArray($context); // determine context $given_context = $context; $context = array('locate_with' => isset($given_context['locate_with']) ? $given_context['locate_with'] : null, 'center_point' => isset($given_context['center_point']) ? $given_context['center_point'] : null, 'list_helpers' => isset($given_content['list_helpers']) ? $given_context['list_helpers'] : true, 'context_urls' => isset($given_context['context_urls']) ? $given_context['context_urls'] : true, 'total_found' => isset($given_context['total_found']) ? $given_context['total_found'] : null, 'group_by_date' => isset($given_context['group_by_date']) ? $given_context['group_by_date'] : null, 'inherit_folder_data' => isset($given_context['inherit_folder_data']) ? $given_context['inherit_folder_data'] : true, 'merge_with_data' => isset($given_context['merge_with_data']) ? $given_context['merge_with_data'] : true); // set up helper variables $center_point = false; if ($context['center_point'] && preg_match(Pattern::COORDINATES, $context['center_point'], $matches)) { $center_point = array($matches[1], $matches[2]); } // contextual urls are based on current page, not individual data records // we can figure this out once and then set it with each one if ($context['context_urls']) { $raw_url = Request::getResourceURI(); $page_url = Path::tidy($raw_url); } // iteration memory $last_date = null; // loop through content, supplementing each record with data foreach ($this->content as $content_key => $data) { // locate if ($context['locate_with']) { $location_data = isset($data[$context['locate_with']]) ? $data[$context['locate_with']] : null; // check that location data is fully set if (is_array($location_data) && isset($location_data['latitude']) && $location_data['latitude'] && isset($location_data['longitude']) && $location_data['longitude']) { $data['latitude'] = $location_data['latitude']; $data['longitude'] = $location_data['longitude']; $data['coordinates'] = $location_data['latitude'] . "," . $location_data['longitude']; // get distance from center if ($center_point) { $location = array($data['latitude'], $data['longitude']); $data['distance_km'] = Math::getDistanceInKilometers($center_point, $location); $data['distance_mi'] = Math::convertKilometersToMiles($data['distance_km']); } } } // contextual urls if ($context['context_urls']) { $data['raw_url'] = $raw_url; $data['page_url'] = $page_url; } // total entries if ($context['total_found']) { $data['total_found'] = (int) $context['total_found']; } // group by date if ($context['group_by_date'] && $data['datestamp']) { $formatted_date = Date::format($context['group_by_date'], $data['datestamp']); if ($formatted_date !== $last_date) { $last_date = $formatted_date; $data['grouped_date'] = $formatted_date; } else { $data['grouped_date'] = ''; } } // loop through content to add data for variables that are arrays foreach ($data as $key => $value) { // Only run on zero indexed arrays/loops if (is_array($value) && isset($value[0]) && !is_array($value[0])) { // list helpers if ($context['list_helpers']) { // make automagic lists $data[$key . "_list"] = join(", ", $value); $data[$key . "_spaced_list"] = join(" ", $value); $data[$key . "_option_list"] = join("|", $value); $data[$key . "_ordered_list"] = "<ol><li>" . join("</li><li>", $value) . "</li></ol>"; $data[$key . "_unordered_list"] = "<ul><li>" . join("</li><li>", $value) . "</li></ul>"; $data[$key . "_sentence_list"] = Helper::makeSentenceList($value); $data[$key . "_ampersand_sentence_list"] = Helper::makeSentenceList($value, "&", false); // handle taxonomies if (Taxonomy::isTaxonomy($key)) { $url_list = array_map(function ($item) use($data, $key, $value) { return '<a href="' . Taxonomy::getURL($data['_folder'], $key, $item) . '">' . $item . '</a>'; }, $value); $data[$key . "_url_list"] = join(", ", $url_list); $data[$key . "_spaced_url_list"] = join(" ", $url_list); $data[$key . "_ordered_url_list"] = "<ol><li>" . join("</li><li>", $url_list) . "</li></ol>"; $data[$key . "_unordered_url_list"] = "<ul><li>" . join("</li><li>", $url_list) . "</li></ul>"; $data[$key . "_sentence_url_list"] = Helper::makeSentenceList($url_list); $data[$key . "_ampersand_sentence_url_list"] = Helper::makeSentenceList($url_list, "&", false); } } } } // update content with supplemented data merged with global config data if ($context['merge_with_data'] || $context['inherit_folder_data']) { $folder_data = array(); $all_config = array(); if ($context['inherit_folder_data']) { $folder_data = $this->getFolderData($data['_file']); } if ($context['merge_with_data']) { $all_config = Config::getAll(); } // merge them all together $this->content[$content_key] = $data + $folder_data + $all_config; } else { $this->content[$content_key] = $data; } } Debug::markEnd($hash); }
public static function is_taxonomy($tax) { Log::warn("Use of Statamic::is_taxonomy() is deprecated. Use Taxonomy::isTaxonomy() instead.", "core", "Statamic"); return Taxonomy::isTaxonomy($tax); }