Guest User

Untitled

a guest
Feb 23rd, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.98 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, $no_link_image = false) {
  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 ($no_link_image == true){
  273. $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 . '/>';
  274.  
  275. // on videos add the play icon
  276. if (get_post_format($this->post->ID) == 'video') {
  277.  
  278. $use_small_post_format_icon_size = false;
  279. // search in all the thumbs for the one that we are currently using here and see if it has post_format_icon_size = small
  280. foreach (td_api_thumb::get_all() as $thumb_from_thumb_list) {
  281. if ($thumb_from_thumb_list['name'] == $thumbType and $thumb_from_thumb_list['post_format_icon_size'] == 'small') {
  282. $use_small_post_format_icon_size = true;
  283. break;
  284. }
  285. }
  286.  
  287. // load the small or medium play icon
  288. if ($use_small_post_format_icon_size === true) {
  289. $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>';
  290. } else {
  291. $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>';
  292. }
  293. } // end on video if
  294. }
  295. else{
  296. $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  297. $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 . '/>';
  298.  
  299. // on videos add the play icon
  300. if (get_post_format($this->post->ID) == 'video') {
  301.  
  302. $use_small_post_format_icon_size = false;
  303. // search in all the thumbs for the one that we are currently using here and see if it has post_format_icon_size = small
  304. foreach (td_api_thumb::get_all() as $thumb_from_thumb_list) {
  305. if ($thumb_from_thumb_list['name'] == $thumbType and $thumb_from_thumb_list['post_format_icon_size'] == 'small') {
  306. $use_small_post_format_icon_size = true;
  307. break;
  308. }
  309. }
  310.  
  311. // load the small or medium play icon
  312. if ($use_small_post_format_icon_size === true) {
  313. $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>';
  314. } else {
  315. $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>';
  316. }
  317. } // end on video if
  318.  
  319. $buffy .= '</a>';
  320. }
  321.  
  322. $buffy .= '</div>'; //end wrapper
  323.  
  324. return $buffy;
  325. }
  326. }
  327.  
  328.  
  329.  
  330. /**
  331. * This function returns the title with the appropriate markup.
  332. * @param string $cut_at - if provided, the method will just cut at that point
  333. * and it will cut after that. If not setting is in the database the function will cut at the default value
  334. * @return string
  335. */
  336.  
  337. function get_title($cut_at = '') {
  338. $buffy = '';
  339. $buffy .= '<h3 class="entry-title td-module-title">';
  340. $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  341.  
  342. //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
  343. if ($cut_at != '') {
  344. //cut at the hard coded size
  345. $buffy .= td_util::excerpt($this->title, $cut_at, 'show_shortcodes');
  346.  
  347. } else {
  348. $current_module_class = get_class($this);
  349.  
  350. //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.
  351. if (td_api_module::_helper_check_excerpt_title($current_module_class)) {
  352. $db_title_excerpt = td_util::get_option($current_module_class . '_title_excerpt');
  353. if ($db_title_excerpt != '') {
  354. //cut from the database settings
  355. $buffy .= td_util::excerpt($this->title, $db_title_excerpt, 'show_shortcodes');
  356. } else {
  357. //cut at the default size
  358. $module_api = td_api_module::get_by_id($current_module_class);
  359. $buffy .= td_util::excerpt($this->title, $module_api['excerpt_title'], 'show_shortcodes');
  360. }
  361. } else {
  362. /**
  363. * no $cut_at provided and no setting in td_config -> return the full title
  364. * @see td_global::$modules_list
  365. */
  366. $buffy .= $this->title;
  367. }
  368.  
  369. }
  370. $buffy .='</a>';
  371. $buffy .= '</h3>';
  372. return $buffy;
  373. }
  374.  
  375.  
  376. /**
  377. * This method is used by modules to get content that has to be excerpted (cut)
  378. * IT RETURNS THE EXCERPT FROM THE POST IF IT'S ENTERED IN THE EXCERPT CUSTOM POST FIELD BY THE USER
  379. * @param string $cut_at - if provided the method will just cat at that point
  380. * @return string
  381. */
  382. function get_excerpt($cut_at = '') {
  383.  
  384. //If the user supplied the excerpt in the post excerpt custom field, we just return that
  385. if ($this->post->post_excerpt != '') {
  386. return $this->post->post_excerpt;
  387. }
  388.  
  389. $buffy = '';
  390. if ($cut_at != '') {
  391. // simple, $cut_at and return
  392. $buffy .= td_util::excerpt($this->post->post_content, $cut_at);
  393. } else {
  394. $current_module_class = get_class($this);
  395.  
  396. //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.
  397. if (td_api_module::_helper_check_excerpt_content($current_module_class)) {
  398. $db_content_excerpt = td_util::get_option($current_module_class . '_content_excerpt');
  399. if ($db_content_excerpt != '') {
  400. //cut from the database settings
  401. $buffy .= td_util::excerpt($this->post->post_content, $db_content_excerpt);
  402. } else {
  403. //cut at the default size
  404. $module_api = td_api_module::get_by_id($current_module_class);
  405. $buffy .= td_util::excerpt($this->post->post_content, $module_api['excerpt_content']);
  406. }
  407. } else {
  408. /**
  409. * no $cut_at provided and no setting in td_config -> return the full $this->post->post_content
  410. * @see td_global::$modules_list
  411. */
  412. $buffy .= $this->post->post_content;
  413. }
  414. }
  415. return $buffy;
  416. }
  417.  
  418.  
  419.  
  420. function get_category() {
  421. $buffy = '';
  422.  
  423. //read the post meta to get the custom primary category
  424. $td_post_theme_settings = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  425. if (!empty($td_post_theme_settings['td_primary_cat'])) {
  426. //we have a custom category selected
  427. $selected_category_obj = get_category($td_post_theme_settings['td_primary_cat']);
  428. } else {
  429.  
  430. //get one auto
  431. $categories = get_the_category($this->post->ID);
  432.  
  433.  
  434. $selected_category_obj = '';
  435.  
  436.  
  437. if (is_category()) {
  438. foreach ($categories as $category) {
  439. if ($category->term_id == get_query_var('cat')) {
  440. $selected_category_obj = $category;
  441. break;
  442. }
  443. }
  444. }
  445.  
  446.  
  447. if (empty($selected_category_obj) and !empty($categories[0])) {
  448. if ($categories[0]->name === TD_FEATURED_CAT and !empty($categories[1])) {
  449. $selected_category_obj = $categories[1];
  450. } else {
  451. $selected_category_obj = $categories[0];
  452. }
  453. }
  454.  
  455.  
  456.  
  457. }
  458.  
  459.  
  460. if (!empty($selected_category_obj)) { //@todo catch error here
  461. $buffy .= '<a href="' . get_category_link($selected_category_obj->cat_ID) . '" class="td-post-category">' . $selected_category_obj->name . '</a>' ;
  462. }
  463.  
  464. //return print_r($post, true);
  465. return $buffy;
  466. }
  467.  
  468.  
  469. //get quotes on blocks
  470. function get_quotes_on_blocks() {
  471.  
  472. // do not show the quote on WordPress loops
  473. if (td_global::$is_wordpress_loop === true or td_util::vc_get_column_number() != 1) {
  474. return '';
  475. }
  476.  
  477.  
  478. //get quotes data from database
  479. $post_data_from_db = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  480.  
  481. if(!empty($post_data_from_db['td_quote_on_blocks'])) {
  482. return '<div class="td_quote_on_blocks">' . $post_data_from_db['td_quote_on_blocks'] . '</div>';
  483. }
  484. }
  485. }
Advertisement
Add Comment
Please, Sign In to add comment