-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathclass-dynamic-content.php
More file actions
975 lines (821 loc) · 25.3 KB
/
class-dynamic-content.php
File metadata and controls
975 lines (821 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
<?php
/**
* Dynamic Content.
*
* @package ThemeIsle\GutenbergBlocks\Plugins
*/
namespace ThemeIsle\GutenbergBlocks\Plugins;
/**
* Class Dynamic_Content
*/
class Dynamic_Content {
/**
* The main instance var.
*
* @var Dynamic_Content|null
*/
protected static $instance = null;
/**
* Initialize the class
*/
public function init() {
add_filter( 'render_block', array( $this, 'apply_dynamic_content' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_magic_tags' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_link' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_link_button' ) );
add_filter( 'render_block', array( $this, 'apply_dynamic_images' ) );
add_filter( 'otter_apply_dynamic_image', array( $this, 'apply_dynamic_images' ) );
}
/**
* Filter post content for dynamic content.
*
* @param string $content Post content.
*
* @return string
*/
public function apply_dynamic_content( $content ) {
if ( false === strpos( $content, '<o-dynamic' ) ) {
return $content;
}
$matches = array();
$num = self::parse_dynamic_content_query( $content, $matches );
if ( isset( $num ) && 0 === $num ) {
return $content;
}
foreach ( $matches as $match ) {
$replacement = $this->apply_data( $match );
$string_to_replace = $match[0];
$position = strstr( $content, $string_to_replace, true );
if ( false === $position ) {
continue;
}
$position = strlen( $position );
if ( is_array( $replacement ) ) {
$re = '/#otterDynamicLink\/?.[^"]*/';
$matches = array();
$num = preg_match_all( $re, $content, $matches, PREG_SET_ORDER, 0 );
if ( isset( $matches[0] ) ) {
$link = $this->apply_link_button( $matches[0] );
}
$_content = array_fill( 0, count( $replacement ), $content );
$content = '';
foreach ( $replacement as $key => $value ) {
$updated_content = substr_replace( $_content[ $key ], $value, $position, strlen( $string_to_replace ) );
if ( isset( $link ) && is_array( $link ) && isset( $link[ $key ] ) ) {
$updated_content = str_replace( $matches[0], $link[ $key ], $updated_content );
}
$content .= $updated_content;
}
} else {
$content = substr_replace( $content, $replacement, $position, strlen( $string_to_replace ) );
}
}
return $content;
}
/**
* Get the Dynamic Content regex.
*
* @return string
*/
public static function dynamic_content_regex() {
// Todo: Improve this Regex, it can't go on for like this. Soon it will be longer than the available space in the universe!!!
return '/<o-dynamic(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-id=["\'](?P<id>[^"\'<>]+)["\']|data-before=["\'](?P<before>[^"\'<>]+)["\']|data-after=["\'](?P<after>[^"\'<>]+)["\']|data-length=["\'](?P<length>[^"\'<>]+)["\']|data-date-type=["\'](?P<dateType>[^"\'<>]+)["\']|data-date-format=["\'](?P<dateFormat>[^"\'<>]+)["\']|data-date-custom=["\'](?P<dateCustom>[^"\'<>]+)["\']|data-time-type=["\'](?P<timeType>[^"\'<>]+)["\']|data-time-format=["\'](?P<timeFormat>[^"\'<>]+)["\']|data-time-custom=["\'](?P<timeCustom>[^"\'<>]+)["\']|data-term-type=["\'](?P<termType>[^"\'<>]+)["\']|data-term-separator=["\'](?P<termSeparator>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-parameter=["\'](?P<parameter>[^"\'<>]+)["\']|data-format=["\'](?P<format>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|data-taxonomy=["\'](?P<taxonomy>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<default>[^ $].*?)<\s*\/\s*o-dynamic>/';
}
/**
* Parse dynamic content query.
*
* @param string $content The content to parse.
* @param array $matches The matches.
* @return mixed
*/
public static function parse_dynamic_content_query( $content, &$matches ) {
$re = self::dynamic_content_regex();
return preg_match_all( $re, $content, $matches, PREG_SET_ORDER, 0 );
}
/**
* Filter post content for Magic Tags.
*
* @param string $content Post content.
*
* @return string
*/
public function apply_dynamic_magic_tags( $content ) {
if ( false === strpos( $content, '{otterDynamic?' ) ) {
return $content;
}
$re = '/{(otterDynamic\/?.[^"]*)}/';
return preg_replace_callback( $re, array( $this, 'apply_magic_tags' ), $content );
}
/**
* Apply dynamic data for Magic Tags.
*
* @param array $data Dynamic request.
*
* @return string|void
*/
public function apply_magic_tags( $data ) {
if ( ! isset( $data[1] ) ) {
return;
}
$data = explode( 'otterDynamic', $data[1] );
$data = self::query_string_to_array( $data[1] );
if ( ! isset( $data['default'] ) ) {
$data['default'] = '';
}
$data = $this->apply_data( $data, true );
return $data;
}
/**
* Filter post content for dynamic link.
*
* @param string $content Post content.
* @param int|null $key Optional key for multiple dynamic links in the same content.
*
* @return string
*/
public function apply_dynamic_link( $content, $key = null ) {
if ( false === strpos( $content, '<o-dynamic-link' ) ) {
return $content;
}
$re = '/<o-dynamic-link(?:\s+(?:data-type=["\'](?P<type>[^"\'<>]+)["\']|data-target=["\'](?P<target>[^"\'<>]+)["\']|data-meta-key=["\'](?P<metaKey>[^"\'<>]+)["\']|data-context=["\'](?P<context>[^"\'<>]+)["\']|[a-zA-Z-]+=["\'][^"\'<>]+["\']))*\s*>(?<text>[^ $].*?)<\s*\/\s*o-dynamic-link>/';
$matches = array();
$num = preg_match_all( $re, $content, $matches, PREG_SET_ORDER, 0 );
if ( isset( $num ) && 0 === $num ) {
return $content;
}
$resolved = array();
$clone_count = 1;
foreach ( $matches as $match ) {
$value = $this->apply_link( $match, $key );
$resolved[] = $value;
if ( is_array( $value ) ) {
$clone_count = max( $clone_count, count( $value ) );
}
}
// Replace the content for multiple dynamic links in the same content.
if ( $clone_count > 1 ) {
$output = '';
for ( $i = 0; $i < $clone_count; $i++ ) {
$clone = $content;
foreach ( $matches as $j => $match ) {
$value = $resolved[ $j ];
$replacement = is_array( $value )
? ( isset( $value[ $i ] ) ? $value[ $i ] : '' )
: $value;
$pos = strpos( $clone, $match[0] );
if ( false !== $pos ) {
$clone = substr_replace( $clone, $replacement, $pos, strlen( $match[0] ) );
}
}
$output .= $clone;
}
return $output;
}
// Replace the content for single dynamic link in the content.
$index = 0;
return preg_replace_callback(
$re,
function ( $data ) use ( &$resolved, &$index ) {
$value = isset( $resolved[ $index ] ) ? $resolved[ $index ] : $data[0];
$index++;
return is_string( $value ) ? $value : $data[0];
},
$content
);
}
/**
* Filter post content for dynamic link buttons.
*
* @param string $content Post content.
*
* @return string
*/
public function apply_dynamic_link_button( $content ) {
if ( false === strpos( $content, '#otterDynamicLink' ) ) {
return $content;
}
$re = '/#otterDynamicLink\/?.[^"]*/';
return preg_replace_callback( $re, array( $this, 'apply_link_button' ), $content );
}
/**
* Filter post content for dynamic images.
*
* @param string $content Post content.
*
* @return string
*/
public function apply_dynamic_images( $content ) {
if ( false === strpos( $content, 'otter/v1/dynamic' ) ) {
return $content;
}
$rest_url = get_rest_url( null, 'otter/v1' );
$rest_url = preg_replace( '/([^A-Za-z0-9\s_-])/', '\\\\$1', $rest_url );
$re = '/' . $rest_url . '\/dynamic\/?.[^"]*/';
$matches = array();
$num = preg_match_all( $re, $content, $matches, PREG_SET_ORDER, 0 );
if ( isset( $num ) && 0 === $num ) {
return $content;
}
$resolved = array();
$clone_count = 1;
foreach ( $matches as $match ) {
$value = $this->apply_images( $match );
$resolved[] = $value;
if ( is_array( $value ) ) {
$clone_count = max( $clone_count, count( $value ) );
}
}
// Replace the content for multiple dynamic images in the same content.
if ( $clone_count > 1 ) {
$output = '';
for ( $i = 0; $i < $clone_count; $i++ ) {
$clone = $content;
foreach ( $matches as $j => $match ) {
$value = $resolved[ $j ];
$replacement = is_array( $value )
? ( isset( $value[ $i ] ) ? $value[ $i ] : '' )
: $value;
$pos = strpos( $clone, $match[0] );
if ( false !== $pos ) {
$clone = substr_replace( $clone, $replacement, $pos, strlen( $match[0] ) );
}
}
$output .= $clone;
}
return $output;
}
// Replace the content for single dynamic image in the content.
$index = 0;
return preg_replace_callback(
$re,
function ( $data ) use ( &$resolved, &$index ) {
$value = isset( $resolved[ $index ] ) ? $resolved[ $index ] : $data[0];
$index++;
return is_string( $value ) ? $value : $data[0];
},
$content
);
}
/**
* Apply dynamic data.
*
* @param array $data Dynamic request.
*
* @return string|string[]|void
*/
public function apply_images( $data ) {
if ( ! isset( $data[0] ) ) {
return;
}
$data = self::query_string_to_array( $data[0] );
$value = $this->get_image( $data );
return $value;
}
/**
* Apply dynamic image.
*
* @param array $data Query array.
*
* @return string
*/
public function get_image( $data ) {
$value = OTTER_BLOCKS_URL . 'assets/images/placeholder.jpg';
global $post;
$id = ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( isset( $data['context'] ) && 'query' === $data['context'] ) ) ? $post->ID : get_queried_object_id();
if ( isset( $data['context'] ) && ( 0 === $data['context'] || 'query' === $data['context'] || ( is_singular() && $data['context'] !== $id ) ) ) {
$data['context'] = $id;
}
if ( isset( $data['fallback'] ) && ! empty( $data['fallback'] ) ) {
$value = esc_url( $data['fallback'] );
}
if ( ! isset( $data['type'] ) && empty( $data['type'] ) ) {
return $value;
}
if ( 'featuredImage' === $data['type'] ) {
$image = get_the_post_thumbnail_url( $data['context'] );
if ( $image ) {
$value = $image;
}
}
if ( 'author' === $data['type'] ) {
$author = get_post_field( 'post_author', $data['context'] );
$value = get_avatar_url( $author );
}
if ( 'loggedInUser' === $data['type'] ) {
$user = get_current_user_id();
if ( true === boolval( $user ) ) {
$value = get_avatar_url( $user );
}
}
if ( 'logo' === $data['type'] ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( $custom_logo_id ) {
$value = wp_get_attachment_image_url( $custom_logo_id, 'full' );
}
}
$value = apply_filters( 'otter_blocks_evaluate_dynamic_content_media_content', $value, $data );
return $value;
}
/**
* Apply dynamic data.
*
* @param array $data Dynamic request.
* @param bool $magic_tags Is a request for Magic Tags.
*
* @return string|string[]
*/
public function apply_data( $data, $magic_tags = false ) {
$value = $this->get_data( $data, $magic_tags );
if ( is_array( $value ) ) {
$updated_value = array();
foreach ( $value as $key => $val ) {
$_value = $this->apply_formatting( $val, $data );
if ( isset( $data['default'] ) && false !== strpos( $data['default'], '<o-dynamic-link' ) ) {
$link = $this->apply_dynamic_link( $data['default'], $key );
if ( ! empty( $link ) ) {
$_value = preg_replace( '/(<a.*?>).*?(<\/a>)/', '$1' . $_value . '$2', $link );
}
}
$updated_value[] = $_value;
}
return $updated_value;
}
if ( isset( $data['before'] ) || isset( $data['after'] ) ) {
$value = $this->apply_formatting( $value, $data );
}
if ( isset( $data['default'] ) && false !== strpos( $data['default'], '<o-dynamic-link' ) ) {
$link = $this->apply_dynamic_link( $data['default'] );
return preg_replace( '/(<a.*?>).*?(<\/a>)/', '$1' . $value . '$2', $link );
}
return $value;
}
/**
* Apply formatting.
*
* @param string $value Dynamic value.
* @param array $data Dynamic request.
*
* @return string
*/
public function apply_formatting( $value, $data ) {
if ( isset( $data['before'] ) ) {
$value = esc_html( $data['before'] ) . $value;
}
if ( isset( $data['after'] ) ) {
$value = $value . esc_html( $data['after'] );
}
return $value;
}
/**
* Get dynamic data.
*
* @param array $data Dynamic request.
* @param bool $magic_tags Is a request for Magic Tags.
*
* @return string|string[]
*/
public function get_data( $data, $magic_tags ) {
if ( ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || true === $magic_tags ) {
if ( isset( $data['context'] ) && 'query' === $data['context'] ) {
$data['context'] = get_the_ID();
} else {
/*
* We use the queried object ID to make sure when posts are displayed inside other posts
* the displayed post is being used as a source for the dynamic tags. Eg. Custom Layouts inside Neve.
*/
$data['context'] = get_queried_object_id();
}
} else {
$data['default'] = '';
}
if ( ! isset( $data['type'] ) && isset( $data['default'] ) ) {
return esc_html( $data['default'] );
}
if ( 'postID' === $data['type'] ) {
return $data['context'];
}
if ( 'postTitle' === $data['type'] ) {
return get_the_title( $data['context'] );
}
if ( 'postContent' === $data['type'] ) {
return $this->get_content( $data );
}
if ( 'postExcerpt' === $data['type'] ) {
return $this->get_excerpt( $data );
}
if ( 'postType' === $data['type'] ) {
return get_post_type( $data['context'] );
}
if ( 'postStatus' === $data['type'] ) {
return get_post_status( $data['context'] );
}
if ( 'siteTitle' === $data['type'] ) {
return get_bloginfo( 'name' );
}
if ( 'siteTagline' === $data['type'] ) {
return get_bloginfo( 'description' );
}
if ( 'authorName' === $data['type'] ) {
return get_the_author_meta( 'display_name', intval( get_post_field( 'post_author', $data['context'] ) ) );
}
if ( 'authorDescription' === $data['type'] ) {
return get_the_author_meta( 'description', intval( get_post_field( 'post_author', $data['context'] ) ) );
}
if ( 'loggedInUserName' === $data['type'] ) {
return $this->get_loggedin_name( $data );
}
if ( 'loggedInUserDescription' === $data['type'] ) {
return $this->get_loggedin_description( $data );
}
if ( 'loggedInUserEmail' === $data['type'] ) {
return $this->get_loggedin_email( $data );
}
if ( 'archiveTitle' === $data['type'] ) {
return get_the_archive_title();
}
if ( 'archiveDescription' === $data['type'] ) {
return $this->get_archive_description( $data );
}
if ( 'date' === $data['type'] ) {
return $this->get_current_date( $data );
}
if ( 'time' === $data['type'] ) {
return $this->get_current_time( $data );
}
return apply_filters( 'otter_blocks_evaluate_dynamic_content_text', $data['default'], $data );
}
/**
* Get Content.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_content( $data ) {
$data = $this->mark_exceptions( $data );
$key = $this->get_exception_key( $data );
if ( isset( $data[ $key ] ) ) {
return '';
}
$content = get_the_content( $data['context'] );
$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) );
return wp_kses_post( $content );
}
/**
* Get Excerpt.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_excerpt( $data ) {
$post = get_post( $data['context'] );
$excerpt = $post->post_excerpt; // Here we don't use get_the_excerpt() function as it causes an infinite loop.
if ( empty( $excerpt ) ) {
$data = $this->mark_exceptions( $data );
$key = $this->get_exception_key( $data, $post->ID );
if ( ! isset( $data[ $key ] ) ) {
remove_filter( 'render_block', array( $this, 'apply_dynamic_content' ), 10 );
$excerpt = wp_trim_excerpt( '', $post );
add_filter( 'render_block', array( $this, 'apply_dynamic_content' ), 10 );
}
}
if ( isset( $data['length'] ) && ! empty( $data['length'] ) ) {
$excerpt = substr( $excerpt, 0, intval( $data['length'] ) ) . '…';
}
if ( empty( $excerpt ) ) {
return $data['default'];
}
return sanitize_text_field( $excerpt );
}
/**
* Get Logged-in User Name.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_loggedin_name( $data ) {
$user_id = get_current_user_id();
$default = isset( $data['default'] ) ? esc_html( $data['default'] ) : '';
$user = get_userdata( $user_id );
if ( false === $user ) {
$display_name = $default;
} else {
$display_name = $user->display_name;
}
return esc_html( $display_name );
}
/**
* Get Logged-in User Description.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_loggedin_description( $data ) {
$user_id = get_current_user_id();
$default = isset( $data['default'] ) ? esc_html( $data['default'] ) : '';
$user = get_userdata( $user_id );
if ( false === $user ) {
$description = $default;
} else {
$description = $user->description;
}
return esc_html( $description );
}
/**
* Get Logged-in User Email.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_loggedin_email( $data ) {
$user = wp_get_current_user();
$default = isset( $data['default'] ) ? esc_html( $data['default'] ) : '';
$email = $user->user_email;
if ( empty( $email ) ) {
$email = $default;
}
return esc_html( $email );
}
/**
* Get Archive Description.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_archive_description( $data ) {
$description = get_the_archive_description();
if ( empty( $description ) ) {
$description = isset( $data['default'] ) ? esc_html( $data['default'] ) : '';
}
return wp_strip_all_tags( $description );
}
/**
* Get Current Date.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_current_date( $data ) {
$format = get_option( 'date_format' );
if ( isset( $data['dateFormat'] ) && ! empty( $data['dateFormat'] ) && 'default' !== $data['dateFormat'] && 'custom' !== $data['dateFormat'] ) {
$format = esc_html( $data['dateFormat'] );
}
if ( isset( $data['dateCustom'] ) && ! empty( $data['dateCustom'] ) && isset( $data['dateFormat'] ) && 'custom' === $data['dateFormat'] ) {
$format = esc_html( $data['dateCustom'] );
}
$date = gmdate( $format );
return $date;
}
/**
* Get Current Date.
*
* @param array $data Dynamic Data.
*
* @return string
*/
public function get_current_time( $data ) {
$format = get_option( 'time_format' );
if ( isset( $data['timeFormat'] ) && ! empty( $data['timeFormat'] ) && 'default' !== $data['timeFormat'] && 'custom' !== $data['timeFormat'] ) {
$format = esc_html( $data['timeFormat'] );
}
if ( isset( $data['timeCustom'] ) && ! empty( $data['timeCustom'] ) && isset( $data['timeFormat'] ) && 'custom' === $data['timeFormat'] ) {
$format = esc_html( $data['timeCustom'] );
}
$time = gmdate( $format );
return $time;
}
/**
* Convert Query String to Array.
*
* @param string $qry URL.
*
* @return array|bool
*/
public static function query_string_to_array( $qry ) {
$result = array();
if ( strpos( $qry, '=' ) ) {
if ( strpos( $qry, '?' ) !== false ) {
$qry = str_replace( array( '&', '&' ), '&', $qry );
$q = wp_parse_url( $qry );
$qry = isset( $q['query'] ) ? $q['query'] : $q['fragment'];
}
} else {
return false;
}
foreach ( explode( '&', $qry ) as $couple ) {
list ( $key, $val ) = array_pad( explode( '=', $couple ), 2, null );
$result[ $key ] = $val;
}
return $result;
}
/**
* Apply dynamic data.
*
* @param array $data Dynamic request.
* @param int|null $key Optional key for multiple dynamic links in the same content.
*
* @return string|string[]
*/
public function apply_link( $data, $key = null ) {
$link = $this->get_link( $data );
if ( empty( $link ) ) {
$link = get_site_url();
}
$attrs = '';
if ( isset( $data['target'] ) && '_blank' === $data['target'] ) {
$attrs = 'target="_blank"';
}
if ( is_array( $link ) ) {
$value = array();
foreach ( $link as $link_item ) {
$value[] = sprintf(
'<a href="%s" %s>%s</a>',
esc_url( $link_item ),
$attrs,
wp_kses_post( $data['text'] )
);
}
if ( null !== $key ) {
return isset( $value[ $key ] ) ? $value[ $key ] : '';
}
return $value;
}
$value = sprintf(
'<a href="%s" %s>%s</a>',
esc_url( $link ),
$attrs,
wp_kses_post( $data['text'] )
);
return $value;
}
/**
* Apply dynamic data for Buttons.
*
* @param array $data Dynamic request.
*
* @return string|string[]|void
*/
public function apply_link_button( $data ) {
if ( ! isset( $data[0] ) ) {
return;
}
$data = explode( '#otterDynamicLink', $data[0] );
$data = self::query_string_to_array( $data[1] );
$link = $this->get_link( $data );
if ( empty( $link ) ) {
$link = get_site_url();
}
return $link;
}
/**
* Apply dynamic data.
*
* @param array $data Dynamic request.
*
* @return string|string[]|void
*/
public function get_link( $data ) {
if ( ! isset( $data['type'] ) ) {
return;
}
if ( isset( $data['context'] ) && 'query' === $data['context'] ) {
$data['context'] = get_the_ID();
} else {
$data['context'] = get_queried_object_id();
}
if ( 'postURL' === $data['type'] ) {
return get_the_permalink( $data['context'] );
}
if ( 'siteURL' === $data['type'] ) {
return get_site_url();
}
if ( 'featuredImageURL' === $data['type'] ) {
return wp_get_attachment_url( get_post_thumbnail_id( $data['context'] ) );
}
if ( 'authorURL' === $data['type'] ) {
return get_author_posts_url( intval( get_post_field( 'post_author', $data['context'] ) ) );
}
if ( 'authorWebsite' === $data['type'] ) {
return get_the_author_meta( 'url', intval( get_post_field( 'post_author', $data['context'] ) ) );
}
return apply_filters( 'otter_blocks_evaluate_dynamic_content_link', '', $data );
}
/**
* Mark the exceptions for Dynamic request.
*
* Features that are using complex functions like `get_content` (directly or indirectly) might not behave correctly in every situation.
* Based on the context, we decide if it is safe to use those functions..
*
* @param array $data Dynamic request.
* @return array Dynamic request with marked exceptions.
*/
public function mark_exceptions( $data ) {
if ( 'postExcerpt' === $data['type'] || 'postContent' === $data['type'] ) {
if (
'valid' === apply_filters( 'product_neve_license_status', false ) &&
class_exists( '\Neve_Pro\Modules\Custom_Layouts\Module' )
) {
$post = get_post( $data['context'] );
if ( isset( $post ) && 'neve_custom_layouts' === $post->post_type ) {
$key = $this->get_exception_key( $data, $post->ID );
if ( $key ) {
$data[ $key ] = true;
}
}
}
if ( 'postContent' === $data['type'] ) {
// To do not trigger postContent action if the given content contains the postContent dynamic tag, because it will cause an infinite loop.
$post = get_post( $data['context'] );
if ( ! $post instanceof \WP_Post ) {
return $data;
}
$content = get_the_content( $data['context'] );
if ( strpos( $content, 'data-type="postContent"' ) ) {
$key = $this->get_exception_key( $data, $post->ID );
if ( $key ) {
$data[ $key ] = true;
}
}
}
}
if ( has_filter( 'otter_blocks_dynamic_content_exception' ) ) {
/**
* Gather exceptions for Dynamic request from other plugins via hooks. Before merging the changes, we make sure that critical data is present.
*/
$data_exceptions = apply_filters( 'otter_blocks_dynamic_content_exception', $data );
if (
isset( $data_exceptions ) &&
is_array( $data_exceptions )
) {
$merged = array_merge( $data, $data_exceptions );
if ( isset( $merged['type'] ) && isset( $merged['context'] ) ) {
$data = $merged;
}
}
}
return $data;
}
/**
* Get the exception key for Dynamic request.
*
* @param array $data Dynamic request.
* @param numeric $post_id Post ID.
* @return string|null
*/
public function get_exception_key( $data, $post_id = null ) {
if ( null == $post_id ) {
$post = get_post( $data['context'] );
if ( isset( $post ) ) {
$post_id = $post->ID;
}
}
return 'exception_dynamic_content_' . $data['type'] . '_' . $post_id;
}
/**
* The instance method for the static class.
* Defines and returns the instance of the static class.
*
* @static
* @since 1.2.0
* @access public
* @return Dynamic_Content
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init();
}
return self::$instance;
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @access public
* @since 1.2.0
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'otter-blocks' ), '1.0.0' );
}
/**
* Disable unserializing of the class
*
* @access public
* @since 1.2.0
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'otter-blocks' ), '1.0.0' );
}
}