Completed
Push — develop ( 72a91d...14a321 )
by
unknown
04:09 queued 12s
created

Post_Jsonld::wl_after_get_jsonld()   B

Complexity

Conditions 10
Paths 11

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 11
nop 2
dl 0
loc 22
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
			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 ) {
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...
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