PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/bridge/plugins/layersliderwp-5.3.2.installable/LayerSlider/classes/class.ls.posts.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 261 lines | 169 code | 50 blank | 42 comment | 46 complexity | 7a4eaa34fde2e9f83bd5da3d34cb2907 MD5 | raw file
  1. <?php
  2. class LS_Posts {
  3. // Stores the last query results
  4. public $post = null;
  5. public $posts = null;
  6. /**
  7. * Returns posts that matches the query params
  8. * @param array $args Array of WP_Query attributes
  9. * @return bool Success of the query
  10. */
  11. public static function find($args = array()) {
  12. // Crate new instance
  13. $instance = new self;
  14. if($instance->posts = get_posts($args)) {
  15. $instance->post = $instance->posts[0];
  16. }
  17. return $instance;
  18. }
  19. public static function getPostTypes() {
  20. // Get post types
  21. $postTypes = get_post_types();
  22. // Remove some defalt post types
  23. if(isset($postTypes['revision'])) { unset($postTypes['revision']); }
  24. if(isset($postTypes['nav_menu_item'])) { unset($postTypes['nav_menu_item']); }
  25. // Convert names to plural
  26. foreach($postTypes as $key => $item) {
  27. if(!empty($item)) {
  28. $postTypes[$key] = array();
  29. $postTypes[$key]['slug'] = $item;
  30. $postTypes[$key]['obj'] = get_post_type_object($item);
  31. $postTypes[$key]['name'] = $postTypes[$key]['obj']->labels->name;
  32. }
  33. }
  34. return $postTypes;
  35. }
  36. public function getParsedObject() {
  37. if(!$this->posts) {
  38. return array();
  39. }
  40. foreach($this->posts as $key => $val) {
  41. $ret[$key]['post-id'] = $val->ID;
  42. $ret[$key]['post-slug'] = $val->post_name;
  43. $ret[$key]['post-url'] = get_permalink($val->ID);
  44. $ret[$key]['date-published'] = date(get_option('date_format'), strtotime($val->post_date));
  45. $ret[$key]['date-modified'] = date(get_option('date_format'), strtotime($val->post_modified));
  46. $ret[$key]['thumbnail'] = $this->getPostThumb($val->ID);
  47. $ret[$key]['thumbnail'] = !empty($ret[$key]['thumbnail']) ? $ret[$key]['thumbnail'] : LS_ROOT_URL . '/static/img/blank.gif';
  48. $ret[$key]['image'] = '<img src="'.$ret[$key]['thumbnail'].'" alt="">';
  49. $ret[$key]['image-url'] = $ret[$key]['thumbnail'];
  50. $ret[$key]['title'] = htmlentities(__($val->post_title));
  51. $ret[$key]['content'] = wp_strip_all_tags(__($val->post_content));
  52. $ret[$key]['excerpt'] = !empty($val->post_excerpt) ? $val->post_excerpt : '';
  53. $ret[$key]['author'] = get_userdata($val->post_author)->user_nicename;
  54. $ret[$key]['author-id'] = $val->post_author;
  55. $ret[$key]['categories'] = $this->getCategoryList($val);
  56. $ret[$key]['tags'] = $this->getTagList($val);
  57. $ret[$key]['comments'] = $val->comment_count;
  58. }
  59. return $ret;
  60. }
  61. public function getWithFormat($str, $textlength = 0) {
  62. if(!is_object($this->post)) {
  63. return $str;
  64. }
  65. // Post ID
  66. if(stripos($str, '[post-id]') !== false) {
  67. $str = str_replace('[post-id]', $this->post->ID, $str); }
  68. // Post slug
  69. if(stripos($str, '[post-slug]') !== false) {
  70. $str = str_replace('[post-slug]', $this->post->post_name, $str); }
  71. // Post URL
  72. if(stripos($str, '[post-url]') !== false) {
  73. $str = str_replace('[post-url]', get_permalink($this->post->ID), $str);
  74. }
  75. // Date published
  76. if(stripos($str, '[date-published]') !== false) {
  77. $str = str_replace('[date-published]', date(get_option('date_format'), strtotime($this->post->post_date)), $str); }
  78. // Date modified
  79. if(stripos($str, '[date-modified]') !== false) {
  80. $str = str_replace('%date-modified]', date(get_option('date_format'), strtotime($this->post->post_modified)), $str); }
  81. // Featured image
  82. if(stripos($str, '[image]') !== false) {
  83. if(has_post_thumbnail($this->post->ID)) {
  84. $src = $this->getPostThumb($this->post->ID);
  85. if(!empty($src)){
  86. $str = str_replace('[image]', '<img src="'.$src.'" />', $str);
  87. }
  88. }
  89. }
  90. // Featured image URL
  91. if(stripos($str, '[image-url]') !== false) {
  92. if(has_post_thumbnail($this->post->ID)) {
  93. $src = $this->getPostThumb($this->post->ID);
  94. if(!empty($src)){
  95. $str = str_replace('[image-url]', $src, $str);
  96. }
  97. }
  98. }
  99. // Title
  100. if(stripos($str, '[title]') !== false) {
  101. if(!empty($textlength)) {
  102. $str = str_replace('[title]', substr($this->getTitle(), 0, $textlength), $str);
  103. } else {
  104. $str = str_replace('[title]', $this->getTitle(), $str);
  105. }
  106. }
  107. // Content
  108. if(stripos($str, '[content]') !== false) {
  109. $str = str_replace('[content]', $this->getContent($textlength), $str); }
  110. // Excerpt
  111. if(stripos($str, '[excerpt]') !== false) {
  112. if(empty($this->post->post_excerpt)) { return ''; }
  113. if(!empty($textlength)) {
  114. $str = str_replace('[excerpt]', substr($this->post->post_excerpt, 0, $textlength), $str);
  115. } else {
  116. $str = str_replace('[excerpt]', $this->post->post_excerpt, $str);
  117. }
  118. }
  119. // Author
  120. if(stripos($str, '[author]') !== false) {
  121. $str = str_replace('[author]', $this->getAuthor(), $str); }
  122. // Author ID
  123. if(stripos($str, '[author-id]') !== false) {
  124. $str = str_replace('[author-id]', $this->post->post_author, $str); }
  125. // Category list
  126. if(stripos($str, '[categories]') !== false) {
  127. $str = str_replace('[categories]', $this->getCategoryList(), $str);
  128. }
  129. // Tags list
  130. if(stripos($str, '[tags]') !== false) {
  131. $str = str_replace('[tags]', $this->getTagList(), $str);
  132. }
  133. // Number of comments
  134. if(stripos($str, '[comments]') !== false) {
  135. $str = str_replace('[comments]', $this->post->comment_count, $str); }
  136. // Meta
  137. if(stripos($str, '[meta:') !== false) {
  138. $matches = array();
  139. preg_match_all('/\[meta:\w+\]/', $str, $matches);
  140. foreach($matches[0] as $match) {
  141. $meta = str_replace('[meta:', '', $match);
  142. $meta = str_replace(']', '', $meta);
  143. $meta = get_post_meta($this->post->ID, $meta, true);
  144. $str = str_replace($match, $meta, $str);
  145. }
  146. }
  147. return $str;
  148. }
  149. /**
  150. * Returns the lastly selected post's title
  151. * @return string The title of the post
  152. */
  153. public function getTitle() {
  154. if(is_object($this->post)) { return __($this->post->post_title); }
  155. else { return false; }
  156. }
  157. public function getAuthor() {
  158. if(is_object($this->post)) { return get_userdata($this->post->post_author)->user_nicename; }
  159. else { return false; }
  160. }
  161. public function getCategoryList($post = null) {
  162. if(!empty($post)) { $post = $this->post; }
  163. if(has_category(false, $this->post->ID)) {
  164. $cats = wp_get_post_categories($this->post->ID);
  165. foreach($cats as $val) {
  166. $cat = get_category($val);
  167. $list[] = '<a href="/category/'.$cat->slug.'/">'.$cat->name.'</a>';
  168. }
  169. return '<div>'.implode(', ', $list).'</div>';
  170. } else {
  171. return '';
  172. }
  173. }
  174. public function getTagList($post = null) {
  175. if(!empty($post)) { $post = $this->post; }
  176. if(has_tag(false, $this->post->ID)) {
  177. $tags = wp_get_post_tags($this->post->ID);
  178. foreach($tags as $val) {
  179. $list[] = '<a href="/tag/'.$val->slug.'/">'.$val->name.'</a>';
  180. }
  181. return '<div>'.implode(', ', $list).'</div>';
  182. } else {
  183. return '';
  184. }
  185. }
  186. /**
  187. * Returns a subset of the post's content,
  188. * or the first paragraph if isn't specified
  189. * @param integer $length The subset's length
  190. * @return string The content
  191. */
  192. public function getContent($length = false) {
  193. if(!is_object($this->post)) { return false; }
  194. if(empty($length)) {
  195. return wp_strip_all_tags(__($this->post->post_content));
  196. } else {
  197. return substr(wp_strip_all_tags(__($this->post->post_content)), 0, $length);
  198. }
  199. }
  200. /**
  201. * Returns the attachment ID of
  202. * featured image in a post
  203. * @param integer $postID The ID of the post
  204. * @return string The ID of the post, or an empty string on failure.
  205. */
  206. public function getPostThumb($postID = 0) {
  207. if(function_exists('get_post_thumbnail_id') && function_exists('wp_get_attachment_url')) {
  208. return wp_get_attachment_url(get_post_thumbnail_id($postID));
  209. }
  210. }
  211. }