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