Guest User

Untitled

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