|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 1.0.0 |
|
4
|
|
|
* @author Akshay Raje <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Vocabulary\Jsonld; |
|
8
|
|
|
|
|
9
|
|
|
use Wordlift\Vocabulary\Api\Entity_Rest_Endpoint; |
|
10
|
|
|
use Wordlift\Vocabulary\Data\Entity_List\Entity_List_Factory; |
|
11
|
|
|
|
|
12
|
|
|
class Post_Jsonld { |
|
13
|
|
|
|
|
14
|
|
|
public function enhance_post_jsonld() { |
|
15
|
|
|
add_filter( 'wl_post_jsonld_array', array( $this, 'wl_post_jsonld_array' ), 11, 2 ); |
|
16
|
|
|
add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 11, 2 ); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
View Code Duplication |
public function wl_post_jsonld_array( $arr, $post_id ) { |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
$jsonld = $arr['jsonld']; |
|
22
|
|
|
$references = $arr['references']; |
|
23
|
|
|
|
|
24
|
|
|
$this->add_mentions( $post_id, $jsonld, $references ); |
|
25
|
|
|
|
|
26
|
|
|
return array( |
|
27
|
|
|
'jsonld' => $jsonld, |
|
28
|
|
|
'references' => $references |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function add_mentions( $post_id, &$jsonld, &$references ) { |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$tags = get_the_tags( $post_id ); |
|
36
|
|
|
|
|
37
|
|
|
if ( ! $tags || is_wp_error( $tags ) ) { |
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ( ! array_key_exists( 'mentions', $jsonld ) ) { |
|
42
|
|
|
$jsonld['mentions'] = array(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
foreach ( $tags as $tag ) { |
|
46
|
|
|
|
|
47
|
|
|
$is_matched = intval( get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1; |
|
48
|
|
|
|
|
49
|
|
|
if ( ! $is_matched ) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$entity = Entity_List_Factory::get_instance( $tag->term_id ); |
|
54
|
|
|
|
|
55
|
|
|
$entities = $entity->get_jsonld_data(); |
|
56
|
|
|
|
|
57
|
|
|
if ( count( $entities ) === 0 ) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$jsonld['mentions'] = array_merge( $jsonld['mentions'], self::add_additional_attrs( $tag, $entities ) ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $term \WP_Term |
|
68
|
|
|
* @param $entities |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function add_additional_attrs( $term, $entities ) { |
|
73
|
|
|
|
|
74
|
|
|
return array_map( function ( $entity ) use ( $term ) { |
|
75
|
|
|
$entity['@id'] = get_term_link( $term->term_id ) . '#id'; |
|
76
|
|
|
if ( ! empty( $term->description ) ) { |
|
77
|
|
|
$entity['description'] = $term->description; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $entity; |
|
81
|
|
|
|
|
82
|
|
|
}, $entities ); |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) { |
|
89
|
|
|
return $jsonld; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
foreach ( $jsonld as $key => $value ) { |
|
93
|
|
|
if ( $value['@type'] === 'Article' && isset( $value['image'] ) ) { |
|
94
|
|
|
$image = $value['image']; |
|
95
|
|
|
} |
|
96
|
|
|
if ( $value['@type'] === 'Recipe' && ! isset( $value['image'] ) ) { |
|
97
|
|
|
$index = $key; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ( isset( $index ) && ! empty( $image ) ) { |
|
102
|
|
|
$jsonld[ $index ]['image'] = $image; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $jsonld; |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.