/** * 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 test_wl_configuration_site_language() { $value = uniqid(); wl_configuration_set_site_language($value); $this->assertEquals($value, wl_configuration_get_site_language()); }
/** * Push the provided entity post to Redlink. * * @param object $entity_post An entity post instance. */ function wl_push_entity_post_to_redlink($entity_post) { // Get the Entity service instance to perform actions related to entities. $entity_service = Wordlift_Entity_Service::get_instance(); // Only handle published entities. if (!$entity_service->is_entity($entity_post->ID) || 'publish' !== $entity_post->post_status) { wl_write_log("wl_push_entity_post_to_redlink : not an entity or not published [ post type :: {$entity_post->post_type} ][ post status :: {$entity_post->post_status} ]"); return; } // get the entity URI and the SPARQL escaped version. $uri = wl_get_entity_uri($entity_post->ID); $uri_e = wl_sparql_escape_uri($uri); // If the URI ends with a trailing slash, then we have a problem. if ('/' === substr($uri, -1, 1)) { wl_write_log("wl_push_entity_post_to_redlink : the URI is invalid [ post ID :: {$entity_post->ID} ][ URI :: {$uri} ]"); return; } // Get the site language in order to define the literals language. $site_language = wl_configuration_get_site_language(); // get the title and content as label and description. $label = wordlift_esc_sparql($entity_post->post_title); $descr = wordlift_esc_sparql(wp_strip_all_tags(strip_shortcodes($entity_post->post_content))); $permalink = wl_sparql_escape_uri(get_permalink($entity_post->ID)); // wl_write_log( "wl_push_entity_post_to_redlink [ entity post id :: $entity_post->ID ][ uri :: $uri ][ label :: $label ]" ); // create a new empty statement. $delete_stmt = ''; $sparql = ''; // delete on RL all statements regarding properties set from WL (necessary when changing entity type) $all_custom_fields = wl_entity_taxonomy_get_custom_fields(); $predicates_to_be_deleted = array(); foreach ($all_custom_fields as $type => $fields) { foreach ($fields as $cf) { $predicate = $cf['predicate']; if (!in_array($predicate, $predicates_to_be_deleted)) { $predicates_to_be_deleted[] = $predicate; $delete_stmt .= "DELETE { <{$uri_e}> <{$predicate}> ?o } WHERE { <{$uri_e}> <{$predicate}> ?o };\n"; } } } // set the same as. $same_as = wl_schema_get_value($entity_post->ID, 'sameAs'); foreach ($same_as as $same_as_uri) { $same_as_uri_esc = wl_sparql_escape_uri($same_as_uri); $sparql .= "<{$uri_e}> owl:sameAs <{$same_as_uri_esc}> . \n"; } // set the label $sparql .= "<{$uri_e}> rdfs:label \"{$label}\"@{$site_language} . \n"; // Set the alternative labels. $alt_labels = $entity_service->get_alternative_labels($entity_post->ID); foreach ($alt_labels as $alt_label) { $sparql .= sprintf('<%s> rdfs:label "%s"@%s . ', $uri_e, Wordlift_Query_Builder::escape_value($alt_label), $site_language); } // set the URL $sparql .= "<{$uri_e}> schema:url <{$permalink}> . \n"; // set the description. if (!empty($descr)) { $sparql .= "<{$uri_e}> schema:description \"{$descr}\"@{$site_language} . \n"; } $main_type = wl_entity_type_taxonomy_get_type($entity_post->ID); if (null != $main_type) { $main_type_uri = wl_sparql_escape_uri($main_type['uri']); $sparql .= " <{$uri_e}> a <{$main_type_uri}> . \n"; // The type define custom fields that hold additional data about the entity. // For example Events may have start/end dates, Places may have coordinates. // The value in the export fields must be rewritten as triple predicates, this // is what we're going to do here. // wl_write_log( 'wl_push_entity_post_to_redlink : checking if entity has export fields [ type :: ' . var_export( $main_type, true ) . ' ]' ); if (isset($main_type['custom_fields'])) { foreach ($main_type['custom_fields'] as $field => $settings) { // wl_write_log( "wl_push_entity_post_to_redlink : entity has export fields" ); $predicate = wordlift_esc_sparql($settings['predicate']); if (!isset($settings['export_type']) || empty($settings['export_type'])) { $type = null; } else { $type = $settings['export_type']; } foreach (get_post_meta($entity_post->ID, $field) as $value) { $sparql .= " <{$uri_e}> <{$predicate}> "; if (!is_null($type) && substr($type, 0, 4) == 'http') { // Type is defined by a raw uri (es. http://schema.org/PostalAddress) // Extract uri if the value is numeric if (is_numeric($value)) { $value = wl_get_entity_uri($value); } $sparql .= '<' . wl_sparql_escape_uri($value) . '>'; } else { // Type is defined in another way (es. xsd:double) $sparql .= '"' . wordlift_esc_sparql($value) . '"^^' . wordlift_esc_sparql($type); } $sparql .= " . \n"; } } } } // Get the entity types. $type_uris = wl_get_entity_rdf_types($entity_post->ID); // Support type are only schema.org ones: it could be null foreach ($type_uris as $type_uri) { $type_uri = wl_sparql_escape_uri($type_uri); $sparql .= "<{$uri_e}> a <{$type_uri}> . \n"; } // get related entities. $related_entities_ids = wl_core_get_related_entity_ids($entity_post->ID); if (is_array($related_entities_ids)) { foreach ($related_entities_ids as $entity_post_id) { $related_entity_uri = wl_sparql_escape_uri(wl_get_entity_uri($entity_post_id)); // create a two-way relationship. $sparql .= " <{$uri_e}> dct:relation <{$related_entity_uri}> . \n"; $sparql .= " <{$related_entity_uri}> dct:relation <{$uri_e}> . \n"; } } // Add SPARQL stmts to write the schema:image. $sparql .= wl_get_sparql_images($uri, $entity_post->ID); $query = rl_sparql_prefixes() . <<<EOF {$delete_stmt} DELETE { <{$uri_e}> rdfs:label ?o } WHERE { <{$uri_e}> rdfs:label ?o }; DELETE { <{$uri_e}> owl:sameAs ?o . } WHERE { <{$uri_e}> owl:sameAs ?o . }; DELETE { <{$uri_e}> schema:description ?o . } WHERE { <{$uri_e}> schema:description ?o . }; DELETE { <{$uri_e}> schema:url ?o . } WHERE { <{$uri_e}> schema:url ?o . }; DELETE { <{$uri_e}> a ?o . } WHERE { <{$uri_e}> a ?o . }; DELETE { <{$uri_e}> dct:relation ?o . } WHERE { <{$uri_e}> dct:relation ?o . }; DELETE { <{$uri_e}> schema:image ?o . } WHERE { <{$uri_e}> schema:image ?o . }; INSERT DATA { {$sparql} }; EOF; rl_execute_sparql_update_query($query); }
/** * 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'))); } }
/** * Push the provided entity post to Redlink. * * @param object $entity_post An entity post instance. */ function wl_push_entity_post_to_redlink($entity_post) { // Only handle published entities. if ('entity' !== $entity_post->post_type or 'publish' !== $entity_post->post_status) { wl_write_log("wl_push_entity_post_to_redlink : not an entity or not published [ post type :: {$entity_post->post_type} ][ post status :: {$entity_post->post_status} ]"); return; } // get the entity URI and the SPARQL escaped version. $uri = wl_get_entity_uri($entity_post->ID); $uri_e = wl_sparql_escape_uri($uri); // If the URI ends with a trailing slash, then we have a problem. if ('/' === substr($uri, -1, 1)) { wl_write_log("wl_push_entity_post_to_redlink : the URI is invalid [ post ID :: {$entity_post->ID} ][ URI :: {$uri} ]"); return; } // Get the site language in order to define the literals language. $site_language = wl_configuration_get_site_language(); // get the title and content as label and description. $label = wordlift_esc_sparql($entity_post->post_title); $descr = wordlift_esc_sparql($entity_post->post_content); $permalink = wl_sparql_escape_uri(get_permalink($entity_post->ID)); wl_write_log("wl_push_entity_post_to_redlink [ entity post id :: {$entity_post->ID} ][ uri :: {$uri} ][ label :: {$label} ]"); // create a new empty statement. $delete_stmt = ''; $sparql = ''; // set the same as. $same_as = wl_schema_get_value($entity_post->ID, 'sameAs'); foreach ($same_as as $same_as_uri) { $same_as_uri_esc = wl_sparql_escape_uri($same_as_uri); $sparql .= "<{$uri_e}> owl:sameAs <{$same_as_uri_esc}> . \n"; } // set the label $sparql .= "<{$uri_e}> rdfs:label \"{$label}\"@{$site_language} . \n"; // set the URL $sparql .= "<{$uri_e}> schema:url <{$permalink}> . \n"; // set the description. if (!empty($descr)) { $sparql .= "<{$uri_e}> schema:description \"{$descr}\"@{$site_language} . \n"; } $main_type = wl_entity_type_taxonomy_get_type($entity_post->ID); if (null != $main_type) { $main_type_uri = wl_sparql_escape_uri($main_type['uri']); $sparql .= " <{$uri_e}> a <{$main_type_uri}> . \n"; // The type define custom fields that hold additional data about the entity. // For example Events may have start/end dates, Places may have coordinates. // The value in the export fields must be rewritten as triple predicates, this // is what we're going to do here. wl_write_log('wl_push_entity_post_to_redlink : checking if entity has export fields [ type :: ' . var_export($main_type, true) . ' ]'); if (isset($main_type['custom_fields'])) { foreach ($main_type['custom_fields'] as $field => $settings) { wl_write_log("wl_push_entity_post_to_redlink : entity has export fields"); $predicate = wordlift_esc_sparql($settings['predicate']); if (!isset($settings['export_type']) || empty($settings['export_type'])) { $type = null; } else { $type = $settings['export_type']; } // add the delete statement for later execution. $delete_stmt .= "DELETE { <{$uri_e}> <{$predicate}> ?o } WHERE { <{$uri_e}> <{$predicate}> ?o };\n"; foreach (get_post_meta($entity_post->ID, $field) as $value) { $sparql .= " <{$uri_e}> <{$predicate}> "; // Establish triple's <object> type if (is_null($type)) { // No type $sparql .= '<' . wl_sparql_escape_uri($value) . '>'; } else { $sparql .= '"' . wordlift_esc_sparql($value) . '"^^'; if (substr($type, 0, 4) == 'http') { // Type is defined by a raw uri (es. http://schema.org/PostalAddress) $sparql .= '<' . wl_sparql_escape_uri($type) . '>'; } else { // Type is defined in another way (es. xsd:double) $sparql .= wordlift_esc_sparql($type); } } $sparql .= " . \n"; } } } } // Get the entity types. $type_uris = wl_get_entity_rdf_types($entity_post->ID); // Support type are only schema.org ones: it could by null foreach ($type_uris as $type_uri) { $type_uri = wl_sparql_escape_uri($type_uri); $sparql .= "<{$uri_e}> a <{$type_uri}> . \n"; } // get related entities. $related_entities_ids = wl_core_get_related_entitY_ids($entity_post->ID); if (is_array($related_entities_ids)) { foreach ($related_entities_ids as $entity_post_id) { $related_entity_uri = wl_sparql_escape_uri(wl_get_entity_uri($entity_post_id)); // create a two-way relationship. $sparql .= " <{$uri_e}> dct:relation <{$related_entity_uri}> . \n"; $sparql .= " <{$related_entity_uri}> dct:relation <{$uri_e}> . \n"; } } // Add SPARQL stmts to write the schema:image. $sparql .= wl_get_sparql_images($uri, $entity_post->ID); $query = rl_sparql_prefixes() . <<<EOF {$delete_stmt} DELETE { <{$uri_e}> rdfs:label ?o } WHERE { <{$uri_e}> rdfs:label ?o }; DELETE { <{$uri_e}> owl:sameAs ?o . } WHERE { <{$uri_e}> owl:sameAs ?o . }; DELETE { <{$uri_e}> schema:description ?o . } WHERE { <{$uri_e}> schema:description ?o . }; DELETE { <{$uri_e}> schema:url ?o . } WHERE { <{$uri_e}> schema:url ?o . }; DELETE { <{$uri_e}> a ?o . } WHERE { <{$uri_e}> a ?o . }; DELETE { <{$uri_e}> dct:relation ?o . } WHERE { <{$uri_e}> dct:relation ?o . }; DELETE { <{$uri_e}> schema:image ?o . } WHERE { <{$uri_e}> schema:image ?o . }; DELETE { <{$uri_e}> geo:lat ?o . } WHERE { <{$uri_e}> geo:lat ?o . }; DELETE { <{$uri_e}> geo:long ?o . } WHERE { <{$uri_e}> geo:long ?o . }; INSERT DATA { {$sparql} }; EOF; rl_execute_sparql_update_query($query); }
/** * Called when a user is updated. * * @param int $user_id The user ID. * * @return true if successful otherwise false. */ function wl_update_user_profile($user_id) { wl_write_log("wl_update_user_profile [ user id :: {$user_id} ]"); // Get the site language setting. $language = wl_configuration_get_site_language(); // Get the user. $user = get_userdata($user_id); // Get the user URI. $uri = wl_sparql_escape_uri(wl_get_user_uri($user_id)); // Get the first/last name and the posts URL. $first_name = wordlift_esc_sparql($user->first_name); $last_name = wordlift_esc_sparql($user->last_name); $posts_url = wl_sparql_escape_uri(get_author_posts_url($user_id)); $query = rl_sparql_prefixes(); $query .= <<<EOF DELETE { <{$uri}> schema:givenName ?o } WHERE { <{$uri}> schema:givenName ?o }; DELETE { <{$uri}> schema:familyName ?o } WHERE { <{$uri}> schema:familyName ?o }; DELETE { <{$uri}> schema:url ?o } WHERE { <{$uri}> schema:url ?o }; INSERT DATA { <{$uri}> schema:givenName '{$first_name}'@{$language} . <{$uri}> schema:familyName '{$last_name}'@{$language} . <{$uri}> schema:url <{$posts_url}> . } EOF; // Execute the query. return rl_execute_sparql_update_query($query); }