Guest User

Untitled

a guest
Feb 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.11 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Class tdb_single_review_overall
  5. */
  6.  
  7. class tdb_single_review_overall extends td_block {
  8.  
  9. public function get_custom_css() {
  10. // $unique_block_class - the unique class that is on the block. use this to target the specific instance via css
  11. $unique_block_class = $this->block_uid . '_rand';
  12.  
  13. $compiled_css = '';
  14.  
  15. $raw_css =
  16. "<style>
  17.  
  18. /* @title_color */
  19. .$unique_block_class .td-review-final-title {
  20. color: @title_color;
  21. }
  22. /* @final_color */
  23. .$unique_block_class .td-review-final-score {
  24. color: @final_color;
  25. }
  26.  
  27. /* @stars_size */
  28. .$unique_block_class .td-review-final-star i {
  29. font-size: @stars_size;
  30. }
  31. /* @stars_space */
  32. .$unique_block_class .td-review-final-star i {
  33. margin-right: @stars_space;
  34. }
  35. .$unique_block_class .td-review-final-star i:last-child {
  36. margin-right: 0;
  37. }
  38. /* @stars_color */
  39. .$unique_block_class .td-review-final-star i {
  40. color: @stars_color;
  41. }
  42.  
  43. /* @padding */
  44. .$unique_block_class .td-review-score {
  45. padding: @padding;
  46. }
  47. /* @all_border_size */
  48. .$unique_block_class td {
  49. border-width: @all_border_size;
  50. border-style: solid;
  51. }
  52. /* @all_border_color */
  53. .$unique_block_class td {
  54. border-color: @all_border_color;
  55. }
  56.  
  57. /* @meta_horiz_align_left */
  58. .$unique_block_class .td-review-score {
  59. text-align: left;
  60. }
  61. /* @meta_horiz_align_right */
  62. .$unique_block_class .td-review-score {
  63. text-align: right;
  64. }
  65.  
  66.  
  67. /* @f_title */
  68. .$unique_block_class .td-review-final-title {
  69. @f_title
  70. }
  71. /* @f_final */
  72. .$unique_block_class .td-review-final-score {
  73. @f_final
  74. }
  75.  
  76. </style>";
  77.  
  78.  
  79. $td_css_res_compiler = new td_css_res_compiler( $raw_css );
  80. $td_css_res_compiler->load_settings( __CLASS__ . '::cssMedia', $this->get_all_atts() );
  81.  
  82. $compiled_css .= $td_css_res_compiler->compile_css();
  83. return $compiled_css;
  84. }
  85.  
  86. static function cssMedia( $res_ctx ) {
  87.  
  88. // title color
  89. $res_ctx->load_settings_raw( 'title_color', $res_ctx->get_shortcode_att('title_color') );
  90. // final score color
  91. $res_ctx->load_settings_raw( 'final_color', $res_ctx->get_shortcode_att('final_color') );
  92.  
  93. // stars size
  94. $stars_size = $res_ctx->get_shortcode_att('stars_size');
  95. if( $stars_size != '' && is_numeric( $stars_size ) ) {
  96. $res_ctx->load_settings_raw( 'stars_size', $stars_size . 'px' );
  97. }
  98. // stars space
  99. $stars_space = $res_ctx->get_shortcode_att('stars_space');
  100. if( $stars_space != '' && is_numeric( $stars_space ) ) {
  101. $res_ctx->load_settings_raw( 'stars_space', $stars_space . 'px' );
  102. }
  103. // stars color
  104. $res_ctx->load_settings_raw( 'stars_color', $res_ctx->get_shortcode_att('stars_color') );
  105.  
  106. // box padding
  107. $padding = $res_ctx->get_shortcode_att('padding');
  108. $res_ctx->load_settings_raw( 'padding', $padding );
  109. if( $padding != '' && is_numeric( $padding ) ) {
  110. $res_ctx->load_settings_raw( 'padding', $padding . 'px' );
  111. }
  112.  
  113. // border width
  114. $all_border_size = $res_ctx->get_shortcode_att('all_border_size');
  115. $res_ctx->load_settings_raw( 'all_border_size', $all_border_size );
  116. if( $all_border_size != '' ) {
  117. if( is_numeric( $all_border_size ) ) {
  118. $res_ctx->load_settings_raw( 'all_border_size', $all_border_size . 'px' );
  119. }
  120. } else {
  121. $res_ctx->load_settings_raw( 'all_border_size', '1px' );
  122. }
  123. // border color
  124. $all_border_color = $res_ctx->get_shortcode_att('all_border_color');
  125. $res_ctx->load_settings_raw( 'all_border_color', '#ededed' );
  126. if( $all_border_color != '' ) {
  127. $res_ctx->load_settings_raw( 'all_border_color', $all_border_color );
  128. }
  129.  
  130. // horizontal align
  131. $content_align = $res_ctx->get_shortcode_att('content_align_horiz');
  132. if ( $content_align == 'content-horiz-left' ) {
  133. $res_ctx->load_settings_raw( 'meta_horiz_align_left', 1 );
  134. } else if ( $content_align == 'content-horiz-right' ) {
  135. $res_ctx->load_settings_raw( 'meta_horiz_align_right', 1 );
  136. }
  137.  
  138.  
  139.  
  140. /*-- FONTS -- */
  141. $res_ctx->load_font_settings( 'f_title' );
  142. $res_ctx->load_font_settings( 'f_final' );
  143.  
  144. }
  145.  
  146. /**
  147. * Disable loop block features. This block does not use a loop and it doesn't need to run a query.
  148. */
  149. function __construct() {
  150. parent::disable_loop_block_features();
  151. }
  152.  
  153.  
  154. function render( $atts, $content = null ) {
  155. parent::render( $atts ); // sets the live atts, $this->atts, $this->block_uid, $this->td_query (it runs the query)
  156.  
  157. global $tdb_state_single;
  158.  
  159. $post_review_data = $tdb_state_single->post_review->__invoke();
  160.  
  161. $title = '';
  162. if( $this->get_att( 'title' ) != '' ) {
  163. $title = $this->get_att( 'title' );
  164. }
  165.  
  166. $buffy = '';
  167.  
  168. if( $post_review_data['review_meta'] != '' ) {
  169. $buffy .= '<div class="' . $this->get_block_classes() . '" ' . $this->get_block_html_atts() . '>';
  170.  
  171. //get the block css
  172. $buffy .= $this->get_block_css();
  173.  
  174. //get the js for this block
  175. $buffy .= $this->get_block_js();
  176.  
  177.  
  178. $buffy .= '<table class="td-review td-fix-index">';
  179. $buffy .= '<tr class="td-review-footer ' . $post_review_data['review_meta'] . '">';
  180. $buffy .= '<td class="td-review-score">';
  181. $buffy .= '<div class="td-review-overall">';
  182. $buffy .= '<div class="td-review-final-score">' . $this->calculate_total($post_review_data) . ($post_review_data['review_meta'] == 'rate_percent' ? ' %' : '') . '</div>';
  183. $buffy .= '<div class="td-review-final-star">' . $this->render_stars($post_review_data) . '</div>';
  184. $buffy .= '<span class="td-review-final-title">' . $title . '</span>';
  185. $buffy .= '</div>';
  186. $buffy .= '</td>';
  187. $buffy .= '</tr>';
  188. $buffy .= '</table>';
  189.  
  190. $buffy .= '</div>';
  191. }
  192.  
  193. return $buffy;
  194. }
  195.  
  196. //calculates the average of the rating
  197. function calculate_total( $post_review_data ) {
  198.  
  199. $total = 0;
  200. $cnt = 0;
  201.  
  202. switch ( $post_review_data['review_meta'] ) {
  203. case 'rate_stars' :
  204. foreach ( $post_review_data['review_meta_stars'] as $section ) {
  205. if ( !empty( $section['rate'] ) ) {
  206. $total = $total + $section['rate'];
  207. $cnt++;
  208. }
  209. }
  210. break;
  211.  
  212. case 'rate_percent' :
  213. foreach ( $post_review_data['review_meta_percents'] as $section ) {
  214. if ( !empty( $section['rate'] ) ) {
  215. $total = $total + $section['rate'];
  216. $cnt++;
  217. }
  218. }
  219. break;
  220.  
  221. case 'rate_point' :
  222. foreach ( $post_review_data['review_meta_points'] as $section ) {
  223. if ( !empty( $section['rate'] ) ) {
  224. $total = $total + $section['rate'];
  225. $cnt++;
  226. }
  227. }
  228. break;
  229. }
  230.  
  231. if ( $total == 0 ) {
  232. $result = 0;
  233. } else {
  234. $result = number_format( $total / $cnt, 1 );
  235. }
  236.  
  237. return $result;
  238.  
  239. }
  240.  
  241. //calculates the average stars rating
  242. function render_stars( $post_review_data ) {
  243. $total_stars = $this->calculate_total_stars( $post_review_data );
  244. return $this->number_to_stars( $total_stars );
  245. }
  246.  
  247. // converts the rating to 0-5 to be used with stars
  248. function calculate_total_stars( $post_review_data ) {
  249. switch ( $post_review_data['review_meta'] ) {
  250. case 'rate_stars' :
  251. return round( $this->calculate_total( $post_review_data ), 1);
  252. break;
  253. case 'rate_percent':
  254. return round($this->calculate_total( $post_review_data ) / 10 / 2, 1);
  255. break;
  256. case 'rate_point' :
  257. return round($this->calculate_total( $post_review_data ) / 2, 1);
  258. break;
  259. }
  260.  
  261. return 0;
  262. }
  263.  
  264. //converts 0 - 5 to stars
  265. function number_to_stars( $total_stars ) {
  266.  
  267. $star_integer = intval( $total_stars );
  268.  
  269. $buffy = '';
  270.  
  271. // this echo full stars
  272. for ( $i = 0; $i < $star_integer; $i++ ) {
  273. $buffy .= '<i class="td-icon-star"></i>';
  274. }
  275.  
  276. $star_rest = $total_stars - $star_integer;
  277.  
  278. // this echo full star or half or empty star
  279. if ( $star_rest >= 0.25 and $star_rest < 0.75 ) {
  280. $buffy .= '<i class="td-icon-star-half"></i>';
  281. } else if ( $star_rest >= 0.75 ) {
  282. $buffy .= '<i class="td-icon-star"></i>';
  283. } else if ( $total_stars != 5 ) {
  284. $buffy .= '<i class="td-icon-star-empty"></i>';
  285. }
  286.  
  287. // this echo empty star
  288. for ( $i = 0; $i < 4-$star_integer; $i++ ) {
  289. $buffy .= '<i class="td-icon-star-empty"></i>';
  290. }
  291.  
  292. return $buffy;
  293. }
  294.  
  295. }
Advertisement
Add Comment
Please, Sign In to add comment