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

Term_Page_Hook::load_scripts()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Vocabulary\Hooks;
4
5
use Wordlift\Scripts\Scripts_Helper;
6
use Wordlift\Vocabulary\Analysis_Background_Service;
7
use Wordlift\Vocabulary\Api\Api_Config;
8
use Wordlift\Vocabulary\Api\Entity_Rest_Endpoint;
9
use Wordlift\Vocabulary\Data\Term_Data\Term_Data_Factory;
10
use Wordlift\Vocabulary\Pages\Match_Terms;
11
12
/**
13
 * This class is used to show the entity match component on the
14
 * term page.
15
 */
16
class Term_Page_Hook {
17
18
	const HANDLE = 'wl-vocabulary-term-page-handle';
19
20
	const LOCALIZED_KEY = '_wlVocabularyTermPageSettings';
21
22
	private $term_data_factory;
23
24
	/**
25
	 * Term_Page_Hook constructor.
26
	 *
27
	 * @param $term_data_factory Term_Data_Factory
28
	 */
29
	public function __construct( $term_data_factory ) {
30
		$this->term_data_factory = $term_data_factory;
31
	}
32
33
	public function connect_hook() {
34
35
		add_action( 'post_tag_edit_form_fields', array( $this, 'load_scripts' ), PHP_INT_MAX );
36
37
	}
38
39
	/**
40
	 * @param $term \WP_Term
41
	 */
42
	public function load_scripts( $term ) {
43
44
		$is_entity_matched = intval( get_term_meta( $term->term_id, Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, true ) ) === 1;
45
46
		$is_entities_present = intval( get_term_meta( $term->term_id, Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, true ) ) === 1;
47
48
		if ( $is_entity_matched || ! $is_entities_present ) {
49
			return;
50
		}
51
52
		$term_data = $this->term_data_factory->get_term_data( $term );
53
54
		Scripts_Helper::enqueue_based_on_wordpress_version(
55
			self::HANDLE,
56
			plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary-term-page',
57
			array( 'react', 'react-dom', 'wp-polyfill' ),
58
			true
59
		);
60
61
		wp_enqueue_style( self::HANDLE, plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary-term-page.full.css' );
62
63
		wp_localize_script( self::HANDLE, self::LOCALIZED_KEY, array(
64
			'termData'  => $term_data->get_data(),
65
			'apiConfig' => Api_Config::get_api_config()
66
		) );
67
68
		echo "<tr class=\"form-field\">
69
				<th>Match</th>
70
				<td style='width: 100%;'><div id='wl_vocabulary_terms_widget'></div></td>
71
			</tr>";
72
	}
73
74
}