Skip to content

Commit

Permalink
Detect classification scope from current entity type. See #297
Browse files Browse the repository at this point in the history
  • Loading branch information
mcolacino committed Apr 18, 2016
1 parent 71f15f8 commit f9b0942
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
32 changes: 30 additions & 2 deletions src/includes/class-wordlift-entity-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,35 @@ public function is_entity( $post_id ) {
return ( self::TYPE_NAME === get_post_type( $post_id ) );
}

/**
* Get the proper classification scope for a given entity post
*
* @since 3.5.0
*
* @param integer $post_id An entity post id.
*
* @return string Returns an uri.
*/
public function get_classification_scope_for( $post_id ) {

if ( FALSE === $this->is_entity( $post_id ) ) {
return null;
}
// Retrieve the entity type
$entity_type_arr = wl_entity_type_taxonomy_get_type( $post_id );
$entity_type = str_replace( 'wl-', '', $entity_type_arr[ 'css_class' ] );
// Retrieve classification boxes configuration
$classification_boxes = unserialize( WL_CORE_POST_CLASSIFICATION_BOXES );
foreach ( $classification_boxes as $cb ) {
if ( in_array( $entity_type , $cb[ 'registeredTypes' ] ) ) {
return $cb[ 'id' ];
}
}
// or null
return null;

}

/**
* Build an entity uri for a given title
* The uri is composed using a given post_type and a title
Expand Down Expand Up @@ -278,8 +307,7 @@ public function build_uri( $title, $post_type, $schema_type = NULL, $increment_d

global $wpdb;
// Check if the candidated uri already is used
// TODO Get post ids instead of count()
$stmt = $wpdb->prepare(
$stmt = $wpdb->prepare(
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s LIMIT 1",
WL_ENTITY_URL_META_NAME,
$new_entity_uri
Expand Down
39 changes: 7 additions & 32 deletions src/modules/linked_data/wordlift_linked_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ function wl_linked_data_save_post_and_related_entities( $post_id ) {

// Store mapping between tmp new entities uris and real new entities uri
$entities_uri_mapping = array();
// Store classification box mapping
$entities_predicates_mapping = null;


// Save the entities coming with POST data.
if ( isset( $_POST['wl_entities'] ) && isset( $_POST['wl_boxes'] ) ) {

Expand Down Expand Up @@ -109,16 +107,6 @@ function wl_linked_data_save_post_and_related_entities( $post_id ) {
}

}

// Populate the $entities_predicates_mapping
foreach ( $boxes_via_post as $predicate => $entity_uris ) {
foreach ( $entity_uris as $entity_uri ) {

$uri = $entities_uri_mapping[ stripslashes( $entity_uri ) ];
wl_write_log("Going to map predicate $predicate to uri $uri");
$entities_predicates_mapping[ $uri ][] = $predicate;
}
}

}

Expand All @@ -144,27 +132,14 @@ function wl_linked_data_save_post_and_related_entities( $post_id ) {
// Save relation instances
foreach ( array_unique( $disambiguated_entities ) as $referenced_entity_id ) {

if ( $entities_predicates_mapping ) {
// Retrieve the entity uri
$referenced_entity_uri = wl_get_entity_uri( $referenced_entity_id );

// Retrieve predicates for the current uri
if ( isset( $entities_predicates_mapping[ $referenced_entity_uri ] ) ) {
foreach ( $entities_predicates_mapping[ $referenced_entity_uri ] as $predicate ) {
wl_core_add_relation_instance( $post_id, $predicate, $referenced_entity_id );
}
} else {
wl_write_log( "Entity uri $referenced_entity_uri missing in the mapping" );
wl_write_log( $entities_predicates_mapping );
}

} else {
// Just for unit tests
wl_core_add_relation_instance( $post_id, 'what', $referenced_entity_id );
}
wl_core_add_relation_instance(
$post_id,
Wordlift_Entity_Service::get_instance()->get_classification_scope_for( $referenced_entity_id ),
$referenced_entity_id
);

}

// Save post metadata if available
$metadata_via_post = ( isset( $_POST['wl_metadata'] ) ) ?
$_POST['wl_metadata'] : array();
Expand Down
1 change: 1 addition & 0 deletions src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ More [Frequently Asked Questions](http://docs.wordlift.it/en/latest/faq.html) ca

== Changelog ==
= 3.5.0 (???) =
* Enhancement: [#297](https://github.com/insideout10/wordlift-plugin/issues/297): Detect classification scope from current entity type.
* Enhancement: [#284](https://github.com/insideout10/wordlift-plugin/issues/284): Disambiguation widget UI refactoring.
* Enhancement: [#289](https://github.com/insideout10/wordlift-plugin/issues/289): Introduced html static templates for angularjs layer components.
* Enhancement: [#288](https://github.com/insideout10/wordlift-plugin/issues/288): Removed selected entities tags from disambiguation widget.
Expand Down
7 changes: 3 additions & 4 deletions tests/test-entity-creation-via-post-creation.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,12 @@ function testEntityIsCreatedAndLinkedWithMultiplePredicatesToThePost() {
$related_entity_ids = wl_core_get_related_entity_ids( $post_id, array( "predicate" => "what" ) );
$this->assertCount( 1, $related_entity_ids );
$this->assertContains( $entity->ID, $related_entity_ids );
// And it should be related to the post as what predicate
// But it should NOT be related to the post as what predicate
$related_entity_ids = wl_core_get_related_entity_ids( $post_id, array( "predicate" => "who" ) );
$this->assertCount( 1, $related_entity_ids );
$this->assertContains( $entity->ID, $related_entity_ids );
$this->assertCount( 0, $related_entity_ids );
// Ensure there are no other relation instances
$relation_instances = wl_tests_get_relation_instances_for( $post_id );
$this->assertCount( 2, $relation_instances );
$this->assertCount( 1, $relation_instances );

}

Expand Down
30 changes: 30 additions & 0 deletions tests/test-entity-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,34 @@ function test_build_uri_override_when_there_is_already_an_entity_with_the_same_l
);

}

/**
* Test the {@link get_classification_scope_for} function
*
* @since 3.5.0
*/
function test_get_classification_scope_for() {

$entity_service = Wordlift_Entity_Service::get_instance();

$post_id = wl_create_post( '', 'post-1', uniqid( 'post', true ), 'draft', 'post' );
$this->assertNull( $entity_service->get_classification_scope_for( 'post_id' ) );
$entity_id = wl_create_post( '', 'entity-1', uniqid( 'entity', true ), 'draft', 'entity' );
$this->assertEquals( 'what', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/Thing' );
$this->assertEquals( 'what', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/CreativeWork' );
$this->assertEquals( 'what', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/Place' );
$this->assertEquals( 'where', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/Event' );
$this->assertEquals( 'when', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/Person' );
$this->assertEquals( 'who', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/Organization' );
$this->assertEquals( 'who', $entity_service->get_classification_scope_for( $entity_id ) );
wl_set_entity_main_type( $entity_id, 'http://schema.org/LocalBusiness' );
$this->assertEquals( 'who', $entity_service->get_classification_scope_for( $entity_id ) );

}
}

0 comments on commit f9b0942

Please sign in to comment.