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

Post_Jsonld   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 98
Duplicated Lines 13.27 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 13
loc 98
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enhance_post_jsonld() 0 4 1
A wl_post_jsonld_array() 13 13 1
B add_mentions() 0 32 7
A add_additional_attrs() 0 13 2
B wl_after_get_jsonld() 0 22 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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