forked from beezwax/WP-Publish-to-Apple-News
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathclass-recipe.php
1206 lines (1092 loc) · 37.3 KB
/
class-recipe.php
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Publish to Apple News Includes: Apple_Exporter\Components\Recipe class
*
* @package Apple_News
* @subpackage Apple_Exporter
* @since 2.7.0
*/
namespace Apple_Exporter\Components;
use Apple_Exporter\Component_Factory;
use Apple_Exporter\Component_Spec;
use Apple_Exporter\Exporter_Content;
use DOMElement;
use DOMNodeList;
/**
* A class to transform a recipe from JSON schema to an Apple News Recipe component.
*
* @since 2.7.0
*/
class Recipe extends Component {
/**
* Look for node matches for this component.
*
* @param DOMElement $node The node to examine for matches.
*
* @access public
* @return DOMElement|null The node on success, or null on no match.
*/
public static function node_matches( $node ) {
// Note: we can't use the component get_setting method or settings array here, because this is a static class.
$class = get_option( 'apple_news_settings' )['recipe_component_class'] ?? '';
if ( $class && self::node_has_class( $node, $class ) ) {
return $node;
}
return null;
}
/**
* Register all specs for the component.
*/
public function register_specs() {
$theme = \Apple_Exporter\Theme::get_used();
$dark_mode_conditions = [
'minSpecVersion' => '1.14',
'preferredColorScheme' => 'dark',
];
$component_style = [
'backgroundColor' => '#recipe_background_color#',
];
if ( $theme->get_value( 'recipe_background_color_dark' ) ) {
$component_style['conditional'] = [
'backgroundColor' => '#recipe_background_color_dark#',
'conditions' => $dark_mode_conditions,
];
}
$caption_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_caption_font#',
'fontSize' => '#recipe_caption_size#',
'tracking' => '#recipe_caption_tracking#',
'lineHeight' => '#recipe_caption_line_height#',
'textColor' => '#recipe_caption_color#',
'linkStyle' => [
'textColor' => '#recipe_caption_link_color#',
],
];
$caption_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_caption_color_dark' ) ) {
$caption_textstyle_conditional['textColor'] = '#recipe_caption_color_dark#';
}
if ( $theme->get_value( 'recipe_caption_link_color_dark' ) ) {
$caption_textstyle_conditional['linkStyle'] = [
'textColor' => '#recipe_caption_link_color_dark#',
];
}
if ( $caption_textstyle_conditional ) {
$caption_textstyle_conditional['conditions'] = $dark_mode_conditions;
$caption_textstyle['conditional'] = $caption_textstyle_conditional;
}
$title_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_title_font#',
'fontSize' => '#recipe_title_size#',
'tracking' => '#recipe_title_tracking#',
'lineHeight' => '#recipe_title_line_height#',
'textColor' => '#recipe_title_color#',
];
$title_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_title_color_dark' ) ) {
$title_textstyle_conditional['textColor'] = '#recipe_title_color_dark#';
}
if ( $title_textstyle_conditional ) {
$title_textstyle_conditional['conditions'] = $dark_mode_conditions;
$title_textstyle['conditional'] = $title_textstyle_conditional;
}
$body_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_body_font#',
'fontSize' => '#recipe_body_size#',
'tracking' => '#recipe_body_tracking#',
'lineHeight' => '#recipe_body_line_height#',
'textColor' => '#recipe_body_color#',
'backgroundColor' => '#recipe_body_background_color#',
'linkStyle' => [
'textColor' => '#recipe_body_link_color#',
],
];
$body_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_body_color_dark' ) ) {
$body_textstyle_conditional['textColor'] = '#recipe_body_color_dark#';
}
if ( $theme->get_value( 'recipe_body_background_color_dark' ) ) {
$body_textstyle_conditional['backgroundColor'] = '#recipe_body_background_color_dark#';
}
if ( $theme->get_value( 'recipe_body_link_color_dark' ) ) {
$body_textstyle_conditional['linkStyle'] = [
'textColor' => '#recipe_body_link_color_dark#',
];
}
if ( $body_textstyle_conditional ) {
$body_textstyle_conditional['conditions'] = $dark_mode_conditions;
$body_textstyle['conditional'] = $body_textstyle_conditional;
}
$header2_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_header2_font#',
'fontSize' => '#recipe_header2_size#',
'tracking' => '#recipe_header2_tracking#',
'lineHeight' => '#recipe_header2_line_height#',
'textColor' => '#recipe_header2_color#',
];
$header2_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_header2_color_dark' ) ) {
$header2_textstyle_conditional['textColor'] = '#recipe_header2_color_dark#';
}
if ( $header2_textstyle_conditional ) {
$header2_textstyle_conditional['conditions'] = $dark_mode_conditions;
$header2_textstyle['conditional'] = $header2_textstyle_conditional;
}
$header3_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_header3_font#',
'fontSize' => '#recipe_header3_size#',
'tracking' => '#recipe_header3_tracking#',
'lineHeight' => '#recipe_header3_line_height#',
'textColor' => '#recipe_header3_color#',
];
$header3_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_header3_color_dark' ) ) {
$header3_textstyle_conditional['textColor'] = '#recipe_header3_color_dark#';
}
if ( $header3_textstyle_conditional ) {
$header3_textstyle_conditional['conditions'] = $dark_mode_conditions;
$header3_textstyle['conditional'] = $header3_textstyle_conditional;
}
$header4_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_header4_font#',
'fontSize' => '#recipe_header4_size#',
'tracking' => '#recipe_header4_tracking#',
'lineHeight' => '#recipe_header4_line_height#',
'textColor' => '#recipe_header4_color#',
];
$header4_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_header4_color_dark' ) ) {
$header4_textstyle_conditional['textColor'] = '#recipe_header4_color_dark#';
}
if ( $header4_textstyle_conditional ) {
$header4_textstyle_conditional['conditions'] = $dark_mode_conditions;
$header4_textstyle['conditional'] = $header4_textstyle_conditional;
}
$details_textstyle = [
'textAlignment' => 'left',
'fontName' => '#recipe_details_font#',
'fontSize' => '#recipe_details_size#',
'tracking' => '#recipe_details_tracking#',
'lineHeight' => '#recipe_details_line_height#',
'textColor' => '#recipe_details_color#',
'linkStyle' => [
'textColor' => '#recipe_details_link_color#',
],
];
$details_textstyle_conditional = [];
if ( $theme->get_value( 'recipe_details_color_dark' ) ) {
$details_textstyle_conditional['textColor'] = '#recipe_details_color_dark#';
}
if ( $theme->get_value( 'recipe_details_link_color_dark' ) ) {
$details_textstyle_conditional['linkStyle'] = [
'textColor' => '#recipe_details_link_color_dark#',
];
}
if ( $details_textstyle_conditional ) {
$details_textstyle_conditional['conditions'] = $dark_mode_conditions;
$details_textstyle['conditional'] = $details_textstyle_conditional;
}
$this->register_spec(
'json',
__( 'JSON', 'apple-news' ),
[
'role' => 'recipe',
'URL' => '#url#',
'layout' => [
'margin' => [
'top' => 5,
'bottom' => 5,
],
],
'style' => $component_style,
'components' => [
[
'role' => 'photo',
'layout' => [
'margin' => [
'top' => 0,
'bottom' => 12,
],
],
'URL' => '#recipe_photo_url#',
'caption' => [
'text' => '#recipe_photo_caption#',
'format' => 'html',
'textStyle' => $caption_textstyle,
],
],
[
'role' => 'container',
'layout' => [
'padding' => [
'left' => 12,
'right' => 12,
],
],
'components' => [
[
'role' => 'title',
'layout' => [
'margin' => [
'top' => 8,
'bottom' => 12,
],
],
'textStyle' => $title_textstyle,
'text' => '#recipe_title#',
'format' => 'html',
],
[
'role' => 'heading2',
'layout' => 'recipe-header2-layout',
'textStyle' => 'recipe-header2-style',
'text' => __( 'Recipe Information', 'apple-news' ),
'format' => 'html',
],
[
'role' => 'container',
'layout' => [
'columnSpan' => 4,
'columnStart' => 0,
'margin' => [
'bottom' => 10,
'top' => 24,
],
'padding' => [
'left' => 0,
'right' => 0,
'top' => 10,
'bottom' => 0,
],
],
'contentDisplay' => [
'type' => 'collection',
'gutter' => '20',
'rowSpacing' => '10',
'alignment' => 'left',
'minimumWidth' => 200,
],
'components' => [
[
'role' => 'body',
'layout' => 'recipe-details-layout',
'textStyle' => 'recipe-details-style',
'text' => '#recipe_yield#',
'format' => 'html',
],
[
'role' => 'body',
'layout' => 'recipe-details-layout',
'textStyle' => 'recipe-details-style',
'text' => '#recipe_prep_time#',
'format' => 'html',
],
[
'role' => 'body',
'layout' => 'recipe-details-layout',
'textStyle' => 'recipe-details-style',
'text' => '#recipe_cook_time#',
'format' => 'html',
],
[
'role' => 'body',
'layout' => 'recipe-details-layout',
'textStyle' => 'recipe-details-style',
'text' => '#recipe_total_time#',
'format' => 'html',
],
[
'role' => 'body',
'layout' => 'recipe-details-layout',
'textStyle' => 'recipe-details-style',
'text' => '#recipe_calories_per_serving#',
'format' => 'html',
],
],
],
[
'role' => 'divider',
'style' => [
'border' => [
'all' => [
'width' => 1,
'style' => 'solid',
'color' => '#000',
],
'left' => false,
'right' => false,
'top' => false,
],
'layout' => [
'margin' => [
'top' => 10,
'bottom' => 30,
],
],
],
],
[
'role' => 'section',
'components' => [
[
'role' => 'container',
'components' => [
[
'role' => 'container',
'layout' => [
'margin' => [
'bottom' => 20,
'top' => 20,
],
],
'components' => [
[
'role' => 'heading2',
'layout' => 'recipe-header2-layout',
'textStyle' => 'recipe-header2-style',
'text' => __( 'Ingredients', 'apple-news' ),
'format' => 'html',
],
[
'role' => 'body',
'layout' => 'recipe-body-layout',
'textStyle' => 'recipe-body-style',
'text' => '#recipe_ingredients#',
'format' => 'html',
],
],
],
[
'role' => 'container',
'components' => [
[
'role' => 'heading2',
'layout' => 'recipe-header2-layout',
'textStyle' => 'recipe-header2-style',
'text' => __( 'Directions', 'apple-news' ),
'format' => 'html',
],
[
'role' => 'container',
'components' => '#recipe_instructions#',
],
],
],
],
],
],
],
],
],
],
],
);
$this->register_spec(
'recipe-body-layout',
__( 'Recipe Body Layout', 'apple-news' ),
[
'margin' => [
'bottom' => 10,
],
],
);
$this->register_spec(
'recipe-body-style',
__( 'Recipe Body Text Style', 'apple-news' ),
$body_textstyle,
);
$this->register_spec(
'recipe-header2-layout',
__( 'Recipe Header 2 Layout', 'apple-news' ),
[
'margin' => [
'top' => 6,
'bottom' => 6,
],
],
);
$this->register_spec(
'recipe-header2-style',
__( 'Recipe Header 2 Text Style', 'apple-news' ),
$header2_textstyle,
);
$this->register_spec(
'recipe-header3-layout',
__( 'Recipe Header 3 Layout', 'apple-news' ),
[
'margin' => [
'top' => 6,
'bottom' => 6,
],
],
);
$this->register_spec(
'recipe-header3-style',
__( 'Recipe Header 3 Text Style', 'apple-news' ),
$header3_textstyle,
);
$this->register_spec(
'recipe-header4-layout',
__( 'Recipe Header 4 Layout', 'apple-news' ),
[
'margin' => [
'top' => 6,
'bottom' => 6,
],
],
);
$this->register_spec(
'recipe-header4-style',
__( 'Recipe Header 4 Text Style', 'apple-news' ),
$header4_textstyle,
);
$this->register_spec(
'recipe-details-layout',
__( 'Recipe Details Layout', 'apple-news' ),
[
'margin' => [
'bottom' => 10,
],
],
);
$this->register_spec(
'recipe-details-style',
__( 'Recipe Details Text Style', 'apple-news' ),
$details_textstyle,
);
$this->register_spec(
'recipe-section-json',
__( 'JSON for a Section of Recipe Instructions', 'apple-news' ),
[
'role' => 'container',
'components' => [
[
'role' => 'heading3',
'layout' => 'recipe-header3-layout',
'textStyle' => 'recipe-header3-style',
'text' => '#recipe_section_name#',
'format' => 'html',
],
[
'role' => 'container',
'components' => '#components#',
],
],
],
);
$this->register_spec(
'recipe-section-step-json',
__( 'JSON for a Step Within a Section of Recipe Instructions', 'apple-news' ),
[
'role' => 'container',
'components' => [
[
'role' => 'heading4',
'layout' => 'recipe-header4-layout',
'textStyle' => 'recipe-header4-style',
'text' => '#recipe_step_name#',
'format' => 'html',
],
[
'role' => 'photo',
'layout' => [
'margin' => [
'top' => 6,
'bottom' => 12,
],
],
'URL' => '#recipe_step_photo_url#',
],
[
'role' => 'body',
'layout' => 'recipe-body-layout',
'textStyle' => 'recipe-body-style',
'text' => '#recipe_step_text#',
'format' => 'html',
],
],
],
);
$this->register_spec(
'recipe-step-json',
__( 'JSON for a Standalone Step in Recipe Instructions', 'apple-news' ),
[
'role' => 'container',
'components' => [
[
'role' => 'heading3',
'layout' => 'recipe-header3-layout',
'textStyle' => 'recipe-header3-style',
'text' => '#recipe_step_name#',
'format' => 'html',
],
[
'role' => 'photo',
'layout' => [
'margin' => [
'top' => 6,
'bottom' => 12,
],
],
'URL' => '#recipe_step_photo_url#',
],
[
'role' => 'body',
'layout' => 'recipe-body-layout',
'textStyle' => 'recipe-body-style',
'text' => '#recipe_step_text#',
'format' => 'html',
],
],
],
);
$this->register_spec(
'wrapper-only-json',
__( 'JSON for a Wrapper Only', 'apple-news' ),
[
'role' => 'recipe',
'URL' => '#url#',
'components' => '#components#',
],
);
}
/**
* Build the component.
*
* @param string $html The HTML to parse into text for processing.
*/
protected function build( $html ) {
$theme = \Apple_Exporter\Theme::get_used();
$schema = $this->matching_schema( $html );
if ( ! $schema ) {
return;
}
if ( $this->get_setting( 'recipe_component_use_schema' ) === 'no' ) {
$export = new Exporter_Content( $this->workspace->content_id, '', $html );
$components = [];
foreach ( $export->nodes() as $node ) {
// $node is the original recipe element. We want the HTML within that element, otherwise we'd match the recipe node again.
if ( $node->hasChildNodes() ) {
foreach ( $node->childNodes as $child ) {
$components = array_merge( $components, Component_Factory::get_components_from_node( $child, $this ) );
}
}
}
$this->register_json(
'wrapper-only-json',
[
'#url#' => $this->value_for_token( '#url#', $schema ),
'#components#' => array_map( fn ( Component $component ) => $component->to_array(), $components ),
],
);
return;
}
// Set up values for the tokens.
$recipe_photo_url = $this->value_for_token( '#recipe_photo_url#', $schema );
if ( $recipe_photo_url ) {
$recipe_photo_url = $this->maybe_bundle_source( $recipe_photo_url );
}
$recipe_instructions = $this->value_for_token( '#recipe_instructions#', $schema );
if ( is_array( $recipe_instructions ) ) {
$recipe_instructions = $this->without_objects_containing_tokens( $recipe_instructions, $this->get_spec( 'recipe-step-json' ) );
}
$this->register_json(
'json',
[
'#url#' => $this->value_for_token( '#url#', $schema ),
'#recipe_photo_url#' => $recipe_photo_url,
'#recipe_photo_caption#' => $this->value_for_token( '#recipe_photo_caption#', $schema ),
'#recipe_title#' => $this->value_for_token( '#recipe_title#', $schema ),
'#recipe_yield#' => $this->value_for_token( '#recipe_yield#', $schema ),
'#recipe_prep_time#' => $this->value_for_token( '#recipe_prep_time#', $schema ),
'#recipe_cook_time#' => $this->value_for_token( '#recipe_cook_time#', $schema ),
'#recipe_total_time#' => $this->value_for_token( '#recipe_total_time#', $schema ),
'#recipe_calories_per_serving#' => $this->value_for_token( '#recipe_calories_per_serving#', $schema ),
'#recipe_ingredients#' => $this->value_for_token( '#recipe_ingredients#', $schema ),
'#recipe_instructions#' => $recipe_instructions,
],
);
$this->register_layout(
'recipe-body-layout',
'recipe-body-layout',
);
$this->register_style(
'recipe-body-style',
'recipe-body-style',
[
'#recipe_body_font#' => $theme->get_value( 'recipe_body_font' ),
'#recipe_body_size#' => (int) $theme->get_value( 'recipe_body_size' ),
'#recipe_body_line_height#' => (int) $theme->get_value( 'recipe_body_line_height' ),
'#recipe_body_tracking#' => (int) $theme->get_value( 'recipe_body_tracking' ) / 100,
'#recipe_body_color#' => $theme->get_value( 'recipe_body_color' ),
'#recipe_body_color_dark#' => $theme->get_value( 'recipe_body_color_dark' ),
'#recipe_body_link_color#' => $theme->get_value( 'recipe_body_link_color' ),
'#recipe_body_link_color_dark#' => $theme->get_value( 'recipe_body_link_color_dark' ),
'#recipe_body_background_color#' => $theme->get_value( 'recipe_body_background_color' ),
'#recipe_body_background_color_dark#' => $theme->get_value( 'recipe_body_background_color_dark' ),
],
'textStyle',
);
$this->register_layout(
'recipe-header2-layout',
'recipe-header2-layout',
);
$this->register_style(
'recipe-header2-style',
'recipe-header2-style',
[
'#recipe_header2_font#' => $theme->get_value( 'recipe_header2_font' ),
'#recipe_header2_size#' => (int) $theme->get_value( 'recipe_header2_size' ),
'#recipe_header2_line_height#' => (int) $theme->get_value( 'recipe_header2_line_height' ),
'#recipe_header2_tracking#' => (int) $theme->get_value( 'recipe_header2_tracking' ) / 100,
'#recipe_header2_color#' => $theme->get_value( 'recipe_header2_color' ),
'#recipe_header2_color_dark#' => $theme->get_value( 'recipe_header2_color_dark' ),
],
'textStyle',
);
$this->register_layout(
'recipe-header3-layout',
'recipe-header3-layout',
);
$this->register_style(
'recipe-header3-style',
'recipe-header3-style',
[
'#recipe_header3_font#' => $theme->get_value( 'recipe_header3_font' ),
'#recipe_header3_size#' => (int) $theme->get_value( 'recipe_header3_size' ),
'#recipe_header3_line_height#' => (int) $theme->get_value( 'recipe_header3_line_height' ),
'#recipe_header3_tracking#' => (int) $theme->get_value( 'recipe_header3_tracking' ) / 100,
'#recipe_header3_color#' => $theme->get_value( 'recipe_header3_color' ),
'#recipe_header3_color_dark#' => $theme->get_value( 'recipe_header3_color_dark' ),
],
'textStyle',
);
$this->register_layout(
'recipe-header4-layout',
'recipe-header4-layout',
);
$this->register_style(
'recipe-header4-style',
'recipe-header4-style',
[
'#recipe_header4_font#' => $theme->get_value( 'recipe_header4_font' ),
'#recipe_header4_size#' => (int) $theme->get_value( 'recipe_header4_size' ),
'#recipe_header4_line_height#' => (int) $theme->get_value( 'recipe_header4_line_height' ),
'#recipe_header4_tracking#' => (int) $theme->get_value( 'recipe_header4_tracking' ) / 100,
'#recipe_header4_color#' => $theme->get_value( 'recipe_header4_color' ),
'#recipe_header4_color_dark#' => $theme->get_value( 'recipe_header4_color_dark' ),
],
'textStyle',
);
$this->register_layout(
'recipe-details-layout',
'recipe-details-layout',
);
$this->register_style(
'recipe-details-style',
'recipe-details-style',
[
'#recipe_details_font#' => $theme->get_value( 'recipe_details_font' ),
'#recipe_details_size#' => (int) $theme->get_value( 'recipe_details_size' ),
'#recipe_details_line_height#' => (int) $theme->get_value( 'recipe_details_line_height' ),
'#recipe_details_tracking#' => (int) $theme->get_value( 'recipe_details_tracking' ) / 100,
'#recipe_details_color#' => $theme->get_value( 'recipe_details_color' ),
'#recipe_details_color_dark#' => $theme->get_value( 'recipe_details_color_dark' ),
'#recipe_details_link_color#' => $theme->get_value( 'recipe_details_link_color' ),
'#recipe_details_link_color_dark#' => $theme->get_value( 'recipe_details_link_color_dark' ),
'#recipe_details_background_color#' => $theme->get_value( 'recipe_details_background_color' ),
'#recipe_details_background_color_dark#' => $theme->get_value( 'recipe_details_background_color_dark' ),
],
'textStyle',
);
}
/**
* Set the JSON for the component.
*
* @param string $spec_name The spec to use for defining the JSON.
* @param array $values Values to substitute for placeholders in the spec.
*/
protected function register_json( $spec_name, $values = [] ) {
if ( 'json' === $spec_name ) {
$values = $this->prepare_tokens( $values );
}
// Go through normal token replacement.
parent::register_json( $spec_name, $values );
if ( 'json' === $spec_name ) {
if ( is_array( $this->json ) ) {
$spec = $this->get_spec( $spec_name );
if ( $spec instanceof Component_Spec ) {
// Update the JSON to remove objects with recipe tokens that weren't replaced.
$this->json = $this->without_objects_containing_tokens( $this->json, $spec );
}
}
}
}
/**
* Find JSON-LD Recipe items for the given recipe HTML.
*
* @param string $recipe The recipe HTML found in the matching node for this component instance.
* @return array|null The matching schema, or null if none found.
*/
private function matching_schema( $recipe ): ?array {
// First, check the post content for a JSON-LD Recipe.
$post = get_post( $this->workspace->content_id );
/** This filter is documented in admin/apple-actions/index/class-export.php */
$post_content = apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
/** This filter is documented in wp-includes/post-template.php */
$post_content = apply_filters( 'the_content', $post_content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
$items = self::recipe_items( $post_content, 'body' );
foreach ( $items as $item ) {
if ( self::is_recipe_item_for_recipe_html( $item, $recipe ) ) {
return $item;
}
}
// Second, try to find schema in the HEAD of the post on the frontend.
// Could check BODY as well, but the post content is already being scanned. That might be enough.
/**
* Filters the URL for an article that will be fetched and searched for JSON-LD Recipe items.
*
* @param string $permalink The URL to fetch.
* @param int $content_id The ID of the content being processed.
*/
$permalink = apply_filters( 'apple_news_recipe_schema_permalink', get_permalink( $this->workspace->content_id ), $this->workspace->content_id );
if ( $permalink ) {
$resp = wp_remote_get( $permalink ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
$code = wp_remote_retrieve_response_code( $resp );
$html = ( $code >= 200 && $code < 300 ) ? wp_remote_retrieve_body( $resp ) : '';
$items = self::recipe_items( $html, 'head' );
foreach ( $items as $item ) {
if ( self::is_recipe_item_for_recipe_html( $item, $recipe ) ) {
return $item;
}
}
}
return null;
}
/**
* All JSON-LD Recipe items in the given HTML.
*
* @param string $document The document to search for recipe items.
* @param string $tag_name The tag name to search for recipe items.
* @return array The recipe items found in the document.
*/
private static function recipe_items( string $document, string $tag_name ): array {
$out = [];
// Do some naive checks before the intensive processing to see if the HTML contains JSON-LD and a Recipe.
if ( str_contains( $document, 'application/ld+json' ) && str_contains( $document, '"Recipe"' ) ) {
$dom = new \DOMDocument();
libxml_use_internal_errors( true );
$dom->loadHTML( '<?xml encoding="utf-8" ?>' . $document );
libxml_clear_errors();
$body = $dom->getElementsByTagName( $tag_name )->item( 0 );
$nodes = $body ? $body->childNodes : new DOMNodeList();
$out = self::recipe_items_in_nodes( [], $nodes );
}
return $out;
}
/**
* Recursively search for JSON-LD Recipe items in the given nodes.
*
* @param array $carry The array to accumulate recipe items.
* @param DOMNodeList $nodes The nodes to search for recipe items.
* @return array The recipe items found in the nodes.
*/
private static function recipe_items_in_nodes( array $carry, DOMNodeList $nodes ): array {
foreach ( $nodes as $node ) {
if ( 'script' === $node->nodeName && 'application/ld+json' === $node->getAttribute( 'type' ) ) {
try {
$json = json_decode( $node->textContent, true, 512, JSON_THROW_ON_ERROR ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( isset( $json['@type'] ) && 'Recipe' === $json['@type'] ) {
$carry[] = $json;
}
} catch ( \JsonException $e ) {
// Do nothing.
unset( $e );
}
}
if ( $node->hasChildNodes() ) {
$carry = self::recipe_items_in_nodes( $carry, $node->childNodes );
}
}
return $carry;
}
/**
* Best-guess if the given schema item is for the given recipe HTML.
*
* @param array $schema The schema item to check.
* @param string $html The recipe HTML to check.
* @return bool True if the schema item is for the recipe HTML, false otherwise.
*/
private static function is_recipe_item_for_recipe_html( array $schema, string $html ) {
return (
isset( $schema['name'] )
&& is_string( $schema['name'] )
&& strlen( $schema['name'] ) > 0
&& str_contains( $html, $schema['name'] )
);
}
/**
* Value for the given Apple News theme token in the given JSON-LD Recipe item.
*
* @param string $token The token to replace.
* @param array $schema The item to search.
* @return string|array|null
*/
private function value_for_token( string $token, array $schema ) {
switch ( $token ) {
case '#url#':
return $schema['url'] ?? get_permalink( $this->workspace->content_id );
case '#recipe_photo_url#':
// The `image` property appears to be predominant in actual uses, e.g. the example at https://schema.org/Recipe. Can be an ImageObject or URL.
$url = $schema['image']['contentUrl'] ?? $schema['image']['url'] ?? $schema['image'][0] ?? $schema['image'] ?? null;
return is_string( $url ) ? $url : null;
case '#recipe_photo_caption#':
// Available when the `image` is an ImageObject. Can be MediaObject or Text, but MediaObject use case ("closed caption, subtitles etc.") doesn't apply here.
return $schema['image']['caption'] ?? null;
case '#recipe_title#':
// Text type.
return $schema['name'] ?? null;
case '#recipe_yield#':
// Can be QuantitativeValue or Text. It isn't clear from the examples how the properties of QuantitativeValue would be compiled into a final value, so not yet implementing.
if ( ! isset( $schema['recipeYield'] ) || ! is_string( $schema['recipeYield'] ) ) {
return null;
}
return sprintf(
'<p><strong>%s</strong> %s</p>',
esc_html__( 'Yields:', 'apple-news' ),
esc_html( $schema['recipeYield'] )
);
case '#recipe_prep_time#':
// ISO 8601 duration format.
if ( ! isset( $schema['prepTime'] ) || ! is_string( $schema['prepTime'] ) ) {
return null;
}
try {
$di = new \DateInterval( $schema['prepTime'] );
} catch ( \Exception $e ) {
return null;
}
return sprintf(
'<p><strong>%s</strong> %s</p>',
esc_html__( 'Prep Time:', 'apple-news' ),
esc_html( $this->duration( $di ) )
);
case '#recipe_cook_time#':
// ISO 8601 duration format.
if ( ! isset( $schema['cookTime'] ) || ! is_string( $schema['cookTime'] ) ) {
return null;
}
try {
$di = new \DateInterval( $schema['cookTime'] );
} catch ( \Exception $e ) {
return null;
}
return sprintf(
'<p><strong>%s</strong> %s</p>',
esc_html__( 'Cook Time:', 'apple-news' ),
esc_html( $this->duration( $di ) )
);
case '#recipe_total_time#':
// ISO 8601 duration format.
if ( ! isset( $schema['totalTime'] ) || ! is_string( $schema['totalTime'] ) ) {
return null;
}
try {
$di = new \DateInterval( $schema['totalTime'] );
} catch ( \Exception $e ) {
return null;
}
return sprintf(
'<p><strong>%s</strong> %s</p>',
esc_html__( 'Total Time:', 'apple-news' ),
esc_html( $this->duration( $di ) )
);
case '#recipe_calories_per_serving#':
// NutritionInformation type.
if ( ! isset( $schema['nutrition']['calories'] ) || ! is_string( $schema['nutrition']['calories'] ) ) {
return null;
}
return sprintf(
'<p><strong>%s</strong> %s</p>',
esc_html__( 'Calories/Serving:', 'apple-news' ),
// `calories` is of type Energy, which is of the form '<Number> <Energy unit of measure>'.
esc_html( preg_replace( '/\D/', '', $schema['nutrition']['calories'] ) )