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

Term_Page_Hook::load_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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