Guest User

Untitled

a guest
Mar 16th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.14 KB | None | 0 0
  1. <?php
  2. abstract class td_module {
  3. var $post;
  4.  
  5. var $title_attribute;
  6. var $title; // by default the WordPress title is not escaped on twenty fifteen
  7. var $href;
  8.  
  9.  
  10. /**
  11. * @var mixed the review metadata - we get it for each $post
  12. */
  13. protected $td_review;
  14.  
  15. /**
  16. * @var bool is true if we have a review for this $post
  17. */
  18. protected $is_review = false;
  19.  
  20. /**
  21. * @var int|null Contains the id of the current $post thumbnail. If no thumbnail is found, the value is NULL
  22. */
  23. protected $post_thumb_id = NULL;
  24.  
  25.  
  26. /**
  27. * @param $post WP_Post
  28. * @throws ErrorException
  29. */
  30. function __construct($post) {
  31. if (gettype($post) != 'object' or get_class($post) != 'WP_Post') {
  32. td_util::error(__FILE__, 'td_module: ' . get_Class($this) . '($post): $post is not WP_Post');
  33. }
  34.  
  35.  
  36. //this filter is used by td_unique_posts.php - to add unique posts to the array for the datasource
  37. apply_filters("td_wp_booster_module_constructor", $this, $post);
  38.  
  39. $this->post = $post;
  40.  
  41. // by default the WordPress title is not escaped on twenty fifteen
  42. $this->title = get_the_title($post->ID);
  43. $this->title_attribute = esc_attr(strip_tags($this->title));
  44. $this->href = esc_url(get_permalink($post->ID));
  45.  
  46. if (has_post_thumbnail($this->post->ID)) {
  47. $tmp_get_post_thumbnail_id = get_post_thumbnail_id($this->post->ID);
  48. if (!empty($tmp_get_post_thumbnail_id)) {
  49. // if we have a wrong id, leave the post_thumb_id NULL
  50. $this->post_thumb_id = $tmp_get_post_thumbnail_id;
  51. }
  52. }
  53.  
  54. //get the review metadata
  55. //$this->td_review = get_post_meta($this->post->ID, 'td_review', true); @todo $this->td_review variable name must be replaced and the 'get_quotes_on_blocks', 'get_category' methods also
  56. $this->td_review = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  57.  
  58. if (!empty($this->td_review['has_review']) and (
  59. !empty($this->td_review['p_review_stars']) or
  60. !empty($this->td_review['p_review_percents']) or
  61. !empty($this->td_review['p_review_points'])
  62. )
  63. ) {
  64. $this->is_review = true;
  65. }
  66. }
  67.  
  68.  
  69. /**
  70. * @deprecated - google changed the structured data requirements and we no longer use them on modules
  71. */
  72. function get_item_scope() {
  73. return '';
  74. }
  75.  
  76.  
  77. /**
  78. * @deprecated - google changed the structured data requirements and we no longer use them on modules
  79. */
  80. function get_item_scope_meta() {
  81. return '';
  82. }
  83.  
  84.  
  85. function get_module_classes($additional_classes_array = '') {
  86. //add the wrap and module id class
  87. $buffy = get_class($this);
  88.  
  89.  
  90. // each module setting has a 'class' key to customize css
  91. $module_class = td_api_module::get_key(get_class($this), 'class');
  92.  
  93. if ($module_class != '') {
  94. $buffy .= ' ' . $module_class;
  95. }
  96.  
  97.  
  98. //show no thumb only if no thumb is detected and image placeholders are disabled
  99. if (is_null($this->post_thumb_id) and td_util::get_option('tds_hide_featured_image_placeholder') == 'hide_placeholder') {
  100. $buffy .= ' td_module_no_thumb';
  101. }
  102.  
  103. if ($additional_classes_array != '' && is_array($additional_classes_array)) {
  104. $buffy .= ' ' . implode(' ', $additional_classes_array);
  105. }
  106.  
  107. // the following case could not be checked
  108. // $buffy = implode(' ', array_unique(explode(' ', $buffy)));
  109.  
  110. return $buffy;
  111. }
  112.  
  113.  
  114. function get_author() {
  115. $buffy = '';
  116.  
  117. if ($this->is_review === false) {
  118. if (td_util::get_option('tds_p_show_author_name') != 'hide') {
  119. $buffy .= '<div class="td-post-author-name">';
  120. $buffy .= '<a href="' . get_author_posts_url($this->post->post_author) . '">' . get_the_author_meta('display_name', $this->post->post_author) . '</a>' ;
  121. if (td_util::get_option('tds_p_show_author_name') != 'hide' and td_util::get_option('tds_p_show_date') != 'hide') {
  122. $buffy .= ' <span>-</span> ';
  123. }
  124. $buffy .= '</div>';
  125. }
  126.  
  127. }
  128. return $buffy;
  129.  
  130. }
  131.  
  132.  
  133. function get_date($show_stars_on_review = true) {
  134. $visibility_class = '';
  135. if (td_util::get_option('tds_p_show_date') == 'hide') {
  136. $visibility_class = ' td-visibility-hidden';
  137. }
  138.  
  139. $buffy = '';
  140. if ($this->is_review and $show_stars_on_review === true) {
  141. //if review show stars
  142. $buffy .= '<div class="entry-review-stars">';
  143. $buffy .= td_review::render_stars($this->td_review);
  144. $buffy .= '</div>';
  145.  
  146. } else {
  147. if (td_util::get_option('tds_p_show_date') != 'hide') {
  148. $td_article_date_unix = get_the_time('U', $this->post->ID);
  149. $buffy .= '<div class="td-post-date">';
  150. $buffy .= '<time class="entry-date updated td-module-date' . $visibility_class . '" datetime="' . date(DATE_W3C, $td_article_date_unix) . '" >' . get_the_time(get_option('date_format'), $this->post->ID) . '</time>';
  151. $buffy .= '</div>';
  152. }
  153. }
  154.  
  155. return $buffy;
  156. }
  157.  
  158. function get_comments() {
  159. $buffy = '';
  160. if (td_util::get_option('tds_p_show_comments') != 'hide') {
  161. $buffy .= '<div class="td-module-comments">';
  162. $buffy .= '<a href="' . get_comments_link($this->post->ID) . '">';
  163. $buffy .= get_comments_number($this->post->ID);
  164. $buffy .= '</a>';
  165. $buffy .= '</div>';
  166. }
  167.  
  168. return $buffy;
  169. }
  170.  
  171.  
  172.  
  173. /**
  174. * get image - v 3.0 23 ian 2015
  175. * @param $thumbType
  176. * @return string
  177. */
  178. function get_image($thumbType, $new_tab = '') {
  179. $buffy = ''; //the output buffer
  180. $tds_hide_featured_image_placeholder = td_util::get_option('tds_hide_featured_image_placeholder');
  181.  
  182. // do we have a post thumb or a placeholder?
  183. if (!is_null($this->post_thumb_id) or ($tds_hide_featured_image_placeholder != 'hide_placeholder')) {
  184.  
  185. if (!is_null($this->post_thumb_id)) {
  186. //if we have a thumb
  187. // check to see if the thumb size is enabled in the panel, we don't have to check for the default wordpress
  188. // thumbs (the default ones are already cut and we don't have a panel setting for them)
  189. if (td_util::get_option('tds_thumb_' . $thumbType) != 'yes' and $thumbType != 'thumbnail') {
  190. //the thumb is disabled, show a placeholder thumb from the theme with the "thumb disabled" message
  191. global $_wp_additional_image_sizes;
  192.  
  193. if (empty($_wp_additional_image_sizes[$thumbType]['width'])) {
  194. $td_temp_image_url[1] = '';
  195. } else {
  196. $td_temp_image_url[1] = $_wp_additional_image_sizes[$thumbType]['width'];
  197. }
  198.  
  199. if (empty($_wp_additional_image_sizes[$thumbType]['height'])) {
  200. $td_temp_image_url[2] = '';
  201. } else {
  202. $td_temp_image_url[2] = $_wp_additional_image_sizes[$thumbType]['height'];
  203. }
  204.  
  205.  
  206. $td_temp_image_url[0] = td_global::$get_template_directory_uri . '/images/thumb-disabled/' . $thumbType . '.png';
  207. $attachment_alt = 'alt=""';
  208. $attachment_title = '';
  209.  
  210. } else {
  211. // the thumb is enabled from the panel, it's time to show the real thumb
  212. $td_temp_image_url = wp_get_attachment_image_src($this->post_thumb_id, $thumbType);
  213. $attachment_alt = get_post_meta($this->post_thumb_id, '_wp_attachment_image_alt', true );
  214. $attachment_alt = 'alt="' . esc_attr(strip_tags($attachment_alt)) . '"';
  215. $attachment_title = ' title="' . esc_attr(strip_tags($this->title)) . '"';
  216.  
  217. if (empty($td_temp_image_url[0])) {
  218. $td_temp_image_url[0] = '';
  219. }
  220.  
  221. if (empty($td_temp_image_url[1])) {
  222. $td_temp_image_url[1] = '';
  223. }
  224.  
  225. if (empty($td_temp_image_url[2])) {
  226. $td_temp_image_url[2] = '';
  227. }
  228. } // end panel thumb enabled check
  229.  
  230.  
  231.  
  232. } else {
  233. //we have no thumb but the placeholder one is activated
  234. global $_wp_additional_image_sizes;
  235.  
  236. if (empty($_wp_additional_image_sizes[$thumbType]['width'])) {
  237. $td_temp_image_url[1] = '';
  238. } else {
  239. $td_temp_image_url[1] = $_wp_additional_image_sizes[$thumbType]['width'];
  240. }
  241.  
  242. if (empty($_wp_additional_image_sizes[$thumbType]['height'])) {
  243. $td_temp_image_url[2] = '';
  244. } else {
  245. $td_temp_image_url[2] = $_wp_additional_image_sizes[$thumbType]['height'];
  246. }
  247.  
  248. /**
  249. * get thumb height and width via api
  250. * first we check the global in case a custom thumb is used
  251. */
  252. if (($td_temp_image_url[1] == '') and ($td_temp_image_url[2] == '')) {
  253. $td_thumb_parameters = td_api_thumb::get_by_id($thumbType);
  254. $td_temp_image_url[1] = $td_thumb_parameters['width'];
  255. $td_temp_image_url[2] = $td_thumb_parameters['height'];
  256. }
  257.  
  258.  
  259. $td_temp_image_url[0] = td_global::$get_template_directory_uri . '/images/no-thumb/' . $thumbType . '.png';
  260. $attachment_alt = 'alt=""';
  261. $attachment_title = '';
  262. } //end if ($this->post_has_thumb) {
  263.  
  264.  
  265.  
  266. $buffy .= '<div class="td-module-thumb">';
  267. if (current_user_can('edit_posts')) {
  268. $buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
  269. }
  270.  
  271.  
  272. if($new_tab == true ){
  273. $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '" target="_blank">';
  274. $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
  275.  
  276. // on videos add the play icon
  277. if (get_post_format($this->post->ID) == 'video') {
  278.  
  279. $use_small_post_format_icon_size = false;
  280. // search in all the thumbs for the one that we are currently using here and see if it has post_format_icon_size = small
  281. foreach (td_api_thumb::get_all() as $thumb_from_thumb_list) {
  282. if ($thumb_from_thumb_list['name'] == $thumbType and $thumb_from_thumb_list['post_format_icon_size'] == 'small') {
  283. $use_small_post_format_icon_size = true;
  284. break;
  285. }
  286. }
  287.  
  288. // load the small or medium play icon
  289. if ($use_small_post_format_icon_size === true) {
  290. $buffy .= '<span class="td-video-play-ico td-video-small"><img width="20" class="td-retina" src="' . td_global::$get_template_directory_uri . '/images/icons/video-small.png' . '" alt="video"/></span>';
  291. } else {
  292. $buffy .= '<span class="td-video-play-ico"><img width="40" class="td-retina" src="' . td_global::$get_template_directory_uri . '/images/icons/ico-video-large.png' . '" alt="video"/></span>';
  293. }
  294. } // end on video if
  295.  
  296. $buffy .= '</a>';
  297. }
  298.  
  299. else {
  300.  
  301. $buffy .= '<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  302. $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
  303.  
  304. // on videos add the play icon
  305. if (get_post_format($this->post->ID) == 'video') {
  306.  
  307. $use_small_post_format_icon_size = false;
  308. // search in all the thumbs for the one that we are currently using here and see if it has post_format_icon_size = small
  309. foreach (td_api_thumb::get_all() as $thumb_from_thumb_list) {
  310. if ($thumb_from_thumb_list['name'] == $thumbType and $thumb_from_thumb_list['post_format_icon_size'] == 'small') {
  311. $use_small_post_format_icon_size = true;
  312. break;
  313. }
  314. }
  315.  
  316. // load the small or medium play icon
  317. if ($use_small_post_format_icon_size === true) {
  318. $buffy .= '<span class="td-video-play-ico td-video-small"><img width="20" class="td-retina" src="' . td_global::$get_template_directory_uri . '/images/icons/video-small.png' . '" alt="video"/></span>';
  319. } else {
  320. $buffy .= '<span class="td-video-play-ico"><img width="40" class="td-retina" src="' . td_global::$get_template_directory_uri . '/images/icons/ico-video-large.png' . '" alt="video"/></span>';
  321. }
  322. } // end on video if
  323.  
  324. $buffy .= '</a>';
  325. }
  326. $buffy .= '</div>'; //end wrapper
  327.  
  328. return $buffy;
  329. }
  330. }
  331.  
  332.  
  333.  
  334. /**
  335. * This function returns the title with the appropriate markup.
  336. * @param string $cut_at - if provided, the method will just cut at that point
  337. * and it will cut after that. If not setting is in the database the function will cut at the default value
  338. * @return string
  339. */
  340.  
  341. function get_title($cut_at = '') {
  342. $buffy = '';
  343. $buffy .= '<h3 class="entry-title td-module-title">';
  344. $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  345.  
  346. //see if we have to cut the title and if we have the title lenght in the panel for ex: td_module_6__title_excerpt
  347. if ($cut_at != '') {
  348. //cut at the hard coded size
  349. $buffy .= td_util::excerpt($this->title, $cut_at, 'show_shortcodes');
  350.  
  351. } else {
  352. $current_module_class = get_class($this);
  353.  
  354. //see if we have a default setting for this module, and if so only apply it if we don't get other things form theme panel.
  355. if (td_api_module::_helper_check_excerpt_title($current_module_class)) {
  356. $db_title_excerpt = td_util::get_option($current_module_class . '_title_excerpt');
  357. if ($db_title_excerpt != '') {
  358. //cut from the database settings
  359. $buffy .= td_util::excerpt($this->title, $db_title_excerpt, 'show_shortcodes');
  360. } else {
  361. //cut at the default size
  362. $module_api = td_api_module::get_by_id($current_module_class);
  363. $buffy .= td_util::excerpt($this->title, $module_api['excerpt_title'], 'show_shortcodes');
  364. }
  365. } else {
  366. /**
  367. * no $cut_at provided and no setting in td_config -> return the full title
  368. * @see td_global::$modules_list
  369. */
  370. $buffy .= $this->title;
  371. }
  372.  
  373. }
  374. $buffy .='</a>';
  375. $buffy .= '</h3>';
  376. return $buffy;
  377. }
  378.  
  379.  
  380. /**
  381. * This method is used by modules to get content that has to be excerpted (cut)
  382. * IT RETURNS THE EXCERPT FROM THE POST IF IT'S ENTERED IN THE EXCERPT CUSTOM POST FIELD BY THE USER
  383. * @param string $cut_at - if provided the method will just cat at that point
  384. * @return string
  385. */
  386. function get_excerpt($cut_at = '') {
  387.  
  388. //If the user supplied the excerpt in the post excerpt custom field, we just return that
  389. if ($this->post->post_excerpt != '') {
  390. return $this->post->post_excerpt;
  391. }
  392.  
  393. $buffy = '';
  394. if ($cut_at != '') {
  395. // simple, $cut_at and return
  396. $buffy .= td_util::excerpt($this->post->post_content, $cut_at);
  397. } else {
  398. $current_module_class = get_class($this);
  399.  
  400. //see if we have a default setting for this module, and if so only apply it if we don't get other things form theme panel.
  401. if (td_api_module::_helper_check_excerpt_content($current_module_class)) {
  402. $db_content_excerpt = td_util::get_option($current_module_class . '_content_excerpt');
  403. if ($db_content_excerpt != '') {
  404. //cut from the database settings
  405. $buffy .= td_util::excerpt($this->post->post_content, $db_content_excerpt);
  406. } else {
  407. //cut at the default size
  408. $module_api = td_api_module::get_by_id($current_module_class);
  409. $buffy .= td_util::excerpt($this->post->post_content, $module_api['excerpt_content']);
  410. }
  411. } else {
  412. /**
  413. * no $cut_at provided and no setting in td_config -> return the full $this->post->post_content
  414. * @see td_global::$modules_list
  415. */
  416. $buffy .= $this->post->post_content;
  417. }
  418. }
  419. return $buffy;
  420. }
  421.  
  422.  
  423.  
  424. function get_category() {
  425. $buffy = '';
  426.  
  427. //read the post meta to get the custom primary category
  428. $td_post_theme_settings = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  429. if (!empty($td_post_theme_settings['td_primary_cat'])) {
  430. //we have a custom category selected
  431. $selected_category_obj = get_category($td_post_theme_settings['td_primary_cat']);
  432. } else {
  433.  
  434. //get one auto
  435. $categories = get_the_category($this->post->ID);
  436.  
  437.  
  438. $selected_category_obj = '';
  439.  
  440.  
  441. if (is_category()) {
  442. foreach ($categories as $category) {
  443. if ($category->term_id == get_query_var('cat')) {
  444. $selected_category_obj = $category;
  445. break;
  446. }
  447. }
  448. }
  449.  
  450.  
  451. if (empty($selected_category_obj) and !empty($categories[0])) {
  452. if ($categories[0]->name === TD_FEATURED_CAT and !empty($categories[1])) {
  453. $selected_category_obj = $categories[1];
  454. } else {
  455. $selected_category_obj = $categories[0];
  456. }
  457. }
  458.  
  459.  
  460.  
  461. }
  462.  
  463.  
  464. if (!empty($selected_category_obj)) { //@todo catch error here
  465. $buffy .= '<a href="' . get_category_link($selected_category_obj->cat_ID) . '" class="td-post-category">' . $selected_category_obj->name . '</a>' ;
  466. }
  467.  
  468. //return print_r($post, true);
  469. return $buffy;
  470. }
  471.  
  472.  
  473. //get quotes on blocks
  474. function get_quotes_on_blocks() {
  475.  
  476. // do not show the quote on WordPress loops
  477. if (td_global::$is_wordpress_loop === true or td_util::vc_get_column_number() != 1) {
  478. return '';
  479. }
  480.  
  481.  
  482. //get quotes data from database
  483. $post_data_from_db = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  484.  
  485. if(!empty($post_data_from_db['td_quote_on_blocks'])) {
  486. return '<div class="td_quote_on_blocks">' . $post_data_from_db['td_quote_on_blocks'] . '</div>';
  487. }
  488. }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment