function testColorCodingOnFrontEnd() { $entity_id = wl_create_post('', 'entity-1', 'Entity 1', 'publish', 'entity'); $entity_uri = wl_get_entity_uri($entity_id); wl_set_entity_main_type($entity_id, 'http://schema.org/Event'); # Create a test entity post. $content = <<<EOF This post is referencing the sample <span id="urn:enhancement-4f0e0fbc-e981-7852-9521-f4718eafa13f" class="textannotation highlight wl-event" itemid="{$entity_uri}">Entity 1</span>. EOF; $post_id = wl_create_post($content, 'post-1', 'Post 1', 'publish', 'post'); $post = get_post($post_id); // Disable front-end color coding. wl_configuration_set_enable_color_coding(false); $this->assertNotContains('class="wl-event"', wl_content_embed_item_microdata($post->post_content, $entity_uri)); // Enable front-end color coding. wl_configuration_set_enable_color_coding(true); $this->assertContains('class="wl-event"', wl_content_embed_item_microdata($post->post_content, $entity_uri)); }
/** * Fills up the microdata_template with entity's values. * * @param string $entity_id An entity ID. * @param string $entity_type Entity type stracture. * @param integer $recursion_level Recursion depth level in microdata compiling. Recursion depth limit is defined by WL_MAX_NUM_RECURSIONS_WHEN_PRINTING_MICRODATA constant. * * @return string The content with embedded microdata. */ function wl_content_embed_compile_microdata_template($entity_id, $entity_type, $recursion_level = 0) { wl_write_log("[ entity id :: {$entity_id} ][ entity type :: " . var_export($entity_type, true) . " ][ recursion level :: {$recursion_level} ]"); $regex = '/{{(.*?)}}/'; $matches = array(); if (null === $entity_type) { return ''; } $template = $entity_type['microdata_template']; // Return empty string if template fields have not been found. if (false === preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { return ''; } foreach ($matches as $match) { $placeholder = $match[0]; $field_name = $match[1]; // Get property value. $meta_collection = wl_schema_get_value($entity_id, $field_name); // If no value is given, just remove the placeholder from the template if (null == $meta_collection) { $template = str_replace($placeholder, '', $template); continue; } // What kind of value is it? // TODO: Performance issue here: meta type retrieving should be centralized $expected_type = wl_get_meta_type($field_name); foreach ($meta_collection as $field_value) { if (WL_DATA_TYPE_URI == $expected_type) { // If is a numeric value we assume it is an ID referencing for an internal entity. if (is_numeric($field_value)) { // Found id, get uri. $field_value = wl_get_entity_uri($field_value); } // Just if the linked entity does exist I can go further with template compiling $nested_entity = wl_get_entity_post_by_uri($field_value); if (!is_null($nested_entity)) { $content = '<span itemid="' . esc_attr($field_value) . '">' . $nested_entity->post_title . '</span>'; $compiled_template = wl_content_embed_item_microdata($content, $field_value, $field_name, ++$recursion_level); $template = str_replace($placeholder, $compiled_template, $template); } else { $template = str_replace($placeholder, '', $template); } continue; } // Standard condition: field containing a raw value $value = '<span itemprop="' . esc_attr($field_name) . '" content="' . esc_attr($field_value) . '"></span>'; $template = str_replace($placeholder, $value, $template); } } return $template; }
/** * Fills up the microdata_template with entity's values. * * @param string $entity_id An entity ID. * @param string $entity_type Entity type structure. * @param integer $recursion_level Recursion depth level in microdata compiling. Recursion depth limit is defined by WL_MAX_NUM_RECURSIONS_WHEN_PRINTING_MICRODATA constant. * * @return string The content with embedded microdata. */ function wl_content_embed_compile_microdata_template($entity_id, $entity_type, $recursion_level = 0) { global $wl_logger; if (WP_DEBUG) { $wl_logger->trace("Embedding microdata [ entity id :: {$entity_id} ][ entity type :: " . var_export($entity_type, true) . " ][ recursion level :: {$recursion_level} ]"); } $regex = '/{{(.*?)}}/'; $matches = array(); if (null === $entity_type) { return ''; } $template = $entity_type['microdata_template']; // Return empty string if template fields have not been found. if (false === preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { return ''; } foreach ($matches as $match) { $placeholder = $match[0]; $field_name = $match[1]; // Get property value. $meta_collection = wl_schema_get_value($entity_id, $field_name); // If no value is given, just remove the placeholder from the template if (null == $meta_collection) { $template = str_replace($placeholder, '', $template); continue; } // What kind of value is it? // TODO: Performance issue here: meta type retrieving should be centralized $expected_type = wl_get_meta_type($field_name); if (WP_DEBUG) { $wl_logger->trace("Embedding microdata [ placeholder :: {$placeholder} ][ field name :: {$field_name} ][ meta collection :: " . (is_array($meta_collection) ? var_export($meta_collection, true) : $meta_collection) . " ][ expected type :: {$expected_type} ]"); } foreach ($meta_collection as $field_value) { // Quick and dirty patch for #163: // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), // - check that $field_value is actually a number, // - check that the referenced post is published. // OR // - if the value is empty then we don't display it. if (Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric($field_value) && 'publish' !== ($post_status = get_post_status($field_value)) || empty($field_value)) { if (WP_DEBUG) { $wl_logger->trace("Microdata refers to a non-published post [ field value :: {$field_value} ][ post status :: {$post_status} ]"); } // Remove the placeholder. $template = str_replace($placeholder, '', $template); continue; } if (Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type) { // If is a numeric value we assume it is an ID referencing for an internal entity. if (is_numeric($field_value)) { // Found id, get uri. $field_value = wl_get_entity_uri($field_value); } // Just if the linked entity does exist I can go further with template compiling $nested_entity = wl_get_entity_post_by_uri($field_value); if (!is_null($nested_entity)) { $content = '<span itemid="' . esc_attr($field_value) . '">' . $nested_entity->post_title . '</span>'; $compiled_template = wl_content_embed_item_microdata($content, $field_value, $field_name, ++$recursion_level); $template = str_replace($placeholder, $compiled_template, $template); } else { $template = str_replace($placeholder, '', $template); } continue; } // Standard condition: field containing a raw value // For non visible test, schema.org dictates to use the *meta* tag. // see http://schema.org/docs/gs.html#advanced_missing $value = '<meta itemprop="' . esc_attr($field_name) . '" content="' . esc_attr($field_value) . '" />'; $template = str_replace($placeholder, $value, $template); } } return $template; }