PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/_app/core/statamic.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 1460 lines | 699 code | 210 blank | 551 comment | 167 complexity | 05bdbbd4449444f8a6b1abae5e543921 MD5 | raw file
Possible License(s): JSON

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Statamic
  4. *
  5. * @author Jack McDade
  6. * @author Mubashar Iqbal
  7. * @author Fred LeBlanc
  8. * @copyright 2012 Statamic
  9. * @link http://www.statamic.com
  10. * @license http://www.statamic.com
  11. *
  12. */
  13. use Symfony\Component\Finder\Finder as Finder;
  14. class Statamic
  15. {
  16. protected static $_yaml_cache = array();
  17. public static $folder_list = array();
  18. public static $publication_states = array('live' => 'Live', 'hidden' => 'Hidden', 'draft' => 'Draft');
  19. public static function loadYamlCached($content)
  20. {
  21. $hash = md5($content);
  22. if (isset(self::$_yaml_cache[$hash])) {
  23. $yaml = self::$_yaml_cache[$hash];
  24. } else {
  25. $yaml = YAML::parse($content);
  26. self::$_yaml_cache[$hash] = $yaml;
  27. }
  28. return $yaml;
  29. }
  30. /**
  31. * Load the config (yaml) files in a specified order:
  32. *
  33. * 1. Loose per-site configs
  34. * 2. Routes
  35. * 3. Settings
  36. * 4. Theme overrides
  37. */
  38. public static function loadAllConfigs($admin = FALSE)
  39. {
  40. /*
  41. |--------------------------------------------------------------------------
  42. | YAML Mode
  43. |--------------------------------------------------------------------------
  44. |
  45. | We need to know the YAML mode first (loose, strict, transitional),
  46. | so we parse the settings file once to check before doing anything else.
  47. |
  48. */
  49. $preload_config = YAML::parse(Config::getConfigPath() . '/settings.yaml');
  50. $yaml_mode = array_get($preload_config, '_yaml_mode', 'loose');
  51. /*
  52. |--------------------------------------------------------------------------
  53. | Default Settings
  54. |--------------------------------------------------------------------------
  55. |
  56. | We keep a set of default options that the user config overrides, allowing
  57. | us to always have clean defaults.
  58. |
  59. */
  60. $default_config = YAML::parse(Config::getAppConfigPath() . '/default.settings.yaml');
  61. /*
  62. |--------------------------------------------------------------------------
  63. | User Site Settings
  64. |--------------------------------------------------------------------------
  65. |
  66. | Next we parse and override the user's settings.
  67. |
  68. */
  69. $user_config = YAML::parse(Config::getConfigPath() . '/settings.yaml', $yaml_mode);
  70. $config = array_merge($default_config, $user_config);
  71. /*
  72. |--------------------------------------------------------------------------
  73. | Routes and vanity URLs
  74. |--------------------------------------------------------------------------
  75. |
  76. | Any URL can be manipulated by routes or vanity urls. We need this info
  77. | early on, before content parsing begins.
  78. |
  79. */
  80. $routes = array();
  81. if (File::exists(Config::getConfigPath() . '/routes.yaml')) {
  82. $routes['_routes'] = YAML::parse('_config/routes.yaml', $yaml_mode);
  83. }
  84. // check for vanity URLs first, we may need to redirect
  85. $vanity = array();
  86. if (File::exists(Config::getConfigPath() . '/vanity.yaml')) {
  87. $vanity['_vanity_urls'] = YAML::parse(Config::getConfigPath() . '/vanity.yaml', $yaml_mode);
  88. }
  89. $config = array_merge($config, $routes, $vanity);
  90. /*
  91. |--------------------------------------------------------------------------
  92. | Global Variables
  93. |--------------------------------------------------------------------------
  94. |
  95. | We parse all the yaml files in the root (except settings and routes) of
  96. | the config folder and make them available as global template variables.
  97. |
  98. */
  99. if (Folder::exists($config_files_location = Config::getConfigPath())) {
  100. $finder = new Finder();
  101. $files = $finder->files()
  102. ->in($config_files_location)
  103. ->name('*.yaml')
  104. ->notName('routes.yaml')
  105. ->notName('vanity.yaml')
  106. ->notName('settings.yaml')
  107. ->depth(0)
  108. ->followLinks();
  109. if (iterator_count($files) > 0) {
  110. foreach ($files as $file) {
  111. $config = array_merge($config, YAML::parse($file->getRealPath(), $yaml_mode));
  112. }
  113. }
  114. }
  115. /*
  116. |--------------------------------------------------------------------------
  117. | Theme Variables
  118. |--------------------------------------------------------------------------
  119. |
  120. | Theme variables need to specifically parsed later so they can override
  121. | any site/global defaults.
  122. |
  123. */
  124. $themes_path = array_get($config, '_themes_path', '_themes');
  125. $theme_name = array_get($config, '_theme', 'denali');
  126. if (Folder::exists($theme_files_location = URL::assemble(BASE_PATH, $themes_path, $theme_name))) {
  127. $finder = new Finder(); // clear previous Finder interator results
  128. $theme_files = $finder->files()
  129. ->in($theme_files_location)
  130. ->name('*.yaml')
  131. ->depth(0)
  132. ->followLinks();
  133. if (iterator_count($theme_files) > 0) {
  134. foreach ($theme_files as $file) {
  135. $config = array_merge($config, YAML::parse($file->getRealPath(), $yaml_mode));
  136. }
  137. }
  138. }
  139. /*
  140. |--------------------------------------------------------------------------
  141. | Load Environment Configs and Variables
  142. |--------------------------------------------------------------------------
  143. |
  144. | Environments settings explicitly overwrite any existing settings, and
  145. | therefore must be loaded late. We also set a few helper variables
  146. | to make working with environments even easier.
  147. |
  148. */
  149. Environment::establish($config);
  150. /*
  151. |--------------------------------------------------------------------------
  152. | MIME Types
  153. |--------------------------------------------------------------------------
  154. */
  155. $config = array_merge($config, array('_mimes' => require Config::getAppConfigPath() . '/mimes.php'));
  156. /*
  157. |--------------------------------------------------------------------------
  158. | Localization
  159. |--------------------------------------------------------------------------
  160. |
  161. | We load up English by default. We're American after all. Doesn't the
  162. | world revolve around us? Hello? Bueller? More hamburgers please.
  163. |
  164. */
  165. $config['_translations'] = array();
  166. $config['_translations']['en'] = YAML::parse(Config::getAppConfigPath() . '/default.en.yaml');
  167. if ($lang = array_get($config, '_language', false)) {
  168. if (File::exists(Config::getTranslation($lang))) {
  169. $config['_translations'][$lang] = YAML::parse(Config::getTranslation($lang));
  170. }
  171. }
  172. /*
  173. |--------------------------------------------------------------------------
  174. | Set Slim Config
  175. |--------------------------------------------------------------------------
  176. |
  177. | Slim needs to be initialized with a set of config options, so these
  178. | need to be set earlier than the set_default_tags() method.
  179. |
  180. */
  181. // $config['view'] = new Statamic_View();
  182. $config['cookies.lifetime'] = $config['_cookies.lifetime'];
  183. if ($admin) {
  184. $admin_theme = array_get($config, '_admin_theme', 'ascent');
  185. if ( ! Folder::exists(Path::tidy('/'.$config['_admin_path'].'/'.'themes/'.$admin_theme))) {
  186. $admin_theme = 'ascent';
  187. }
  188. $theme_path = Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme . '/');
  189. $config['_admin_path'] = $config['_admin_path'];
  190. $config['theme_path'] = $theme_path;
  191. $config['templates.path'] = '.'.$theme_path;
  192. } else {
  193. $public_path = isset($config['_public_path']) ? $config['_public_path'] : '';
  194. $config['theme_path'] = $themes_path.'/'.$config['_theme'].'/';
  195. $config['templates.path'] = Path::tidy($public_path.$themes_path.'/'.$config['_theme'].'/');
  196. }
  197. return $config;
  198. }
  199. /**
  200. * If the given redirect conditions are met, redirects the site to the given URL
  201. *
  202. * @param array $config Configuration of the site
  203. * @return bool
  204. */
  205. public static function processVanityURLs($config)
  206. {
  207. // if no array or an empty one, we're done here
  208. if ( ! isset($config['_vanity_urls']) || empty($config['_vanity_urls'])) {
  209. return FALSE;
  210. }
  211. // current path
  212. // note: not using API because it's not ready yet
  213. $uri = $_SERVER['REQUEST_URI'];
  214. $query = $_SERVER['QUERY_STRING'];
  215. $current = ($query) ? str_replace("?" . $query, "", $uri) : $uri;
  216. // loop through configured vanity URLs
  217. foreach ($config['_vanity_urls'] as $url => $redirect) {
  218. $redirect_forward = FALSE;
  219. $redirect_url = NULL;
  220. // if this wasn't a match, move on
  221. if (Path::tidy("/" . $url) != $current) {
  222. continue;
  223. }
  224. // we have a match
  225. // now check to see how this redirect was set up
  226. if (is_array($redirect)) {
  227. // this was an array
  228. if ( ! isset($redirect['url'])) {
  229. Log::warn("Vanity URL `" . $url . "` matched, but no redirect URL was configred.", "core", "vanity");
  230. continue;
  231. }
  232. $redirect_start = Helper::choose($redirect, 'start', NULL);
  233. $redirect_until = Helper::choose($redirect, 'until', NULL);
  234. $redirect_forward = Helper::choose($redirect, 'forward_query_string', FALSE);
  235. $redirect_url = Helper::choose($redirect, 'url', $redirect);
  236. $redirect_type = (int) Helper::choose($redirect, 'type', 302);
  237. // if start date is set and it's before that date
  238. if ($redirect_start && time() < Date::resolve($redirect_start)) {
  239. Log::info("Vanity URL `" . $url . "` matched, but scheduling does not allowed redirecting yet.", "core", "vanity");
  240. continue;
  241. }
  242. // if until date is set and it's after after that date
  243. if ($redirect_until && time() > Date::resolve($redirect_until)) {
  244. Log::info("Vanity URL `" . $url . "` matched, but scheduling for this redirect has expired.", "core", "vanity");
  245. continue;
  246. }
  247. } else {
  248. // this was a string
  249. $redirect_url = $redirect;
  250. $redirect_type = 302;
  251. }
  252. // optionally forward any query string variables
  253. if ($query && $redirect_forward) {
  254. $redirect_url .= (strstr($redirect_url, "?") !== FALSE) ? "&" : "?";
  255. $redirect_url .= $query;
  256. }
  257. // ensure a complete URL
  258. if (!substr($redirect_url, 0, 4) == "http") {
  259. $redirect_url = Path::tidy(Config::getSiteRoot() . "/" . $redirect_url);
  260. }
  261. // ensure a valid redirect type
  262. if (!in_array($redirect_type, array(301, 302))) {
  263. $redirect_type = 302;
  264. }
  265. // redirect
  266. header("Location: " . $redirect_url, TRUE, $redirect_type);
  267. exit();
  268. }
  269. }
  270. /**
  271. * Set up any and all global vars, tags, and other defaults
  272. *
  273. * @return void
  274. */
  275. public static function setDefaultTags()
  276. {
  277. $app = \Slim\Slim::getInstance();
  278. /*
  279. |--------------------------------------------------------------------------
  280. | User & Session Authentication
  281. |--------------------------------------------------------------------------
  282. |
  283. | This may be overwritten later, but let's go ahead and set the default
  284. | layout file to start assembling our front-end view.
  285. |
  286. */
  287. $current_user = Statamic_Auth::get_current_user();
  288. $app->config['logged_in'] = $current_user !== FALSE;
  289. $app->config['username'] = $current_user ? $current_user->get_name() : FALSE;
  290. $app->config['is_admin'] = $current_user ? $current_user->has_role('admin') : FALSE;
  291. /*
  292. |--------------------------------------------------------------------------
  293. | GET and POST global vars
  294. |--------------------------------------------------------------------------
  295. |
  296. | Use these at your own risk, of course. Don't be stupid.
  297. |
  298. */
  299. $app->config['get'] = $_GET;
  300. $app->config['post'] = $_POST;
  301. $app->config['get_post'] = array_merge($_GET, $_POST);
  302. /**
  303. * @deprecated
  304. * The {{ user }} tag has been replaced by {{ member:profile }} and
  305. * will be removed in v1.6
  306. */
  307. $app->config['user'] = $current_user ? array(
  308. array('first_name' => $current_user->get_first_name()),
  309. array('last_name' => $current_user->get_last_name()),
  310. array('bio' => $current_user->get_biography())
  311. ) : FALSE;
  312. $app->config['homepage'] = Config::getSiteRoot();
  313. }
  314. /**
  315. * A cache-friendly wrapper for the get_content_list() method
  316. *
  317. * @return array
  318. **/
  319. public static function get_folder_list($folder,$future=FALSE,$past=TRUE)
  320. {
  321. if (isset(self::$folder_list[$folder])) {
  322. $folder_list = self::$folder_list[$folder];
  323. } else {
  324. $folder_list = Statamic::get_content_list($folder, NULL, 0, $future, $past, 'date', 'desc', NULL, NULL, FALSE, FALSE, NULL, NULL);
  325. self::$folder_list[$folder] = $folder_list;
  326. }
  327. return $folder_list;
  328. }
  329. public static function get_site_root()
  330. {
  331. Log::warn("Use of Statamic::get_site_root() is deprecated. Use Config::getSiteRoot() instead.", "core", "Statamic");
  332. return Config::getSiteRoot();
  333. }
  334. public static function get_site_url()
  335. {
  336. Log::warn("Use of Statamic::get_site_url() is deprecated. Use Config::getSiteURL() instead.", "core", "Statamic");
  337. return Config::getSiteURL();
  338. }
  339. public static function get_site_name()
  340. {
  341. Log::warn("Use of Statamic::get_site_name() is deprecated. Use Config::getSiteName() instead.", "core", "Statamic");
  342. return Config::getSiteName();
  343. }
  344. public static function get_license_key()
  345. {
  346. Log::warn("Use of Statamic::get_license_key() is deprecated. Use Config::getLicenseKey() instead.", "core", "Statamic");
  347. return Config::getLicenseKey();
  348. }
  349. public static function get_theme_name()
  350. {
  351. Log::warn("Use of Statamic::get_theme_name() is deprecated. Use Config::getTheme() instead.", "core", "Statamic");
  352. return Config::getTheme();
  353. }
  354. public static function get_theme()
  355. {
  356. Log::warn("Use of Statamic::get_theme() is deprecated. Use Config::getTheme() instead.", "core", "Statamic");
  357. return Config::getTheme();
  358. }
  359. public static function get_theme_assets_path()
  360. {
  361. Log::warn("Use of Statamic::get_theme_assets_path() is deprecated. Use Config::getThemeAssetsPath() instead.", "core", "Statamic");
  362. return Config::getThemeAssetsPath();
  363. }
  364. public static function get_theme_path()
  365. {
  366. Log::warn("Use of Statamic::get_theme_path() is deprecated. Use Config::getCurrentThemePath() instead.", "core", "Statamic");
  367. return Config::getCurrentThemePath();
  368. }
  369. public static function get_templates_path()
  370. {
  371. Log::warn("Use of Statamic::get_templates_path() is deprecated. Use Config::getTemplatesPath() instead.", "core", "Statamic");
  372. return Config::getTemplatesPath();
  373. }
  374. public static function get_admin_path()
  375. {
  376. Log::warn("Use of Statamic::get_admin_path() is deprecated. Use Config::getAdminPath() instead.", "core", "Statamic");
  377. return Config::getAdminPath();
  378. }
  379. public static function get_addon_path($addon=NULL)
  380. {
  381. Log::warn("Use of Statamic::get_addon_path() is deprecated. Use Config::getAddOnPath() instead.", "core", "Statamic");
  382. return Config::getAddOnPath($addon);
  383. }
  384. public static function get_content_root()
  385. {
  386. Log::warn("Use of Statamic::get_content_root() is deprecated. Use Config::getContentRoot() instead.", "core", "Statamic");
  387. return Config::getContentRoot();
  388. }
  389. public static function get_content_type()
  390. {
  391. Log::warn("Use of Statamic::get_content_type() is deprecated. Use Config::getContentType() instead.", "core", "Statamic");
  392. return Config::getContentType();
  393. }
  394. public static function get_date_format()
  395. {
  396. Log::warn("Use of Statamic::get_date_format() is deprecated. Use Config::getDateFormat() instead.", "core", "Statamic");
  397. return Config::getDateFormat();
  398. }
  399. public static function get_time_format()
  400. {
  401. Log::warn("Use of Statamic::get_time_format() is deprecated. Use Config::getTimeFormat() instead.", "core", "Statamic");
  402. return Config::getTimeFormat();
  403. }
  404. public static function get_entry_timestamps()
  405. {
  406. Log::warn("Use of Statamic::get_entry_timestamps() is deprecated. Use Config::getEntryTimestamps() instead.", "core", "Statamic");
  407. return Config::getEntryTimestamps();
  408. }
  409. public static function get_setting($setting, $default = FALSE)
  410. {
  411. Log::warn("Use of Statamic::get_setting() is deprecated. Use Config::get() instead.", "core", "Statamic");
  412. return Config::get($setting, $default);
  413. }
  414. public static function get_entry_type($path)
  415. {
  416. $type = 'none';
  417. $content_root = Config::getContentRoot();
  418. if (File::exists("{$content_root}/{$path}/fields.yaml")) {
  419. $fields_raw = File::get("{$content_root}/{$path}/fields.yaml");
  420. $fields_data = YAML::parse($fields_raw);
  421. if (isset($fields_data['type']) && ! is_array($fields_data['type'])) {
  422. $type = $fields_data['type']; # simplify, no "prefix" necessary
  423. } elseif (isset($fields_data['type']['prefix'])) {
  424. $type = $fields_data['type']['prefix'];
  425. }
  426. }
  427. return $type;
  428. }
  429. public static function get_templates_list()
  430. {
  431. Log::warn("Use of Statamic::get_templates_list() is deprecated. Use Theme::getTemplates() instead.", "core", "Statamic");
  432. return Theme::getTemplates();
  433. }
  434. public static function get_layouts_list()
  435. {
  436. Log::warn("Use of Statamic::get_templates_list() is deprecated. Use Theme::getLayouts() instead.", "core", "Statamic");
  437. return Theme::getLayouts();
  438. }
  439. public static function get_pagination_variable()
  440. {
  441. Log::warn("Use of Statamic::get_pagination_variable() is deprecated. Use Config::getPaginationVariable() instead.", "core", "Statamic");
  442. return Config::getPaginationVariable();
  443. }
  444. public static function get_pagination_style()
  445. {
  446. Log::warn("Use of Statamic::get_pagination_style() is deprecated. Use Config::getPaginationStyle() instead.", "core", "Statamic");
  447. return Config::getPaginationStyle();
  448. }
  449. public static function get_parse_order()
  450. {
  451. Log::warn("Use of Statamic::get_parse_order() is deprecated. Use Config::getParseOrder() instead.", "core", "Statamic");
  452. return Config::getParseOrder();
  453. }
  454. public static function is_content_writable()
  455. {
  456. return Folder::isWritable(Config::getContentRoot());
  457. }
  458. public static function are_users_writable()
  459. {
  460. return Folder::isWritable('_config/users/');
  461. }
  462. public static function get_content_meta($slug, $folder=NULL, $raw=FALSE, $parse=TRUE)
  463. {
  464. $app = \Slim\Slim::getInstance();
  465. $site_root = Config::getSiteRoot();
  466. $content_root = Config::getContentRoot();
  467. $content_type = Config::getContentType();
  468. $file = $folder ? "{$content_root}/{$folder}/{$slug}.{$content_type}" : "{$content_root}/{$slug}.{$content_type}";
  469. $file = Path::tidy($file);
  470. $meta_raw = File::exists($file) ? file_get_contents($file) : '';
  471. if (Pattern::endsWith($meta_raw, "---")) {
  472. $meta_raw .= "\n"; # prevent parse failure
  473. }
  474. # Parse YAML Front Matter
  475. if (strpos($meta_raw, "---") === FALSE) {
  476. $meta = self::loadYamlCached($meta_raw);
  477. if (is_array($meta)) {
  478. $meta = array_merge($meta, $app->config);
  479. }
  480. $meta['content'] = "";
  481. if ($raw) {
  482. $meta['content_raw'] = "";
  483. }
  484. } else {
  485. list($yaml, $content) = preg_split("/---/", $meta_raw, 2, PREG_SPLIT_NO_EMPTY);
  486. $meta = self::loadYamlCached($yaml);
  487. if ($raw) {
  488. $meta['content_raw'] = $content;
  489. }
  490. // Parse the content if necessary
  491. //$meta['content'] = $parse ? Content::parse($content, $meta) : $content;
  492. $meta['content'] = $content;
  493. }
  494. if (File::exists($file)) {
  495. $meta['last_modified'] = filemtime($file);
  496. }
  497. if (! $raw) {
  498. $meta['homepage'] = Config::getSiteRoot();
  499. $meta['raw_url'] = Request::getResourceURI();
  500. $meta['page_url'] = Request::getResourceURI();
  501. # Is date formatted correctly?
  502. if (Config::getEntryTimestamps() && Slug::isDateTime($slug)) {
  503. $datetimestamp = Slug::getTimestamp($slug);
  504. $datestamp = Slug::getTimestamp($slug);
  505. $meta['datetimestamp'] = $datetimestamp;
  506. $meta['datestamp'] = $datestamp;
  507. $meta['date'] = Date::format(Config::getDateFormat(), $datestamp);
  508. $meta['time'] = Date::format(Config::getTimeFormat(), $datetimestamp);
  509. $meta['page_url'] = preg_replace(Pattern::DATETIME, '', $meta['page_url']); # clean url override
  510. } elseif (Slug::isDate($slug)) {
  511. $datestamp = Slug::getTimestamp($slug);
  512. $meta['datestamp'] = $datestamp;
  513. $meta['date'] = Date::format(Config::getDateFormat(), $datestamp);
  514. $meta['page_url'] = preg_replace(Pattern::DATE, '', $meta['page_url']); # clean url override
  515. } elseif (Slug::isNumeric($slug)) {
  516. $meta['numeric'] = Slug::getOrderNumber($slug);
  517. }
  518. $meta['permalink'] = Path::tidy(Config::getSiteURL().'/'.$meta['page_url']);
  519. $taxonomy_slugify = (isset($app->config['_taxonomy_slugify']) && $app->config['_taxonomy_slugify']);
  520. # Jam it all together, brother.
  521. # @todo: functionize/abstract this method for more flexibility and readability
  522. foreach ($meta as $key => $value) {
  523. if (! is_array($value) && Taxonomy::isTaxonomy($key)) {
  524. $value = array($value);
  525. $meta[$key] = $value;
  526. }
  527. if (is_array($value)) {
  528. $list = array();
  529. $url_list = array();
  530. $i = 1;
  531. $total_results = count($meta[$key]);
  532. foreach ($meta[$key] as $k => $v) {
  533. $url = NULL;
  534. if (Taxonomy::isTaxonomy($key)) {
  535. // DO NOT DO numerical regex replace on the actual taxonomy item
  536. $url = Path::tidy(strtolower($site_root.'/'.$folder.'/'.$key));
  537. $url = preg_replace(Pattern::NUMERIC, '', $url);
  538. if ($taxonomy_slugify) {
  539. $url .= "/".(strtolower(Slug::make($v)));
  540. } else {
  541. $url .= "/".(strtolower($v));
  542. }
  543. $list[] = array(
  544. 'name' => $v,
  545. 'count' => $i,
  546. 'url' => $url,
  547. 'total_results' => $total_results,
  548. 'first' => $i == 1 ? TRUE : FALSE,
  549. 'last' => $i == $total_results ? TRUE : FALSE
  550. );
  551. $url_list[] = '<a href="'.$url.'">'.$v.'</a>';
  552. } elseif ( ! is_array($v)) {
  553. $list[] = array(
  554. 'name' => $v,
  555. 'count' => $i,
  556. 'url' => $url,
  557. 'total_results' => $total_results,
  558. 'first' => $i == 1 ? TRUE : FALSE,
  559. 'last' => $i == $total_results ? TRUE : FALSE
  560. );
  561. }
  562. // account for known structure
  563. // -
  564. // name: something
  565. // url: http://example.com
  566. if (is_array($v) && isset($v['name']) && isset($v['url'])) {
  567. $url_list[] = '<a href="'.$v['url'].'">'.$v['name'].'</a>';
  568. }
  569. $i++;
  570. }
  571. if (isset($url) || count($url_list)) {
  572. $meta[$key.'_url_list'] = implode(', ', $url_list);
  573. $meta[$key.'_spaced_url_list'] = join(" ", $url_list);
  574. $meta[$key.'_ordered_url_list'] = "<ol><li>" . join("</li><li>", $url_list) . "</li></ol>";
  575. $meta[$key.'_unordered_url_list'] = "<ul><li>" . join("</li><li>", $url_list) . "</li></ul>";
  576. $meta[$key.'_sentence_url_list'] = Helper::makeSentenceList($url_list);
  577. $meta[$key.'_ampersand_sentence_url_list'] = Helper::makeSentenceList($url_list, "&", false);
  578. }
  579. if (isset($meta[$key][0]) && ! is_array($meta[$key][0])) {
  580. $meta[$key.'_list'] = implode(', ', $meta[$key]);
  581. $meta[$key.'_option_list'] = implode('|', $meta[$key]);
  582. $meta[$key.'_spaced_list'] = implode(' ', $meta[$key]);
  583. $meta[$key.'_ordered_list'] = "<ol><li>" . join("</li><li>", $meta[$key]) . "</li></ol>";
  584. $meta[$key.'_unordered_list'] = "<ul><li>" . join("</li><li>", $meta[$key]) . "</li></ul>";
  585. $meta[$key.'_sentence_list'] = Helper::makeSentenceList($meta[$key]);
  586. $meta[$key.'_ampersand_sentence_list'] = Helper::makeSentenceList($meta[$key], "&", false);
  587. $meta[$key] = $list;
  588. }
  589. }
  590. }
  591. }
  592. return $meta;
  593. }
  594. public static function get_content_list($folder=NULL,$limit=NULL,$offset=0,$future=FALSE,$past=TRUE,$sort_by='date',$sort_dir='desc',$conditions=NULL,$switch=NULL,$skip_status=FALSE,$parse=TRUE,$since=NULL,$until=NULL,$location=NULL,$distance_from=NULL)
  595. {
  596. $folder_list = Helper::explodeOptions($folder);
  597. $list = array();
  598. foreach ($folder_list as $list_item) {
  599. $results = self::get_content_all($list_item, $future, $past, $conditions, $skip_status, $parse, $since, $until, $location, $distance_from);
  600. // if $location was set, filter out results that don't work
  601. if (!is_null($location)) {
  602. foreach ($results as $result => $variables) {
  603. try {
  604. foreach ($variables as $key => $value) {
  605. // checks for $location variables, and that it has a latitude and longitude within it
  606. if (strtolower($location) == strtolower($key)) {
  607. if (!is_array($value) || !isset($value['latitude']) || !$value['latitude'] || !isset($value['longitude']) || !$value['longitude']) {
  608. throw new Exception("nope");
  609. }
  610. }
  611. }
  612. } catch (Exception $e) {
  613. unset($results[$result]);
  614. }
  615. }
  616. }
  617. $list = $list+$results;
  618. }
  619. // default sort is by date
  620. if ($sort_by == 'date') {
  621. uasort($list, 'statamic_sort_by_datetime');
  622. } elseif ($sort_by == 'title') {
  623. uasort($list, "statamic_sort_by_title");
  624. } elseif ($sort_by == 'random') {
  625. shuffle($list);
  626. } elseif ($sort_by == 'numeric' || $sort_by == 'number') {
  627. ksort($list);
  628. } elseif ($sort_by == 'distance' && !is_null($location) && !is_null($distance_from) && preg_match(Pattern::COORDINATES, trim($distance_from))) {
  629. uasort($list, "statamic_sort_by_distance");
  630. } elseif ($sort_by != 'date') {
  631. # sort by any other field
  632. uasort($list, function($a, $b) use ($sort_by) {
  633. if (isset($a[$sort_by]) && isset($b[$sort_by])) {
  634. return strcmp($b[$sort_by], $a[$sort_by]);
  635. }
  636. });
  637. }
  638. // default sort is asc
  639. if ($sort_dir == 'desc') {
  640. $list = array_reverse($list);
  641. }
  642. // handle offset/limit
  643. if ($offset > 0) {
  644. $list = array_splice($list, $offset);
  645. }
  646. if ($limit) {
  647. $list = array_splice($list, 0, $limit);
  648. }
  649. if ($switch) {
  650. $switch_vars = explode('|',$switch);
  651. $switch_count = count($switch_vars);
  652. $count = 1;
  653. foreach ($list as $key => $post) {
  654. $list[$key]['switch'] = $switch_vars[($count -1) % $switch_count];
  655. $count++;
  656. }
  657. }
  658. return $list;
  659. }
  660. public static function fetch_content_by_url($path)
  661. {
  662. $data = NULL;
  663. $content_root = Config::getContentRoot();
  664. $content_type = Config::getContentType();
  665. if (File::exists("{$content_root}/{$path}.{$content_type}") || is_dir("{$content_root}/{$path}")) {
  666. // endpoint or folder exists!
  667. } else {
  668. $path = Path::resolve($path);
  669. }
  670. if (File::exists("{$content_root}/{$path}.{$content_type}")) {
  671. $page = basename($path);
  672. $folder = substr($path, 0, (-1*strlen($page))-1);
  673. $data = Statamic::get_content_meta($page, $folder);
  674. } elseif (Folder::exists("{$content_root}/{$path}")) {
  675. $data = Statamic::get_content_meta("page", $path);
  676. }
  677. return $data;
  678. }
  679. public static function get_next_numeric($folder=NULL)
  680. {
  681. $next = '01';
  682. $list = self::get_content_list($folder, NULL, 0, TRUE, TRUE, 'numeric', 'asc');
  683. if (sizeof($list) > 0) {
  684. $item = array_pop($list);
  685. $current = $item['numeric'];
  686. if ($current <> '') {
  687. $next = $current + 1;
  688. $format= '%1$0'.strlen($current).'d';
  689. $next = sprintf($format, $next);
  690. }
  691. }
  692. return $next;
  693. }
  694. public static function get_next_numeric_folder($folder=NULL)
  695. {
  696. $next = '01';
  697. $list = self::get_content_tree($folder,1,1,TRUE,FALSE,TRUE);
  698. if (sizeof($list) > 0) {
  699. $item = array_pop($list);
  700. if (isset($item['numeric'])) {
  701. $current = $item['numeric'];
  702. if ($current <> '') {
  703. $next = $current + 1;
  704. $format= '%1$0'.strlen($current).'d';
  705. $next = sprintf($format, $next);
  706. }
  707. }
  708. }
  709. return $next;
  710. }
  711. public static function get_content_count($folder=NULL,$future=FALSE,$past=TRUE,$conditions=NULL,$since=NULL,$until=NULL)
  712. {
  713. Log::warn("Use of Statamic::get_content_count() is deprecated. Use Content::count() instead.", "core", "Statamic");
  714. return Content::count($folder, $future, $past, $conditions, $since, $until);
  715. }
  716. public static function get_content_all($folder=NULL,$future=FALSE,$past=TRUE,$conditions=NULL,$skip_status=FALSE,$parse=TRUE,$since=NULL,$until=NULL,$location=NULL,$distance_from=NULL)
  717. {
  718. $content_type = Config::getContentType();
  719. $site_root = Config::getSiteRoot();
  720. $absolute_folder = Path::resolve($folder);
  721. $posts = self::get_file_list($absolute_folder);
  722. $list = array();
  723. // should we factor in location and distance?
  724. $measure_distance = (!is_null($location) && !is_null($distance_from) && preg_match(Pattern::COORDINATES, $distance_from, $matches));
  725. if ($measure_distance) {
  726. $center_point = array($matches[1], $matches[2]);
  727. }
  728. foreach ($posts as $key => $post) {
  729. // starts with numeric value
  730. unset($list[$key]);
  731. if ((preg_match(Pattern::DATE, $key) || preg_match(Pattern::NUMERIC, $key)) && File::exists($post.".{$content_type}")) {
  732. $data = Statamic::get_content_meta($key, $absolute_folder, FALSE, $parse);
  733. $list[$key] = $data;
  734. $list[$key]['url'] = $folder ? $site_root.$folder."/".$key : $site_root.$key;
  735. $list[$key]['raw_url'] = $list[$key]['url'];
  736. // Clean the folder numbers out
  737. $list[$key]['url'] = Path::clean($list[$key]['url']);
  738. # Set status and "raw" slug
  739. if (substr($key, 0, 2) === "__") {
  740. $list[$key]['status'] = 'draft';
  741. $list[$key]['slug'] = substr($key, 2);
  742. } elseif (substr($key, 0, 1) === "_") {
  743. $list[$key]['status'] = 'hidden';
  744. $list[$key]['slug'] = substr($key, 1);
  745. } else {
  746. $list[$key]['slug'] = $key;
  747. }
  748. $slug = $list[$key]['slug'];
  749. $date_entry = FALSE;
  750. if (Config::getEntryTimestamps() && Slug::isDateTime($slug)) {
  751. $datestamp = Slug::getTimestamp($key);
  752. $date_entry = TRUE;
  753. # strip the date
  754. $list[$key]['slug'] = preg_replace(Pattern::DATETIME, '', $slug);
  755. $list[$key]['url'] = preg_replace(Pattern::DATETIME, '', $list[$key]['url']); #override
  756. $list[$key]['datestamp'] = $data['datestamp'];
  757. $list[$key]['date'] = $data['date'];
  758. } elseif (Slug::isDate($slug)) {
  759. $datestamp = Slug::getTimestamp($slug);
  760. $date_entry = TRUE;
  761. # strip the date
  762. // $list[$key]['slug'] = substr($key, 11);
  763. $list[$key]['slug'] = preg_replace(Pattern::DATE, '', $slug);
  764. $list[$key]['url'] = preg_replace(Pattern::DATE, '', $list[$key]['url']); #override
  765. $list[$key]['datestamp'] = $data['datestamp'];
  766. $list[$key]['date'] = $data['date'];
  767. } else {
  768. $list[$key]['slug'] = preg_replace(Pattern::NUMERIC, '', $slug);
  769. $list[$key]['url'] = preg_replace(Pattern::NUMERIC, '', $list[$key]['url'], 1); #override
  770. }
  771. $list[$key]['url'] = Path::tidy('/'.$list[$key]['url']);
  772. # fully qualified url
  773. $list[$key]['permalink'] = Path::tidy(Config::getSiteURL().'/'.$list[$key]['url']);
  774. /* $content = preg_replace('/<img(.*)src="(.*?)"(.*)\/?>/', '<img \/1 src="'.Statamic::get_asset_path(null).'/\2" /\3 />', $data['content']); */
  775. //$list[$key]['content'] = Statamic::transform_content($data['content']);
  776. // distance
  777. if (isset($list[$key][$location]['latitude']) && $list[$key][$location]['latitude'] && isset($list[$key][$location]['longitude']) && $list[$key][$location]['longitude']) {
  778. $list[$key]['coordinates'] = $list[$key][$location]['latitude'] . "," . $list[$key][$location]['longitude'];
  779. }
  780. if ($measure_distance && is_array($center_point)) {
  781. if (!isset($list[$key][$location]) || !is_array($list[$key][$location])) {
  782. unset($list[$key]);
  783. }
  784. if (isset($list[$key][$location]['latitude']) && $list[$key][$location]['latitude'] && isset($list[$key][$location]['longitude']) && $list[$key][$location]['longitude']) {
  785. $list[$key]['distance_km'] = Statamic_Helper::get_distance_in_km($center_point, array($list[$key][$location]['latitude'], $list[$key][$location]['longitude']));
  786. $list[$key]['distance_mi'] = Statamic_Helper::convert_km_to_miles($list[$key]['distance_km']);
  787. } else {
  788. unset($list[$key]);
  789. }
  790. }
  791. if (! $skip_status) {
  792. if (isset($data['status']) && $data['status'] != 'live') {
  793. unset($list[$key]);
  794. }
  795. }
  796. // Remove future entries
  797. if ($date_entry && $future === FALSE && $datestamp > time()) {
  798. unset($list[$key]);
  799. }
  800. // Remove past entries
  801. if ($date_entry && $past === FALSE && $datestamp < time()) {
  802. unset($list[$key]);
  803. }
  804. // Remove entries before $since
  805. if ($date_entry && !is_null($since) && $datestamp < strtotime($since)) {
  806. unset($list[$key]);
  807. }
  808. // Remove entries after $until
  809. if ($date_entry && !is_null($until) && $datestamp > strtotime($until)) {
  810. unset($list[$key]);
  811. }
  812. if ($conditions) {
  813. $keepers = array();
  814. $conditions_array = explode(",", $conditions);
  815. foreach ($conditions_array as $condition) {
  816. $condition = trim($condition);
  817. $inclusive = TRUE;
  818. list($condition_key, $condition_values) = explode(":", $condition);
  819. # yay php!
  820. $pos = strpos($condition_values, 'not ');
  821. if ($pos === FALSE) {
  822. } else {
  823. if ($pos == 0) {
  824. $inclusive = FALSE;
  825. $condition_values = substr($condition_values, 4);
  826. }
  827. }
  828. $condition_values = explode("|", $condition_values);
  829. foreach ($condition_values as $k => $condition_value) {
  830. $keep = FALSE;
  831. if (isset($list[$key][$condition_key])) {
  832. if (is_array($list[$key][$condition_key])) {
  833. foreach ($list[$key][$condition_key] as $key2 => $value2) {
  834. #todo add regex driven taxonomy matching here
  835. if ($inclusive) {
  836. if (strtolower($value2['name']) == strtolower($condition_value)) {
  837. $keepers[$key] = $key;
  838. break;
  839. }
  840. } else {
  841. if (strtolower($value2['name']) != strtolower($condition_value)) {
  842. $keepers[$key] = $key;
  843. } else {
  844. // EXCLUDE!
  845. unset($keepers[$key]);
  846. break;
  847. }
  848. }
  849. }
  850. } else {
  851. if ($list[$key][$condition_key] == $condition_value) {
  852. if ($inclusive) {
  853. $keepers[$key] = $key;
  854. } else {
  855. unset($keepers[$key]);
  856. }
  857. } else {
  858. if (! $inclusive) {
  859. $keepers[$key] = $key;
  860. }
  861. }
  862. }
  863. } else {
  864. $keep = FALSE;
  865. }
  866. }
  867. if ( ! $keep && ! in_array($key, $keepers)) {
  868. unset($list[$key]);
  869. }
  870. }
  871. }
  872. }
  873. }
  874. return $list;
  875. }
  876. public static function get_content_tree($directory='/',$depth=1,$max_depth=5,$folders_only=FALSE,$include_entries=FALSE,$hide_hidden=TRUE,$include_content=FALSE,$site_root=FALSE)
  877. {
  878. // $folders_only = true only page.md
  879. // folders_only = false includes any numbered or non-numbered page (excluding anything with a fields.yaml file)
  880. // if include_entries is true then any numbered files are included
  881. $content_root = Config::getContentRoot();
  882. $content_type = Config::getContentType();
  883. $site_root = $site_root ? $site_root : Config::getSiteRoot();
  884. $current_url = Path::tidy($site_root.'/'.Request::getResourceURI());
  885. $taxonomy_url = FALSE;
  886. if (Taxonomy::isTaxonomyURL($current_url)) {
  887. list($taxonomy_type, $taxonomy_name) = Taxonomy::getCriteria($current_url);
  888. $taxonomy_url = self::remove_taxonomy_from_path($current_url, $taxonomy_type, $taxonomy_name);
  889. }
  890. $directory = '/'.$directory.'/'; #ensure proper slashing
  891. if ($directory <> '/') {
  892. $base = Path::tidy("{$content_root}/{$directory}");
  893. } elseif ($directory == '/') {
  894. $base = "{$content_root}";
  895. } else {
  896. $base = "{$content_root}";
  897. }
  898. $files = glob("{$base}/*");
  899. $data = array();
  900. if ($files) {
  901. foreach ($files as $path) {
  902. $current_name = basename($path);
  903. if (!Pattern::endsWith($current_name, '.yaml')) {
  904. // Hidden page that should be removed
  905. if ($hide_hidden && Pattern::startsWith($current_name, '_')) continue;
  906. $node = array();
  907. $file = substr($path, strlen($base)+1, strlen($path)-strlen($base)-strlen($content_type)-2);
  908. if (is_dir($path)) {
  909. $folder = substr($path, strlen($base)+1);
  910. $node['type'] = 'folder';
  911. $node['slug'] = basename($folder);
  912. $node['title'] = ucwords(basename($folder));
  913. $node['numeric'] = Slug::getOrderNumber($folder);
  914. $node['file_path'] = Path::tidy($site_root.'/'.$directory.'/'.$folder.'/page');
  915. if (Slug::isNumeric($folder)) {
  916. $pos = strpos($folder, ".");
  917. if ($pos !== FALSE) {
  918. $node['raw_url'] = Path::tidy(Path::clean($site_root.'/'.$directory.'/'.$folder));
  919. $node['url'] = Path::clean($node['raw_url']);
  920. $node['title'] = ucwords(basename(substr($folder, $pos+1)));
  921. } else {
  922. $node['title'] = ucwords(basename($folder));
  923. $node['raw_url'] = Path::tidy($site_root.'/'.$directory.'/'.$folder);
  924. $node['url'] = Path::clean($node['raw_url']);
  925. }
  926. } else {
  927. $node['title'] = ucwords(basename($folder));
  928. $node['raw_url'] = Path::tidy($site_root.'/'.$directory.'/'.$folder);
  929. $node['url'] = Path::clean($node['raw_url']);
  930. }
  931. $node['depth'] = $depth;
  932. $node['children'] = $depth < $max_depth ? self::get_content_tree($directory.$folder.'/', $depth+1, $max_depth, $folders_only, $include_entries, $hide_hidden, $include_content, $site_root) : NULL;
  933. $node['is_current'] = $node['raw_url'] == $current_url || $node['url'] == $current_url ? TRUE : FALSE;
  934. $node['is_parent'] = FALSE;
  935. if ($node['url'] == URL::popLastSegment($current_url) || ($taxonomy_url && $node['url'] == $taxonomy_url)) {
  936. $node['is_parent'] = TRUE;
  937. }
  938. $node['has_children'] = $node['children'] ? TRUE : FALSE;
  939. // has entries?
  940. if (File::exists(Path::tidy($path."/fields.yaml"))) {
  941. $node['has_entries'] = TRUE;
  942. } else {
  943. $node['has_entries'] = FALSE;
  944. }
  945. $meta = self::get_content_meta("page", Path::tidy($directory."/".$folder), FALSE, TRUE);
  946. //$meta = self::get_content_meta("page", Statamic_Helper::reduce_double_slashes($directory."/".$folder));
  947. if (isset($meta['title'])) {
  948. $node['title'] = $meta['title'];
  949. }
  950. if (isset($meta['last_modified'])) {
  951. $node['last_modified'] = $meta['last_modified'];
  952. }
  953. if ($hide_hidden === TRUE && (isset($meta['status']) && (($meta['status'] == 'hidden' || $meta['status'] == 'draft')))) {
  954. // placeholder condition
  955. } else {
  956. $data[] = $include_content ? array_merge($meta, $node) : $node;
  957. // print_r($data);
  958. }
  959. } else {
  960. if (Pattern::endsWith($path, $content_type)) {
  961. if ($folders_only == FALSE) {
  962. if ($file == 'page' || $file == 'feed' || $file == '404') {
  963. // $node['url'] = $directory;
  964. // $node['title'] = basename($directory);
  965. // $meta = self::get_content_meta('page', substr($directory, 1));
  966. // $node['depth'] = $depth;
  967. } else {
  968. $include = TRUE;
  969. // date based is never included
  970. if (Config::getEntryTimestamps() && Slug::isDateTime(basename($path))) {
  971. $include = FALSE;
  972. } elseif (Slug::isDate(basename($path))) {
  973. $include = FALSE;
  974. } elseif (Slug::isNumeric(basename($path))) {
  975. if ($include_entries == FALSE) {
  976. if (File::exists(Path::tidy(dirname($path)."/fields.yaml"))) {
  977. $include = FALSE;
  978. }
  979. }
  980. }
  981. if ($include) {
  982. $node['type'] = 'file';
  983. $node['raw_url'] = Path::tidy($directory).basename($path);
  984. $pretty_url = Path::clean($node['raw_url']);
  985. $node['url'] = substr($pretty_url, 0, -1*(strlen($content_type)+1));
  986. $node['is_current'] = $node['url'] == $current_url || $node['url'] == $current_url ? TRUE : FALSE;
  987. $node['slug'] = substr(basename($path), 0, -1*(strlen($content_type)+1));
  988. $meta = self::get_content_meta(substr(basename($path), 0, -1*(strlen($content_type)+1)), substr($directory, 1), FALSE, TRUE);
  989. //$node['meta'] = $meta;
  990. if (isset($meta['title'])) $node['title'] = $meta['title'];
  991. $node['depth'] = $depth;
  992. if ($hide_hidden === TRUE && (isset($meta['status']) && (($meta['status'] == 'hidden' || $meta['status'] == 'draft')))) {
  993. } else {
  994. $data[] = $include_content ? array_merge($meta, $node) : $node;
  995. }
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. }
  1004. return $data;
  1005. }
  1006. public static function get_listings()
  1007. {
  1008. $listings = array();
  1009. $finder = new Finder();
  1010. $files = $finder->files()
  1011. ->in(Config::getContentRoot())
  1012. ->name('fields.yaml')
  1013. ->followLinks();
  1014. foreach ($files as $file) {
  1015. $slug = str_replace(Config::getContentRoot().'/', '', $file->getPath());
  1016. $meta = array(
  1017. 'slug' => $slug,
  1018. 'title' => ucwords(Slug::humanize(Path::clean($slug)))
  1019. );
  1020. $item = self::yamlize_content(BASE_PATH . '/' . $file->getPath().'/page.' . Config::getContentType());
  1021. // if (is_array($item)) {
  1022. $listings[] = array_merge($meta, $item);
  1023. // }
  1024. }
  1025. // Sort by Title
  1026. uasort($listings, function($a, $b) {
  1027. return strcmp($a['title'], $b['title']);
  1028. });
  1029. return $listings;
  1030. }
  1031. public static function get_file_list($directory=NULL)
  1032. {
  1033. $content_root = Config::getContentRoot();
  1034. $content_type = Config::getContentType();
  1035. if ($directory) {
  1036. $files = glob("{$content_root}{$directory}/*.{$content_type}");
  1037. } else {
  1038. $files = glob('{$content_root}*.{$content_type}');
  1039. }
  1040. $posts = array();
  1041. if ($files) {
  1042. foreach ($files as $file) {
  1043. $len = strlen($content_type);
  1044. $len = $len + 1;
  1045. $len = $len * -1;
  1046. $key = substr(basename($file), 0, $len);
  1047. // Statamic_helper::reduce_double_slashes($key = '/'.$key);
  1048. $posts[$key] = substr($file, 0, $len);
  1049. }
  1050. }
  1051. return $posts;
  1052. }
  1053. public static function find_relative($current, $folder=null, $future=false, $past=true, $show_hidden=false)
  1054. {
  1055. $content_set = ContentService::getContentByFolders($folder);
  1056. $content_set->filter(array(
  1057. 'show_hidden' => $show_hidden,
  1058. 'show_drafts' => false,
  1059. 'show_future' => $future,
  1060. 'show_past' => $past,
  1061. 'type' => 'entries'
  1062. ));
  1063. $content_set->sort();
  1064. $content = $content_set->get(false, false);
  1065. $relative = array(
  1066. 'prev' => null,
  1067. 'next' => null
  1068. );
  1069. $use_next = false;
  1070. $prev = false;
  1071. foreach ($content as $data) {
  1072. // find previous
  1073. if (!$prev && $current != $data['url']) {
  1074. $relative['prev'] = $data['url'];
  1075. continue;
  1076. }
  1077. // we have found the current url
  1078. // set the currently-set previous url to be `prev`
  1079. // and mark the next iteration to use its value as `next`
  1080. if ($current == $data['url']) {
  1081. $prev = true;
  1082. $use_next = true;
  1083. continue;
  1084. }
  1085. // we should use this url as `next`
  1086. if ($use_next) {
  1087. $relative['next'] = $data['url'];
  1088. break;
  1089. }
  1090. }
  1091. return $relative;
  1092. }
  1093. public static function get_asset_path($asset)
  1094. {
  1095. $content_root = Config::getContentRoot();
  1096. return "{$content_root}".Request::getResourceURI().''.$asset;
  1097. }
  1098. public static function parse_content($template_data, $data, $type=NULL)
  1099. {
  1100. Log::warn("Use of Statamic::parse_content() is deprecated. Use Content::parse() instead.", "core", "Statamic");
  1101. return Content::parse($template_data, $data, $type);
  1102. }
  1103. public static function yamlize_content($meta_raw, $content_key = 'content')
  1104. {
  1105. if (File::exists($meta_raw)) {
  1106. $meta_raw = File::get($meta_raw);
  1107. }
  1108. if (Pattern::endsWith($meta_raw, "---")) {
  1109. $meta_raw .= "\n"; # prevent parse failure
  1110. }
  1111. // Parse YAML Front Matter
  1112. if (strpos($meta_raw, "---") === FALSE) {
  1113. $meta = YAML::parse($meta_raw);
  1114. $meta['content'] = "";
  1115. } else {
  1116. list($yaml, $content) = preg_split("/---/", $meta_raw, 2, PREG_SPLIT_NO_EMPTY);
  1117. $meta = YAML::parse($yaml);
  1118. $meta[$content_key.'_raw'] = trim($content);
  1119. $meta[$content_key] = Content::transform($content);
  1120. return $meta;
  1121. }
  1122. }
  1123. public static function transform_content($content, $content_type=NULL)
  1124. {
  1125. Log::warn("Use of Statamic::transform_content() is deprecated. Use Content::render() instead.", "core", "Statamic");
  1126. return Content::transform($content, $content_type);
  1127. }
  1128. public static function is_taxonomy($tax)
  1129. {
  1130. Log::warn("Use of Statamic::is_taxonomy() is deprecated. Use Taxonomy::isTaxonomy() instead.", "core", "Statamic");
  1131. return Taxonomy::isTaxonomy($tax);
  1132. }
  1133. public static function is_taxonomy_url($path)
  1134. {
  1135. Log::warn("Use of Statamic::is_taxonomy_url() is deprecated. Use Taxonomy::isTaxonomyURL() instead.", "core", "Statamic");
  1136. return Taxonomy::isTaxonomyURL($path);
  1137. }
  1138. public static function get_taxonomy_criteria($path)
  1139. {
  1140. Log::warn("Use of Statamic::get_taxonomy_criteria() is deprecated. Use Taxonomy::getCriteria() instead.", "core", "Statamic_Helper");
  1141. return Taxonomy::getCriteria($path);
  1142. }
  1143. public static function remove_taxonomy_from_path($path, $type, $slug)
  1144. {
  1145. return substr($path, 0, -1 * strlen("/{$type}/{$slug}"));
  1146. }
  1147. public static function detect_environment(array $environments, $uri)
  1148. {
  1149. Log::warn("Use of Statamic::detect_environment() is deprecated. Use Environment::detect() instead.", "core", "Statamic_Helper");
  1150. return Environment::detect();
  1151. }
  1152. }
  1153. function statamic_sort_by_title($a, $b)
  1154. {
  1155. return strcmp($a['title'], $b['title']);
  1156. }
  1157. function statamic_sort_by_field($field, $a, $b)
  1158. {
  1159. if (isset($a[$field]) && isset($b[$field])) {
  1160. return strcmp($a[$field], $b[$field]);
  1161. } else {
  1162. return strcmp($a['title'], $b['title']);
  1163. }
  1164. }
  1165. function statamic_sort_by_datetime($a, $b)
  1166. {
  1167. if (isset($a['datetimestamp']) && isset($b['datetimestamp'])) {
  1168. return $a['datetimestamp'] - $b['datetimestamp'];
  1169. } elseif (isset($a['datestamp']) && isset($b['datestamp'

Large files files are truncated, but you can click here to view the full file