PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/bridge/includes/shortcodes/shortcode-elements/latest-posts/latest-posts.php

https://bitbucket.org/tulsagraphics/themes
PHP | 330 lines | 282 code | 25 blank | 23 comment | 30 complexity | e949ea578d6a8bcdced798bd2ab4e55a MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, GPL-2.0
  1. <?php
  2. namespace Bridge\Shortcodes\LatestPosts;
  3. use Bridge\Shortcodes\Lib\ShortcodeInterface;
  4. /**
  5. * Class CardsGallery
  6. */
  7. class LatestPosts implements ShortcodeInterface {
  8. /**
  9. * @var string
  10. */
  11. private $base;
  12. /**
  13. * CardsGallery constructor.
  14. */
  15. public function __construct() {
  16. $this->base = 'latest_post';
  17. add_action('qode_vc_map', array($this, 'vcMap'));
  18. }
  19. /**
  20. * @return string
  21. */
  22. public function getBase() {
  23. return $this->base;
  24. }
  25. /**
  26. *
  27. */
  28. public function vcMap() {
  29. vc_map(array(
  30. "name" => "Latest Posts",
  31. "base" => $this->base,
  32. "icon" => "extended-custom-icon-qode icon-wpb-latest_post",
  33. "category" => 'by QODE',
  34. "allowed_container_element" => 'vc_row',
  35. "params" => array(
  36. array(
  37. "type" => "dropdown",
  38. "heading" => __("Type", 'qode'),
  39. "param_name" => "type",
  40. "value" => array(
  41. "Image on the left" => "image_in_box",
  42. "Image on the left - boxed" => "image_on_the_left_boxed",
  43. "Minimal" => "minimal",
  44. "Boxes" => "boxes",
  45. "Boxes With Dividers" => "dividers"
  46. ),
  47. 'save_always' => true,
  48. "admin_label" => true
  49. ),
  50. array(
  51. "type" => "dropdown",
  52. "heading" => "Set first post as featured",
  53. "value" => array(
  54. "No" => "no",
  55. "Yes" => "yes"
  56. ),
  57. "param_name" => "firts_as_featured",
  58. "dependency" => Array('element' => "type", 'value' => array('image_on_the_left_boxed'))
  59. ),
  60. array(
  61. "type" => "textfield",
  62. "heading" => "Number of Posts",
  63. "param_name" => "number_of_posts",
  64. "admin_label" => true,
  65. "dependency" => Array('element' => "type", 'value' => array('date_in_box', 'image_in_box', 'minimal', 'image_on_the_left_boxed'))
  66. ),
  67. array(
  68. "type" => "dropdown",
  69. "heading" => "Number of Colums",
  70. "param_name" => "number_of_colums",
  71. "value" => array(
  72. "Two" => "2",
  73. "Three" => "3",
  74. "Four" => "4"
  75. ),
  76. 'save_always' => true,
  77. "admin_label" => true,
  78. "dependency" => Array('element' => "type", 'value' => array('boxes','dividers'))
  79. ),
  80. array(
  81. "type" => "dropdown",
  82. "heading" => "Number of Rows",
  83. "param_name" => "number_of_rows",
  84. "value" => array(
  85. "One" => "1",
  86. "Two" => "2",
  87. "Three" => "3",
  88. "Four" => "4",
  89. "Five" => "5"
  90. ),
  91. 'save_always' => true,
  92. "description" => "",
  93. "dependency" => Array('element' => "type", 'value' => array('boxes','dividers'))
  94. ),
  95. array(
  96. "type" => "dropdown",
  97. "heading" => "Text from edge",
  98. "param_name" => "text_from_edge",
  99. "value" => array(
  100. "Default" => "",
  101. "No" => "no",
  102. "Yes" => "yes"
  103. ),
  104. "description" => "",
  105. "dependency" => Array('element' => "type", 'value' => array('boxes'))
  106. ),
  107. array(
  108. "type" => "dropdown",
  109. "heading" => "Order By",
  110. "param_name" => "order_by",
  111. "value" => array(
  112. "Title" => "title",
  113. "Date" => "date"
  114. ),
  115. 'save_always' => true,
  116. "admin_label" => true
  117. ),
  118. array(
  119. "type" => "dropdown",
  120. "heading" => "Order",
  121. "param_name" => "order",
  122. "value" => array(
  123. "ASC" => "ASC",
  124. "DESC" => "DESC"
  125. ),
  126. 'save_always' => true,
  127. "admin_label" => true
  128. ),
  129. array(
  130. "type" => "textfield",
  131. "heading" => "Category Slug",
  132. "param_name" => "category",
  133. "description" => "Leave empty for all or use comma for list",
  134. "admin_label" => true
  135. ),
  136. array(
  137. "type" => "textfield",
  138. "heading" => "Text length",
  139. "param_name" => "text_length",
  140. "description" => "Number of characters"
  141. ),
  142. array(
  143. "type" => "dropdown",
  144. "heading" => "Title Tag",
  145. "param_name" => "title_tag",
  146. "value" => array(
  147. "" => "",
  148. "h2" => "h2",
  149. "h3" => "h3",
  150. "h4" => "h4",
  151. "h5" => "h5",
  152. "h6" => "h6",
  153. )
  154. ),
  155. array(
  156. "type" => "dropdown",
  157. "heading" => "Display category",
  158. "param_name" => "display_category",
  159. "value" => array(
  160. "Default" => "",
  161. "Yes" => "1",
  162. "No" => "0"
  163. )
  164. ),
  165. array(
  166. "type" => "dropdown",
  167. "heading" => "Display date",
  168. "param_name" => "display_time",
  169. "value" => array(
  170. "Default" => "",
  171. "Yes" => "1",
  172. "No" => "0"
  173. ),
  174. "dependency" => array('element' => 'type', 'value' => array("image_in_box","boxes","minimal","image_on_the_left_boxed"))
  175. ),
  176. array(
  177. "type" => "dropdown",
  178. "heading" => "Display comments",
  179. "param_name" => "display_comments",
  180. "value" => array(
  181. "Default" => "",
  182. "Yes" => "1",
  183. "No" => "0"
  184. )
  185. ),
  186. array(
  187. "type" => "dropdown",
  188. "heading" => "Display like",
  189. "param_name" => "display_like",
  190. "value" => array(
  191. "Default" => "",
  192. "Yes" => "1",
  193. "No" => "0"
  194. )
  195. ),
  196. array(
  197. "type" => "dropdown",
  198. "heading" => "Display share",
  199. "param_name" => "display_share",
  200. "value" => array(
  201. "Default" => "",
  202. "Yes" => "1",
  203. "No" => "0"
  204. )
  205. )
  206. )
  207. ));
  208. }
  209. /**
  210. * @param array $atts
  211. * @param null $content
  212. *
  213. * @return string
  214. */
  215. public function render($atts, $content = null) {
  216. $args = array(
  217. "type" => "date_in_box",
  218. "number_of_posts" => "",
  219. "number_of_colums" => "",
  220. "number_of_rows" => "1",
  221. "text_from_edge" => "",
  222. "rows" => "",
  223. "order_by" => "",
  224. "order" => "",
  225. "category" => "",
  226. "text_length" => "",
  227. "title_tag" => "h5",
  228. "display_category" => "0",
  229. "display_time" => "1",
  230. "display_comments" => "1",
  231. "display_like" => "0",
  232. "display_share" => "0",
  233. "firts_as_featured" => "no",
  234. );
  235. $params = shortcode_atts($args, $atts);
  236. $params['this_object'] = $this;
  237. $params['blog_hide_comments'] = "";
  238. if (isset($qode_options_proya['blog_hide_comments'])) {
  239. $params['blog_hide_comments'] = qode_options()->getOptionValue('blog_hide_comments');
  240. }
  241. $params['qode_like'] = "on";
  242. if (isset($qode_options_proya['qode_like'])) {
  243. $params['qode_like'] = qode_options()->getOptionValue('qode_like');
  244. }
  245. $headings_array = array('h2', 'h3', 'h4', 'h5', 'h6');
  246. //get correct heading value. If provided heading isn't valid get the default one
  247. $params['title_tag'] = (in_array($params['title_tag'], $headings_array)) ? $params['title_tag'] : $args['title_tag'];
  248. if($params['type'] != "boxes" && $params['type'] != "dividers"){
  249. $params['q'] = new \WP_Query(
  250. array('orderby' => $params['order_by'], 'order' => $params['order'], 'posts_per_page' => $params['number_of_posts'], 'category_name' => $params['category'])
  251. );
  252. } else {
  253. $params['q'] = new \WP_Query(
  254. array('orderby' => $params['order_by'], 'order' => $params['order'], 'posts_per_page' => $params['number_of_colums']*$params['number_of_rows'], 'category_name' => $params['category'])
  255. );
  256. }
  257. $params['columns_number'] = "";
  258. if($params['type'] == 'boxes' || $params['type'] == 'dividers') {
  259. if($params['number_of_colums'] == 2){
  260. $params['columns_number'] = "two_columns";
  261. } else if ($params['number_of_colums'] == 3) {
  262. $params['columns_number'] = "three_columns";
  263. } else if ($params['number_of_colums'] == 4) {
  264. $params['columns_number'] = "four_columns";
  265. }
  266. }
  267. //get number of rows class for boxes type
  268. $params['rows_number'] = "";
  269. if($params['type'] == 'boxes' || $params['type'] == 'dividers') {
  270. switch($params['number_of_rows']) {
  271. case 1:
  272. $params['rows_number'] = 'one_row';
  273. break;
  274. case 2:
  275. $params['rows_number'] = 'two_rows';
  276. break;
  277. case 3:
  278. $params['rows_number'] = 'three_rows';
  279. break;
  280. case 4:
  281. $params['rows_number'] = 'four_rows';
  282. break;
  283. case 5:
  284. $params['rows_number'] = 'five_rows';
  285. break;
  286. default:
  287. break;
  288. }
  289. }
  290. $params['padding_latest_post'] = "";
  291. if($params['text_from_edge'] == "yes" && $params['type'] == "boxes"){
  292. $params['padding_latest_post'] = " style=' padding-left:0; padding-right:0; '";
  293. }
  294. return qode_get_shortcode_template_part('templates/latest-posts', 'latest-posts', $params['type'], $params);
  295. }
  296. public function getExcerpt($id, $text_length){
  297. $excerpt = '';
  298. if($text_length !== '0') {
  299. $excerpt .= '<p itemprop="description" class="excerpt">';
  300. $excerpt .= $text_length > 0 ? mb_substr(get_the_excerpt(), 0, intval($text_length)) : get_the_excerpt($id);
  301. $excerpt .= '...</p>';
  302. }
  303. return $excerpt;
  304. }
  305. }