Advertisement
Ornela

Home featured page. Theme Customizr

Jan 21st, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.96 KB | None | 0 0
  1. <?php
  2. /**
  3. * Featured pages actions
  4. *
  5. *
  6. * @package Customizr
  7. * @subpackage classes
  8. * @since 3.0
  9. * @author Nicolas GUILLAUME <nicolas@themesandco.com>
  10. * @copyright Copyright (c) 2013, Nicolas GUILLAUME
  11. * @link http://themesandco.com/customizr
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14.  
  15. class TC_featured_pages {
  16.  
  17. //Access any method or var of the class with classname::$instance -> var or method():
  18. static $instance;
  19.  
  20. function __construct () {
  21.  
  22. self::$instance =& $this;
  23.  
  24. add_action ( '__before_main_container' , array( $this , 'tc_fp_block_display'), 10 );
  25. }
  26.  
  27.  
  28.  
  29. /**
  30. * The template displaying the front page featured page block.
  31. *
  32. *
  33. * @package Customizr
  34. * @since Customizr 3.0
  35. */
  36. function tc_fp_block_display() {
  37.  
  38. //gets display options
  39. $tc_show_featured_pages = esc_attr( tc__f( '__get_option' , 'tc_show_featured_pages' ) );
  40. $tc_show_featured_pages_img = esc_attr( tc__f( '__get_option' , 'tc_show_featured_pages_img' ) );
  41.  
  42. if ( !apply_filters( 'tc_show_fp', 0 != $tc_show_featured_pages && tc__f('__is_home') ) )
  43. return;
  44.  
  45. //gets the featured pages array and sets the fp layout
  46. $fp_ids = apply_filters( 'tc_featured_pages_ids' , TC_init::$instance -> fp_ids);
  47. $fp_nb = count($fp_ids);
  48. $fp_per_row = apply_filters( 'tc_fp_per_line', 3 );
  49.  
  50. //defines the span class
  51. $span_array = array(
  52. 1 => 12,
  53. 2 => 6,
  54. 3 => 4,
  55. 4 => 3,
  56. 5 => 2,
  57. 6 => 2,
  58. 7 => 2
  59. );
  60. $span_value = 4;
  61. $span_value = ( $fp_per_row > 7) ? 1 : $span_value;
  62. $span_value = isset( $span_array[$fp_per_row] ) ? $span_array[$fp_per_row] : $span_value;
  63.  
  64. //save $args for filter
  65. $args = array($fp_ids, $fp_nb, $fp_per_row, $span_value);
  66.  
  67. ?>
  68.  
  69. <?php ob_start(); ?>
  70.  
  71. <div class="container marketing">
  72.  
  73. <?php
  74. do_action ('__before_fp') ;
  75.  
  76. $j = 1;
  77. for ($i = 1; $i <= $fp_nb ; $i++ ) {
  78. printf('%1$s<div class="span%2$s fp-%3$s">%4$s</div>%5$s',
  79. ( 1 == $j ) ? '<div class="row widget-area" role="complementary">' : '',
  80. $span_value,
  81. $fp_ids[$i - 1],
  82. $this -> tc_fp_single_display( $fp_ids[$i - 1] , $tc_show_featured_pages_img ),
  83. ( $j == $fp_per_row || $i == $fp_nb ) ? '</div>' : ''
  84. );
  85. //set $j back to start value if reach $fp_per_row
  86. $j++;
  87. $j = ($j == ($fp_per_row + 1)) ? 1 : $j;
  88. }
  89.  
  90. do_action ('__after_fp') ;
  91. ?>
  92.  
  93. </div><!-- .container -->
  94.  
  95. <?php echo !tc__f( '__is_home_empty') ? apply_filters( 'tc_after_fp_separator', '<hr class="featurette-divider '.current_filter().'">' ) : ''; ?>
  96.  
  97. <?php
  98. $html = ob_get_contents();
  99. if ($html) ob_end_clean();
  100. echo apply_filters( 'tc_fp_block_display' , $html, $args );
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. /**
  108. * The template displaying one single featured page
  109. *
  110. * @package Customizr
  111. * @since Customizr 3.0
  112. * @param area are defined in featured-pages templates,show_img is a customizer option
  113. * @todo better area definition : dynamic
  114. */
  115. function tc_fp_single_display( $fp_single_id,$show_img) {
  116. //holder declaration
  117. $fp_holder_img = apply_filters ('fp_holder_img' , '<img data-src="holder.js/270x250" alt="Holder Thumbnail" />' );
  118.  
  119. //if fps are not set
  120. if ( null == tc__f( '__get_option' , 'tc_featured_page_'.$fp_single_id ) ) {
  121. //admin link if user logged in
  122. $featured_page_link = is_user_logged_in() ? apply_filters( 'tc_fp_link_url', admin_url().'customize.php' , $fp_single_id ) : '';
  123. $admin_link = is_user_logged_in() ? '<a href="'.admin_url().'customize.php" title="'.__( 'Customizer screen' , 'customizr' ).'">'.__( ' here' , 'customizr' ).'</a>' : '';
  124.  
  125. //rendering
  126. $featured_page_id = null;
  127. $featured_page_title = apply_filters( 'tc_fp_title', __( 'Featured page' , 'customizr' ) );
  128. $text = apply_filters(
  129. 'tc_fp_text',
  130. sprintf( __( 'Featured page description text : use the page excerpt or set your own custom text in the Customizr screen%s.' , 'customizr' ),
  131. $admin_link
  132. ),
  133. $fp_single_id,
  134. $featured_page_id
  135. );
  136. $fp_img = apply_filters ('fp_img_src' , $fp_holder_img );
  137.  
  138. }
  139.  
  140. else {
  141. $featured_page_id = esc_attr( tc__f( '__get_option' , 'tc_featured_page_'.$fp_single_id) );
  142. $featured_page_link = apply_filters( 'tc_fp_link_url', get_permalink( $featured_page_id ), $fp_single_id );
  143. $featured_page_title = apply_filters( 'tc_fp_title', get_the_title( $featured_page_id ), $fp_single_id, $featured_page_id );
  144. $featured_text = apply_filters( 'tc_fp_text', tc__f( '__get_option' , 'tc_featured_text_'.$fp_single_id ), $fp_single_id, $featured_page_id );
  145. $featured_text = apply_filters( 'tc_fp_text_sanitize', strip_tags( html_entity_decode( $featured_text ) ), $fp_single_id, $featured_page_id );
  146.  
  147. //get the page/post object
  148. $page = get_post($featured_page_id);
  149.  
  150. //set page excerpt as default text if no $featured_text
  151. $text = ( empty($featured_text) && !post_password_required($featured_page_id) ) ? strip_tags(apply_filters( 'the_content' , $page->post_excerpt )) : $featured_text ;
  152. $text = ( empty($text) && !post_password_required($featured_page_id) ) ? strip_tags(apply_filters( 'the_content' , $page->post_content )) : $text ;
  153.  
  154. //limit text to 200 car
  155. $default_fp_text_length = apply_filters( 'tc_fp_text_length', 200 );
  156. $text = ( strlen($text) > $default_fp_text_length ) ? substr( $text , 0 , strpos( $text, ' ' , $default_fp_text_length) ). ' ...' : $text;
  157.  
  158. //set the image : uses thumbnail if any then >> the first attached image then >> a holder script
  159. $fp_img_size = apply_filters( 'tc_fp_img_size' , 'tc-thumb' );
  160.  
  161. if ( has_post_thumbnail( $featured_page_id) ) {
  162. $fp_img_id = get_post_thumbnail_id( $featured_page_id);
  163.  
  164. //check if tc-thumb size exists for attachment and return large if not
  165. $image = wp_get_attachment_image_src( $fp_img_id, $fp_img_size);
  166. $fp_img_size = ( null == $image[3] ) ? 'medium' : $fp_img_size ;
  167.  
  168. $fp_img = get_the_post_thumbnail( $featured_page_id , $fp_img_size);
  169. //get height and width
  170. $fp_img_height = $image[2];
  171. $fp_img_width = $image[1];
  172. }
  173.  
  174. //If not uses the first attached image
  175. else {
  176. //look for attachements
  177. $tc_args = array(
  178. 'numberposts' => 1,
  179. 'post_type' => 'attachment' ,
  180. 'post_status' => null,
  181. 'post_parent' => $featured_page_id,
  182. 'post_mime_type' => array( 'image/jpeg' , 'image/gif' , 'image/jpg' , 'image/png' )
  183. );
  184.  
  185. $attachments = get_posts( $tc_args);
  186.  
  187. if ( $attachments) {
  188.  
  189. foreach ( $attachments as $attachment) {
  190. //check if tc-thumb size exists for attachment and return large if not
  191. $image = wp_get_attachment_image_src( $attachment->ID, $fp_img_size );
  192. $fp_img_size = ( false == $image[3] ) ? 'medium' : $fp_img_size;
  193. $fp_img = wp_get_attachment_image( $attachment->ID, $fp_img_size );
  194. //get height and width
  195. $fp_img_height = $image[2];
  196. $fp_img_width = $image[1];
  197. }//end foreach
  198.  
  199. }//end if
  200.  
  201. }//end else
  202.  
  203. //finally we define a default holder if no thumbnail found or page is protected
  204. $fp_img = apply_filters ('fp_img_src' ,
  205. ( !isset( $fp_img) || post_password_required($featured_page_id) ) ? $fp_holder_img : $fp_img
  206. );
  207. }//end if
  208.  
  209. //Let's render this
  210. ob_start();
  211. ?>
  212.  
  213. <div class="widget-front">
  214. <?php
  215. if ( isset( $show_img) && $show_img == 1 ) { //check if image option is checked
  216. printf('<div class="thumb-wrapper %1$s">%2$s%3$s</div>',
  217. ( $fp_img == $fp_holder_img ) ? 'tc-holder' : '',
  218. apply_filters('tc_fp_round_div' , sprintf('<a class="round-div" href="%1$s" title="%2$s"></a>',
  219. $featured_page_link,
  220. $featured_page_title
  221. ) ,
  222. $fp_single_id ),
  223. $fp_img
  224. );
  225. }//end if image enabled check
  226.  
  227.  
  228. //title block
  229. $tc_fp_title_block = sprintf('<%1$s>%2$s</%1$s>',
  230. apply_filters( 'tc_fp_title_tag' , 'h2' ),
  231. $featured_page_title
  232. );
  233. echo apply_filters( 'tc_fp_title_block' , $tc_fp_title_block , $featured_page_title );
  234.  
  235. //text block
  236. $tc_fp_text_block = sprintf('<p class="fp-text-%1$s">%2$s</p>',
  237. $fp_single_id,
  238. $text
  239. );
  240. echo apply_filters( 'tc_fp_text_block' , $tc_fp_text_block , $fp_single_id , $text);
  241.  
  242. //button block
  243. $tc_fp_button_block = sprintf('<a class="%1$s" href="%2$s" title="%3$s">%4$s</a>',
  244. apply_filters( 'tc_fp_button_class' , 'btn btn-primary fp-button', $fp_single_id ),
  245. $featured_page_link,
  246. $featured_page_title,
  247. apply_filters( 'tc_fp_button_text' , esc_attr( tc__f( '__get_option' , 'tc_featured_page_button_text') ) , $fp_single_id )
  248. );
  249. echo apply_filters( 'tc_fp_button_block' , $tc_fp_button_block , $featured_page_link , $featured_page_title , $fp_single_id );
  250.  
  251. ?>
  252.  
  253. </div><!-- /.widget-front -->
  254.  
  255. <?php
  256. $html = ob_get_contents();
  257. if ($html) ob_end_clean();
  258. return apply_filters( 'tc_fp_single_display' , $html, $fp_single_id, $show_img, $fp_img, $featured_page_link, $featured_page_title, $text );
  259. }//end of function
  260.  
  261. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement