/**
  * Test the {@link get_alternative_labels} function by creating an entity and checking that the number of alternative
  * labels matches the one we set via {@link save_post}.
  *
  * @since 3.2.0
  */
 function test_get_alternative_labels()
 {
     $entity_service = Wordlift_Entity_Service::get_instance();
     // Create a test entity.
     $entity_id = wl_create_post('This is Entity 1', 'entity-1', 'Entity 1', 'publish', 'entity');
     wl_set_entity_main_type($entity_id, 'http://schema.org/Thing');
     // Check that we have no alternative labels.
     $this->assertCount(0, $entity_service->get_alternative_labels($entity_id));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array('ABC 1', 'ABD 2', 'EFG 3');
     $entity_service->save_post($entity_id, null, null);
     // Check that we have 3 alternative labels.
     $this->assertCount(3, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(2, wl_entity_get_by_title('AB', true));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array('ABC 1', 'ABD 2');
     $entity_service->save_post($entity_id, null, null);
     // Check that we have 2 alternative labels.
     $this->assertCount(2, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(2, wl_entity_get_by_title('AB', true));
     // Call save_post to set the alternative labels, mock the request first.
     $_REQUEST['wl_alternative_label'] = array();
     $entity_service->save_post($entity_id, null, null);
     // Check that we have no alternative labels.
     $this->assertCount(0, $entity_service->get_alternative_labels($entity_id));
     $this->assertCount(0, wl_entity_get_by_title('AB'));
 }
 /**
  * Only accept URIs or local entity IDs.
  * Build new entity if the user inputted a name that is not present in DB.
  */
 public function sanitize_data_filter($value)
 {
     if (empty($value)) {
         return null;
     }
     // Check that the inserted URI, ID or name does not point to a saved entity.
     if (is_numeric($value)) {
         $absent_from_db = is_null(get_post($value));
         // search by ID
     } else {
         $absent_from_db = is_null(wl_get_entity_post_by_uri($value)) && is_null(get_page_by_title($value, OBJECT, Wordlift_Entity_Service::TYPE_NAME));
         // search by name
     }
     // Is it an URI?
     $name_is_uri = strpos($value, 'http') === 0;
     // We create a new entity only if the entity is not present in the DB.
     // In the case of an external uri, we just save the uri.
     if ($absent_from_db && !$name_is_uri) {
         // ...we create a new entity!
         $new_entity_id = wp_insert_post(array('post_status' => 'publish', 'post_type' => Wordlift_Entity_Service::TYPE_NAME, 'post_title' => $value));
         $new_entity = get_post($new_entity_id);
         $type = 'http://schema.org/' . (isset($this->expected_uri_type) ? $this->expected_uri_type[0] : 'Thing');
         wl_set_entity_main_type($new_entity_id, $type);
         // Build uri for this entity
         $new_uri = wl_build_entity_uri($new_entity_id);
         wl_set_entity_uri($new_entity_id, $new_uri);
         wl_push_entity_post_to_redlink($new_entity);
         // Update the value that will be saved as meta
         $value = $new_entity_id;
     }
     return $value;
 }
Exemplo n.º 3
0
 function testSaveEventWithStartAndEndDates()
 {
     $entity_1_id = wl_create_post('', 'entity-1', 'Entity 1', 'draft', 'entity');
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2013-01-02');
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2013-02-03');
     wl_set_entity_main_type($entity_1_id, 'http://schema.org/Event');
     wp_update_post(array('ID' => $entity_1_id, 'post_content' => 'Lorem Ipsum.'));
     // TODO: add checks for SPARQL query.
 }
 /**
  * Create:
  *  * 1 Post
  *  * 3 Place entities referenced by the Post
  *  * 1 Person entity reference by the Post
  *
  */
 function testFieldShortcode()
 {
     $place_id = wl_create_post("Entity 1 Text", 'entity-1', "Entity 1 Title", 'publish', 'entity');
     wl_set_entity_main_type($place_id, 'http://schema.org/Place');
     add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 40.12, true);
     add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 72.3, true);
     // Correct use
     $result = do_shortcode("[wl_field id={$place_id} name='latitude']");
     $this->assertContains('40.12', $result);
     // Implicit ID (like we inserted the shortcode in the entity editor)
     $GLOBALS['post'] = get_post($place_id);
     // Set manually post_id
     $result = do_shortcode("[wl_field name='latitude']");
     $this->assertEquals('40.12', $result);
     // Invalid ID (will ignore it)
     $result = do_shortcode("[wl_field id='yea' name='latitude']");
     $this->assertEquals('40.12', $result);
     unset($GLOBALS['post']);
     // Invalid property name
     $result = do_shortcode("[wl_field id={$place_id} name='tuhdaaaa!']");
     $this->assertEquals('', $result);
 }
/**
 * Saves the values of wordlift metaboxes set in the entity editor page
 */
function wl_entity_metabox_save($post_id)
{
    if (!isset($_POST['wl_metaboxes'])) {
        return;
    }
    // Loop over the wl_metaboxes array and save metaboxes values
    foreach ($_POST['wl_metaboxes'] as $meta_name => $meta_values) {
        // First, verify nonce is set for this meta
        $nonce_name = 'wordlift_' . $meta_name . '_entity_box_nonce';
        $nonce_verify = 'wordlift_' . $meta_name . '_entity_box';
        if (!isset($_POST[$nonce_name])) {
            return $post_id;
        }
        // Verify that the nonce is valid.
        if (!wp_verify_nonce($_POST[$nonce_name], $nonce_verify)) {
            return $post_id;
        }
        // Delete values before updating
        delete_post_meta($post_id, $meta_name);
        // Save the property value(s)
        if (isset($meta_name) && isset($meta_values) && $meta_values !== '') {
            // There can be one or more property values, so we force to array:
            if (!is_array($meta_values)) {
                $meta_values = array($meta_values);
            }
            foreach ($meta_values as $meta_value) {
                // If the meta expects an entity...
                $expecting_uri = wl_get_meta_type($meta_name) === WL_DATA_TYPE_URI;
                // ...and the user inputs an entity that is not present in the db...
                $absent_from_db = is_null(wl_get_entity_post_by_uri($meta_value));
                // ...and that is not a external uri
                $name_is_uri = strpos($meta_value, 'http') === 0;
                if ($expecting_uri && $absent_from_db && !$name_is_uri) {
                    // ...we create a new entity!
                    $new_entity = wl_save_entity('', $meta_value, WL_ENTITY_TYPE_NAME, '');
                    // Assign type
                    $constraints = wl_get_meta_constraints($meta_name);
                    $type = 'http://schema.org/' . $constraints['uri_type'];
                    wl_set_entity_main_type($new_entity->ID, $type);
                    // TODO: directly publish the new entity
                    // Update the value that will be saved as meta
                    $meta_value = wl_get_entity_uri($new_entity->ID);
                }
                // TODO: use WL methods
                add_post_meta($post_id, $meta_name, $meta_value);
            }
        }
    }
    // Push changes on RedLink
    wl_linked_data_push_to_redlink($post_id);
}
 function testMicrodataCompilingForAnEntityPage()
 {
     // A place
     $place_id = wl_create_post('', 'my-place', 'MyPlace', 'publish', 'entity');
     wl_set_entity_main_type($place_id, 'http://schema.org/Place');
     wl_schema_set_value($place_id, 'latitude', 40.12);
     wl_schema_set_value($place_id, 'longitude', 72.3);
     wl_schema_set_value($place_id, 'streetAddress', 'via del ciuccio 23');
     // Compile markup for the given content
     $compiled_markup = _wl_content_embed_microdata($place_id, '');
     $expected_markup = file_get_contents(dirname(__FILE__) . '/assets/microdata_compiling_entity_page.txt');
     // Verify correct markup
     $this->assertEquals($this->prepareMarkup($expected_markup), $this->prepareMarkup($compiled_markup));
 }
 /**
  * Create:
  *  * 2 Post
  *  * 2 Event entities referenced, one per Post
  *  * 1 Place entity as a distractor
  * Check that the 2 events are retrieved from the global timeline (no post specified).
  */
 function testGlobalTimeline()
 {
     // Create posts
     $post_1_id = wl_create_post('', 'post-1', 'Post 1', 'publish', 'post');
     $post_2_id = wl_create_post('', 'post-2', 'Post 2', 'publish', 'post');
     // Create entities (2 events and one place)
     $entity_1_id = wl_create_post("Entity 1's Text", 'entity-1', "Entity 1's Title", 'publish', 'entity');
     wl_set_entity_main_type($entity_1_id, 'http://schema.org/Event');
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2014-01-01', true);
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2014-01-07', true);
     $entity_2_id = wl_create_post("Entity 2's Text", 'entity-2', "Entity 2's Title", 'publish', 'entity');
     wl_set_entity_main_type($entity_2_id, 'http://schema.org/Event');
     add_post_meta($entity_2_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2014-01-02', true);
     add_post_meta($entity_2_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2014-01-08', true);
     $entity_3_id = wl_create_post('Entity 3 Text', 'entity-3', 'Entity 3 Title', 'publish', 'entity');
     wl_set_entity_main_type($entity_2_id, 'http://schema.org/Place');
     add_post_meta($entity_3_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 45.12, true);
     add_post_meta($entity_3_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 90.3, true);
     wl_core_add_relation_instances($post_1_id, WL_WHAT_RELATION, array($entity_1_id, $entity_3_id));
     wl_core_add_relation_instance($post_2_id, WL_WHAT_RELATION, $entity_2_id);
     // Call retrieving function with null argument (i.e. global timeline)
     $events = wl_shortcode_timeline_get_events(null);
     $this->assertCount(2, $events);
     $event_ids = array_map(function ($item) {
         return $item->ID;
     }, $events);
     $this->assertContains($entity_1_id, $event_ids);
     $this->assertContains($entity_2_id, $event_ids);
 }
Exemplo n.º 8
0
 function testWL_Metabox_Field_uri_data()
 {
     // Create an entity of type Person
     $person_id = wl_create_post('', 'p', 'A person', 'publish', Wordlift_Entity_Service::TYPE_NAME);
     wl_set_entity_main_type($person_id, 'http://schema.org/Person');
     wl_schema_set_value($person_id, 'author', 43);
     // Create an entity of type CreativeWork
     $creative_work_id = wl_create_post('', 'cw', 'A creative work', 'publish', Wordlift_Entity_Service::TYPE_NAME);
     wl_set_entity_main_type($creative_work_id, 'http://schema.org/CreativeWork');
     wl_schema_set_value($creative_work_id, 'author', $person_id);
     // Set authorship
     // Create fake context
     global $post;
     $post = get_post($creative_work_id);
     // Build a single Field
     $author_custom_field = $this->getSampleCustomField(Wordlift_Schema_Service::DATA_TYPE_URI);
     $field = new WL_Metabox_Field_uri($author_custom_field);
     // Verify data is loaded correctly from DB
     $field->get_data();
     $this->assertEquals(array($person_id), $field->data);
     // Save new DB values (third value is invalid and fourth is a new entity)
     $field->save_data(array($person_id, 'http://some-triplestore/person2', null, 'Annibale'));
     // Verify data is loaded correctly from DB
     $new_entity = get_page_by_title('Annibale', OBJECT, Wordlift_Entity_Service::TYPE_NAME);
     $field->get_data();
     $this->assertEquals(array($person_id, 'http://some-triplestore/person2', $new_entity->ID), $field->data);
 }
/**
 * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
 * found (by its URI), then the original post is returned.
 *
 * @param array $entity_properties, associative array containing: 
 * string 'uri' The entity URI.
 * string 'label' The entity label.
 * string 'main_type_uri' The entity type URI.
 * string 'description' The entity description.
 * array 'type_uris' An array of entity type URIs.
 * array 'images' An array of image URLs.
 * int 'related_post_id' A related post ID.
 * array 'same_as' An array of sameAs URLs.
 *
 * @return null|WP_Post A post instance or null in case of failure.
 */
function wl_save_entity($entity_properties)
{
    $uri = $entity_properties['uri'];
    $label = $entity_properties['label'];
    $type_uri = $entity_properties['main_type_uri'];
    $description = $entity_properties['description'];
    $entity_types = $entity_properties['type_uris'];
    $images = $entity_properties['images'];
    $related_post_id = $entity_properties['related_post_id'];
    $same_as = $entity_properties['same_as'];
    // Avoid errors due to null.
    if (is_null($entity_types)) {
        $entity_types = array();
    }
    wl_write_log("[ uri :: {$uri} ][ label :: {$label} ][ type uri :: {$type_uri} ]");
    // Prepare properties of the new entity.
    $params = array('post_status' => is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft', 'post_type' => WL_ENTITY_TYPE_NAME, 'post_title' => $label, 'post_content' => $description, 'post_excerpt' => '');
    // Check whether an entity already exists with the provided URI.
    $post = wl_get_entity_post_by_uri($uri);
    if (null !== $post) {
        // We insert into the params the entity ID, so it will be updated and not inserted.
        $params['ID'] = $post->ID;
    }
    // create or update the post.
    $post_id = wp_insert_post($params, true);
    // TODO: handle errors.
    if (is_wp_error($post_id)) {
        wl_write_log(': error occurred');
        // inform an error occurred.
        return null;
    }
    wl_set_entity_main_type($post_id, $type_uri);
    // Save the entity types.
    wl_set_entity_rdf_types($post_id, $entity_types);
    // Get a dataset URI for the entity.
    $wl_uri = wl_build_entity_uri($post_id);
    // Save the entity URI.
    wl_set_entity_uri($post_id, $wl_uri);
    // Add the uri to the sameAs data if it's not a local URI.
    if ($wl_uri !== $uri) {
        array_push($same_as, $uri);
    }
    $new_uri = wl_get_entity_uri($post_id);
    // Save the sameAs data for the entity.
    wl_schema_set_value($post_id, 'sameAs', $same_as);
    // Call hooks.
    do_action('wl_save_entity', $post_id);
    wl_write_log("[ post id :: {$post_id} ][ uri :: {$uri} ][ label :: {$label} ][ wl uri :: {$wl_uri} ][ types :: " . implode(',', $entity_types) . " ][ images count :: " . count($images) . " ][ same_as count :: " . count($same_as) . " ]");
    foreach ($images as $image_remote_url) {
        // Check if image is already present in local DB
        if (strpos($image_remote_url, site_url()) !== false) {
            // Do nothing.
            continue;
        }
        // Check if there is an existing attachment for this post ID and source URL.
        $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
        // Skip if an existing image is found.
        if (null !== $existing_image) {
            continue;
        }
        // Save the image and get the local path.
        $image = wl_save_image($image_remote_url);
        // Get the local URL.
        $filename = $image['path'];
        $url = $image['url'];
        $content_type = $image['content_type'];
        $attachment = array('guid' => $url, 'post_title' => $label, 'post_content' => '', 'post_status' => 'inherit', 'post_mime_type' => $content_type);
        // Create the attachment in WordPress and generate the related metadata.
        $attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
        // Set the source URL for the image.
        wl_set_source_url($attachment_id, $image_remote_url);
        $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
        wp_update_attachment_metadata($attachment_id, $attachment_data);
        // Set it as the featured image.
        set_post_thumbnail($post_id, $attachment_id);
    }
    // The entity is pushed to Redlink on save by the function hooked to save_post.
    // save the entity in the triple store.
    wl_linked_data_push_to_redlink($post_id);
    // finally return the entity post.
    return get_post($post_id);
}
Exemplo n.º 10
0
 public function test_shortcode_geomap_ajax()
 {
     // TODO: fix content-type tests.
     $this->markTestSkipped('Content Type tests are failing, needs fix');
     if (!function_exists('xdebug_get_headers')) {
         $this->markTestSkipped('xdebug is required for this test');
     }
     $post_id = wl_create_post('This is Post 1', 'post-1', 'Post 1', 'publish');
     $entity_1_id = wl_create_post("Entity 1 Text", 'entity-1', "Entity 1 Title", 'publish', 'entity');
     wl_set_entity_main_type($entity_1_id, 'http://schema.org/Place');
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 40.12, true);
     add_post_meta($entity_1_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 72.3, true);
     $entity_2_id = wl_create_post("Entity 2 Text", 'entity-2', "Entity 2 Title", 'publish', 'entity');
     wl_set_entity_main_type($entity_2_id, 'http://schema.org/Place');
     add_post_meta($entity_2_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 41.2, true);
     add_post_meta($entity_2_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 78.2, true);
     wl_core_add_relation_instances($post_id, WL_WHAT_RELATION, array($entity_1_id, $entity_2_id));
     $_REQUEST['post_id'] = $post_id;
     wl_shortcode_geomap_ajax();
     $headers = xdebug_get_headers();
     $this->assertTrue(in_array('Content-Type: application/json', $headers));
 }
/**
 * Sets the entity type(s) for the specified post ID. Support is now for only one type per entity.
 *
 * @param $post_id numeric The numeric post ID
 * @param $type_names array An array of strings, each defining a type (e.g. Type, for the http://schema.org/Type)
 *
 * @return boolean True if everything went ok, an error string otherwise.
 */
function wl_schema_set_types($post_id, $type_names)
{
    // Some checks on the parameters
    if (!is_numeric($post_id) || empty($type_names) || is_null($type_names)) {
        return null;
    }
    // TODO: support more than one type
    if (is_array($type_names)) {
        $type_names = $type_names[0];
    }
    // Build full schema uri if necessary
    $type_names = wl_build_full_schema_uri_from_schema_slug($type_names);
    // Actually sets the taxonomy type
    wl_set_entity_main_type($post_id, $type_names);
}
 function testWlEntityTaxonomyMicrodataTemplateInheritance()
 {
     // Create entity and set type
     $business_id = wl_create_post("Entity 1 Text", 'entity-1', "Entity 1 Title", 'publish', 'entity');
     wl_set_entity_main_type($business_id, 'http://schema.org/LocalBusiness');
     // Get microdata template
     $entity_type_details = wl_entity_type_taxonomy_get_type($business_id);
     $microdata_template = $entity_type_details['microdata_template'];
     // Check inherited microdata templates:
     // latitude from Place with 'itemtype="http://schema.org/GeoCoordinates"' markup
     $this->assertContains('itemtype="http://schema.org/GeoCoordinates"', $microdata_template);
     $this->assertContains('{{latitude}}', $microdata_template);
     // founder from Organization
     $this->assertContains('{{founder}}', $microdata_template);
     // negative test
     $this->assertNotContains('{{startDate}}', $microdata_template);
 }
/**
 * Save the specified data as an entity in WordPress. This method only create new entities. When an existing entity is
 * found (by its URI), then the original post is returned.
 *
 * @param array $entity_data, associative array containing: 
 * string 'uri'             The entity URI.
 * string 'label'           The entity label.
 * string 'main_type'       The entity type URI.
 * array  'type'            An array of entity type URIs.
 * string 'description'     The entity description.
 * array  'images'          An array of image URLs.
 * int    'related_post_id' A related post ID.
 * array  'same_as'         An array of sameAs URLs.
 *
 * @return null|WP_Post A post instance or null in case of failure.
 */
function wl_save_entity($entity_data)
{
    $uri = $entity_data['uri'];
    $label = $entity_data['label'];
    $type_uri = $entity_data['main_type'];
    $entity_types = isset($entity_data['type']) ? $entity_data['type'] : array();
    $description = $entity_data['description'];
    $images = isset($entity_data['image']) ? wl_force_to_array($entity_data['image']) : array();
    $same_as = isset($entity_data['sameas']) ? wl_force_to_array($entity_data['sameas']) : array();
    $related_post_id = isset($entity_data['related_post_id']) ? $entity_data['related_post_id'] : null;
    $other_properties = isset($entity_data['properties']) ? $entity_data['properties'] : array();
    // wl_write_log( "[ uri :: $uri ][ label :: $label ][ type uri :: $type_uri ]" );
    // Prepare properties of the new entity.
    $params = array('post_status' => is_numeric($related_post_id) ? get_post_status($related_post_id) : 'draft', 'post_type' => Wordlift_Entity_Service::TYPE_NAME, 'post_title' => $label, 'post_content' => $description, 'post_excerpt' => '');
    // Check whether an entity already exists with the provided URI.
    $post = wl_get_entity_post_by_uri($uri);
    if (null !== $post) {
        // We insert into the params the entity ID, so it will be updated and not inserted.
        $params['ID'] = $post->ID;
        // Preserve the current entity status
        if ('public' === $post->post_status) {
            $params['post_status'] = $post->post_status;
        }
        // Preserve the current entity post_content.
        $params['post_content'] = $post->post_content;
        // Preserve the entity post_title to avoid de-synch between WP and RL
        // See: https://github.com/insideout10/wordlift-plugin/issues/221
        $params['post_title'] = $post->post_title;
    }
    // If Yoast is installed and active, we temporary remove the save_postdata hook which causes Yoast to "pass over"
    // the local SEO form values to the created entity (https://github.com/insideout10/wordlift-plugin/issues/156)
    // Same thing applies to SEO Ultimate (https://github.com/insideout10/wordlift-plugin/issues/148)
    // This does NOT affect saving an entity from the entity admin page since this function is called when an entity
    // is created when saving a post.
    global $wpseo_metabox, $seo_ultimate;
    if (isset($wpseo_metabox)) {
        remove_action('wp_insert_post', array($wpseo_metabox, 'save_postdata'));
    }
    if (isset($seo_ultimate)) {
        remove_action('save_post', array($seo_ultimate, 'save_postmeta_box'));
    }
    // The fact that we're calling here wp_insert_post is causing issues with plugins (and ourselves too) that hook to
    // save_post in order to save additional inputs from the edit page. In order to avoid issues, we pop all the hooks
    // to the save_post and restore them after we saved the entity.
    // see https://github.com/insideout10/wordlift-plugin/issues/203
    // see https://github.com/insideout10/wordlift-plugin/issues/156
    // see https://github.com/insideout10/wordlift-plugin/issues/148
    global $wp_filter;
    $save_post_filters = $wp_filter['save_post'];
    $wp_filter['save_post'] = array();
    // create or update the post.
    $post_id = wp_insert_post($params, true);
    // Restore all the existing filters.
    $wp_filter['save_post'] = $save_post_filters;
    // If Yoast is installed and active, we restore the Yoast save_postdata hook (https://github.com/insideout10/wordlift-plugin/issues/156)
    if (isset($wpseo_metabox)) {
        add_action('wp_insert_post', array($wpseo_metabox, 'save_postdata'));
    }
    // If SEO Ultimate is installed, add back the hook we removed a few lines above.
    if (isset($seo_ultimate)) {
        add_action('save_post', array($seo_ultimate, 'save_postmeta_box'), 10, 2);
    }
    // TODO: handle errors.
    if (is_wp_error($post_id)) {
        wl_write_log(': error occurred');
        // inform an error occurred.
        return null;
    }
    wl_set_entity_main_type($post_id, $type_uri);
    // Save the entity types.
    wl_set_entity_rdf_types($post_id, $entity_types);
    // Get a dataset URI for the entity.
    $wl_uri = wl_build_entity_uri($post_id);
    // Save the entity URI.
    wl_set_entity_uri($post_id, $wl_uri);
    // Add the uri to the sameAs data if it's not a local URI.
    if ($wl_uri !== $uri) {
        array_push($same_as, $uri);
    }
    $new_uri = wl_get_entity_uri($post_id);
    // Save the sameAs data for the entity.
    wl_schema_set_value($post_id, 'sameAs', $same_as);
    // Save the other properties (latitude, langitude, startDate, endDate, etc.)
    foreach ($other_properties as $property_name => $property_value) {
        wl_schema_set_value($post_id, $property_name, $property_value);
    }
    // Call hooks.
    do_action('wl_save_entity', $post_id);
    wl_write_log("[ post id :: {$post_id} ][ uri :: {$uri} ][ label :: {$label} ][ wl uri :: {$wl_uri} ][ types :: " . implode(',', $entity_types) . " ][ images count :: " . count($images) . " ][ same_as count :: " . count($same_as) . " ]");
    foreach ($images as $image_remote_url) {
        // Check if image is already present in local DB
        if (strpos($image_remote_url, site_url()) !== false) {
            // Do nothing.
            continue;
        }
        // Check if there is an existing attachment for this post ID and source URL.
        $existing_image = wl_get_attachment_for_source_url($post_id, $image_remote_url);
        // Skip if an existing image is found.
        if (null !== $existing_image) {
            continue;
        }
        // Save the image and get the local path.
        $image = wl_save_image($image_remote_url);
        // Get the local URL.
        $filename = $image['path'];
        $url = $image['url'];
        $content_type = $image['content_type'];
        $attachment = array('guid' => $url, 'post_title' => $label, 'post_content' => '', 'post_status' => 'inherit', 'post_mime_type' => $content_type);
        // Create the attachment in WordPress and generate the related metadata.
        $attachment_id = wp_insert_attachment($attachment, $filename, $post_id);
        // Set the source URL for the image.
        wl_set_source_url($attachment_id, $image_remote_url);
        $attachment_data = wp_generate_attachment_metadata($attachment_id, $filename);
        wp_update_attachment_metadata($attachment_id, $attachment_data);
        // Set it as the featured image.
        set_post_thumbnail($post_id, $attachment_id);
    }
    // The entity is pushed to Redlink on save by the function hooked to save_post.
    // save the entity in the triple store.
    wl_linked_data_push_to_redlink($post_id);
    // finally return the entity post.
    return get_post($post_id);
}
Exemplo n.º 14
0
    function testMicrodataCompilingRecursivityLimitation()
    {
        // A place
        $place_id = wl_create_post('Just a place', 'my-place', 'MyPlace', 'publish', 'entity');
        wl_set_entity_main_type($place_id, 'http://schema.org/Place');
        // Trying out both the schema API and the classic WP method
        wl_schema_set_value($place_id, 'latitude', 40.12);
        add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 72.3, true);
        // An Event having as location the place above
        $event_id = wl_create_post('Just an event', 'my-event', 'MyEvent', 'publish', 'entity');
        wl_set_entity_main_type($event_id, 'http://schema.org/Event');
        add_post_meta($event_id, WL_CUSTOM_FIELD_CAL_DATE_START, '2014-10-21', true);
        add_post_meta($event_id, WL_CUSTOM_FIELD_CAL_DATE_END, '2015-10-26', true);
        wl_schema_set_value($event_id, 'sameAs', array('http://rdf.freebase.com/my-event', 'http://dbpedia.org/resource/my-event'));
        //wl_schema_set_value($event_id, 'sameAs', 'http://dbpedia.org/resource/my-event');
        add_post_meta($event_id, WL_CUSTOM_FIELD_LOCATION, $place_id, true);
        // Create an annotated post containing the entities
        $entity_uri = wl_get_entity_uri($event_id);
        $content = <<<EOF
    <span itemid="{$entity_uri}">MyEvent</span>
EOF;
        $post_id = wl_create_post($content, 'post', 'A post');
        // Set to 0 the recursivity limitation on entity metadata compiling
        $this->setRecursionDepthLimit(0);
        // The expected mark-up expects color coding to be on.
        wl_configuration_set_enable_color_coding(true);
        // Compile markup for the given content
        $compiled_markup = _wl_content_embed_microdata($post_id, $content);
        $expected_markup = file_get_contents(dirname(__FILE__) . '/assets/microdata_compiling_recursivity_limitation.txt');
        // Verify correct markup
        $this->assertEquals($this->prepareMarkup($expected_markup), $this->prepareMarkup($compiled_markup));
        $this->setRecursionDepthLimit(1);
        // Compile markup for the given content
        $compiled_markup = _wl_content_embed_microdata($post_id, $content);
        $expected_markup = file_get_contents(dirname(__FILE__) . '/assets/microdata_compiling_for_an_entity_with_nested_entities.txt');
        // Verify correct markup
        $this->assertEquals($this->prepareMarkup($expected_markup), $this->prepareMarkup($compiled_markup));
    }
 /**
  * Create:
  *  * 2 Posts
  *  * 1 Place entity referenced by both posts
  *
  * Check that the geomap popup of the place contains a link to the two posts.
  */
 function testPlacePopupRelatedPosts()
 {
     // Create two posts.
     $post_id_1 = wl_create_post('', 'post-1', 'Post 1', 'publish', 'post');
     $post_id_2 = wl_create_post('', 'post-2', 'Post 2', 'publish', 'post');
     // Create a place-
     $place_id = wl_create_post("Entity 1 Text", 'entity-1', "Entity 1 Title", 'publish', 'entity');
     wl_set_entity_main_type($place_id, 'http://schema.org/Place');
     add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LATITUDE, 40.12, true);
     add_post_meta($place_id, WL_CUSTOM_FIELD_GEO_LONGITUDE, 72.3, true);
     // Reference place from both posts-
     wl_core_add_relation_instance($post_id_1, WL_WHERE_RELATION, $place_id);
     wl_core_add_relation_instance($post_id_2, WL_WHERE_RELATION, $place_id);
     // Check referencing.
     $places = wl_shortcode_geomap_get_places($post_id_1);
     $this->assertCount(1, $places);
     $this->assertEquals($places[0]->ID, $place_id);
     // Check json formatted data.
     $response = wl_shortcode_geomap_prepare_map($places);
     // Check object attributes
     $poi = $response['features'][0];
     $this->assertTrue(isset($poi));
     $this->assertTrue(isset($poi['properties']));
     $this->assertTrue(isset($poi['properties']['popupContent']));
     // Check if popup contains links to the two posts
     $popup = $poi['properties']['popupContent'];
     $link1 = esc_attr(get_permalink($post_id_1));
     $this->assertContains($link1, $popup);
     $link2 = esc_attr(get_permalink($post_id_2));
     $this->assertContains($link2, $popup);
     // Check no thumbnail has been echoed
     $this->assertNotContains('<img', $popup);
 }