/**
 * Displays the meta box contents (called by *add_meta_box* callback).
 *
 * @param WP_Post $post The current post.
 */
function wl_entities_box_content($post)
{
    // wl_write_log( "wl_entities_box_content [ post id :: $post->ID ]" );
    // Angularjs edit-post widget wrapper
    echo '<div id="wordlift-edit-post-outer-wrapper"></div>';
    // Angularjs edit-post widget classification boxes configuration
    $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES);
    // Array to store all related entities ids
    $all_referenced_entities_ids = array();
    // Add selected entities to classification_boxes
    foreach ($classification_boxes as $i => $box) {
        // Build the proper relation name
        $relation_name = $box['id'];
        // wl_write_log( "Going to related of $relation_name" );
        // Get entity ids related to the current post for the given relation name (both draft and published entities)
        $draft_entity_ids = wl_core_get_related_entity_ids($post->ID, array('predicate' => $relation_name, 'status' => 'draft'));
        $publish_entity_ids = wl_core_get_related_entity_ids($post->ID, array('predicate' => $relation_name, 'status' => 'publish'));
        $entity_ids = array_unique(array_merge($draft_entity_ids, $publish_entity_ids));
        // Store the entity ids for all the 4W
        $all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids);
        // Transform entity ids array in entity uris array
        array_walk($entity_ids, function (&$entity_id) {
            // Retrieve the entity uri for the given entity id
            $entity_id = wl_get_entity_uri($entity_id);
        });
        // Enhance current box selected entities
        $classification_boxes[$i]['selectedEntities'] = $entity_ids;
    }
    // Json encoding for classification boxes structure
    $classification_boxes = json_encode($classification_boxes);
    // Ensure there are no repetitions of the referenced entities
    $all_referenced_entities_ids = array_unique($all_referenced_entities_ids);
    // Build the entity storage object
    $referenced_entities_obj = array();
    foreach ($all_referenced_entities_ids as $referenced_entity) {
        $entity = wl_serialize_entity($referenced_entity);
        $referenced_entities_obj[$entity['id']] = $entity;
    }
    $referenced_entities_obj = empty($referenced_entities_obj) ? '{}' : json_encode($referenced_entities_obj);
    $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH;
    $dataset_uri = wl_configuration_get_redlink_dataset_uri();
    echo <<<EOF
    <script type="text/javascript">
        jQuery( function() {

        \tif ('undefined' == typeof window.wordlift) {
            \twindow.wordlift = {}
            \twindow.wordlift.entities = {}  \t\t
        \t}

        \twindow.wordlift.classificationBoxes = {$classification_boxes};
        \twindow.wordlift.entities = {$referenced_entities_obj};
        \twindow.wordlift.currentPostId = {$post->ID};
\t\t\twindow.wordlift.defaultThumbnailPath = '{$default_thumbnail_path}';
\t\t\twindow.wordlift.datasetUri = '{$dataset_uri}';

        });
    </script>
EOF;
}
/**
 * Build an URI for the specified user ID.
 *
 * @param int $user_id The user ID.
 *
 * @return null|string Null if the user is not found, or the URI.
 */
function wl_build_user_uri($user_id)
{
    // Get the user with the specified ID.
    $user = wl_get_user($user_id);
    // If the user is not found return null.
    if (false === $user) {
        wl_write_log("wl_build_user_uri : no user found [ user id :: {$user_id} ]");
        return null;
    }
    // Build the ID using the First and Last Name.
    if (!(empty($user->first_name) && empty($user->last_name))) {
        $id = wl_sanitize_uri_path($user->first_name . ' ' . $user->last_name);
    } else {
        // If there's no First and Last Name use the user ID.
        $id = $user_id;
    }
    //    $uri = sprintf(
    //        'http://data.redlink.io/%s/%s/%s/%s',
    //        wl_configuration_get_redlink_user_id(),
    //        wl_configuration_get_redlink_dataset_name(),
    //        'user',
    //        $id
    //    );
    // Create the URL (dataset base URI has a trailing slash).
    $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'user', $id);
    // Check that the URI doesn't exist already. If it exists, add a numeric suffix.
    $base_uri = $uri;
    $counter = 1;
    while (null !== wl_get_user_by_uri($uri)) {
        $uri = $base_uri . "_" . $counter++;
    }
    wl_write_log("wl_build_user_uri [ user id :: {$user_id} ][ uri :: {$uri} ]");
    return $uri;
}
 /**
  * Create an instance of WL_View.
  *
  * @since 3.0.0
  *
  * @uses wl_configuration_get_redlink_dataset_uri() to get the default dataset URI.
  *
  * @param string $base_uri The base URI for resources, default the WordLift dataset.
  * @param string $suffix The suffix to append to URI in order to load the JSON-LD file, default *.json*.
  * @param string $title The predicate used to set the title of the page, default *rdfs:label*.
  * @param string $language The language for values, default *en*.
  */
 function __construct($base_uri = null, $suffix = '.json', $title = 'rdfs:label', $language = 'en')
 {
     // Set the instance variables.
     $this->base_uri = null === $base_uri ? wl_configuration_get_redlink_dataset_uri() : $base_uri;
     $this->suffix = $suffix;
     $this->title = $title;
     $this->language = $language;
     wl_write_log("[ base URI :: {$this->base_uri} ][ suffix :: {$this->suffix} ][ title :: {$this->title} ][ language :: {$this->language} ]");
     add_filter('wp_title', array($this, 'filter_page_title'), 10, 2);
 }
Beispiel #4
0
 /**
  * Test the plugin configuration.
  */
 function testConfiguration()
 {
     // #43: https://github.com/insideout10/wordlift-plugin/issues/43
     // We're using WordLift Server, we do not require a Redlink Key nor a Dataset Name to be set:
     // we now require a WordLift key to be set. In turn, setting WordLift key should set the dataset URI,
     // that we'll continue to check.
     // $this->assertNotNull(wl_configuration_get_redlink_key());
     // $this->assertNotNull( wl_configuration_get_redlink_dataset_name() );
     // $this->assertNotNull( wl_configuration_get_redlink_user_id() );
     $this->assertNotNull(wl_configuration_get_key());
     $this->assertNotNull(wl_configuration_get_redlink_dataset_uri());
     $this->assertEquals(WL_CONFIG_DEFAULT_SITE_LANGUAGE, wl_configuration_get_site_language());
 }
function wl_entity_view_shortcode($atts, $content = null)
{
    // Extract attributes and set default values.
    $params = shortcode_atts(array('uri' => wl_configuration_get_redlink_dataset_uri(), 'suffix' => '.json', 'title' => 'rdfs:label', 'language' => 'en'), $atts);
    global $graph, $wl_entity_view_suffix, $wl_entity_view_title;
    $wl_entity_view_suffix = $params['suffix'];
    $url = $params['uri'] . get_query_var(WL_ENTITY_VIEW_ENTITY_ID_QUERY_VAR);
    // Load the graph.
    $graph = wl_jsonld_load_remote($url);
    // Get the title.
    $wl_entity_view_title = wl_jsonld_get_property($graph, $params['title'], $params['language']);
    ob_end_flush();
    return do_shortcode($content);
}
 function testEntityUriWithMissingDatasetUri()
 {
     $post_id = wl_create_post('A body', 'post-1', uniqid('post', true), 'draft', 'post');
     $dataset_uri = wl_configuration_get_redlink_dataset_uri();
     $this->assertNotEmpty($dataset_uri);
     // Remove the dataset uri
     wl_configuration_set_redlink_dataset_uri('');
     $entity_uri = wl_get_entity_uri($post_id);
     // Check the wl_get_entity_uri properly returns null
     $this->assertNull($entity_uri);
     // Check the are not custom meta set for the current post
     $this->assertEmpty(get_post_meta($post_id, WL_ENTITY_URL_META_NAME));
     // Set the dataset uri again
     wl_configuration_set_redlink_dataset_uri($dataset_uri);
 }
/**
 * Save the post to the triple store. Also saves the entities locally and on the triple store.
 *
 * @since 3.0.0
 *
 * @param int $post_id The post id being saved.
 */
function wl_linked_data_save_post_and_related_entities($post_id)
{
    // Ignore auto-saves
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // get the current post.
    $post = get_post($post_id);
    remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
    wl_write_log("[ post id :: {$post_id} ][ autosave :: false ][ post type :: {$post->post_type} ]");
    // 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'])) {
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_entities) :: ");
        wl_write_log(json_encode($_POST['wl_entities']));
        wl_write_log("]");
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_boxes) :: ");
        wl_write_log(json_encode($_POST['wl_boxes'], true));
        wl_write_log("]");
        $entities_via_post = $_POST['wl_entities'];
        $boxes_via_post = $_POST['wl_boxes'];
        foreach ($entities_via_post as $entity_uri => $entity) {
            // Local entities have a tmp uri with 'local-entity-'
            // These uris need to be rewritten here and replaced in the content
            if (preg_match('/^local-entity-.+/', $entity_uri) > 0) {
                // Build the proper uri
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($entity['label']));
                // Populate the mapping
                $entities_uri_mapping[$entity_uri] = $uri;
                // Override the entity obj
                $entities_via_post[$entity_uri]['uri'] = $uri;
            }
        }
        // Populate the $entities_predicates_mapping
        // Local Redlink uris need to be used here
        foreach ($boxes_via_post as $predicate => $entity_uris) {
            foreach ($entity_uris as $entity_uri) {
                wl_write_log("Going to map predicates for uri {$entity_uri} ");
                // Retrieve the entity label needed to build the uri
                $label = $entities_via_post[stripslashes($entity_uri)]['label'];
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label));
                wl_write_log("Going to map predicate {$predicate} to uri {$uri} ");
                $entities_predicates_mapping[$uri][] = $predicate;
            }
        }
        // Save entities and push them to Redlink
        // TODO: pass also latitude, longitude, etc.
        wl_save_entities(array_values($entities_via_post), $post_id);
    }
    // Replace tmp uris in content post if needed
    $updated_post_content = $post->post_content;
    // Save each entity and store the post id.
    foreach ($entities_uri_mapping as $tmp_uri => $uri) {
        $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
    }
    // Update the post content
    wp_update_post(array('ID' => $post->ID, 'post_content' => $updated_post_content));
    // Extract related/referenced entities from text.
    $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content);
    // Reset previously saved instances
    wl_core_delete_relation_instances($post_id);
    // Save relation instances
    foreach (array_unique($disambiguated_entities) as $referenced_entity_id) {
        wl_write_log(" Going to manage relation between Post {$post_id} and {$referenced_entity_id}");
        if ($entities_predicates_mapping) {
            wl_write_log(" Going to manage relation instances according to the following 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_write_log(" Going to add relation with predicate {$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);
        }
        // TODO Check if is needed
        wl_linked_data_push_to_redlink($referenced_entity_id);
    }
    // Push the post to Redlink.
    wl_linked_data_push_to_redlink($post->ID);
    add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
}
 function test_wl_configuration_redlink_dataset_uri()
 {
     $value = uniqid();
     wl_configuration_set_redlink_dataset_uri($value);
     $this->assertEquals($value, wl_configuration_get_redlink_dataset_uri());
 }
 function buildEntityUriForLabel($label)
 {
     return sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label));
 }
Beispiel #10
0
/**
 * Configure WordPress with the test settings (may vary according to the local PHP and WordPress versions).
 */
function wl_configure_wordpress_test()
{
    add_filter('wl_write_log_handler', 'wl_test_get_write_log_handler');
    do_action('activate_wordlift/wordlift.php');
    // If the WordLift key is set, then we'll configure it, otherwise we configure Redlink.
    if (false !== getenv('WORDLIFT_KEY')) {
        // When setting the WordLift Key, the Redlink dataset URI is provisioned by WordLift Server.
        wl_configuration_set_key(getenv('WORDLIFT_KEY'));
        if ('' === wl_configuration_get_redlink_dataset_uri()) {
            die('The Redlink dataset URI is not set (maybe the WordLift key is not valid?)');
        }
    } else {
        // TODO: remove this part.
        // or use Redlink.
        // Set the dataset name to the specified dataset or define it based on the current environment.
        $dataset_name = false !== getenv('REDLINK_DATASET_NAME') ? getenv('REDLINK_DATASET_NAME') : str_replace('.', '-', sprintf('%s-php-%s.%s-wp-%s-ms-%s', 'wordlift-tests', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, getenv('WP_VERSION'), getenv('WP_MULTISITE')));
        $app_name = false !== getenv('REDLINK_APP_NAME') ? getenv('REDLINK_APP_NAME') : 'wordlift';
        // Check that the API_URL env is set.
        if (false === getenv('API_URL')) {
            die('The API_URL environment variable is not set.');
        }
        wl_configuration_set_redlink_key(getenv('REDLINK_APP_KEY'));
        wl_configuration_set_redlink_user_id(getenv('REDLINK_USER_ID'));
        wl_configuration_set_api_url(getenv('API_URL'));
        wl_configuration_set_redlink_dataset_name($dataset_name);
        wl_configuration_set_redlink_application_name($app_name);
        wl_configuration_set_redlink_dataset_uri('http://data.redlink.io/' . getenv('REDLINK_USER_ID') . '/' . $dataset_name);
    }
}
Beispiel #11
0
 /**
  * Get an URI for testing.
  *
  * @param $id
  *
  * @return string
  */
 function getURI($id)
 {
     return wl_configuration_get_redlink_dataset_uri() . "/user/{$id}";
 }
 function prepareMarkup($markup)
 {
     $markup = preg_replace('/\\s+/', '', $markup);
     $markup = preg_replace('/{{REDLINK_ENDPOINT}}/', wl_configuration_get_redlink_dataset_uri(), $markup);
     return $markup;
 }
/**
 * Get the entity URI of the provided post.
 *
 * @uses wl_build_entity_uri() to create a new URI if the entity doesn't have an URI yet.
 * @uses wl_set_entity_uri() to set a newly create URI.
 *
 * @param int $post_id The post ID.
 *
 * @return string|null The URI of the entity or null if not configured.
 */
function wl_get_entity_uri($post_id)
{
    $uri = get_post_meta($post_id, WL_ENTITY_URL_META_NAME, true);
    // If the dataset uri is not properly configured, null is returned
    if ('' === wl_configuration_get_redlink_dataset_uri()) {
        return null;
    }
    // Set the URI if it isn't set yet.
    $post_status = get_post_status($post_id);
    if (empty($uri) && 'auto-draft' !== $post_status && 'revision' !== $post_status) {
        $uri = wl_build_entity_uri($post_id);
        //  "http://data.redlink.io/$user_id/$dataset_id/post/$post->ID";
        wl_set_entity_uri($post_id, $uri);
    }
    return $uri;
}
 /**
  * Test saving a post without a title. Check the URI.
  */
 function testSavePostWithoutTitle()
 {
     $post_id = wl_create_post('Sample Post', 'post-1', '', 'publish');
     $uri = wl_get_entity_uri($post_id);
     $expected_uri = wl_configuration_get_redlink_dataset_uri() . "/post/id/{$post_id}";
     $this->assertEquals($expected_uri, $uri);
 }
/**
 * Configure all the configuration parameters. The configuration parameters are grouped in two tabs:
 *  * General
 *  * Advanced (only available if the WL_ENABLE_ADVANCED_CONFIGURATION constant exists and is set to True)
 *
 * Called by the *admin_init* hook.
 *
 * @since 3.0.0
 */
function wl_configuration_settings()
{
    register_setting('wl_general_settings', 'wl_general_settings', 'wl_configuration_sanitize_settings');
    add_settings_section('wl_general_settings_section', 'General Settings', 'wl_configuration_general_settings_section_callback', 'wl_general_settings');
    add_settings_field(WL_CONFIG_WORDLIFT_KEY, __('WordLift Key', 'wordlift'), 'wl_configuration_input_box', 'wl_general_settings', 'wl_general_settings_section', array('id' => 'wl-key', 'name' => 'wl_general_settings[key]', 'value' => wl_configuration_get_key(), 'description' => __('Insert the WordLift Key', 'wordlift')));
    add_settings_field(WL_CONFIG_SITE_LANGUAGE_NAME, __('Site Language', 'wordlift'), 'wl_configuration_select', 'wl_general_settings', 'wl_general_settings_section', array('id' => 'wl-site-language', 'name' => 'wl_general_settings[site_language]', 'value' => wl_configuration_get_site_language(), 'description' => __('The site language', 'wordlift'), 'options' => wl_configuration_get_languages()));
    if (defined('WL_ENABLE_ADVANCED_CONFIGURATION') && WL_ENABLE_ADVANCED_CONFIGURATION) {
        register_setting('wl_advanced_settings', 'wl_advanced_settings', 'wl_configuration_sanitize_settings');
        add_settings_section('wl_advanced_settings_section', 'Advanced', 'wl_configuration_advanced_settings_section_callback', 'wl_advanced_settings');
        add_settings_field(WL_CONFIG_API_URL, __('API URL', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-api-url', 'name' => 'wl_advanced_settings[api_url]', 'value' => wl_configuration_get_api_url(), 'description' => __('The API URL', 'wordlift')));
        add_settings_field(WL_CONFIG_APPLICATION_KEY_NAME, __('Redlink Key', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-redlink-key', 'name' => 'wl_advanced_settings[redlink_key]', 'value' => wl_configuration_get_redlink_key(), 'description' => __('The Redlink key', 'wordlift')));
        add_settings_field(WL_CONFIG_USER_ID_NAME, __('Redlink User Id', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-redlink-user-id', 'name' => 'wl_advanced_settings[redlink_user_id]', 'value' => wl_configuration_get_redlink_user_id(), 'description' => __('The Redlink User Id', 'wordlift')));
        add_settings_field(WL_CONFIG_DATASET_NAME, __('Redlink Dataset name', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-redlink-dataset-name', 'name' => 'wl_advanced_settings[redlink_dataset_name]', 'value' => wl_configuration_get_redlink_dataset_name(), 'description' => __('The Redlink Dataset Name', 'wordlift')));
        add_settings_field(WL_CONFIG_DATASET_BASE_URI_NAME, __('Redlink Dataset URI', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-redlink-dataset-uri', 'name' => 'wl_advanced_settings[redlink_dataset_uri]', 'value' => wl_configuration_get_redlink_dataset_uri(), 'description' => __('The Redlink Dataset URI', 'wordlift')));
        add_settings_field(WL_CONFIG_ANALYSIS_NAME, __('Redlink Application Name', 'wordlift'), 'wl_configuration_input_box', 'wl_advanced_settings', 'wl_advanced_settings_section', array('id' => 'wl-redlink-application-name', 'name' => 'wl_advanced_settings[redlink_application_name]', 'value' => wl_configuration_get_redlink_application_name(), 'description' => __('The Redlink Application Name', 'wordlift')));
    }
}
 /**
  * Build an URI for a user.
  *
  * @since 3.1.7
  *
  * @param int $user_id The user's id.
  *
  * @return false|string The user's URI or false in case of failure.
  */
 private function _build_uri($user_id)
 {
     // Get the user, return false in case of failure.
     if (false === ($user = get_userdata($user_id))) {
         return false;
     }
     // If the nicename is not set, return a failure.
     if (empty($user->user_nicename)) {
         return false;
     }
     return wl_configuration_get_redlink_dataset_uri() . "/user/{$user->user_nicename}";
 }
 /**
  * Check entity URI building.
  */
 function testEntityURIforAnEntity()
 {
     $post_id = wl_create_post('', 'test', 'This is a test', 'draft', 'entity');
     $expected_uri = wl_configuration_get_redlink_dataset_uri() . '/entity/this_is_a_test';
     $this->assertEquals($expected_uri, wl_build_entity_uri($post_id));
 }