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

Default_Entity::clear_data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Vocabulary\Data\Entity;
4
5
use Wordlift\Vocabulary\Api\Entity_Rest_Endpoint;
6
7
/**
8
 * This class is created to support new multiple entity matches in db.
9
 * @since 3.30.0
10
 * @author Naveen Muthusamy <[email protected]>
11
 */
12
class Default_Entity extends Entity {
13
14
	const META_KEY = '_wl_vocabulary_entity_match_for_term';
15
16
	/**
17
	 * @var Legacy_Entity
18
	 */
19
	private $legacy_entity;
20
21
	/**
22
	 * Default_Entity constructor.
23
	 *
24
	 * @param $term_id int
25
	 * @param $legacy_entity Legacy_Entity
26
	 */
27
	public function __construct( $term_id, $legacy_entity ) {
28
		parent::__construct( $term_id );
29
		$this->legacy_entity = $legacy_entity;
30
	}
31
32
	public function get_jsonld_data() {
33
		$default_data = get_term_meta( $this->term_id, self::META_KEY, true );
34
		if ( is_array( $default_data ) ) {
35
			return $default_data;
36
		}
37
38
		// Use legacy entity if the data doesnt exist on that key.
39
		return $this->legacy_entity->get_jsonld_data();
40
	}
41
42
	public function save_jsonld_data( $entity_data ) {
43
		$same_as_list = array_merge( $entity_data['sameAs'], array( $entity_data['@id'] ) );
44
45
		$alt_labels = array( (string) $entity_data['name'] );
46
47
		$entity_list = get_term_meta( $this->term_id, self::META_KEY, true );
48
49
		$entity = array(
50
			'@type'         => $entity_data['@type'],
51
			'description'   => $entity_data['description'],
52
			'sameAs'        => $same_as_list,
53
			'alternateName' => $alt_labels
54
		);
55
56
		if ( ! is_array( $entity_list ) ) {
57
			// Then the data is not present, so wrap the data in array
58
			$entity_list = array( $entity );
59
		} else {
60
			array_push( $entity_list, $entity );
61
		}
62
63
		update_term_meta( $this->term_id, self::META_KEY, $entity_list );
64
65
	}
66
67
	public function clear_data() {
68
		delete_term_meta( $this->term_id, self::META_KEY );
69
70
	}
71
}