PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/_sohy/classes/posts.class.php

https://github.com/ampersarnie/Sohy
PHP | 493 lines | 256 code | 129 blank | 108 comment | 56 complexity | 693d398c933826948ffd64e523135ffa MD5 | raw file
  1. <?php
  2. /*
  3. ======================
  4. == Sohy Posts Class ==
  5. ======================
  6. */
  7. if(!defined('INDEX')) die();
  8. class posts {
  9. public function get_posts($post_limit = NULL) {
  10. global $sohy;
  11. if(!is_null($post_limit)) {
  12. $this->limit = $post_limit;
  13. }
  14. if($this->limit > 0) {
  15. $this->list = $this->get_start($sohy->base->posts);
  16. }
  17. $output = array();
  18. for($i=0; $i < $this->limit; $i++) {
  19. if(!isset($this->list[$i])) continue;
  20. if($this->get_index() == $this->list[$i]) {
  21. unset($this->list[$i]);
  22. continue;
  23. }
  24. $output[$i] = $this->list[$i];
  25. }
  26. return $output;
  27. }
  28. /*
  29. collect_posts()
  30. ===============
  31. */
  32. public function collect_posts($directory = 'content') {
  33. global $sohy;
  34. $files = glob($directory.'/*.{'.$sohy->file_types.'}', GLOB_BRACE);
  35. $dirs = glob($directory.'/*', GLOB_ONLYDIR);
  36. sort($files);
  37. $posts = array();
  38. if(isset($dirs[0])) {
  39. foreach($dirs as $key => $dir) {
  40. $posts[$key] = $this->collect_posts($dir);
  41. }
  42. }
  43. $posts = array_merge($posts, $files);
  44. $posts = array_reverse($posts);
  45. return $posts;
  46. }
  47. public function get_start($posts) {
  48. global $sohy;
  49. if(isset($sohy->page)) {
  50. $page = $sohy->page-1;
  51. } else {
  52. $page = 0;
  53. }
  54. sort($posts);
  55. $this->all = array_reverse($posts);
  56. if(count($this->all) == 0) return array();
  57. if($this->get_index() == $this->all[0]) {
  58. unset($this->all[0]);
  59. array_merge($this->all);
  60. }
  61. for($i=0; $i <= count($this->all); $i++) {
  62. if(!isset($this->all[$i])) continue;
  63. $this->all[$i] = $this->process_data($this->all[$i], $i);
  64. if(!isset($this->all[$i]->date)) {
  65. unset($this->all[$i]);
  66. }
  67. }
  68. if($this->limit > 0) {
  69. $this->all = array_chunk($this->all, $this->limit);
  70. }
  71. $sohy->page_count = count($this->all);
  72. if($this->limit > 0 && !empty($this->all)) {
  73. return $this->all[$page];
  74. }
  75. return $this->all;
  76. }
  77. /*
  78. process_data()
  79. ============
  80. Processing point before content is returned.
  81. Parses and reformats any date, url and content
  82. to the desired formats.
  83. Arguments:
  84. $file - File to be parsed and reformatted.
  85. */
  86. private function process_data($file, $key = 0) {
  87. global $sohy;
  88. if(empty($file)) return;
  89. $the_file = explode('/', $file);
  90. $the_file = end($the_file);
  91. $post = $sohy->load_file($file);
  92. if(!isset($post['date'])) $post['date'] = '';
  93. if($this->is_future_post($post['date'])) return false;
  94. $post['title'] = $this->create_post_title(@$post['title'], $file);
  95. $post['file'] = $file;
  96. $post['url'] = $this->create_url($file);
  97. $post['key'] = $key;
  98. if(!empty($post['content'])) {
  99. $post['rawcontent'] = $post['content'];
  100. $post['content'] = $post['content'];
  101. } else {
  102. $post['rawcontent'] = NULL;
  103. $post['content'] = NULL;
  104. }
  105. $post['date'] = $this->create_post_date($post['date']);
  106. if(isset($post['meta'])) {
  107. if($sohy->is_post() || $sohy->is_cat()) {
  108. $sohy->get_meta($post['meta']);
  109. }
  110. }
  111. $post['position']['first'] = NULL;
  112. $post['position']['last'] = NULL;
  113. $post = arrToObj($post);
  114. return $post;
  115. }
  116. /*
  117. create_post_date()
  118. ============
  119. Formats the date.
  120. Arguments:
  121. $date - Data to be processed.
  122. */
  123. private function create_post_date($date = NULL) {
  124. global $sohy;
  125. if(!is_null($date)) {
  126. return the_date($date,$sohy->date_format);
  127. } else {
  128. return false;
  129. }
  130. }
  131. /*
  132. is_future_post()
  133. ================
  134. Checks if the post date is for a
  135. future post.
  136. Arguments:
  137. $date - Date to be checked.
  138. */
  139. public function is_future_post($date) {
  140. if(time() < strtotime($date)) return true;
  141. return false;
  142. }
  143. /*
  144. create_post_title()
  145. ===================
  146. */
  147. public function create_post_title($title, $file) {
  148. global $sohy;
  149. if(!isset($title)) {
  150. $slug = $sohy->file_slug($file);
  151. $slug = str_replace('-', ' ', $slug);
  152. $title = ucwords($slug);
  153. }
  154. return $title;
  155. }
  156. /*
  157. post()
  158. ======
  159. Find and return a single post.
  160. Arguments:
  161. $file - A specific file to return.
  162. */
  163. public function post($file = NULL) {
  164. global $sohy;
  165. if(is_null($file)) {
  166. return $this->process_data($sohy->check_file());
  167. } else {
  168. return $this->process_data($file);
  169. }
  170. }
  171. /*
  172. CATEGORY FUNCTIONS
  173. */
  174. /*
  175. has_categories()
  176. ================
  177. */
  178. public function has_categories($base_cat = NULL) {
  179. global $sohy;
  180. $categories = array();
  181. if(is_null($base_cat)) {
  182. $base_cat = $sohy->true_base->categories;
  183. } elseif($base_cat == 'current') {
  184. $base_cat = glob($sohy->base->dir.$sohy->check_file().'/*', GLOB_ONLYDIR);
  185. } elseif($base_cat == 'parent') {
  186. $category = $sohy->base->dir.$sohy->check_file();
  187. $category = strrev($category);
  188. $dirs = explode('/', $category, 2);
  189. $base_cat = strrev($dirs[1]);
  190. $base_cat = glob($base_cat.'/*', GLOB_ONLYDIR);
  191. }
  192. if(count($base_cat) > 0) {
  193. return $base_cat;
  194. }
  195. return array();
  196. }
  197. /*
  198. categories()
  199. ============
  200. */
  201. public function categories($base_cat = NULL) {
  202. global $sohy;
  203. $categories = array();
  204. $base_cat = $this->has_categories($base_cat);
  205. foreach($base_cat as $category) {
  206. $file = glob($category.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
  207. if(!isset($file[0])) continue;
  208. $index = $sohy->load_file($file[0]);
  209. $index['url'] = $this->create_url($file[0]);
  210. $categories[] = $index;
  211. }
  212. return arrToObj($categories);
  213. }
  214. /*
  215. category()
  216. ==========
  217. */
  218. public function category() {
  219. global $sohy;
  220. $index = $this->get_index();
  221. if(!$index) {
  222. $file['title'] = ucwords($sohy->url->params[1]);
  223. $file['content'] = NULL;
  224. } else {
  225. $file = $sohy->load_file($index);
  226. $file['url'] = $this->create_url($index);
  227. $file['slug'] = $this->create_slug($index);
  228. $file['file'] = $index;
  229. $file['parent'] = $this->get_category_parent();
  230. }
  231. return arrToObj($file);
  232. }
  233. /*
  234. get_category_parent()
  235. =====================
  236. */
  237. public function get_category_parent() {
  238. global $sohy;
  239. $category = $sohy->base->dir.$sohy->check_file();
  240. $category = strrev($category);
  241. $dirs = explode('/', $category, 2);
  242. $parent_file = strrev($dirs[1]);
  243. $parent_file = glob($parent_file.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
  244. if(isset($parent_file[0])) {
  245. $parent = $sohy->load_file($parent_file[0]);
  246. $parent['url'] = $this->create_url($parent_file[0]);
  247. return $parent;
  248. }
  249. }
  250. /*
  251. GENERAL POST/CATEGORY FUNCTIONS
  252. */
  253. /*
  254. create_url()
  255. ============
  256. Creates the URL for the post.
  257. Arguments:
  258. $file - string - file path to create URL from.
  259. */
  260. public function create_url($file = NULL) {
  261. global $sohy;
  262. if(is_null($file)) {
  263. return;
  264. }
  265. $file_parts = $this->file_parts($file);
  266. $url = $sohy->url->home.'';
  267. if(!$sohy->prettylinks) {
  268. $url .= '/index.php?post=';
  269. }
  270. foreach($file_parts as $file) {
  271. $string = explode('.', $file, 2);
  272. $url .= '/'.$string[1];
  273. }
  274. return $url;
  275. }
  276. /*
  277. create_slug()
  278. ============
  279. Creates the slug for the post.
  280. Arguments:
  281. $file - string - file path to create URL from.
  282. */
  283. public function create_slug($file = NULL) {
  284. global $sohy;
  285. if(is_null($file)) {
  286. return;
  287. }
  288. $file_parts = $this->file_parts($file);
  289. $slug = end($file_parts);
  290. $slug = explode('.', $slug, 2);
  291. $slug = str_ireplace(array('_','.'), '-', $slug);
  292. return $slug[1];
  293. }
  294. /*
  295. file_parts()
  296. ============
  297. Arguments:
  298. $file - string - splits up file string to allow
  299. for use with create_url() and
  300. create_slug()
  301. */
  302. private function file_parts($file = NULL) {
  303. $file = str_replace('content/', '', $file);
  304. if(count(explode('/', $file)) > 0) {
  305. $file = strrev($file);
  306. $file = explode('.', $file, 2);
  307. $file = strrev($file[1]);
  308. }
  309. $file = str_replace('/index', '', $file);
  310. $file_parts = explode('/', $file);
  311. return $file_parts;
  312. }
  313. /*
  314. get_index()
  315. ===========
  316. Checks if index exists and returns file if true.
  317. Arguments:
  318. $dir - string - directory of file.
  319. */
  320. public function get_index($dir = NULL) {
  321. global $sohy;
  322. if(is_null($dir)) {
  323. $file = glob($sohy->check_file().'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
  324. if(isset($file[0])) return $file[0];
  325. }
  326. if(empty($file)) {
  327. return;
  328. }
  329. if($sohy->check_file() == $file) {
  330. $file = glob($dir.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
  331. if(isset($file[0])) return $file[0];
  332. }
  333. return;
  334. }
  335. /*
  336. post_stylesheet();
  337. ==================
  338. Checks if current post has a stylesheet.
  339. */
  340. public function post_stylesheet() {
  341. global $sohy;
  342. if(!$sohy->is_post()) {
  343. return false;
  344. }
  345. if(isset($this->post()->stylesheet)) {
  346. return false;
  347. }
  348. $file = str_ireplace(array('.md', '.markdown', '.yml', '.yaml'), '.css', $this->post()->file);
  349. if(file_exists($file)) {
  350. return $file;
  351. } else {
  352. return false;
  353. }
  354. }
  355. }
  356. ?>