@@ -10,38 +10,38 @@ |
||
10 | 10 | */ |
11 | 11 | class Badge_Generator { |
12 | 12 | |
13 | - /** |
|
14 | - * Returns the term count which needs to be shown on ui. |
|
15 | - * |
|
16 | - * @param $number |
|
17 | - * |
|
18 | - * @return int |
|
19 | - */ |
|
20 | - public static function get_ui_count( $number ) { |
|
21 | - |
|
22 | - $number = (int) $number; |
|
23 | - |
|
24 | - if ( $number < 100 ) { |
|
25 | - return $number; |
|
26 | - } |
|
27 | - |
|
28 | - return 100; |
|
29 | - } |
|
30 | - |
|
31 | - public static function generate_html( $number ) { |
|
32 | - $count_string = self::get_formatted_count_string( $number ); |
|
33 | - return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @param $number |
|
38 | - * |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public static function get_formatted_count_string( $number ) { |
|
42 | - $round = self::get_ui_count( $number ); |
|
43 | - |
|
44 | - return $round < 100 ? "$round" : "$round+"; |
|
45 | - } |
|
13 | + /** |
|
14 | + * Returns the term count which needs to be shown on ui. |
|
15 | + * |
|
16 | + * @param $number |
|
17 | + * |
|
18 | + * @return int |
|
19 | + */ |
|
20 | + public static function get_ui_count( $number ) { |
|
21 | + |
|
22 | + $number = (int) $number; |
|
23 | + |
|
24 | + if ( $number < 100 ) { |
|
25 | + return $number; |
|
26 | + } |
|
27 | + |
|
28 | + return 100; |
|
29 | + } |
|
30 | + |
|
31 | + public static function generate_html( $number ) { |
|
32 | + $count_string = self::get_formatted_count_string( $number ); |
|
33 | + return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @param $number |
|
38 | + * |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public static function get_formatted_count_string( $number ) { |
|
42 | + $round = self::get_ui_count( $number ); |
|
43 | + |
|
44 | + return $round < 100 ? "$round" : "$round+"; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | } |
@@ -17,19 +17,19 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @return int |
19 | 19 | */ |
20 | - public static function get_ui_count( $number ) { |
|
20 | + public static function get_ui_count($number) { |
|
21 | 21 | |
22 | 22 | $number = (int) $number; |
23 | 23 | |
24 | - if ( $number < 100 ) { |
|
24 | + if ($number < 100) { |
|
25 | 25 | return $number; |
26 | 26 | } |
27 | 27 | |
28 | 28 | return 100; |
29 | 29 | } |
30 | 30 | |
31 | - public static function generate_html( $number ) { |
|
32 | - $count_string = self::get_formatted_count_string( $number ); |
|
31 | + public static function generate_html($number) { |
|
32 | + $count_string = self::get_formatted_count_string($number); |
|
33 | 33 | return "<span class=\"wl-admin-menu-badge\">$count_string</span>"; |
34 | 34 | } |
35 | 35 | |
@@ -38,8 +38,8 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @return string |
40 | 40 | */ |
41 | - public static function get_formatted_count_string( $number ) { |
|
42 | - $round = self::get_ui_count( $number ); |
|
41 | + public static function get_formatted_count_string($number) { |
|
42 | + $round = self::get_ui_count($number); |
|
43 | 43 | |
44 | 44 | return $round < 100 ? "$round" : "$round+"; |
45 | 45 | } |
@@ -12,54 +12,54 @@ |
||
12 | 12 | * @author Naveen Muthusamy <[email protected]> |
13 | 13 | */ |
14 | 14 | class Match_Terms { |
15 | - /** |
|
16 | - * @var Term_Count |
|
17 | - */ |
|
18 | - private $term_count; |
|
15 | + /** |
|
16 | + * @var Term_Count |
|
17 | + */ |
|
18 | + private $term_count; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Match_Terms constructor. |
|
22 | - * |
|
23 | - * @param $term_count Term_Count |
|
24 | - */ |
|
25 | - public function __construct( $term_count ) { |
|
20 | + /** |
|
21 | + * Match_Terms constructor. |
|
22 | + * |
|
23 | + * @param $term_count Term_Count |
|
24 | + */ |
|
25 | + public function __construct( $term_count ) { |
|
26 | 26 | |
27 | - $this->term_count = $term_count; |
|
28 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
27 | + $this->term_count = $term_count; |
|
28 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
29 | 29 | |
30 | - } |
|
30 | + } |
|
31 | 31 | |
32 | - public function admin_menu() { |
|
33 | - $number = $this->term_count->get_term_count(); |
|
34 | - add_submenu_page( |
|
35 | - 'wl_admin_menu', |
|
36 | - __( 'Match Terms', 'wordlift' ), |
|
37 | - __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
38 | - 'manage_options', |
|
39 | - 'wl-vocabulary-match-terms', |
|
40 | - array( $this, 'submenu_page_callback' ) |
|
41 | - ); |
|
42 | - remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
43 | - } |
|
32 | + public function admin_menu() { |
|
33 | + $number = $this->term_count->get_term_count(); |
|
34 | + add_submenu_page( |
|
35 | + 'wl_admin_menu', |
|
36 | + __( 'Match Terms', 'wordlift' ), |
|
37 | + __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
38 | + 'manage_options', |
|
39 | + 'wl-vocabulary-match-terms', |
|
40 | + array( $this, 'submenu_page_callback' ) |
|
41 | + ); |
|
42 | + remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
43 | + } |
|
44 | 44 | |
45 | - public function submenu_page_callback() { |
|
45 | + public function submenu_page_callback() { |
|
46 | 46 | |
47 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
48 | - 'wl-vocabulary-reconcile-script', |
|
49 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | - array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
51 | - true |
|
52 | - ); |
|
47 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
48 | + 'wl-vocabulary-reconcile-script', |
|
49 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | + array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
51 | + true |
|
52 | + ); |
|
53 | 53 | |
54 | - wp_enqueue_style( |
|
55 | - 'wl-vocabulary-reconcile-script', |
|
56 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
57 | - array(), |
|
58 | - WORDLIFT_VERSION |
|
59 | - ); |
|
60 | - wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
61 | - echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
|
62 | - echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
|
63 | - } |
|
54 | + wp_enqueue_style( |
|
55 | + 'wl-vocabulary-reconcile-script', |
|
56 | + plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
57 | + array(), |
|
58 | + WORDLIFT_VERSION |
|
59 | + ); |
|
60 | + wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
61 | + echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
|
62 | + echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
|
63 | + } |
|
64 | 64 | |
65 | 65 | } |
@@ -22,10 +22,10 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param $term_count Term_Count |
24 | 24 | */ |
25 | - public function __construct( $term_count ) { |
|
25 | + public function __construct($term_count) { |
|
26 | 26 | |
27 | 27 | $this->term_count = $term_count; |
28 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
28 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
29 | 29 | |
30 | 30 | } |
31 | 31 | |
@@ -33,31 +33,31 @@ discard block |
||
33 | 33 | $number = $this->term_count->get_term_count(); |
34 | 34 | add_submenu_page( |
35 | 35 | 'wl_admin_menu', |
36 | - __( 'Match Terms', 'wordlift' ), |
|
37 | - __( 'Match Terms', 'wordlift' ) . ' ' . Badge_Generator::generate_html( $number ), |
|
36 | + __('Match Terms', 'wordlift'), |
|
37 | + __('Match Terms', 'wordlift').' '.Badge_Generator::generate_html($number), |
|
38 | 38 | 'manage_options', |
39 | 39 | 'wl-vocabulary-match-terms', |
40 | - array( $this, 'submenu_page_callback' ) |
|
40 | + array($this, 'submenu_page_callback') |
|
41 | 41 | ); |
42 | - remove_submenu_page( 'wl_admin_menu', 'wl_admin_menu' ); |
|
42 | + remove_submenu_page('wl_admin_menu', 'wl_admin_menu'); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | public function submenu_page_callback() { |
46 | 46 | |
47 | 47 | Scripts_Helper::enqueue_based_on_wordpress_version( |
48 | 48 | 'wl-vocabulary-reconcile-script', |
49 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary', |
|
50 | - array( 'react', 'react-dom', 'wp-i18n', 'wp-polyfill' ), |
|
49 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/vocabulary', |
|
50 | + array('react', 'react-dom', 'wp-i18n', 'wp-polyfill'), |
|
51 | 51 | true |
52 | 52 | ); |
53 | 53 | |
54 | 54 | wp_enqueue_style( |
55 | 55 | 'wl-vocabulary-reconcile-script', |
56 | - plugin_dir_url( dirname( dirname( __DIR__ ) ) ) . 'js/dist/vocabulary.full.css', |
|
56 | + plugin_dir_url(dirname(dirname(__DIR__))).'js/dist/vocabulary.full.css', |
|
57 | 57 | array(), |
58 | 58 | WORDLIFT_VERSION |
59 | 59 | ); |
60 | - wp_localize_script( 'wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config() ); |
|
60 | + wp_localize_script('wl-vocabulary-reconcile-script', '_wlVocabularyMatchTermsConfig', Api_Config::get_api_config()); |
|
61 | 61 | echo "<div id='wl_cmkg_reconcile_progress' class='wrap'></div>"; |
62 | 62 | echo "<div id='wl_cmkg_table' class='wrap'></div>"; |
63 | 63 | } |
@@ -4,26 +4,26 @@ |
||
4 | 4 | use Wordlift\Vocabulary\Analysis_Service; |
5 | 5 | |
6 | 6 | class Term_Data_Factory { |
7 | - /** |
|
8 | - * @var Analysis_Service |
|
9 | - */ |
|
10 | - private $analysis_service; |
|
7 | + /** |
|
8 | + * @var Analysis_Service |
|
9 | + */ |
|
10 | + private $analysis_service; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @var Analysis_Service |
|
14 | - */ |
|
15 | - public function __construct( $analysis_service ) { |
|
16 | - $this->analysis_service = $analysis_service; |
|
17 | - } |
|
12 | + /** |
|
13 | + * @var Analysis_Service |
|
14 | + */ |
|
15 | + public function __construct( $analysis_service ) { |
|
16 | + $this->analysis_service = $analysis_service; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param $term \WP_Term |
|
21 | - * |
|
22 | - * @return Term_Data |
|
23 | - */ |
|
24 | - public function get_term_data( $term ) { |
|
25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
26 | - return new Default_Term_Data( $term, $entities ); |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param $term \WP_Term |
|
21 | + * |
|
22 | + * @return Term_Data |
|
23 | + */ |
|
24 | + public function get_term_data( $term ) { |
|
25 | + $entities = $this->analysis_service->get_entities( $term ); |
|
26 | + return new Default_Term_Data( $term, $entities ); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | /** |
13 | 13 | * @var Analysis_Service |
14 | 14 | */ |
15 | - public function __construct( $analysis_service ) { |
|
15 | + public function __construct($analysis_service) { |
|
16 | 16 | $this->analysis_service = $analysis_service; |
17 | 17 | } |
18 | 18 | |
@@ -21,9 +21,9 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @return Term_Data |
23 | 23 | */ |
24 | - public function get_term_data( $term ) { |
|
25 | - $entities = $this->analysis_service->get_entities( $term ); |
|
26 | - return new Default_Term_Data( $term, $entities ); |
|
24 | + public function get_term_data($term) { |
|
25 | + $entities = $this->analysis_service->get_entities($term); |
|
26 | + return new Default_Term_Data($term, $entities); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | } |
@@ -8,11 +8,11 @@ |
||
8 | 8 | |
9 | 9 | interface Term_Data { |
10 | 10 | |
11 | - /** |
|
12 | - * Should return an array which can be used by ui components. |
|
13 | - * |
|
14 | - * @return array |
|
15 | - */ |
|
16 | - public function get_data(); |
|
11 | + /** |
|
12 | + * Should return an array which can be used by ui components. |
|
13 | + * |
|
14 | + * @return array |
|
15 | + */ |
|
16 | + public function get_data(); |
|
17 | 17 | |
18 | 18 | } |
@@ -12,52 +12,52 @@ |
||
12 | 12 | */ |
13 | 13 | class Entity_List_Utils { |
14 | 14 | |
15 | - /** |
|
16 | - * @param $term_id int Term id. |
|
17 | - * @param $entities array An array of entities |
|
18 | - * |
|
19 | - * @return array<array> An Array of entities with isActive filter. |
|
20 | - */ |
|
21 | - public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
22 | - |
|
23 | - $active_entities = self::get_active_entities( $term_id ); |
|
24 | - |
|
25 | - if ( ! is_array( $entities ) ) { |
|
26 | - return $entities; |
|
27 | - } |
|
28 | - |
|
29 | - foreach ( $entities as &$entity ) { |
|
30 | - $entity_id = $entity['entityId']; |
|
31 | - $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
32 | - } |
|
33 | - |
|
34 | - return $entities; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @param $term_id |
|
39 | - * |
|
40 | - * @return array<string> An array of Entity URIs |
|
41 | - */ |
|
42 | - public static function get_active_entities( $term_id ) { |
|
43 | - |
|
44 | - // retrieve jsonld data. |
|
45 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
46 | - |
|
47 | - $entity_data_list = $entity->get_jsonld_data(); |
|
48 | - |
|
49 | - $active_entity_ids = array(); |
|
50 | - |
|
51 | - foreach ( $entity_data_list as $item ) { |
|
52 | - $sameas = $item['sameAs']; |
|
53 | - if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
54 | - // The entity id is stored on last position, so |
|
55 | - // we get the entity id from there. |
|
56 | - $active_entity_ids[] = array_pop( $sameas ); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - return $active_entity_ids; |
|
61 | - } |
|
15 | + /** |
|
16 | + * @param $term_id int Term id. |
|
17 | + * @param $entities array An array of entities |
|
18 | + * |
|
19 | + * @return array<array> An Array of entities with isActive filter. |
|
20 | + */ |
|
21 | + public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
22 | + |
|
23 | + $active_entities = self::get_active_entities( $term_id ); |
|
24 | + |
|
25 | + if ( ! is_array( $entities ) ) { |
|
26 | + return $entities; |
|
27 | + } |
|
28 | + |
|
29 | + foreach ( $entities as &$entity ) { |
|
30 | + $entity_id = $entity['entityId']; |
|
31 | + $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
32 | + } |
|
33 | + |
|
34 | + return $entities; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @param $term_id |
|
39 | + * |
|
40 | + * @return array<string> An array of Entity URIs |
|
41 | + */ |
|
42 | + public static function get_active_entities( $term_id ) { |
|
43 | + |
|
44 | + // retrieve jsonld data. |
|
45 | + $entity = Entity_List_Factory::get_instance( $term_id ); |
|
46 | + |
|
47 | + $entity_data_list = $entity->get_jsonld_data(); |
|
48 | + |
|
49 | + $active_entity_ids = array(); |
|
50 | + |
|
51 | + foreach ( $entity_data_list as $item ) { |
|
52 | + $sameas = $item['sameAs']; |
|
53 | + if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
54 | + // The entity id is stored on last position, so |
|
55 | + // we get the entity id from there. |
|
56 | + $active_entity_ids[] = array_pop( $sameas ); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + return $active_entity_ids; |
|
61 | + } |
|
62 | 62 | |
63 | 63 | } |
@@ -18,17 +18,17 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @return array<array> An Array of entities with isActive filter. |
20 | 20 | */ |
21 | - public static function mark_is_active_for_entities( $term_id, $entities ) { |
|
21 | + public static function mark_is_active_for_entities($term_id, $entities) { |
|
22 | 22 | |
23 | - $active_entities = self::get_active_entities( $term_id ); |
|
23 | + $active_entities = self::get_active_entities($term_id); |
|
24 | 24 | |
25 | - if ( ! is_array( $entities ) ) { |
|
25 | + if ( ! is_array($entities)) { |
|
26 | 26 | return $entities; |
27 | 27 | } |
28 | 28 | |
29 | - foreach ( $entities as &$entity ) { |
|
29 | + foreach ($entities as &$entity) { |
|
30 | 30 | $entity_id = $entity['entityId']; |
31 | - $entity['isActive'] = in_array( $entity_id, $active_entities, true ); |
|
31 | + $entity['isActive'] = in_array($entity_id, $active_entities, true); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | return $entities; |
@@ -39,21 +39,21 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return array<string> An array of Entity URIs |
41 | 41 | */ |
42 | - public static function get_active_entities( $term_id ) { |
|
42 | + public static function get_active_entities($term_id) { |
|
43 | 43 | |
44 | 44 | // retrieve jsonld data. |
45 | - $entity = Entity_List_Factory::get_instance( $term_id ); |
|
45 | + $entity = Entity_List_Factory::get_instance($term_id); |
|
46 | 46 | |
47 | 47 | $entity_data_list = $entity->get_jsonld_data(); |
48 | 48 | |
49 | 49 | $active_entity_ids = array(); |
50 | 50 | |
51 | - foreach ( $entity_data_list as $item ) { |
|
51 | + foreach ($entity_data_list as $item) { |
|
52 | 52 | $sameas = $item['sameAs']; |
53 | - if ( is_array( $sameas ) && count( $sameas ) > 0 ) { |
|
53 | + if (is_array($sameas) && count($sameas) > 0) { |
|
54 | 54 | // The entity id is stored on last position, so |
55 | 55 | // we get the entity id from there. |
56 | - $active_entity_ids[] = array_pop( $sameas ); |
|
56 | + $active_entity_ids[] = array_pop($sameas); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 |
@@ -7,8 +7,8 @@ |
||
7 | 7 | */ |
8 | 8 | class Entity_List_Factory { |
9 | 9 | |
10 | - public static function get_instance( $term_id ) { |
|
11 | - return new Default_Entity_List( $term_id, new Legacy_Entity_List( $term_id ) ); |
|
12 | - } |
|
10 | + public static function get_instance( $term_id ) { |
|
11 | + return new Default_Entity_List( $term_id, new Legacy_Entity_List( $term_id ) ); |
|
12 | + } |
|
13 | 13 | |
14 | 14 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | */ |
8 | 8 | class Entity_List_Factory { |
9 | 9 | |
10 | - public static function get_instance( $term_id ) { |
|
11 | - return new Default_Entity_List( $term_id, new Legacy_Entity_List( $term_id ) ); |
|
10 | + public static function get_instance($term_id) { |
|
11 | + return new Default_Entity_List($term_id, new Legacy_Entity_List($term_id)); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | } |
@@ -13,52 +13,52 @@ |
||
13 | 13 | */ |
14 | 14 | class Legacy_Entity_List extends Entity_List { |
15 | 15 | |
16 | - /** |
|
17 | - * This function is called when default entity doesnt find any data. |
|
18 | - * |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public function get_jsonld_data() { |
|
22 | - $tag = get_term( $this->term_id ); |
|
23 | - $type = get_term_meta( $tag->term_id, Entity_Rest_Endpoint::TYPE_META_KEY, true ); |
|
24 | - if ( '' === $type ) { |
|
25 | - return array(); |
|
26 | - } |
|
16 | + /** |
|
17 | + * This function is called when default entity doesnt find any data. |
|
18 | + * |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public function get_jsonld_data() { |
|
22 | + $tag = get_term( $this->term_id ); |
|
23 | + $type = get_term_meta( $tag->term_id, Entity_Rest_Endpoint::TYPE_META_KEY, true ); |
|
24 | + if ( '' === $type ) { |
|
25 | + return array(); |
|
26 | + } |
|
27 | 27 | |
28 | - return array( |
|
29 | - array( |
|
30 | - '@id' => get_term_link( $tag->term_id ) . '#id', |
|
31 | - '@type' => $type, |
|
32 | - 'name' => $tag->name, |
|
33 | - 'description' => ! empty( $tag->description ) ? $tag->description : get_term_meta( $tag->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY, true ), |
|
34 | - 'sameAs' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ), |
|
35 | - 'alternateName' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ), |
|
36 | - ), |
|
37 | - ); |
|
38 | - } |
|
28 | + return array( |
|
29 | + array( |
|
30 | + '@id' => get_term_link( $tag->term_id ) . '#id', |
|
31 | + '@type' => $type, |
|
32 | + 'name' => $tag->name, |
|
33 | + 'description' => ! empty( $tag->description ) ? $tag->description : get_term_meta( $tag->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY, true ), |
|
34 | + 'sameAs' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ), |
|
35 | + 'alternateName' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ), |
|
36 | + ), |
|
37 | + ); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * This function is never used. |
|
42 | - * |
|
43 | - * @param \WP_REST_Request $request |
|
44 | - * |
|
45 | - * @return bool |
|
46 | - */ |
|
47 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
48 | - public function save_jsonld_data( $request ) { |
|
49 | - return false; |
|
50 | - } |
|
40 | + /** |
|
41 | + * This function is never used. |
|
42 | + * |
|
43 | + * @param \WP_REST_Request $request |
|
44 | + * |
|
45 | + * @return bool |
|
46 | + */ |
|
47 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
48 | + public function save_jsonld_data( $request ) { |
|
49 | + return false; |
|
50 | + } |
|
51 | 51 | |
52 | - public function clear_data() { |
|
53 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ); |
|
54 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ); |
|
55 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY ); |
|
56 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::TYPE_META_KEY ); |
|
57 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::EXTERNAL_ENTITY_META_KEY ); |
|
58 | - } |
|
52 | + public function clear_data() { |
|
53 | + delete_term_meta( $this->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ); |
|
54 | + delete_term_meta( $this->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ); |
|
55 | + delete_term_meta( $this->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY ); |
|
56 | + delete_term_meta( $this->term_id, Entity_Rest_Endpoint::TYPE_META_KEY ); |
|
57 | + delete_term_meta( $this->term_id, Entity_Rest_Endpoint::EXTERNAL_ENTITY_META_KEY ); |
|
58 | + } |
|
59 | 59 | |
60 | - // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
61 | - public function remove_entity_by_id( $entity_id ) { |
|
62 | - $this->clear_data(); |
|
63 | - } |
|
60 | + // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
61 | + public function remove_entity_by_id( $entity_id ) { |
|
62 | + $this->clear_data(); |
|
63 | + } |
|
64 | 64 | } |
@@ -19,20 +19,20 @@ discard block |
||
19 | 19 | * @return array |
20 | 20 | */ |
21 | 21 | public function get_jsonld_data() { |
22 | - $tag = get_term( $this->term_id ); |
|
23 | - $type = get_term_meta( $tag->term_id, Entity_Rest_Endpoint::TYPE_META_KEY, true ); |
|
24 | - if ( '' === $type ) { |
|
22 | + $tag = get_term($this->term_id); |
|
23 | + $type = get_term_meta($tag->term_id, Entity_Rest_Endpoint::TYPE_META_KEY, true); |
|
24 | + if ('' === $type) { |
|
25 | 25 | return array(); |
26 | 26 | } |
27 | 27 | |
28 | 28 | return array( |
29 | 29 | array( |
30 | - '@id' => get_term_link( $tag->term_id ) . '#id', |
|
30 | + '@id' => get_term_link($tag->term_id).'#id', |
|
31 | 31 | '@type' => $type, |
32 | 32 | 'name' => $tag->name, |
33 | - 'description' => ! empty( $tag->description ) ? $tag->description : get_term_meta( $tag->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY, true ), |
|
34 | - 'sameAs' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ), |
|
35 | - 'alternateName' => get_term_meta( $tag->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ), |
|
33 | + 'description' => ! empty($tag->description) ? $tag->description : get_term_meta($tag->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY, true), |
|
34 | + 'sameAs' => get_term_meta($tag->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY), |
|
35 | + 'alternateName' => get_term_meta($tag->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY), |
|
36 | 36 | ), |
37 | 37 | ); |
38 | 38 | } |
@@ -45,20 +45,20 @@ discard block |
||
45 | 45 | * @return bool |
46 | 46 | */ |
47 | 47 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
48 | - public function save_jsonld_data( $request ) { |
|
48 | + public function save_jsonld_data($request) { |
|
49 | 49 | return false; |
50 | 50 | } |
51 | 51 | |
52 | 52 | public function clear_data() { |
53 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY ); |
|
54 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY ); |
|
55 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY ); |
|
56 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::TYPE_META_KEY ); |
|
57 | - delete_term_meta( $this->term_id, Entity_Rest_Endpoint::EXTERNAL_ENTITY_META_KEY ); |
|
53 | + delete_term_meta($this->term_id, Entity_Rest_Endpoint::SAME_AS_META_KEY); |
|
54 | + delete_term_meta($this->term_id, Entity_Rest_Endpoint::ALTERNATIVE_LABEL_META_KEY); |
|
55 | + delete_term_meta($this->term_id, Entity_Rest_Endpoint::DESCRIPTION_META_KEY); |
|
56 | + delete_term_meta($this->term_id, Entity_Rest_Endpoint::TYPE_META_KEY); |
|
57 | + delete_term_meta($this->term_id, Entity_Rest_Endpoint::EXTERNAL_ENTITY_META_KEY); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
61 | - public function remove_entity_by_id( $entity_id ) { |
|
61 | + public function remove_entity_by_id($entity_id) { |
|
62 | 62 | $this->clear_data(); |
63 | 63 | } |
64 | 64 | } |
@@ -10,165 +10,165 @@ |
||
10 | 10 | */ |
11 | 11 | class Default_Entity_List extends Entity_List { |
12 | 12 | |
13 | - const META_KEY = '_wl_vocabulary_entity_match_for_term'; |
|
14 | - |
|
15 | - /** |
|
16 | - * @var Legacy_Entity_List |
|
17 | - */ |
|
18 | - private $legacy_entity; |
|
19 | - |
|
20 | - /** |
|
21 | - * Default_Entity constructor. |
|
22 | - * |
|
23 | - * @param $term_id int |
|
24 | - * @param $legacy_entity Legacy_Entity_List |
|
25 | - */ |
|
26 | - public function __construct( $term_id, $legacy_entity ) { |
|
27 | - parent::__construct( $term_id ); |
|
28 | - $this->legacy_entity = $legacy_entity; |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param $values |
|
33 | - * |
|
34 | - * @return array |
|
35 | - */ |
|
36 | - private static function extract_jsonld_values( $values ) { |
|
37 | - return array_map( |
|
38 | - function ( $value ) { |
|
39 | - if ( ! is_array( $value ) || ! array_key_exists( '@value', $value ) ) { |
|
40 | - return $value; |
|
41 | - } |
|
42 | - |
|
43 | - return $value['@value']; |
|
44 | - }, |
|
45 | - $values |
|
46 | - ); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Check if the key exists and value is array. |
|
51 | - * |
|
52 | - * @param $entity_data |
|
53 | - * |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - private static function is_value_array( $key, $entity_data ) { |
|
57 | - return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
58 | - } |
|
59 | - |
|
60 | - public function get_jsonld_data() { |
|
61 | - $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
62 | - if ( is_array( $default_data ) && $default_data ) { |
|
63 | - return $default_data; |
|
64 | - } |
|
65 | - |
|
66 | - // Use legacy entity if the data doesnt exist on that key. |
|
67 | - return $this->legacy_entity->get_jsonld_data(); |
|
68 | - } |
|
69 | - |
|
70 | - public function save_jsonld_data( $entity_data ) { |
|
71 | - |
|
72 | - $entity_id = $entity_data['@id']; |
|
73 | - |
|
74 | - if ( $entity_id ) { |
|
75 | - $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
76 | - } |
|
77 | - |
|
78 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
79 | - |
|
80 | - $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
81 | - |
|
82 | - $this->clear_and_save_list( $entity_list ); |
|
83 | - |
|
84 | - } |
|
85 | - |
|
86 | - public function clear_data() { |
|
87 | - delete_term_meta( $this->term_id, self::META_KEY ); |
|
88 | - } |
|
89 | - |
|
90 | - public function remove_entity_by_id( $entity_id ) { |
|
91 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
92 | - foreach ( $entity_list as $key => $entity ) { |
|
93 | - $same_as = $entity['sameAs']; |
|
94 | - if ( in_array( $entity_id, $same_as, true ) ) { |
|
95 | - // since entity ids are unique, we break after finding the first instance. |
|
96 | - unset( $entity_list[ $key ] ); |
|
97 | - break; |
|
98 | - } |
|
99 | - } |
|
100 | - $this->clear_and_save_list( $entity_list ); |
|
101 | - |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @param $entity_list |
|
106 | - */ |
|
107 | - private function save_entity_list( $entity_list ) { |
|
108 | - foreach ( $entity_list as $single_entity ) { |
|
109 | - add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @param $entity_list |
|
115 | - */ |
|
116 | - private function clear_and_save_list( $entity_list ) { |
|
117 | - // Clear all data and add the new one. |
|
118 | - $this->clear_data(); |
|
119 | - $this->save_entity_list( $entity_list ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * For now support only these properties. |
|
124 | - * |
|
125 | - * @param $entity_data |
|
126 | - * |
|
127 | - * @return array |
|
128 | - */ |
|
129 | - private function filter_entity_data( $entity_data ) { |
|
130 | - $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
131 | - $data = array(); |
|
132 | - foreach ( $entity_data as $key => $value ) { |
|
133 | - if ( in_array( $key, $allowed_keys, true ) ) { |
|
134 | - $data[ $key ] = $value; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return $data; |
|
139 | - } |
|
140 | - |
|
141 | - public static function compact_jsonld( $entity_data ) { |
|
142 | - |
|
143 | - if ( self::is_value_array( '@type', $entity_data ) ) { |
|
144 | - $entity_data['@type'] = array_map( |
|
145 | - function ( $type ) { |
|
146 | - return str_replace( 'http://schema.org/', '', $type ); |
|
147 | - }, |
|
148 | - $entity_data['@type'] |
|
149 | - ); |
|
150 | - } |
|
151 | - |
|
152 | - if ( self::is_value_array( 'description', $entity_data ) ) { |
|
153 | - $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
154 | - } |
|
155 | - |
|
156 | - if ( self::is_value_array( 'name', $entity_data ) ) { |
|
157 | - $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
158 | - } |
|
159 | - |
|
160 | - if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
161 | - $entity_data['sameAs'] = array_map( |
|
162 | - function ( $sameas ) { |
|
163 | - if ( ! is_array( $sameas ) || ! array_key_exists( '@id', $sameas ) ) { |
|
164 | - return $sameas; |
|
165 | - } |
|
166 | - return $sameas['@id']; |
|
167 | - }, |
|
168 | - $entity_data['sameAs'] |
|
169 | - ); |
|
170 | - } |
|
171 | - |
|
172 | - return $entity_data; |
|
173 | - } |
|
13 | + const META_KEY = '_wl_vocabulary_entity_match_for_term'; |
|
14 | + |
|
15 | + /** |
|
16 | + * @var Legacy_Entity_List |
|
17 | + */ |
|
18 | + private $legacy_entity; |
|
19 | + |
|
20 | + /** |
|
21 | + * Default_Entity constructor. |
|
22 | + * |
|
23 | + * @param $term_id int |
|
24 | + * @param $legacy_entity Legacy_Entity_List |
|
25 | + */ |
|
26 | + public function __construct( $term_id, $legacy_entity ) { |
|
27 | + parent::__construct( $term_id ); |
|
28 | + $this->legacy_entity = $legacy_entity; |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param $values |
|
33 | + * |
|
34 | + * @return array |
|
35 | + */ |
|
36 | + private static function extract_jsonld_values( $values ) { |
|
37 | + return array_map( |
|
38 | + function ( $value ) { |
|
39 | + if ( ! is_array( $value ) || ! array_key_exists( '@value', $value ) ) { |
|
40 | + return $value; |
|
41 | + } |
|
42 | + |
|
43 | + return $value['@value']; |
|
44 | + }, |
|
45 | + $values |
|
46 | + ); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Check if the key exists and value is array. |
|
51 | + * |
|
52 | + * @param $entity_data |
|
53 | + * |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + private static function is_value_array( $key, $entity_data ) { |
|
57 | + return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
58 | + } |
|
59 | + |
|
60 | + public function get_jsonld_data() { |
|
61 | + $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
62 | + if ( is_array( $default_data ) && $default_data ) { |
|
63 | + return $default_data; |
|
64 | + } |
|
65 | + |
|
66 | + // Use legacy entity if the data doesnt exist on that key. |
|
67 | + return $this->legacy_entity->get_jsonld_data(); |
|
68 | + } |
|
69 | + |
|
70 | + public function save_jsonld_data( $entity_data ) { |
|
71 | + |
|
72 | + $entity_id = $entity_data['@id']; |
|
73 | + |
|
74 | + if ( $entity_id ) { |
|
75 | + $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
76 | + } |
|
77 | + |
|
78 | + $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
79 | + |
|
80 | + $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
81 | + |
|
82 | + $this->clear_and_save_list( $entity_list ); |
|
83 | + |
|
84 | + } |
|
85 | + |
|
86 | + public function clear_data() { |
|
87 | + delete_term_meta( $this->term_id, self::META_KEY ); |
|
88 | + } |
|
89 | + |
|
90 | + public function remove_entity_by_id( $entity_id ) { |
|
91 | + $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
92 | + foreach ( $entity_list as $key => $entity ) { |
|
93 | + $same_as = $entity['sameAs']; |
|
94 | + if ( in_array( $entity_id, $same_as, true ) ) { |
|
95 | + // since entity ids are unique, we break after finding the first instance. |
|
96 | + unset( $entity_list[ $key ] ); |
|
97 | + break; |
|
98 | + } |
|
99 | + } |
|
100 | + $this->clear_and_save_list( $entity_list ); |
|
101 | + |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @param $entity_list |
|
106 | + */ |
|
107 | + private function save_entity_list( $entity_list ) { |
|
108 | + foreach ( $entity_list as $single_entity ) { |
|
109 | + add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @param $entity_list |
|
115 | + */ |
|
116 | + private function clear_and_save_list( $entity_list ) { |
|
117 | + // Clear all data and add the new one. |
|
118 | + $this->clear_data(); |
|
119 | + $this->save_entity_list( $entity_list ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * For now support only these properties. |
|
124 | + * |
|
125 | + * @param $entity_data |
|
126 | + * |
|
127 | + * @return array |
|
128 | + */ |
|
129 | + private function filter_entity_data( $entity_data ) { |
|
130 | + $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
131 | + $data = array(); |
|
132 | + foreach ( $entity_data as $key => $value ) { |
|
133 | + if ( in_array( $key, $allowed_keys, true ) ) { |
|
134 | + $data[ $key ] = $value; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return $data; |
|
139 | + } |
|
140 | + |
|
141 | + public static function compact_jsonld( $entity_data ) { |
|
142 | + |
|
143 | + if ( self::is_value_array( '@type', $entity_data ) ) { |
|
144 | + $entity_data['@type'] = array_map( |
|
145 | + function ( $type ) { |
|
146 | + return str_replace( 'http://schema.org/', '', $type ); |
|
147 | + }, |
|
148 | + $entity_data['@type'] |
|
149 | + ); |
|
150 | + } |
|
151 | + |
|
152 | + if ( self::is_value_array( 'description', $entity_data ) ) { |
|
153 | + $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
154 | + } |
|
155 | + |
|
156 | + if ( self::is_value_array( 'name', $entity_data ) ) { |
|
157 | + $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
158 | + } |
|
159 | + |
|
160 | + if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
161 | + $entity_data['sameAs'] = array_map( |
|
162 | + function ( $sameas ) { |
|
163 | + if ( ! is_array( $sameas ) || ! array_key_exists( '@id', $sameas ) ) { |
|
164 | + return $sameas; |
|
165 | + } |
|
166 | + return $sameas['@id']; |
|
167 | + }, |
|
168 | + $entity_data['sameAs'] |
|
169 | + ); |
|
170 | + } |
|
171 | + |
|
172 | + return $entity_data; |
|
173 | + } |
|
174 | 174 | } |
@@ -23,8 +23,8 @@ discard block |
||
23 | 23 | * @param $term_id int |
24 | 24 | * @param $legacy_entity Legacy_Entity_List |
25 | 25 | */ |
26 | - public function __construct( $term_id, $legacy_entity ) { |
|
27 | - parent::__construct( $term_id ); |
|
26 | + public function __construct($term_id, $legacy_entity) { |
|
27 | + parent::__construct($term_id); |
|
28 | 28 | $this->legacy_entity = $legacy_entity; |
29 | 29 | } |
30 | 30 | |
@@ -33,10 +33,10 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return array |
35 | 35 | */ |
36 | - private static function extract_jsonld_values( $values ) { |
|
36 | + private static function extract_jsonld_values($values) { |
|
37 | 37 | return array_map( |
38 | - function ( $value ) { |
|
39 | - if ( ! is_array( $value ) || ! array_key_exists( '@value', $value ) ) { |
|
38 | + function($value) { |
|
39 | + if ( ! is_array($value) || ! array_key_exists('@value', $value)) { |
|
40 | 40 | return $value; |
41 | 41 | } |
42 | 42 | |
@@ -53,13 +53,13 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @return bool |
55 | 55 | */ |
56 | - private static function is_value_array( $key, $entity_data ) { |
|
57 | - return array_key_exists( $key, $entity_data ) && is_array( $entity_data[ $key ] ); |
|
56 | + private static function is_value_array($key, $entity_data) { |
|
57 | + return array_key_exists($key, $entity_data) && is_array($entity_data[$key]); |
|
58 | 58 | } |
59 | 59 | |
60 | 60 | public function get_jsonld_data() { |
61 | - $default_data = get_term_meta( $this->term_id, self::META_KEY ); |
|
62 | - if ( is_array( $default_data ) && $default_data ) { |
|
61 | + $default_data = get_term_meta($this->term_id, self::META_KEY); |
|
62 | + if (is_array($default_data) && $default_data) { |
|
63 | 63 | return $default_data; |
64 | 64 | } |
65 | 65 | |
@@ -67,56 +67,56 @@ discard block |
||
67 | 67 | return $this->legacy_entity->get_jsonld_data(); |
68 | 68 | } |
69 | 69 | |
70 | - public function save_jsonld_data( $entity_data ) { |
|
70 | + public function save_jsonld_data($entity_data) { |
|
71 | 71 | |
72 | 72 | $entity_id = $entity_data['@id']; |
73 | 73 | |
74 | - if ( $entity_id ) { |
|
75 | - $entity_data['sameAs'] = array_merge( $entity_data['sameAs'], array( $entity_id ) ); |
|
74 | + if ($entity_id) { |
|
75 | + $entity_data['sameAs'] = array_merge($entity_data['sameAs'], array($entity_id)); |
|
76 | 76 | } |
77 | 77 | |
78 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
78 | + $entity_list = get_term_meta($this->term_id, self::META_KEY); |
|
79 | 79 | |
80 | - $entity_list[] = $this->compact_jsonld( $this->filter_entity_data( $entity_data ) ); |
|
80 | + $entity_list[] = $this->compact_jsonld($this->filter_entity_data($entity_data)); |
|
81 | 81 | |
82 | - $this->clear_and_save_list( $entity_list ); |
|
82 | + $this->clear_and_save_list($entity_list); |
|
83 | 83 | |
84 | 84 | } |
85 | 85 | |
86 | 86 | public function clear_data() { |
87 | - delete_term_meta( $this->term_id, self::META_KEY ); |
|
87 | + delete_term_meta($this->term_id, self::META_KEY); |
|
88 | 88 | } |
89 | 89 | |
90 | - public function remove_entity_by_id( $entity_id ) { |
|
91 | - $entity_list = get_term_meta( $this->term_id, self::META_KEY ); |
|
92 | - foreach ( $entity_list as $key => $entity ) { |
|
90 | + public function remove_entity_by_id($entity_id) { |
|
91 | + $entity_list = get_term_meta($this->term_id, self::META_KEY); |
|
92 | + foreach ($entity_list as $key => $entity) { |
|
93 | 93 | $same_as = $entity['sameAs']; |
94 | - if ( in_array( $entity_id, $same_as, true ) ) { |
|
94 | + if (in_array($entity_id, $same_as, true)) { |
|
95 | 95 | // since entity ids are unique, we break after finding the first instance. |
96 | - unset( $entity_list[ $key ] ); |
|
96 | + unset($entity_list[$key]); |
|
97 | 97 | break; |
98 | 98 | } |
99 | 99 | } |
100 | - $this->clear_and_save_list( $entity_list ); |
|
100 | + $this->clear_and_save_list($entity_list); |
|
101 | 101 | |
102 | 102 | } |
103 | 103 | |
104 | 104 | /** |
105 | 105 | * @param $entity_list |
106 | 106 | */ |
107 | - private function save_entity_list( $entity_list ) { |
|
108 | - foreach ( $entity_list as $single_entity ) { |
|
109 | - add_term_meta( $this->term_id, self::META_KEY, $single_entity ); |
|
107 | + private function save_entity_list($entity_list) { |
|
108 | + foreach ($entity_list as $single_entity) { |
|
109 | + add_term_meta($this->term_id, self::META_KEY, $single_entity); |
|
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
113 | 113 | /** |
114 | 114 | * @param $entity_list |
115 | 115 | */ |
116 | - private function clear_and_save_list( $entity_list ) { |
|
116 | + private function clear_and_save_list($entity_list) { |
|
117 | 117 | // Clear all data and add the new one. |
118 | 118 | $this->clear_data(); |
119 | - $this->save_entity_list( $entity_list ); |
|
119 | + $this->save_entity_list($entity_list); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -126,41 +126,41 @@ discard block |
||
126 | 126 | * |
127 | 127 | * @return array |
128 | 128 | */ |
129 | - private function filter_entity_data( $entity_data ) { |
|
130 | - $allowed_keys = array( '@id', 'description', 'sameAs', '@type', 'name' ); |
|
129 | + private function filter_entity_data($entity_data) { |
|
130 | + $allowed_keys = array('@id', 'description', 'sameAs', '@type', 'name'); |
|
131 | 131 | $data = array(); |
132 | - foreach ( $entity_data as $key => $value ) { |
|
133 | - if ( in_array( $key, $allowed_keys, true ) ) { |
|
134 | - $data[ $key ] = $value; |
|
132 | + foreach ($entity_data as $key => $value) { |
|
133 | + if (in_array($key, $allowed_keys, true)) { |
|
134 | + $data[$key] = $value; |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | 138 | return $data; |
139 | 139 | } |
140 | 140 | |
141 | - public static function compact_jsonld( $entity_data ) { |
|
141 | + public static function compact_jsonld($entity_data) { |
|
142 | 142 | |
143 | - if ( self::is_value_array( '@type', $entity_data ) ) { |
|
143 | + if (self::is_value_array('@type', $entity_data)) { |
|
144 | 144 | $entity_data['@type'] = array_map( |
145 | - function ( $type ) { |
|
146 | - return str_replace( 'http://schema.org/', '', $type ); |
|
145 | + function($type) { |
|
146 | + return str_replace('http://schema.org/', '', $type); |
|
147 | 147 | }, |
148 | 148 | $entity_data['@type'] |
149 | 149 | ); |
150 | 150 | } |
151 | 151 | |
152 | - if ( self::is_value_array( 'description', $entity_data ) ) { |
|
153 | - $entity_data['description'] = self::extract_jsonld_values( $entity_data['description'] ); |
|
152 | + if (self::is_value_array('description', $entity_data)) { |
|
153 | + $entity_data['description'] = self::extract_jsonld_values($entity_data['description']); |
|
154 | 154 | } |
155 | 155 | |
156 | - if ( self::is_value_array( 'name', $entity_data ) ) { |
|
157 | - $entity_data['name'] = self::extract_jsonld_values( $entity_data['name'] ); |
|
156 | + if (self::is_value_array('name', $entity_data)) { |
|
157 | + $entity_data['name'] = self::extract_jsonld_values($entity_data['name']); |
|
158 | 158 | } |
159 | 159 | |
160 | - if ( self::is_value_array( 'sameAs', $entity_data ) ) { |
|
160 | + if (self::is_value_array('sameAs', $entity_data)) { |
|
161 | 161 | $entity_data['sameAs'] = array_map( |
162 | - function ( $sameas ) { |
|
163 | - if ( ! is_array( $sameas ) || ! array_key_exists( '@id', $sameas ) ) { |
|
162 | + function($sameas) { |
|
163 | + if ( ! is_array($sameas) || ! array_key_exists('@id', $sameas)) { |
|
164 | 164 | return $sameas; |
165 | 165 | } |
166 | 166 | return $sameas['@id']; |
@@ -7,40 +7,40 @@ |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | abstract class Entity_List { |
10 | - /** |
|
11 | - * @var int |
|
12 | - */ |
|
13 | - protected $term_id; |
|
10 | + /** |
|
11 | + * @var int |
|
12 | + */ |
|
13 | + protected $term_id; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Entity constructor. |
|
17 | - * |
|
18 | - * @param $term_id int |
|
19 | - */ |
|
20 | - public function __construct( $term_id ) { |
|
21 | - $this->term_id = $term_id; |
|
22 | - } |
|
15 | + /** |
|
16 | + * Entity constructor. |
|
17 | + * |
|
18 | + * @param $term_id int |
|
19 | + */ |
|
20 | + public function __construct( $term_id ) { |
|
21 | + $this->term_id = $term_id; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * Return a structure of jsonld data. |
|
26 | - * |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - abstract public function get_jsonld_data(); |
|
24 | + /** |
|
25 | + * Return a structure of jsonld data. |
|
26 | + * |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + abstract public function get_jsonld_data(); |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param $entity_data array |
|
33 | - * |
|
34 | - * @return bool |
|
35 | - */ |
|
36 | - abstract public function save_jsonld_data( $entity_data ); |
|
31 | + /** |
|
32 | + * @param $entity_data array |
|
33 | + * |
|
34 | + * @return bool |
|
35 | + */ |
|
36 | + abstract public function save_jsonld_data( $entity_data ); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Clear the data on the meta. |
|
40 | - * |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - abstract public function clear_data(); |
|
38 | + /** |
|
39 | + * Clear the data on the meta. |
|
40 | + * |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + abstract public function clear_data(); |
|
44 | 44 | |
45 | - abstract public function remove_entity_by_id( $entity_id ); |
|
45 | + abstract public function remove_entity_by_id( $entity_id ); |
|
46 | 46 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * |
18 | 18 | * @param $term_id int |
19 | 19 | */ |
20 | - public function __construct( $term_id ) { |
|
20 | + public function __construct($term_id) { |
|
21 | 21 | $this->term_id = $term_id; |
22 | 22 | } |
23 | 23 | |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return bool |
35 | 35 | */ |
36 | - abstract public function save_jsonld_data( $entity_data ); |
|
36 | + abstract public function save_jsonld_data($entity_data); |
|
37 | 37 | |
38 | 38 | /** |
39 | 39 | * Clear the data on the meta. |
@@ -42,5 +42,5 @@ discard block |
||
42 | 42 | */ |
43 | 43 | abstract public function clear_data(); |
44 | 44 | |
45 | - abstract public function remove_entity_by_id( $entity_id ); |
|
45 | + abstract public function remove_entity_by_id($entity_id); |
|
46 | 46 | } |