Guest User

Untitled

a guest
Dec 4th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.62 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. function get_item_scope() {
  71. //used on modules - all the links are articles - google doesn't like multiple reviews on one page
  72. return 'itemscope itemtype="' . td_global::$http_or_https . '://schema.org/Article"';
  73. }
  74.  
  75.  
  76. /**
  77. * this method generates the item scope metadata for ALL the modules that appear on our theme.
  78. * NOTICE: - the item scope generated for single posts is located in @see td_module_single_base::get_item_scope_meta
  79. * - this method is not used on single posts pages
  80. * @updated 23 July 2015 by Ra
  81. * @return string
  82. */
  83. function get_item_scope_meta() {
  84. $buffy = ''; //the vampire slayer
  85.  
  86. // author
  87. $author_id = $this->post->post_author;
  88. $buffy .= '<meta itemprop="author" content = "' . esc_attr(get_the_author_meta('display_name', $author_id)) . '">';
  89.  
  90. // datePublished
  91. $td_article_date_unix = get_the_time('U', $this->post->ID);
  92. $buffy .= '<meta itemprop="datePublished" content="' . date(DATE_W3C, $td_article_date_unix) . '">';
  93.  
  94. // headline @todo we may improve this one to use the subtitle or excerpt? - We could not find specs about what it should be.
  95. $buffy .= '<meta itemprop="headline " content="' . esc_attr( $this->post->post_title) . '">';
  96.  
  97. if (!is_null($this->post_thumb_id)) {
  98. /**
  99. * from google documentation:
  100. * A URL, or list of URLs pointing to the representative image file(s). Images must be
  101. * at least 160x90 pixels and at most 1920x1080 pixels.
  102. * We recommend images in .jpg, .png, or. gif formats.
  103. * https://developers.google.com/structured-data/rich-snippets/articles
  104. */
  105. $td_image = wp_get_attachment_image_src($this->post_thumb_id, 'full');
  106. if (!empty($td_image[0])) {
  107. $buffy .= '<meta itemprop="image" content="' . esc_attr($td_image[0]) . '">';
  108. }
  109. }
  110.  
  111. // optional interaction count - basically here we add the comments number
  112. $buffy .= '<meta itemprop="interactionCount" content="UserComments:' . get_comments_number($this->post->ID) . '"/>';
  113. return $buffy;
  114. }
  115.  
  116.  
  117. function get_module_classes($additional_classes_array = '') {
  118. //add the wrap and module id class
  119. $buffy = get_class($this);
  120.  
  121.  
  122. // each module setting has a 'class' key to customize css
  123. $module_class = td_api_module::get_key(get_class($this), 'class');
  124.  
  125. if ($module_class != '') {
  126. $buffy .= ' ' . $module_class;
  127. }
  128.  
  129.  
  130. //show no thumb only if no thumb is detected and image placeholders are disabled
  131. if (is_null($this->post_thumb_id) and td_util::get_option('tds_hide_featured_image_placeholder') == 'hide_placeholder') {
  132. $buffy .= ' td_module_no_thumb';
  133. }
  134.  
  135. if ($additional_classes_array != '' && is_array($additional_classes_array)) {
  136. $buffy .= ' ' . implode(' ', $additional_classes_array);
  137. }
  138.  
  139. // the following case could not be checked
  140. // $buffy = implode(' ', array_unique(explode(' ', $buffy)));
  141.  
  142. return $buffy;
  143. }
  144.  
  145.  
  146. function get_author() {
  147. $buffy = '';
  148.  
  149. if ($this->is_review === false) {
  150. if (td_util::get_option('tds_p_show_author_name') != 'hide') {
  151. $buffy .= '<div class="td-post-author-name">';
  152. $buffy .= '<a itemprop="author" href="' . get_author_posts_url($this->post->post_author) . '">' . get_the_author_meta('display_name', $this->post->post_author) . '</a>' ;
  153. if (td_util::get_option('tds_p_show_author_name') != 'hide' and td_util::get_option('tds_p_show_date') != 'hide') {
  154. $buffy .= ' <span>-</span> ';
  155. }
  156. $buffy .= '</div>';
  157. }
  158.  
  159. }
  160. return $buffy;
  161.  
  162. }
  163.  
  164.  
  165. function get_date($show_stars_on_review = true) {
  166. $visibility_class = '';
  167. if (td_util::get_option('tds_p_show_date') == 'hide') {
  168. $visibility_class = ' td-visibility-hidden';
  169. }
  170.  
  171. $buffy = '';
  172. if ($this->is_review and $show_stars_on_review === true) {
  173. //if review show stars
  174. $buffy .= '<div class="entry-review-stars">';
  175. $buffy .= td_review::render_stars($this->td_review);
  176. $buffy .= '</div>';
  177.  
  178. } else {
  179. if (td_util::get_option('tds_p_show_date') != 'hide') {
  180. $td_article_date_unix = get_the_time('U', $this->post->ID);
  181. $buffy .= '<div class="td-post-date">';
  182. $buffy .= '<time itemprop="dateCreated" 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>';
  183. $buffy .= '<meta itemprop="interactionCount" content="UserComments:' . get_comments_number($this->post->ID) . '"/>';
  184. $buffy .= '</div>';
  185. }
  186. }
  187.  
  188. return $buffy;
  189. }
  190.  
  191. function get_comments() {
  192. $buffy = '';
  193. if (td_util::get_option('tds_p_show_comments') != 'hide') {
  194. $buffy .= '<div class="td-module-comments">';
  195. $buffy .= '<a href="' . get_comments_link($this->post->ID) . '">';
  196. $buffy .= get_comments_number($this->post->ID);
  197. $buffy .= '</a>';
  198. $buffy .= '</div>';
  199. }
  200.  
  201. return $buffy;
  202. }
  203.  
  204.  
  205.  
  206. /**
  207. * get image - v 3.0 23 ian 2015
  208. * @param $thumbType
  209. * @return string
  210. */
  211. function get_image($thumbType) {
  212. $buffy = ''; //the output buffer
  213. $tds_hide_featured_image_placeholder = td_util::get_option('tds_hide_featured_image_placeholder');
  214.  
  215. // do we have a post thumb or a placeholder?
  216. if (!is_null($this->post_thumb_id) or ($tds_hide_featured_image_placeholder != 'hide_placeholder')) {
  217.  
  218. if (!is_null($this->post_thumb_id)) {
  219. //if we have a thumb
  220. // check to see if the thumb size is enabled in the panel, we don't have to check for the default wordpress
  221. // thumbs (the default ones are already cut and we don't have a panel setting for them)
  222. if (td_util::get_option('tds_thumb_' . $thumbType) != 'yes' and $thumbType != 'thumbnail') {
  223. //the thumb is disabled, show a placeholder thumb from the theme with the "thumb disabled" message
  224. global $_wp_additional_image_sizes;
  225.  
  226. if (empty($_wp_additional_image_sizes[$thumbType]['width'])) {
  227. $td_temp_image_url[1] = '';
  228. } else {
  229. $td_temp_image_url[1] = $_wp_additional_image_sizes[$thumbType]['width'];
  230. }
  231.  
  232. if (empty($_wp_additional_image_sizes[$thumbType]['height'])) {
  233. $td_temp_image_url[2] = '';
  234. } else {
  235. $td_temp_image_url[2] = $_wp_additional_image_sizes[$thumbType]['height'];
  236. }
  237.  
  238.  
  239. $td_temp_image_url[0] = td_global::$get_template_directory_uri . '/images/thumb-disabled/' . $thumbType . '.png';
  240. $attachment_alt = 'alt=""';
  241. $attachment_title = '';
  242.  
  243. } else {
  244. // the thumb is enabled from the panel, it's time to show the real thumb
  245. $td_temp_image_url = wp_get_attachment_image_src($this->post_thumb_id, $thumbType);
  246. $attachment_alt = get_post_meta($this->post_thumb_id, '_wp_attachment_image_alt', true );
  247. $attachment_alt = 'alt="' . esc_attr(strip_tags($attachment_alt)) . '"';
  248. $attachment_title = ' title="' . esc_attr(strip_tags($this->title)) . '"';
  249.  
  250. if (empty($td_temp_image_url[0])) {
  251. $td_temp_image_url[0] = '';
  252. }
  253.  
  254. if (empty($td_temp_image_url[1])) {
  255. $td_temp_image_url[1] = '';
  256. }
  257.  
  258. if (empty($td_temp_image_url[2])) {
  259. $td_temp_image_url[2] = '';
  260. }
  261. } // end panel thumb enabled check
  262.  
  263.  
  264.  
  265. } else {
  266. //we have no thumb but the placeholder one is activated
  267. global $_wp_additional_image_sizes;
  268.  
  269. if (empty($_wp_additional_image_sizes[$thumbType]['width'])) {
  270. $td_temp_image_url[1] = '';
  271. } else {
  272. $td_temp_image_url[1] = $_wp_additional_image_sizes[$thumbType]['width'];
  273. }
  274.  
  275. if (empty($_wp_additional_image_sizes[$thumbType]['height'])) {
  276. $td_temp_image_url[2] = '';
  277. } else {
  278. $td_temp_image_url[2] = $_wp_additional_image_sizes[$thumbType]['height'];
  279. }
  280.  
  281. /**
  282. * get thumb height and width via api
  283. * first we check the global in case a custom thumb is used
  284. */
  285. if (($td_temp_image_url[1] == '') and ($td_temp_image_url[2] == '')) {
  286. $td_thumb_parameters = td_api_thumb::get_by_id($thumbType);
  287. $td_temp_image_url[1] = $td_thumb_parameters['width'];
  288. $td_temp_image_url[2] = $td_thumb_parameters['height'];
  289. }
  290.  
  291.  
  292. $td_temp_image_url[0] = td_global::$get_template_directory_uri . '/images/no-thumb/' . $thumbType . '.png';
  293. $attachment_alt = 'alt=""';
  294. $attachment_title = '';
  295. } //end if ($this->post_has_thumb) {
  296.  
  297.  
  298.  
  299. $buffy .= '<div class="td-module-thumb">';
  300. if (current_user_can('edit_posts')) {
  301. $buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
  302. }
  303.  
  304. $buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  305. $buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
  306.  
  307. // on videos add the play icon
  308. if (get_post_format($this->post->ID) == 'video') {
  309.  
  310. $use_small_post_format_icon_size = false;
  311. // search in all the thumbs for the one that we are currently using here and see if it has post_format_icon_size = small
  312. foreach (td_api_thumb::get_all() as $thumb_from_thumb_list) {
  313. if ($thumb_from_thumb_list['name'] == $thumbType and $thumb_from_thumb_list['post_format_icon_size'] == 'small') {
  314. $use_small_post_format_icon_size = true;
  315. break;
  316. }
  317. }
  318.  
  319. // load the small or medium play icon
  320. if ($use_small_post_format_icon_size === true) {
  321. $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>';
  322. } else {
  323. $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>';
  324. }
  325. } // end on video if
  326.  
  327. $buffy .= '</a>';
  328. $buffy .= '</div>'; //end wrapper
  329.  
  330. return $buffy;
  331. }
  332. }
  333.  
  334.  
  335.  
  336. /**
  337. * This function returns the title with the appropriate markup.
  338. * @param string $cut_at - if provided, the method will just cut at that point
  339. * and it will cut after that. If not setting is in the database the function will cut at the default value
  340. * @return string
  341. */
  342.  
  343. function get_title($cut_at = '') {
  344. $buffy = '';
  345. $buffy .= '<h3 itemprop="name" class="entry-title td-module-title">';
  346. $buffy .='<a itemprop="url" href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
  347.  
  348. //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
  349. if ($cut_at != '') {
  350. //cut at the hard coded size
  351. $buffy .= td_util::excerpt($this->title, $cut_at, 'show_shortcodes');
  352.  
  353. } else {
  354. $current_module_class = get_class($this);
  355.  
  356. //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.
  357. if (td_api_module::_helper_check_excerpt_title($current_module_class)) {
  358. $db_title_excerpt = td_util::get_option($current_module_class . '_title_excerpt');
  359. if ($db_title_excerpt != '') {
  360. //cut from the database settings
  361. $buffy .= td_util::excerpt($this->title, $db_title_excerpt, 'show_shortcodes');
  362. } else {
  363. //cut at the default size
  364. $module_api = td_api_module::get_by_id($current_module_class);
  365. $buffy .= td_util::excerpt($this->title, $module_api['excerpt_title'], 'show_shortcodes');
  366. }
  367. } else {
  368. /**
  369. * no $cut_at provided and no setting in td_config -> return the full title
  370. * @see td_global::$modules_list
  371. */
  372. $buffy .= $this->title;
  373. }
  374.  
  375. }
  376. $buffy .='</a>';
  377. $buffy .= '</h3>';
  378. return $buffy;
  379. }
  380.  
  381.  
  382. /**
  383. * This method is used by modules to get content that has to be excerpted (cut)
  384. * IT RETURNS THE EXCERPT FROM THE POST IF IT'S ENTERED IN THE EXCERPT CUSTOM POST FIELD BY THE USER
  385. * @param string $cut_at - if provided the method will just cat at that point
  386. * @return string
  387. */
  388. function get_excerpt($cut_at = '') {
  389.  
  390. //If the user supplied the excerpt in the post excerpt custom field, we just return that
  391. if ($this->post->post_excerpt != '') {
  392. return $this->post->post_excerpt;
  393. }
  394.  
  395. $buffy = '';
  396. if ($cut_at != '') {
  397. // simple, $cut_at and return
  398. $buffy .= td_util::excerpt($this->post->post_content, $cut_at);
  399. } else {
  400. $current_module_class = get_class($this);
  401.  
  402. //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.
  403. if (td_api_module::_helper_check_excerpt_content($current_module_class)) {
  404. $db_content_excerpt = td_util::get_option($current_module_class . '_content_excerpt');
  405. if ($db_content_excerpt != '') {
  406. //cut from the database settings
  407. $buffy .= td_util::excerpt($this->post->post_content, $db_content_excerpt);
  408. } else {
  409. //cut at the default size
  410. $module_api = td_api_module::get_by_id($current_module_class);
  411. $buffy .= td_util::excerpt($this->post->post_content, $module_api['excerpt_content']);
  412. }
  413. } else {
  414. /**
  415. * no $cut_at provided and no setting in td_config -> return the full $this->post->post_content
  416. * @see td_global::$modules_list
  417. */
  418. $buffy .= $this->post->post_content;
  419. }
  420. }
  421. return $buffy;
  422. }
  423.  
  424.  
  425.  
  426. function get_category() {
  427. $terms_ui_array = array();
  428.  
  429.  
  430. if ($this->post->post_type != 'post') {
  431.  
  432. // CUSTOM POST TYPES - on custom post types, we retrieve the taxonomy setting from the panel and we show
  433. // only the ones that are selected in wp-admin, just like a normal theme
  434. $category_spot_taxonomy = td_util::get_ctp_option($this->post->post_type, 'tds_category_spot_taxonomy');
  435. $terms_for_category_spot = wp_get_post_terms($this->post->ID, $category_spot_taxonomy);
  436. foreach ($terms_for_category_spot as $term_for_category_spot) {
  437. $term_for_category_spot_url = get_term_link($term_for_category_spot, $category_spot_taxonomy);
  438. if (!is_wp_error($term_for_category_spot_url)) {
  439. $terms_ui_array[ $term_for_category_spot->name ] = array(
  440. 'color' => '',
  441. 'link' => $term_for_category_spot_url,
  442. 'hide_on_post' => ''
  443. );
  444. }
  445. }
  446.  
  447. } else {
  448. // POST TYPE - here we work with categories
  449. // NOTE: due to legacy, the theme also outputs the parent of the first category if available
  450. $categories = get_the_category( $this->post->ID );
  451. if (!empty($categories)) {
  452. foreach ( $categories as $category ) {
  453. if ( $category->name != TD_FEATURED_CAT ) { //ignore the featured category name
  454. //get the parent of this cat
  455. $td_parent_cat_obj = get_category( $category->category_parent );
  456.  
  457. //if we have a parent, shot it first
  458. if ( ! empty( $td_parent_cat_obj->name ) ) {
  459. $tax_meta__color_parent = td_util::get_category_option( $td_parent_cat_obj->cat_ID, 'tdc_color' );//swich by RADU A, get_tax_meta($td_parent_cat_obj->cat_ID,'tdc_color');
  460. $tax_meta__hide_on_post_parent = td_util::get_category_option( $td_parent_cat_obj->cat_ID, 'tdc_hide_on_post' );//swich by RADU A, get_tax_meta($td_parent_cat_obj->cat_ID,'tdc_hide_on_post');
  461. $terms_ui_array[ $td_parent_cat_obj->name ] = array(
  462. 'color' => $tax_meta__color_parent,
  463. 'link' => get_category_link( $td_parent_cat_obj->cat_ID ),
  464. 'hide_on_post' => $tax_meta__hide_on_post_parent
  465. );
  466. }
  467.  
  468. //show the category, only if we didn't already showed the parent
  469. $tax_meta_color = td_util::get_category_option( $category->cat_ID, 'tdc_color' );//swich by RADU A, get_tax_meta($category->cat_ID,'tdc_color');
  470. $tax_meta__hide_on_post_parent = td_util::get_category_option( $category->cat_ID, 'tdc_hide_on_post' );//swich by RADU A, get_tax_meta($category->cat_ID,'tdc_hide_on_post');
  471. $terms_ui_array[ $category->name ] = array(
  472. 'color' => $tax_meta_color,
  473. 'link' => get_category_link( $category->cat_ID ),
  474. 'hide_on_post' => $tax_meta__hide_on_post_parent
  475. );
  476. }
  477. }
  478. }
  479. }
  480.  
  481.  
  482. /**
  483. * output stage
  484. * we go in with an array of:
  485. * array (
  486. * $terms_ui_array[category_name] = array (
  487. * 'color' => '',
  488. * 'link' => ''
  489. * 'hide_on_post'
  490. * )
  491. * )
  492. */
  493. $buffy = '';
  494. if (td_util::get_option('tds_p_categories_tags') != 'hide') {
  495. $buffy .= '<ul class="td-category">';
  496.  
  497.  
  498. foreach ( $terms_ui_array as $term_name => $term_params ) {
  499. if ( $term_params['hide_on_post'] == 'hide' ) {
  500. continue;
  501. }
  502.  
  503. if ( ! empty( $term_params['color'] ) ) {
  504. // set title color based on background color contrast
  505. //$td_cat_title_color = $this->readableColour($term_params['color']);
  506. $td_cat_color = ' style="background-color:' . $term_params['color'] . '; color:' . $td_cat_title_color . '; border-color:' . $term_params['color'] . ';"';
  507. } else {
  508. $td_cat_color = '';
  509. }
  510.  
  511.  
  512. $buffy .= '<li class="entry-category"><a ' . $td_cat_color . ' href="' . $term_params['link'] . '">' . $term_name . '</a></li>';
  513. }
  514. $buffy .= '</ul>';
  515. }
  516.  
  517. return $buffy;
  518. }
  519.  
  520.  
  521. //get quotes on blocks
  522. function get_quotes_on_blocks() {
  523.  
  524. // do not show the quote on WordPress loops
  525. if (td_global::$is_wordpress_loop === true or td_util::vc_get_column_number() != 1) {
  526. return '';
  527. }
  528.  
  529.  
  530. //get quotes data from database
  531. $post_data_from_db = get_post_meta($this->post->ID, 'td_post_theme_settings', true);
  532.  
  533. if(!empty($post_data_from_db['td_quote_on_blocks'])) {
  534. return '<div class="td_quote_on_blocks">' . $post_data_from_db['td_quote_on_blocks'] . '</div>';
  535. }
  536. }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment