@@ -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 | } |
@@ -11,25 +11,25 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Cached_Term_Count_Manager { |
| 13 | 13 | |
| 14 | - public function connect_hook() { |
|
| 15 | - |
|
| 16 | - add_action( |
|
| 17 | - 'wordlift_vocabulary_analysis_complete_for_terms_batch', |
|
| 18 | - function () { |
|
| 19 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 20 | - } |
|
| 21 | - ); |
|
| 22 | - |
|
| 23 | - $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 24 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 25 | - add_action( |
|
| 26 | - "created_${taxonomy}", |
|
| 27 | - function () { |
|
| 28 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 29 | - } |
|
| 30 | - ); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - } |
|
| 14 | + public function connect_hook() { |
|
| 15 | + |
|
| 16 | + add_action( |
|
| 17 | + 'wordlift_vocabulary_analysis_complete_for_terms_batch', |
|
| 18 | + function () { |
|
| 19 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 20 | + } |
|
| 21 | + ); |
|
| 22 | + |
|
| 23 | + $taxonomies = Terms_Compat::get_public_taxonomies(); |
|
| 24 | + foreach ( $taxonomies as $taxonomy ) { |
|
| 25 | + add_action( |
|
| 26 | + "created_${taxonomy}", |
|
| 27 | + function () { |
|
| 28 | + delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 29 | + } |
|
| 30 | + ); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | 35 | } |
@@ -15,17 +15,17 @@ |
||
| 15 | 15 | |
| 16 | 16 | add_action( |
| 17 | 17 | 'wordlift_vocabulary_analysis_complete_for_terms_batch', |
| 18 | - function () { |
|
| 19 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 18 | + function() { |
|
| 19 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 20 | 20 | } |
| 21 | 21 | ); |
| 22 | 22 | |
| 23 | 23 | $taxonomies = Terms_Compat::get_public_taxonomies(); |
| 24 | - foreach ( $taxonomies as $taxonomy ) { |
|
| 24 | + foreach ($taxonomies as $taxonomy) { |
|
| 25 | 25 | add_action( |
| 26 | 26 | "created_${taxonomy}", |
| 27 | - function () { |
|
| 28 | - delete_transient( Cached_Term_Count::TRANSIENT_KEY ); |
|
| 27 | + function() { |
|
| 28 | + delete_transient(Cached_Term_Count::TRANSIENT_KEY); |
|
| 29 | 29 | } |
| 30 | 30 | ); |
| 31 | 31 | } |
@@ -14,33 +14,33 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Default_Term_Count implements Term_Count { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Return count of terms which have entities and also not matched by editor, currently |
|
| 19 | - * it is used for the menu icon badge. |
|
| 20 | - * |
|
| 21 | - * @return int |
|
| 22 | - */ |
|
| 23 | - public function get_term_count() { |
|
| 24 | - return count( |
|
| 25 | - Terms_Compat::get_terms( |
|
| 26 | - Terms_Compat::get_public_taxonomies(), |
|
| 27 | - array( |
|
| 28 | - 'hide_empty' => false, |
|
| 29 | - 'fields' => 'ids', |
|
| 30 | - 'meta_query' => array( |
|
| 31 | - array( |
|
| 32 | - 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 33 | - 'compare' => 'NOT EXISTS', |
|
| 34 | - ), |
|
| 35 | - array( |
|
| 36 | - 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 37 | - 'compare' => 'EXISTS', |
|
| 38 | - ), |
|
| 39 | - ), |
|
| 40 | - ) |
|
| 41 | - ) |
|
| 42 | - ); |
|
| 17 | + /** |
|
| 18 | + * Return count of terms which have entities and also not matched by editor, currently |
|
| 19 | + * it is used for the menu icon badge. |
|
| 20 | + * |
|
| 21 | + * @return int |
|
| 22 | + */ |
|
| 23 | + public function get_term_count() { |
|
| 24 | + return count( |
|
| 25 | + Terms_Compat::get_terms( |
|
| 26 | + Terms_Compat::get_public_taxonomies(), |
|
| 27 | + array( |
|
| 28 | + 'hide_empty' => false, |
|
| 29 | + 'fields' => 'ids', |
|
| 30 | + 'meta_query' => array( |
|
| 31 | + array( |
|
| 32 | + 'key' => Entity_Rest_Endpoint::IGNORE_TAG_FROM_LISTING, |
|
| 33 | + 'compare' => 'NOT EXISTS', |
|
| 34 | + ), |
|
| 35 | + array( |
|
| 36 | + 'key' => Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM, |
|
| 37 | + 'compare' => 'EXISTS', |
|
| 38 | + ), |
|
| 39 | + ), |
|
| 40 | + ) |
|
| 41 | + ) |
|
| 42 | + ); |
|
| 43 | 43 | |
| 44 | - } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | } |
@@ -10,14 +10,14 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Term_Count_Factory { |
| 12 | 12 | |
| 13 | - const CACHED_TERM_COUNT = 'cached_term_count'; |
|
| 13 | + const CACHED_TERM_COUNT = 'cached_term_count'; |
|
| 14 | 14 | |
| 15 | - public static function get_instance( $type ) { |
|
| 16 | - if ( self::CACHED_TERM_COUNT === $type ) { |
|
| 17 | - return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 18 | - } |
|
| 15 | + public static function get_instance( $type ) { |
|
| 16 | + if ( self::CACHED_TERM_COUNT === $type ) { |
|
| 17 | + return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - return null; |
|
| 21 | - } |
|
| 20 | + return null; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | 23 | } |
@@ -12,9 +12,9 @@ |
||
| 12 | 12 | |
| 13 | 13 | const CACHED_TERM_COUNT = 'cached_term_count'; |
| 14 | 14 | |
| 15 | - public static function get_instance( $type ) { |
|
| 16 | - if ( self::CACHED_TERM_COUNT === $type ) { |
|
| 17 | - return new Cached_Term_Count( new Default_Term_Count() ); |
|
| 15 | + public static function get_instance($type) { |
|
| 16 | + if (self::CACHED_TERM_COUNT === $type) { |
|
| 17 | + return new Cached_Term_Count(new Default_Term_Count()); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | return null; |