PluginProbe ʕ •ᴥ•ʔ
Timeline Blocks / 2.0.0
Timeline Blocks v2.0.0
2.0.1 2.0.0 trunk
timeline-blocks / src / blocks / index.php
timeline-blocks / src / blocks Last commit date
components 1 week ago styles 1 week ago edit.js 1 week ago index.js 1 week ago index.php 1 week ago tb_generatecss.js 1 week ago tb_styling.js 1 week ago
index.php
851 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4 }
5
6 /**
7 * Server-side rendering for the post template block
8 *
9 * @since 1.0.0
10 * @package Timeline Blocks for Gutenberg
11 */
12
13 /**
14 * Renders the post block on server.
15 */
16 function timeline_block_render_block_core_latest_posts($attributes) {
17 $list_items_markup = $post_thumb_id = '';
18 $template = $attributes['layoutcount'];
19
20 if ($attributes['layoutcount']) {
21 switch ($template) {
22 case '1' :
23 $list_items_markup = timeline_blocks_layout1($attributes);
24 break;
25 case '2' :
26 $list_items_markup = timeline_blocks_layout2($attributes);
27 break;
28 }
29 }
30
31 // Build the classes
32 $class = "tb-timeline-template{$attributes['layoutcount']} align-{$attributes['align']}";
33
34 $block_id = 'tb_post_layouts-' . $attributes['block_id'];
35
36 if (isset($attributes['className'])) {
37 $class .= ' ' . $attributes['className'];
38 }
39 $timeline_class = 'tb-timeline';
40
41 // Output the post markup
42 $block_content = sprintf('<div id="%1$s" class="%2$s"><div class="%3$s">%4$s</div></div>',esc_attr($block_id), esc_attr($class), esc_attr($timeline_class), $list_items_markup);
43
44 return $block_content;
45 }
46
47 /*
48 * Get the featured image
49 */
50
51 function timeline_block_get_featured_image($post_id, $attributes) {
52 // Get the post thumbnail
53 $fetured_image = '';
54 $post_thumb_id = get_post_thumbnail_id($post_id);
55 if (isset($attributes['displayPostImage']) && $attributes['displayPostImage'] && $post_thumb_id) {
56 if ($attributes['imageCrop'] === 'landscape') {
57 $post_thumb_size = 'tb-blogpost-landscape';
58 } else {
59 $post_thumb_size = 'tb-blogpost-square';
60 }
61
62 $fetured_image = sprintf('<div class="tb-image"><a href="%1$s" rel="bookmark">%2$s</a></div>', esc_url(get_permalink($post_id)), wp_get_attachment_image($post_thumb_id, $post_thumb_size));
63 }
64 return $fetured_image;
65 }
66
67 /*
68 * Get the category of post
69 */
70
71 function timeline_block_get_category($post_id, $seprator) {
72 $categories_list = get_the_category_list($seprator, " ", $post_id);
73 $category = "";
74
75 if (!empty($categories_list)){
76 $category = sprintf('<div class="tb-timeline-category-link tb-inline"> %1$s </div> ', $categories_list);
77 return $category;
78 }
79 }
80
81 /*
82 * Get the excerpt
83 */
84
85 function timeline_block_get_excerpt($post_id, $post_query, $attributes) {
86 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
87 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id, 'display'));
88
89 if (!isset($attributes['wordsExcerpt'])) {
90 $wordsExcerpt = 25;
91 } else {
92 $wordsExcerpt = $attributes['wordsExcerpt'];
93 }
94
95 if (empty($excerpt)) {
96 $excerpt = apply_filters('the_excerpt', wp_trim_words(get_the_content(), $wordsExcerpt));
97 }
98
99 if (!$excerpt) {
100 $excerpt = null;
101 }
102
103 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
104 $excerpt_data = wp_kses_post($excerpt ?? '');
105 }
106 }
107 return $excerpt_data;
108 }
109
110 /*
111 * Get the post author
112 */
113
114 function timeline_block_get_tags_get_author( $post_id, $post ) {
115 $list_items_markup = sprintf( '<a href="%2$s">%1$s</a></span></span>', esc_html( get_the_author_meta( 'display_name', $post->post_author ) ), esc_url( get_author_posts_url( $post->post_author ) ) );
116 return $list_items_markup;
117 }
118
119 /*
120 * Get the post tags
121 */
122
123 function timeline_block_get_tags($post_id, $tag_text) {
124 $list_items_markup= '';
125 $tags_list = get_the_tag_list("", ", ", "", $post_id);
126 if (!empty($tags_list)) {
127 if(!empty($tag_text)){
128 $list_items_markup .= sprintf('<div class="tb-timeline-post-tags"><span class="link-label">%1$s </span> %2$s </div> ', $tag_text, $tags_list);
129 }else{
130 $list_items_markup .= sprintf('<div class="tb-timeline-post-tags"> %1$s </div> ', $tags_list);
131 }
132 return $list_items_markup;
133 }
134 }
135 /*
136 * Get the post social share icon
137 */
138
139 function timeline_block_get_social_share_icons( $post_id ) {
140 $social_share_info = sprintf( '<a data-share="facebook" href="https://www.facebook.com/sharer.php?u=%1$s" class="tb-facebook-share social-share-default tb-social-share" target="_blank"><i class="fab fa-facebook-f" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $post_id ) ) );
141 $social_share_info .= sprintf( '<a data-share="twitter" href="https://twitter.com/share?url=%1$s" class="tb-twitter-share social-share-default tb-social-share" target="_blank"><i class="fab fa-twitter" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $post_id ) ) );
142 $social_share_info .= sprintf( '<a data-share="linkedin" href="https://www.linkedin.com/shareArticle?url=%1$s" class="tb-linkedin-share social-share-default tb-social-share" target="_blank"><i class="fab fa-linkedin-in" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $post_id ) ) );
143 return $social_share_info;
144 }
145
146 /*
147 * Timeline layout 1
148 */
149
150 function timeline_blocks_layout1($attributes) {
151 $post_thumb_id = '';
152 $list_items_markup= '';
153
154 $recent_posts = array(
155 'posts_per_page' => $attributes['postsToShow'],
156 'post_status' => 'publish',
157 'order' => $attributes['order'],
158 'orderby' => $attributes['orderBy'],
159 'cat' => !empty($attributes['categories']) ? $attributes['categories'] : '',
160 );
161
162 $post_query = new WP_Query($recent_posts);
163
164 $list_items_markup = $post_thumb_id = '';
165
166 //loop the query
167 if ($post_query->have_posts()) {
168
169 //loop the query
170 while ($post_query->have_posts()) {
171
172 $post_query->the_post();
173
174 // Get the post ID
175 $post_id = get_the_ID();
176
177 $post_thumb_id = get_post_thumbnail_id($post_id);
178
179 if ($post_thumb_id && isset($attributes['displayPostImage']) && $attributes['displayPostImage']) {
180 $post_thumb_class = 'has-thumb';
181 } else {
182 $post_thumb_class = 'no-thumb';
183 }
184
185 // Start the markup for the post
186 $list_items_markup .= sprintf('<article class="%1$s tb-timeline-item">',esc_attr($post_thumb_class));
187 $list_items_markup .= sprintf('<div class="tb-timeline-content">');
188 $list_items_markup .= sprintf ('<div class="tb-first-inner-wrap">', esc_attr($post_thumb_class));
189
190 // Get the post title
191 $title = get_the_title($post_id);
192 if (!$title) {
193 $title = __('Untitled', 'timeline-blocks');
194 }
195
196 //Get title tag
197 $allowed_tags = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'div');
198 if (isset($attributes['titleTag']) && in_array($attributes['titleTag'], $allowed_tags, true)) {
199 $post_title_tag = $attributes['titleTag'];
200 } else {
201 $post_title_tag = 'h2';
202 }
203 $list_items_markup .= sprintf('<div class="tb-timeline-title-wrap"><'.$post_title_tag.' class="tb-title"><a href="%1$s" rel="bookmark" class="tb-timeline-title tb-layout-1">%2$s</a></'.$post_title_tag.'></div>',esc_url(get_permalink($post_id)), esc_html($title));
204 $list_items_markup .= timeline_block_get_featured_image($post_id,$attributes);
205
206 $list_items_markup .= sprintf('<div class="tb-timeline-second-content-wrap" >');
207
208 $list_items_markup .= sprintf('<div class="tb-content-wrap" >');
209 // Wrap the text content
210 $list_items_markup .= sprintf('<div class="tb-category-link-wraper">');
211 if (isset($attributes['displayPostCategory']) && $attributes['displayPostCategory']) {
212 $list_items_markup .= timeline_block_get_category($post_id,", ");
213 }
214 $list_items_markup .= sprintf( '</div>');
215
216 // Wrap the byline content
217 $list_items_markup .= sprintf('<div class="tb-timeline-byline"> <div class="tb-timeline-metadatabox">');
218 if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) {
219 $list_items_markup .= sprintf('<div class="post-author"><i class="fas fa-pencil-alt"></i><span class="tb-blogpost-author">');
220 $list_items_markup .= timeline_block_get_author($post_id);
221 $list_items_markup .= sprintf('</div>');
222 }
223
224 // Get the post date
225 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
226 $list_items_markup .= sprintf('<div class="mdate "><i class="fas fa-calendar-alt"></i> %1$s </div>', get_the_date('F, Y', $post_id));
227 }
228
229 //Get comments
230 $comments = get_comments_number($post_id);
231 if ($comments > 0 ){
232 $number_commnet = '<i class="fas fa-comment"></i>' . $comments;
233 }else{
234 $number_commnet = '<i class="fas fa-comment"></i> 0' ;
235 }
236 if (isset($attributes['displayPostComments']) && $attributes['displayPostComments']) {
237 $list_items_markup .= sprintf('<div class="post-comments "> %1$s </div>', $number_commnet);
238 }
239
240 // Wrap the excerpt content
241 $list_items_markup .= sprintf('<div class="tb-timeline-text">');
242 $list_items_markup .= sprintf('<div class="tb-timeline-excerpt">');
243 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
244 $list_items_markup .= timeline_block_get_excerpt($post_id,$post_query,$attributes);
245 }
246
247 //Display Readmore
248 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
249
250 if ($attributes['readmoreView'] == 'text-only') {
251
252 $list_items_markup .= sprintf('<div class="tb-text-only"><a class="tb-blogpost-link" href="%1$s" rel="bookmark">%2$s</a></div>', esc_url(get_permalink($post_id)), esc_html($attributes['readMoreText']));
253 } else if ($attributes['readmoreView'] == 'tb-button') {
254
255 $list_items_markup .= sprintf('<div class="tb-button-view"><a class="tb-button tb-link gb-text-link" href="%1$s" rel="bookmark">%2$s</a></div>', esc_url(get_permalink($post_id)), esc_html($attributes['readMoreText']));
256 }
257 }
258
259 // Close the excerpt content
260 $list_items_markup .= sprintf('</div></div><div class="tb-timeline-bototm-wrap">');
261 if (isset($attributes['displayPostTag']) && $attributes['displayPostTag']) {
262 $list_items_markup .= timeline_block_get_tags($post_id,'');
263 }
264 $list_items_markup .= sprintf('<div class="tb-timeline-social-wrap"><div class="social-share-data">');
265 if (isset($attributes['displayPostSocialshare']) && $attributes['displayPostSocialshare']) {
266 $list_items_markup .= timeline_block_get_social_share_icons($post_id);
267 }
268 $list_items_markup .= sprintf('</div></div></div></div>');
269 $list_items_markup .= "</article>\n";
270 }
271 }
272
273 return $list_items_markup;
274 }
275
276 /*
277 * Timeline layout 2
278 */
279
280 function timeline_blocks_layout2($attributes) {
281 $post_thumb_id = '';
282 $list_items_markup= '';
283
284 $args = array(
285 'posts_per_page' => $attributes['postsToShow'],
286 'post_status' => 'publish',
287 'order' => $attributes['order'],
288 'orderby' => $attributes['orderBy'],
289 'cat' => !empty($attributes['categories']) ? $attributes['categories'] : '',
290 );
291
292 $post_query = new WP_Query($args);
293
294 $list_items_markup = $post_thumb_id = '';
295
296 if ($post_query->have_posts()) {
297
298 //loop the query
299 while ($post_query->have_posts()) {
300
301 $post_query->the_post();
302
303 // Get the post ID
304 $post_id = get_the_ID();
305
306 $post_thumb_id = get_post_thumbnail_id($post_id);
307
308 if ($post_thumb_id && isset($attributes['displayPostImage']) && $attributes['displayPostImage']) {
309 $post_thumb_class = 'has-thumb';
310 } else {
311 $post_thumb_class = 'no-thumb';
312 }
313 // Start the markup for the post
314 $list_items_markup .= sprintf('<article class="%1$s tb-timeline-item">',esc_attr($post_thumb_class));
315 $list_items_markup .= sprintf('<div class="timeline-icon"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
316 width="21px" height="20px" viewBox="0 0 21 20" enable-background="new 0 0 21 20" xml:space="preserve">
317 <path fill="#FFFFFF" d="M19.998,6.766l-5.759-0.544c-0.362-0.032-0.676-0.264-0.822-0.61l-2.064-4.999
318 c-0.329-0.825-1.5-0.825-1.83,0L7.476,5.611c-0.132,0.346-0.462,0.578-0.824,0.61L0.894,6.766C0.035,6.848-0.312,7.921,0.333,8.499
319 l4.338,3.811c0.279,0.246,0.395,0.609,0.314,0.975l-1.304,5.345c-0.199,0.842,0.708,1.534,1.468,1.089l4.801-2.822
320 c0.313-0.181,0.695-0.181,1.006,0l4.803,2.822c0.759,0.445,1.666-0.23,1.468-1.089l-1.288-5.345
321 c-0.081-0.365,0.035-0.729,0.313-0.975l4.34-3.811C21.219,7.921,20.855,6.848,19.998,6.766z"/>
322 </svg></div>');
323 $list_items_markup .= sprintf('<div class="tb-timeline-content">');
324
325 $list_items_markup .= sprintf('<div>');
326 $list_items_markup .= timeline_block_get_featured_image($post_id,$attributes);
327 $list_items_markup .= sprintf('</div>');
328
329 $list_items_markup .= sprintf ('<div class="tb-first-inner-wrap">', esc_attr($post_thumb_class));
330 $list_items_markup .= sprintf('<div class="tb-content-wrap">');
331
332 // Get the post title
333 $title = get_the_title($post_id);
334 if (!$title) {
335 $title = __('Untitled', 'timeline-blocks');
336 }
337
338 //Get title tag
339 $allowed_tags = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'div');
340 if (isset($attributes['titleTag']) && in_array($attributes['titleTag'], $allowed_tags, true)) {
341 $post_title_tag = $attributes['titleTag'];
342 } else {
343 $post_title_tag = 'h2';
344 }
345 $list_items_markup .= sprintf('<div class="tb-timeline-title-wrap "><'.$post_title_tag.' className="tb-title"><a href="%1$s" rel="bookmark" class="tb-timeline-title">%2$s</a></'.$post_title_tag.'>',esc_url(get_permalink($post_id)), esc_html($title));
346 $list_items_markup .= sprintf('</div>');
347
348 // Wrap the text content
349 $list_items_markup .= sprintf('<div class="tb-category-link-wraper">');
350 if (isset($attributes['displayPostDate']) && $attributes['displayPostDate']) {
351 $list_items_markup .= sprintf('<div class="mdate tb-inline"><i class="fas fa-calendar-alt"></i> %1$s </div>', get_the_date('d F, Y', $post_id));
352 }
353
354 // Get the post date
355 if (isset($attributes['displayPostAuthor']) && $attributes['displayPostAuthor']) {
356 $list_items_markup .= sprintf('<div class="post-author tb-inline"><i class="fas fa-pencil-alt"></i><span class="tb-blogpost-author">');
357 $list_items_markup .= timeline_block_get_author($post_id);
358 $list_items_markup .= sprintf('</div>');
359 }
360
361 //Get comments
362 $comments = get_comments_number($post_id);
363 if ($comments > 0 ){
364 $number_commnet = '<i class="fas fa-comment"></i>' . $comments;
365 }else{
366 $number_commnet = '<i class="fas fa-comment"></i> 0' ;
367 }
368 if (isset($attributes['displayPostComments']) && $attributes['displayPostComments']) {
369 $list_items_markup .= sprintf('<div class="post-comments tb-inline"> %1$s </div>', $number_commnet);
370 }
371 $list_items_markup .= sprintf( '</div>');
372 if (isset($attributes['displayPostCategory']) && $attributes['displayPostCategory']) {
373 $list_items_markup .= timeline_block_get_category($post_id,", ");
374 }
375 $list_items_markup .= sprintf('<div class="tb-timeline-second-content-wrap" >');
376
377 // Wrap the byline content
378 $list_items_markup .= sprintf('<div class="tb-timeline-byline"> <div class="tb-timeline-metadatabox">');
379
380 // Wrap the excerpt content
381 $list_items_markup .= sprintf('<div class="tb-timeline-text">');
382 $list_items_markup .= sprintf('<div class="tb-timeline-excerpt">');
383 if (isset($attributes['displayPostExcerpt']) && $attributes['displayPostExcerpt']) {
384 $list_items_markup .= timeline_block_get_excerpt($post_id,$post_query,$attributes);
385 }
386
387 //Display Readmore
388 if (isset($attributes['displayPostLink']) && $attributes['displayPostLink']) {
389 if ($attributes['readmoreView'] == 'text-only') {
390
391 $list_items_markup .= sprintf('<p class="tb-text-only"><a class="tb-blogpost-link" href="%1$s" rel="bookmark">%2$s</a></p>', esc_url(get_permalink($post_id)), esc_html($attributes['readMoreText']));
392 } else if ($attributes['readmoreView'] == 'tb-button') {
393
394 $list_items_markup .= sprintf('<div class="tb-button-view"><a class="tb-button tb-link gb-text-link" href="%1$s" rel="bookmark">%2$s</a></div>', esc_url(get_permalink($post_id)), esc_html($attributes['readMoreText']));
395 }
396 }
397
398 //Get comments
399 $comments = get_comments_number($post_id);
400 if ($comments > 0 ){
401 $number_commnet = '<i class="fas fa-comment"></i>' . $comments;
402 }else{
403 $number_commnet = '<i class="fas fa-comment"></i> 0' ;
404 }
405
406 // Close the excerpt content
407 $list_items_markup .= sprintf('</div></div><div class="tb-timeline-bototm-wrap">');
408 if (isset($attributes['displayPostTag']) && $attributes['displayPostTag']) {
409 $list_items_markup .= timeline_block_get_tags($post_id,'');
410 }
411 $list_items_markup .= sprintf('<div class="tb-timeline-social-wrap"><div class="social-share-data">');
412 if (isset($attributes['displayPostSocialshare']) && $attributes['displayPostSocialshare']) {
413 $list_items_markup .= timeline_block_get_social_share_icons($post_id);
414 }
415 $list_items_markup .= sprintf('</div></div></div></div>');
416 $list_items_markup .= "</article>\n";
417 }
418 }
419
420 return $list_items_markup;
421 }
422
423 //**
424 function timeline_register_block_core_latest_posts() {
425
426 // Check if the register function exists
427 if (!function_exists('register_block_type')) {
428 return;
429 }
430
431 register_block_type('timeline-blocks/tb-timeline-blocks', array(
432 'attributes' => array(
433 'block_id' => array(
434 'type' => 'string',
435 'default' => 'not_set',
436 ),
437 'categories' => array(
438 'type' => 'string',
439 ),
440 'className' => array(
441 'type' => 'string',
442 ),
443 'postsToShow' => array(
444 'type' => 'number',
445 'default' => 4,
446 ),
447 'displayPostDate' => array(
448 'type' => 'boolean',
449 'default' => true,
450 ),
451 'displayPostExcerpt' => array(
452 'type' => 'boolean',
453 'default' => true,
454 ),
455 'wordsExcerpt' => array(
456 'type' => 'number',
457 'default' => 25,
458 ),
459 'displayPostAuthor' => array(
460 'type' => 'boolean',
461 'default' => true,
462 ),
463 'displayPostTag' => array(
464 'type' => 'boolean',
465 'default' => true,
466 ),
467 'displayPostCategory' => array(
468 'type' => 'boolean',
469 'default' => true,
470 ),
471 'displayPostImage' => array(
472 'type' => 'boolean',
473 'default' => true,
474 ),
475 'displayPostLink' => array(
476 'type' => 'boolean',
477 'default' => true,
478 ),
479 'displayPostComments' => array(
480 'type' => 'boolean',
481 'default' => true,
482 ),
483 'displayPostSocialshare' => array(
484 'type' => 'boolean',
485 'default' => true,
486 ),
487 'align' => array(
488 'type' => 'string',
489 'default' => 'center',
490 ),
491 'width' => array(
492 'type' => 'string',
493 'default' => 'wide',
494 ),
495 'order' => array(
496 'type' => 'string',
497 'default' => 'desc',
498 ),
499 'orderBy' => array(
500 'type' => 'string',
501 'default' => 'date',
502 ),
503 'imageCrop' => array(
504 'type' => 'string',
505 'default' => 'landscape',
506 ),
507 'layoutcount' => array(
508 'type' => 'number',
509 'default' => 1,
510 ),
511 'readMoreText' => array(
512 'type' => 'string',
513 'default' => 'Read More',
514 ),
515 'titleTag' => array(
516 'type' => 'string',
517 'default' => 'h3',
518 ),
519 'titlefontSize' => array(
520 'type' => 'number',
521 'default' => '',
522 ),
523 'titleFontFamily' => array(
524 'type' => 'string',
525 'default' => '',
526 ),
527 'titleFontWeight' => array(
528 'type' => 'string',
529 'default' => '',
530 ),
531 'titleFontSubset' => array(
532 'type' => 'string',
533 'default' => '',
534 ),
535 'postmetafontSize' => array(
536 'type' => 'number',
537 'default' => '',
538 ),
539 'postexcerptfontSize' => array(
540 'type' => 'number',
541 'default' => '',
542 ),
543 'postctafontSize' => array(
544 'type' => 'number',
545 'default' => '',
546 ),
547 'metaFontFamily' => array(
548 'type' => 'string',
549 'default' => '',
550 ),
551 'metaFontSubset' => array(
552 'type' => 'string',
553 'default' => '',
554 ),
555 'metafontWeight' => array(
556 'type' => 'string',
557 'default' => '',
558 ),
559 'excerptFontFamily' => array(
560 'type' => 'string',
561 'default' => '',
562 ),
563 'excerptFontWeight' => array(
564 'type' => 'string',
565 'default' => '',
566 ),
567 'excerptFontSubset' => array(
568 'type' => 'string',
569 'default' => '',
570 ),
571 'ctaFontFamily' => array(
572 'type' => 'string',
573 'default' => '',
574 ),
575 'ctaFontSubset' => array(
576 'type' => 'string',
577 'default' => '',
578 ),
579 'ctafontWeight' => array(
580 'type' => 'string',
581 'default' => '',
582 ),
583 'socialSharefontSize' => array(
584 'type' => 'number',
585 'default' => '',
586 ),
587 'readmoreView' => array(
588 'type' => 'string',
589 'default' => 'text-only',
590 ),
591 'belowTitleSpace' => array(
592 'type' => 'number',
593 'default' => '',
594 ),
595 'belowImageSpace' => array(
596 'type' => 'number',
597 'default' => '',
598 ),
599 'belowexerptSpace' => array(
600 'type' => 'number',
601 'default' => '',
602 ),
603 'belowctaSpace' => array(
604 'type' => 'number',
605 'default' => 10,
606 ),
607 'innerSpace' => array(
608 'type' => 'number',
609 'default' => 20,
610 ),
611 'boxbgColor' => array(
612 'type' => 'string',
613 'default' => '',
614 ),
615 'titleColor' => array(
616 'type' => 'string',
617 'default' => '',
618 ),
619 'postmetaColor' => array(
620 'type' => 'string',
621 'default' => '',
622 ),
623 'postexcerptColor' => array(
624 'type' => 'string',
625 'default' => '',
626 ),
627 'postctaColor' => array(
628 'type' => 'string',
629 'default' => '#abb8c3',
630 ),
631 'socialShareColor' => array(
632 'type' => 'string',
633 'default' => '',
634 ),
635 'designtwoboxbgColor' => array(
636 'type' => 'string',
637 'default' => '',
638 ),
639 'timelineBgColor' => array(
640 'type' => 'string',
641 'default' => '',
642 ),
643 'timelineFgColor' => array(
644 'type' => 'string',
645 'default' => '',
646 ),
647 'readmoreBgColor' => array(
648 'type' => 'string',
649 'default' => '#282828',
650 ),
651 ),
652 'render_callback' => 'timeline_block_render_block_core_latest_posts',
653 ));
654 }
655
656 add_action('init', 'timeline_register_block_core_latest_posts');
657
658 /**
659 * Create API fields for additional info
660 */
661 function timeline_block_register_rest_fields() {
662
663 // Add landscape featured image source
664 register_rest_field(
665 'post', 'featured_image_src', array(
666 'get_callback' => 'timeline_block_get_image_src_landscape',
667 'update_callback' => null,
668 'schema' => null,
669 )
670 );
671
672 // Add square featured image source
673 register_rest_field(
674 'post', 'featured_image_src_square', array(
675 'get_callback' => 'timeline_block_get_image_src_square',
676 'update_callback' => null,
677 'schema' => null,
678 )
679 );
680
681 // Add author info
682 register_rest_field(
683 'post', 'author_info', array(
684 'get_callback' => 'timeline_block_get_author_info',
685 'update_callback' => null,
686 'schema' => null,
687 )
688 );
689
690 // Add category info
691 register_rest_field(
692 'post', 'category_info', array(
693 'get_callback' => 'timeline_block_get_catgeory_info',
694 'update_callback' => null,
695 'schema' => null,
696 )
697 );
698
699 // Add tags info
700 register_rest_field(
701 'post', 'tags_info', array(
702 'get_callback' => 'timeline_block_get_tags_info',
703 'update_callback' => null,
704 'schema' => null,
705 )
706 );
707
708 // Add Social share info
709 register_rest_field(
710 'post', 'social_share_info', array(
711 'get_callback' => 'timeline_block_get_social_share_info',
712 'update_callback' => null,
713 'schema' => null,
714 )
715 );
716
717 // Add PostExcert
718 register_rest_field(
719 'post', 'wordExcerpt_info', array(
720 'get_callback' => 'timeline_block_get_wordExcerpt',
721 'update_callback' => null,
722 'schema' => null,
723 )
724 );
725
726 // Add Comment
727 register_rest_field(
728 'post', 'comment_info', array(
729 'get_callback' => 'timeline_block_get_comment_info',
730 'update_callback' => null,
731 'schema' => null,
732 )
733 );
734 }
735
736 add_action('rest_api_init', 'timeline_block_register_rest_fields');
737
738 /**
739 * Get landscape featured image source for the rest field
740 */
741 function timeline_block_get_image_src_landscape($object, $field_name, $request) {
742 $feat_img_array = wp_get_attachment_image_src(
743 $object['featured_media'], 'tb-blogpost-landscape', false
744 );
745 return $feat_img_array[0] ?? ' ';
746 }
747
748 /**
749 * Get square featured image source for the rest field
750 */
751 function timeline_block_get_image_src_square($object, $field_name, $request) {
752 $feat_img_array = wp_get_attachment_image_src(
753 $object['featured_media'], 'tb-blogpost-square', false
754 );
755 return $feat_img_array[0] ?? ' ' ;
756 }
757
758 /**
759 * Get author info for the rest field
760 */
761 function timeline_block_get_author_info($object, $field_name, $request) {
762 // Get the author name
763 $author_data['display_name'] = get_the_author_meta('display_name', $object['author']);
764
765 // Get the author link
766 $author_data['author_link'] = get_author_posts_url($object['author']);
767
768 // Return the author data
769 return $author_data;
770 }
771
772 /**
773 * Get category info for the rest field
774 */
775 function timeline_block_get_catgeory_info($object, $field_name, $request) {
776 $object['ID'] = '';
777 $categories_list = get_the_category_list(", ", " ", $object['ID']);
778 $cat_class = '';
779
780 $category_info = sprintf('%1$s', $categories_list);
781 // Return the category data
782 return $category_info;
783 }
784
785 /**
786 * Get tags info for the rest field
787 */
788 function timeline_block_get_tags_info($object, $field_name, $request) {
789 // Get the author name
790 $object['ID'] = '';
791 $tags_list = get_the_tag_list("", ", ", "", $object['ID']);
792 $tags_info = sprintf('%1$s', $tags_list);
793 // Return the tag data
794 return $tags_info;
795 }
796
797 /**
798 * Get excerpt info for the rest field
799 */
800 function timeline_block_get_wordExcerpt($object, $field_name, $request) {
801 $object['ID'] = '';
802 $excerpt = apply_filters('the_excerpt', get_post_field('post_excerpt', $object['ID'], 'display'));
803
804 if (empty($excerpt)) {
805 $excerpt = apply_filters('the_excerpt', get_the_content($object['ID']));
806 }
807
808 if (!$excerpt) {
809 $excerpt = null;
810 }
811 $list_items_markup = wp_kses_post($excerpt);
812
813 return $list_items_markup;
814 }
815
816 /**
817 * Get social share info for the rest field
818 */
819 function timeline_block_get_social_share_info( $object, $field_name, $request ) {
820 $object['ID'] = '';
821 $social_share_info = sprintf( '<a data-share="facebook" href="https://www.facebook.com/sharer.php?u=%1$s" class="tb-facebook-share social-share-default tb-social-share" target="_blank"><i class="fab fa-facebook-f" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $object['ID'] ) ) );
822 $social_share_info .= sprintf( '<a data-share="twitter" href="https://twitter.com/share?url=%1$s" class="tb-twiiter-share social-share-default tb-social-share" target="_blank"><i class="fab fa-twitter" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $object['ID'] ) ) );
823 $social_share_info .= sprintf( '<a data-share="linkedin" href="https://www.linkedin.com/shareArticle?url=%1$s" class="tb-linkedin-share social-share-default tb-social-share" target="_blank"><i class="fab fa-linkedin-in" aria-hidden="true"></i></a>', esc_url( get_the_permalink( $object['ID'] ) ) );
824 return $social_share_info;
825 }
826
827 /**
828 * Get commentinfo for the rest field
829 */
830 function timeline_block_get_comment_info($object, $field_name, $request) {
831 $object['ID'] = '';
832 $comments = get_comments_number($object['ID']);
833 if ($comments > 0) {
834 return $comments;
835 } else {
836 return '0';
837 }
838 }
839
840 /**
841 * Get author_name for the rest field
842 */
843 function timeline_block_get_author( $post_id ) {
844 $list_items_markup = sprintf(
845 '<a href="%2$s">%1$s</a></span></span>',
846 esc_html( get_the_author_meta( 'display_name', get_the_author_meta( 'ID' ) ) ),
847 esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) )
848 );
849 return $list_items_markup;
850 }
851