Advertisement
Guest User

blog.php

a guest
Jan 13th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.88 KB | None | 0 0
  1. <?php
  2. /**
  3. * Sidebar
  4. * Displays one of the registered Widget Areas of the theme
  5. */
  6.  
  7. if ( !class_exists( 'avia_sc_blog' ) )
  8. {
  9. class avia_sc_blog extends aviaShortcodeTemplate
  10. {
  11. /**
  12. * Create the config array for the shortcode button
  13. */
  14. function shortcode_insert_button()
  15. {
  16. $this->config['name'] = __('Blog Posts', 'avia_framework' );
  17. $this->config['tab'] = __('Content Elements', 'avia_framework' );
  18. $this->config['icon'] = AviaBuilder::$path['imagesURL']."sc-blog.png";
  19. $this->config['order'] = 40;
  20. $this->config['target'] = 'avia-target-insert';
  21. $this->config['shortcode'] = 'av_blog';
  22. $this->config['tooltip'] = __('Displays Posts from your Blog', 'avia_framework' );
  23. }
  24.  
  25. /**
  26. * Popup Elements
  27. *
  28. * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  29. * opens a modal window that allows to edit the element properties
  30. *
  31. * @return void
  32. */
  33. function popup_elements()
  34. {
  35. $this->elements = array(
  36.  
  37. array( "name" => __("Do you want to display blog posts?", 'avia_framework' ),
  38. "desc" => __("Do you want to display blog posts or entries from a custom taxonomy?", 'avia_framework' ),
  39. "id" => "blog_type",
  40. "type" => "select",
  41. "std" => "posts",
  42. "subtype" => array( __('Display blog posts', 'avia_framework') =>'posts',
  43. __('Display entries from a custom taxonomy', 'avia_framework') =>'taxonomy')),
  44.  
  45.  
  46.  
  47. array( "name" => __("Which categories should be used for the blog?", 'avia_framework' ),
  48. "desc" => __("You can select multiple categories here. The Page will then show posts from only those categories.", 'avia_framework' ),
  49. "id" => "categories",
  50. "type" => "select",
  51. "multiple" => 6,
  52. "required" => array('blog_type', 'equals', 'posts'),
  53. "subtype" => "cat"),
  54.  
  55. array(
  56. "name" => __("Which Entries?", 'avia_framework' ),
  57. "desc" => __("Select which entries should be displayed by selecting a taxonomy", 'avia_framework' ),
  58. "id" => "link",
  59. "fetchTMPL" => true,
  60. "type" => "linkpicker",
  61. "subtype" => array( __('Display Entries from:', 'avia_framework' )=>'taxonomy'),
  62. "multiple" => 6,
  63. "required" => array('blog_type', 'equals', 'taxonomy'),
  64. "std" => "category"
  65. ),
  66.  
  67. array(
  68. "name" => __("Blog Style", 'avia_framework' ),
  69. "desc" => __("Choose the default blog layout here.", 'avia_framework' ),
  70. "id" => "blog_style",
  71. "type" => "select",
  72. "std" => "single-big",
  73. "no_first"=>true,
  74. "subtype" => array( __('Multi Author Blog (displays Gravatar of the article author beside the entry and feature images above)', 'avia_framework') =>'multi-big',
  75. __('Single Author, small preview Pic (no author picture is displayed, feature image is small)', 'avia_framework') =>'single-small',
  76. __('Single Author, big preview Pic (no author picture is displayed, feature image is big)', 'avia_framework') =>'single-big',
  77. __('Grid Layout', 'avia_framework') =>'blog-grid',
  78. /* 'no sidebar'=>'fullsize' */
  79. )),
  80.  
  81. array(
  82. "name" => __("Blog Grid Columns", 'avia_framework' ),
  83. "desc" => __("How many columns do you want to display?", 'avia_framework' ),
  84. "id" => "columns",
  85. "type" => "select",
  86. "std" => "3",
  87. "required" => array('blog_style', 'equals', 'blog-grid'),
  88. "subtype" => AviaHtmlHelper::number_array(1,5,1)),
  89.  
  90. array(
  91. "name" => __("Define Blog Grid layout", 'avia_framework' ),
  92. "desc" => __("Do you want to display a read more link?", 'avia_framework' ),
  93. "id" => "contents",
  94. "type" => "select",
  95. "std" => "excerpt",
  96. "required" => array('blog_style', 'equals', 'blog-grid'),
  97. "subtype" => array(
  98. __('Title and Excerpt', 'avia_framework' ) =>'excerpt',
  99. __('Title and Excerpt + Read More Link', 'avia_framework' ) =>'excerpt_read_more',
  100. __('Only Title', 'avia_framework' ) =>'title',
  101. __('Only Title + Read More Link', 'avia_framework' ) =>'title_read_more',
  102. __('Only excerpt', 'avia_framework' ) =>'only_excerpt',
  103. __('Only excerpt + Read More Link', 'avia_framework' ) =>'only_excerpt_read_more',
  104. __('No Title and no excerpt', 'avia_framework' ) =>'no')
  105. ),
  106.  
  107.  
  108. array(
  109. "name" => __("Blog Content length", 'avia_framework' ),
  110. "desc" => __("Should the full entry be displayed or just a small excerpt?", 'avia_framework' ),
  111. "id" => "content_length",
  112. "type" => "select",
  113. "std" => "content",
  114. "required" => array('blog_style', 'not', 'blog-grid'),
  115. "subtype" => array(
  116. __('Full Content', 'avia_framework' ) =>'content',
  117. __('Excerpt', 'avia_framework' ) =>'excerpt',
  118. __('Excerpt With Read More Link', 'avia_framework' ) =>'excerpt_read_more')),
  119.  
  120. array(
  121. "name" => __("Preview Image Size", 'avia_framework' ),
  122. "desc" => __("Set the image size of the preview images", 'avia_framework' ),
  123. "id" => "preview_mode",
  124. "type" => "select",
  125. "std" => "auto",
  126. "subtype" => array(__('Set the preview image size automatically based on column or layout width','avia_framework' ) =>'auto',__('Choose the preview image size manually (select thumbnail size)','avia_framework' ) =>'custom')),
  127.  
  128. array(
  129. "name" => __("Select custom preview image size", 'avia_framework' ),
  130. "desc" => __("Choose image size for Preview Image", 'avia_framework' ),
  131. "id" => "image_size",
  132. "type" => "select",
  133. "required" => array('preview_mode','equals','custom'),
  134. "std" => "portfolio",
  135. "subtype" => AviaHelper::get_registered_image_sizes(array('logo'))
  136. ),
  137.  
  138.  
  139. array(
  140. "name" => __("Post Number", 'avia_framework' ),
  141. "desc" => __("How many items should be displayed per page?", 'avia_framework' ),
  142. "id" => "items",
  143. "type" => "select",
  144. "std" => "3",
  145. "subtype" => AviaHtmlHelper::number_array(1,100,1, array('All'=>'-1'))),
  146.  
  147. array(
  148. "name" => __("Offset Number", 'avia_framework' ),
  149. "desc" => __("The offset determines where the query begins pulling posts. Useful if you want to remove a certain number of posts because you already query them with another blog or magazine element.", 'avia_framework' ),
  150. "id" => "offset",
  151. "type" => "select",
  152. "std" => "0",
  153. "subtype" => AviaHtmlHelper::number_array(1,100,1, array(__('Deactivate offset','avia_framework')=>'0', __('Do not allow duplicate posts on the entire page (set offset automatically)', 'avia_framework' ) =>'no_duplicates'))),
  154.  
  155.  
  156. array(
  157. "name" => __("Pagination", 'avia_framework' ),
  158. "desc" => __("Should a pagination be displayed?", 'avia_framework' ),
  159. "id" => "paginate",
  160. "type" => "select",
  161. "std" => "yes",
  162. "subtype" => array(
  163. __('yes', 'avia_framework' ) =>'yes',
  164. __('no', 'avia_framework' ) =>'no')),
  165.  
  166.  
  167. array(
  168. "name" => __("Conditional display", 'avia_framework' ),
  169. "desc" => __("When should the element be displayed?", 'avia_framework' ),
  170. "id" => "conditional",
  171. "type" => "select",
  172. "std" => "yes",
  173. "subtype" => array(
  174. __('Always display the element', 'avia_framework' ) =>'',
  175. __('Remove element if the user navigated away from page 1 to page 2,3,4 etc ', 'avia_framework' ) =>'is_subpage')),
  176.  
  177. );
  178.  
  179.  
  180.  
  181.  
  182.  
  183. if(current_theme_supports('add_avia_builder_post_type_option'))
  184. {
  185. $element = array(
  186. "name" => __("Select Post Type", 'avia_framework' ),
  187. "desc" => __("Select which post types should be used. Note that your taxonomy will be ignored if you do not select an assign post type.
  188. If yo don't select post type all registered post types will be used", 'avia_framework' ),
  189. "id" => "post_type",
  190. "type" => "select",
  191. "required" => array('blog_type', 'equals', 'taxonomy'),
  192. "multiple" => 6,
  193. "std" => "",
  194. "subtype" => AviaHtmlHelper::get_registered_post_type_array()
  195. );
  196.  
  197. array_splice($this->elements, 2, 0, array($element));
  198. }
  199.  
  200. }
  201.  
  202.  
  203.  
  204. /**
  205. * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  206. * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  207. * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  208. *
  209. *
  210. * @param array $params this array holds the default values for $content and $args.
  211. * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  212. */
  213. function editor_element($params)
  214. {
  215. $params['innerHtml'] = "<img src='".$this->config['icon']."' title='".$this->config['name']."' />";
  216. $params['innerHtml'].= "<div class='avia-element-label'>".$this->config['name']."</div>";
  217. $params['content'] = NULL; //remove to allow content elements
  218.  
  219. return $params;
  220. }
  221.  
  222.  
  223.  
  224. /**
  225. * Frontend Shortcode Handler
  226. *
  227. * @param array $atts array of attributes
  228. * @param string $content text within enclosing form of shortcode element
  229. * @param string $shortcodename the shortcode found, when == callback name
  230. * @return string $output returns the modified html string
  231. */
  232. function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
  233. {
  234. global $avia_config, $more;
  235.  
  236. if(empty($atts['categories'])) $atts['categories'] = "";
  237. if(isset($atts['link']) && isset($atts['blog_type']) && $atts['blog_type'] == 'taxonomy')
  238. {
  239. $atts['link'] = explode(',', $atts['link'], 2 );
  240. $atts['taxonomy'] = $atts['link'][0];
  241.  
  242. if(!empty($atts['link'][1]))
  243. {
  244. $atts['categories'] = $atts['link'][1];
  245. }
  246. else if(!empty($atts['taxonomy']))
  247. {
  248. $taxonomy_terms_obj = get_terms($atts['taxonomy']);
  249. foreach ($taxonomy_terms_obj as $taxonomy_term)
  250. {
  251. $atts['categories'] .= $taxonomy_term->term_id . ',';
  252. }
  253. }
  254. }
  255.  
  256. $atts = shortcode_atts(array('blog_style' => '',
  257. 'columns' => 3,
  258. 'blog_type' => 'posts',
  259. 'items' => '16',
  260. 'paginate' => 'yes',
  261. 'categories' => '',
  262. 'preview_mode' => 'auto',
  263. 'image_size' => 'portfolio',
  264. 'taxonomy' => 'category',
  265. 'post_type'=> get_post_types(),
  266. 'contents' => 'excerpt',
  267. 'content_length' => 'content',
  268. 'offset' => '0',
  269. 'conditional' => ''
  270. ), $atts, $this->config['shortcode']);
  271.  
  272. $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  273. if(!$page) $page = 1;
  274.  
  275. if($atts['blog_style'] == "blog-grid")
  276. {
  277. $atts['class'] = $meta['el_class'];
  278. $atts['type'] = 'grid';
  279.  
  280. //using the post slider with inactive js will result in displaying a nice post grid
  281. $slider = new avia_post_slider($atts);
  282. $slider->query_entries();
  283.  
  284. if($atts['conditional'] && $page != 1)
  285. {
  286. return;
  287. }
  288. else
  289. {
  290. return $slider->html();
  291. }
  292. }
  293.  
  294. $this->query_entries($atts);
  295.  
  296. $avia_config['blog_style'] = $atts['blog_style'];
  297. $avia_config['preview_mode'] = $atts['preview_mode'];
  298. $avia_config['image_size'] = $atts['image_size'];
  299. $avia_config['blog_content'] = $atts['content_length'];
  300. $avia_config['remove_pagination'] = $atts['paginate'] === "yes" ? false :true;
  301.  
  302. $more = 0;
  303. ob_start(); //start buffering the output instead of echoing it
  304. get_template_part( 'includes/loop', 'index' );
  305. $output = ob_get_clean();
  306. wp_reset_query();
  307. avia_set_layout_array();
  308.  
  309. if($output)
  310. {
  311. $extraclass = function_exists('avia_blog_class_string') ? avia_blog_class_string() : "";
  312. $markup = avia_markup_helper(array('context' => 'blog','echo'=>false, 'custom_markup'=>$meta['custom_markup']));
  313. $output = "<div class='template-blog {$extraclass}' {$markup}>{$output}</div>";
  314. }
  315.  
  316. if($atts['conditional'] && $page != 1)
  317. {
  318. return;
  319. }
  320. else
  321. {
  322. return $output;
  323. }
  324. }
  325.  
  326.  
  327. function query_entries($params)
  328. {
  329. global $avia_config;
  330. $query = array();
  331.  
  332. if(!empty($params['categories']) && is_string($params['categories']))
  333. {
  334. //get the categories
  335. $terms = explode(',', $params['categories']);
  336. }
  337.  
  338. $page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : get_query_var( 'page' );
  339. if(!$page || $params['paginate'] == 'no') $page = 1;
  340.  
  341. if($params['offset'] == 'no_duplicates')
  342. {
  343. $params['offset'] = 0;
  344. $no_duplicates = true;
  345. }
  346.  
  347. if(empty($params['blog_type']) || $params['blog_type'] == 'posts') $params['post_type'] = 'post';
  348. if(empty($params['post_type'])) $params['post_type'] = get_post_types();
  349. if(is_string($params['post_type'])) $params['post_type'] = explode(',', $params['post_type']);
  350.  
  351. //wordpress 4.4 offset fix
  352. if( $params['offset'] == 0 )
  353. {
  354. $params['offset'] = false;
  355. }
  356. else
  357. {
  358. //if the offset is set the paged param is ignored. therefore we need to factor in the page number
  359. $params['offset'] = $params['offset'] + ( ($page -1 ) * $params['items']);
  360. }
  361.  
  362. //if we find categories perform complex query, otherwise simple one
  363. if(isset($terms[0]) && !empty($terms[0]) && !is_null($terms[0]) && $terms[0] != "null" && !empty($params['taxonomy']))
  364. {
  365. $query = array( 'paged' => $page,
  366. 'posts_per_page' => $params['items'],
  367. 'offset' => !(int)$params['offset'] ? "" : $params['offset'],
  368. 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
  369. 'post_type' => $params['post_type'],
  370. 'tax_query' => array( array( 'taxonomy' => $params['taxonomy'],
  371. 'field' => 'id',
  372. 'terms' => $terms,
  373. 'operator' => 'IN'))
  374. );
  375. }
  376. else
  377. {
  378. $query = array( 'paged'=> $page,
  379. 'posts_per_page' => $params['items'],
  380. 'offset' => $params['offset'],
  381. 'post__not_in' => (!empty($no_duplicates)) ? $avia_config['posts_on_current_page'] : array(),
  382. 'post_type' => $params['post_type']);
  383. }
  384.  
  385. $query = apply_filters('avia_blog_post_query', $query, $params);
  386.  
  387. $results = query_posts($query);
  388.  
  389. // store the queried post ids in
  390. if( have_posts() )
  391. {
  392. while( have_posts() )
  393. {
  394. the_post();
  395. $avia_config['posts_on_current_page'][] = get_the_ID();
  396. }
  397. }
  398. }
  399.  
  400.  
  401.  
  402.  
  403. }
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement