Completed
Pull Request — develop (#1328)
by Naveen
03:10
created

Post_Jsonld::add_mentions()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 7
nop 3
dl 0
loc 26
rs 8.8817
c 0
b 0
f 0
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 ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $references is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
35
		$tags = get_the_tags( $post_id );
36
37
		if ( $tags && ! is_wp_error( $tags ) ) {
38
39
			if ( ! array_key_exists('mentions', $jsonld) ) {
40
				$jsonld['mentions'] = array();
41
			}
42
43
			// Loop through the tags and push it to references.
44
			foreach ( $tags as $tag ) {
45
46
				$is_matched = intval( get_term_meta( $tag->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1;
47
48
				if ( $is_matched ) {
49
50
					$entity = Entity_List_Factory::get_instance( $tag->term_id );
51
52
					$jsonld['mentions'] = array_merge( $jsonld['mentions'], self::add_additional_attrs( $tag, $entity->get_jsonld_data() ) );
53
				}
54
55
			}
56
		}
57
58
	}
59
60
	/**
61
	 * @param $term \WP_Term
62
	 * @param $entities
63
	 *
64
	 * @return array
65
	 */
66
	public static function add_additional_attrs( $term, $entities ) {
67
68
		return array_map( function ( $entity ) use ( $term ) {
69
			$entity['@id'] = get_term_link( $term->term_id ) . '#id';
70
			if ( ! empty( $term->description ) ) {
71
				$entity['description'] = $term->description;
72
			}
73
74
			return $entity;
75
76
		}, $entities );
77
78
	}
79
80
	public function wl_after_get_jsonld( $jsonld, $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
82
		if ( ! is_array( $jsonld ) || count( $jsonld ) === 0 ) {
83
			return $jsonld;
84
		}
85
86
		foreach ( $jsonld as $key => $value ) {
87
			if ( $value['@type'] === 'Article' && isset( $value['image'] ) ) {
88
				$image = $value['image'];
89
			}
90
			if ( $value['@type'] === 'Recipe' && ! isset( $value['image'] ) ) {
91
				$index = $key;
92
			}
93
		}
94
95
		if ( isset( $index ) && ! empty( $image ) ) {
96
			$jsonld[ $index ]['image'] = $image;
97
		}
98
99
		return $jsonld;
100
101
	}
102
103
}
104