@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @param string $uri Uri of the entity to search in the post content. |
| 11 | 11 | */ |
| 12 | 12 | function wl_content_embed_build_regex_from_uri( $uri ) { |
| 13 | - return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 13 | + return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | */ |
| 23 | 23 | function wl_content_embed_microdata( $content ) { |
| 24 | 24 | |
| 25 | - // Apply microdata only to single pages. |
|
| 26 | - /*if ( ! is_single() ) { |
|
| 25 | + // Apply microdata only to single pages. |
|
| 26 | + /*if ( ! is_single() ) { |
|
| 27 | 27 | wl_write_log( "wl_content_embed_microdata : is not single" ); |
| 28 | 28 | |
| 29 | 29 | return $content; |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | global $post; |
| 33 | 33 | */ |
| 34 | - return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 34 | + return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -44,31 +44,31 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | function _wl_content_embed_microdata( $post_id, $content ) { |
| 46 | 46 | |
| 47 | - // If it is an entity, add its own microdata to the content. |
|
| 48 | - if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | - $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | - $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 51 | - } |
|
| 47 | + // If it is an entity, add its own microdata to the content. |
|
| 48 | + if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | + $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | + $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - // Now search in the text entity mentions |
|
| 54 | - $regex = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 55 | - $matches = array(); |
|
| 53 | + // Now search in the text entity mentions |
|
| 54 | + $regex = '/<(\\w+)[^<]* itemid=\"([^"]+)\"[^>]*>([^<]*)<\\/\\1>/i'; |
|
| 55 | + $matches = array(); |
|
| 56 | 56 | |
| 57 | - // Return the content if not item IDs have been found. |
|
| 58 | - if ( FALSE === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 59 | - return $content; |
|
| 60 | - } |
|
| 57 | + // Return the content if not item IDs have been found. |
|
| 58 | + if ( FALSE === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 59 | + return $content; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
|
| 63 | - foreach ( $matches as $match ) { |
|
| 64 | - $item_id = $match[2]; |
|
| 62 | + // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
|
| 63 | + foreach ( $matches as $match ) { |
|
| 64 | + $item_id = $match[2]; |
|
| 65 | 65 | |
| 66 | - // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
|
| 66 | + // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
|
| 67 | 67 | |
| 68 | - $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 69 | - } |
|
| 68 | + $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $content; |
|
| 71 | + return $content; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -82,76 +82,76 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | function wl_content_embed_item_microdata( $content, $uri, $itemprop = NULL, $recursion_level = 0 ) { |
| 84 | 84 | |
| 85 | - if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | - wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 85 | + if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | + wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 87 | 87 | |
| 88 | - return ''; |
|
| 89 | - } |
|
| 88 | + return ''; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - $post = Wordlift_Entity_Service::get_instance() |
|
| 92 | - ->get_entity_post_by_uri( $uri ); |
|
| 91 | + $post = Wordlift_Entity_Service::get_instance() |
|
| 92 | + ->get_entity_post_by_uri( $uri ); |
|
| 93 | 93 | |
| 94 | - // Entity not found or not published. Delete <span> tags but leave their content on page. |
|
| 95 | - if ( NULL === $post || $post->post_status !== 'publish' ) { |
|
| 94 | + // Entity not found or not published. Delete <span> tags but leave their content on page. |
|
| 95 | + if ( NULL === $post || $post->post_status !== 'publish' ) { |
|
| 96 | 96 | |
| 97 | - // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
|
| 97 | + // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
|
| 98 | 98 | |
| 99 | - // Replace the original tagging with the new tagging. |
|
| 100 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 101 | - $content = preg_replace( $regex, '$2', $content ); |
|
| 99 | + // Replace the original tagging with the new tagging. |
|
| 100 | + $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 101 | + $content = preg_replace( $regex, '$2', $content ); |
|
| 102 | 102 | |
| 103 | - return $content; |
|
| 104 | - } |
|
| 103 | + return $content; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - // Get the entity URI and its escaped version for the regex. |
|
| 107 | - $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 108 | - // Get the main type. |
|
| 109 | - $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 106 | + // Get the entity URI and its escaped version for the regex. |
|
| 107 | + $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 108 | + // Get the main type. |
|
| 109 | + $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 110 | 110 | |
| 111 | - // Set the item type if available. |
|
| 112 | - $item_type = ( NULL === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 111 | + // Set the item type if available. |
|
| 112 | + $item_type = ( NULL === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 113 | 113 | |
| 114 | - // Define attribute itemprop if this entity is nested. |
|
| 115 | - if ( ! is_null( $itemprop ) ) { |
|
| 116 | - $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 117 | - } |
|
| 114 | + // Define attribute itemprop if this entity is nested. |
|
| 115 | + if ( ! is_null( $itemprop ) ) { |
|
| 116 | + $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - // Get additional properties (this may imply a recursion of this method on a sub-entity). |
|
| 120 | - $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 119 | + // Get additional properties (this may imply a recursion of this method on a sub-entity). |
|
| 120 | + $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 121 | 121 | |
| 122 | - $same_as = ''; |
|
| 123 | - // Get the array of sameAs uris. |
|
| 124 | - $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 125 | - // Prepare the sameAs fragment. |
|
| 126 | - foreach ( $same_as_uris as $same_as_uri ) { |
|
| 127 | - $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
|
| 128 | - } |
|
| 122 | + $same_as = ''; |
|
| 123 | + // Get the array of sameAs uris. |
|
| 124 | + $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 125 | + // Prepare the sameAs fragment. |
|
| 126 | + foreach ( $same_as_uris as $same_as_uri ) { |
|
| 127 | + $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - // Get the entity URL. |
|
| 131 | - $permalink = get_permalink( $post->ID ); |
|
| 132 | - $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 130 | + // Get the entity URL. |
|
| 131 | + $permalink = get_permalink( $post->ID ); |
|
| 132 | + $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | - // If entity is nested, we do not show a link, but a hidden meta. |
|
| 136 | - // See https://github.com/insideout10/wordlift-plugin/issues/348 |
|
| 137 | - $name = ! is_null( $itemprop ) |
|
| 138 | - ? "<meta itemprop='name' content='$post->post_title' /></$1>" |
|
| 139 | - : '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>'; |
|
| 135 | + // If entity is nested, we do not show a link, but a hidden meta. |
|
| 136 | + // See https://github.com/insideout10/wordlift-plugin/issues/348 |
|
| 137 | + $name = ! is_null( $itemprop ) |
|
| 138 | + ? "<meta itemprop='name' content='$post->post_title' /></$1>" |
|
| 139 | + : '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>'; |
|
| 140 | 140 | |
| 141 | - // Replace the original tagging with the new tagging. |
|
| 142 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 143 | - $content = preg_replace( $regex, |
|
| 144 | - '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 145 | - . $same_as |
|
| 146 | - . $additional_properties |
|
| 147 | - . $url |
|
| 148 | - . $name, |
|
| 149 | - $content |
|
| 150 | - ); |
|
| 141 | + // Replace the original tagging with the new tagging. |
|
| 142 | + $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 143 | + $content = preg_replace( $regex, |
|
| 144 | + '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 145 | + . $same_as |
|
| 146 | + . $additional_properties |
|
| 147 | + . $url |
|
| 148 | + . $name, |
|
| 149 | + $content |
|
| 150 | + ); |
|
| 151 | 151 | |
| 152 | - // wl_write_log( "wl_content_embed_item_microdata [ uri :: $uri ][ regex :: $regex ]" ); |
|
| 152 | + // wl_write_log( "wl_content_embed_item_microdata [ uri :: $uri ][ regex :: $regex ]" ); |
|
| 153 | 153 | |
| 154 | - return $content; |
|
| 154 | + return $content; |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | add_filter( 'the_content', 'wl_content_embed_microdata' ); |
@@ -166,94 +166,94 @@ discard block |
||
| 166 | 166 | * @return string The content with embedded microdata. |
| 167 | 167 | */ |
| 168 | 168 | function wl_content_embed_compile_microdata_template( $entity_id, $entity_type, $recursion_level = 0 ) { |
| 169 | - global $wl_logger; |
|
| 170 | - |
|
| 171 | - if ( WP_DEBUG ) { |
|
| 172 | - $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, TRUE ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $regex = '/{{(.*?)}}/'; |
|
| 176 | - $matches = array(); |
|
| 177 | - |
|
| 178 | - if ( NULL === $entity_type ) { |
|
| 179 | - return ''; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $template = $entity_type['microdata_template']; |
|
| 183 | - // Return empty string if template fields have not been found. |
|
| 184 | - if ( FALSE === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 185 | - return ''; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - foreach ( $matches as $match ) { |
|
| 189 | - |
|
| 190 | - $placeholder = $match[0]; |
|
| 191 | - $field_name = $match[1]; |
|
| 192 | - |
|
| 193 | - // Get property value. |
|
| 194 | - $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 195 | - |
|
| 196 | - // If no value is given, just remove the placeholder from the template |
|
| 197 | - if ( NULL == $meta_collection ) { |
|
| 198 | - $template = str_replace( $placeholder, '', $template ); |
|
| 199 | - continue; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - // What kind of value is it? |
|
| 203 | - // TODO: Performance issue here: meta type retrieving should be centralized |
|
| 204 | - $expected_type = wl_get_meta_type( $field_name ); |
|
| 205 | - |
|
| 206 | - if ( WP_DEBUG ) { |
|
| 207 | - $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 ]" ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - foreach ( $meta_collection as $field_value ) { |
|
| 211 | - |
|
| 212 | - // Quick and dirty patch for #163: |
|
| 213 | - // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
|
| 214 | - // - check that $field_value is actually a number, |
|
| 215 | - // - check that the referenced post is published. |
|
| 216 | - // OR |
|
| 217 | - // - if the value is empty then we don't display it. |
|
| 218 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 219 | - || empty( $field_value ) |
|
| 220 | - ) { |
|
| 221 | - |
|
| 222 | - if ( WP_DEBUG ) { |
|
| 223 | - $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Remove the placeholder. |
|
| 227 | - $template = str_replace( $placeholder, '', $template ); |
|
| 228 | - continue; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 232 | - // If is a numeric value we assume it is an ID referencing for an internal entity. |
|
| 233 | - if ( is_numeric( $field_value ) ) { |
|
| 234 | - // Found id, get uri. |
|
| 235 | - $field_value = wl_get_entity_uri( $field_value ); |
|
| 236 | - } |
|
| 237 | - // Just if the linked entity does exist I can go further with template compiling |
|
| 238 | - $nested_entity = Wordlift_Entity_Service::get_instance() |
|
| 239 | - ->get_entity_post_by_uri( $field_value ); |
|
| 240 | - if ( ! is_null( $nested_entity ) ) { |
|
| 241 | - $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 242 | - $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 243 | - $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 244 | - } else { |
|
| 245 | - $template = str_replace( $placeholder, '', $template ); |
|
| 246 | - } |
|
| 247 | - continue; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - // Standard condition: field containing a raw value |
|
| 251 | - // For non visible test, schema.org dictates to use the *meta* tag. |
|
| 252 | - // see http://schema.org/docs/gs.html#advanced_missing |
|
| 253 | - $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 254 | - $template = str_replace( $placeholder, $value, $template ); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - return $template; |
|
| 169 | + global $wl_logger; |
|
| 170 | + |
|
| 171 | + if ( WP_DEBUG ) { |
|
| 172 | + $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, TRUE ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $regex = '/{{(.*?)}}/'; |
|
| 176 | + $matches = array(); |
|
| 177 | + |
|
| 178 | + if ( NULL === $entity_type ) { |
|
| 179 | + return ''; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $template = $entity_type['microdata_template']; |
|
| 183 | + // Return empty string if template fields have not been found. |
|
| 184 | + if ( FALSE === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 185 | + return ''; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + foreach ( $matches as $match ) { |
|
| 189 | + |
|
| 190 | + $placeholder = $match[0]; |
|
| 191 | + $field_name = $match[1]; |
|
| 192 | + |
|
| 193 | + // Get property value. |
|
| 194 | + $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 195 | + |
|
| 196 | + // If no value is given, just remove the placeholder from the template |
|
| 197 | + if ( NULL == $meta_collection ) { |
|
| 198 | + $template = str_replace( $placeholder, '', $template ); |
|
| 199 | + continue; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + // What kind of value is it? |
|
| 203 | + // TODO: Performance issue here: meta type retrieving should be centralized |
|
| 204 | + $expected_type = wl_get_meta_type( $field_name ); |
|
| 205 | + |
|
| 206 | + if ( WP_DEBUG ) { |
|
| 207 | + $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 ]" ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + foreach ( $meta_collection as $field_value ) { |
|
| 211 | + |
|
| 212 | + // Quick and dirty patch for #163: |
|
| 213 | + // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
|
| 214 | + // - check that $field_value is actually a number, |
|
| 215 | + // - check that the referenced post is published. |
|
| 216 | + // OR |
|
| 217 | + // - if the value is empty then we don't display it. |
|
| 218 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 219 | + || empty( $field_value ) |
|
| 220 | + ) { |
|
| 221 | + |
|
| 222 | + if ( WP_DEBUG ) { |
|
| 223 | + $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Remove the placeholder. |
|
| 227 | + $template = str_replace( $placeholder, '', $template ); |
|
| 228 | + continue; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 232 | + // If is a numeric value we assume it is an ID referencing for an internal entity. |
|
| 233 | + if ( is_numeric( $field_value ) ) { |
|
| 234 | + // Found id, get uri. |
|
| 235 | + $field_value = wl_get_entity_uri( $field_value ); |
|
| 236 | + } |
|
| 237 | + // Just if the linked entity does exist I can go further with template compiling |
|
| 238 | + $nested_entity = Wordlift_Entity_Service::get_instance() |
|
| 239 | + ->get_entity_post_by_uri( $field_value ); |
|
| 240 | + if ( ! is_null( $nested_entity ) ) { |
|
| 241 | + $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 242 | + $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 243 | + $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 244 | + } else { |
|
| 245 | + $template = str_replace( $placeholder, '', $template ); |
|
| 246 | + } |
|
| 247 | + continue; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + // Standard condition: field containing a raw value |
|
| 251 | + // For non visible test, schema.org dictates to use the *meta* tag. |
|
| 252 | + // see http://schema.org/docs/gs.html#advanced_missing |
|
| 253 | + $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 254 | + $template = str_replace( $placeholder, $value, $template ); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + return $template; |
|
| 259 | 259 | } |
| 260 | 260 | \ No newline at end of file |
@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | * |
| 10 | 10 | * @param string $uri Uri of the entity to search in the post content. |
| 11 | 11 | */ |
| 12 | -function wl_content_embed_build_regex_from_uri( $uri ) { |
|
| 13 | - return '|<(\\w+)[^<]* itemid=\"' . esc_attr( $uri ) . '\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 12 | +function wl_content_embed_build_regex_from_uri($uri) { |
|
| 13 | + return '|<(\\w+)[^<]* itemid=\"'.esc_attr($uri).'\"[^>]*>([^<]*)<\\/\\1>|i'; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * |
| 21 | 21 | * @return string The updated post content. |
| 22 | 22 | */ |
| 23 | -function wl_content_embed_microdata( $content ) { |
|
| 23 | +function wl_content_embed_microdata($content) { |
|
| 24 | 24 | |
| 25 | 25 | // Apply microdata only to single pages. |
| 26 | 26 | /*if ( ! is_single() ) { |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | global $post; |
| 33 | 33 | */ |
| 34 | - return _wl_content_embed_microdata( get_the_ID(), $content ); |
|
| 34 | + return _wl_content_embed_microdata(get_the_ID(), $content); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -42,12 +42,12 @@ discard block |
||
| 42 | 42 | * |
| 43 | 43 | * @return string The updated post content. |
| 44 | 44 | */ |
| 45 | -function _wl_content_embed_microdata( $post_id, $content ) { |
|
| 45 | +function _wl_content_embed_microdata($post_id, $content) { |
|
| 46 | 46 | |
| 47 | 47 | // If it is an entity, add its own microdata to the content. |
| 48 | - if ( get_post_type( $post_id ) == Wordlift_Entity_Service::TYPE_NAME ) { |
|
| 49 | - $own_uri = wl_get_entity_uri( $post_id ); |
|
| 50 | - $content .= '<span itemid="' . $own_uri . '"></span>'; |
|
| 48 | + if (get_post_type($post_id) == Wordlift_Entity_Service::TYPE_NAME) { |
|
| 49 | + $own_uri = wl_get_entity_uri($post_id); |
|
| 50 | + $content .= '<span itemid="'.$own_uri.'"></span>'; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Now search in the text entity mentions |
@@ -55,17 +55,17 @@ discard block |
||
| 55 | 55 | $matches = array(); |
| 56 | 56 | |
| 57 | 57 | // Return the content if not item IDs have been found. |
| 58 | - if ( FALSE === preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
|
| 58 | + if (FALSE === preg_match_all($regex, $content, $matches, PREG_SET_ORDER)) { |
|
| 59 | 59 | return $content; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | // TODO: Retrieve here just one time entities type structure to avoid multiple queries. |
| 63 | - foreach ( $matches as $match ) { |
|
| 63 | + foreach ($matches as $match) { |
|
| 64 | 64 | $item_id = $match[2]; |
| 65 | 65 | |
| 66 | 66 | // wl_write_log( "_wl_content_embed_microdata [ item ID :: $item_id ]" ); |
| 67 | 67 | |
| 68 | - $content = wl_content_embed_item_microdata( $content, $item_id ); |
|
| 68 | + $content = wl_content_embed_item_microdata($content, $item_id); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | return $content; |
@@ -80,68 +80,68 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return string The content with embedded microdata. |
| 82 | 82 | */ |
| 83 | -function wl_content_embed_item_microdata( $content, $uri, $itemprop = NULL, $recursion_level = 0 ) { |
|
| 83 | +function wl_content_embed_item_microdata($content, $uri, $itemprop = NULL, $recursion_level = 0) { |
|
| 84 | 84 | |
| 85 | - if ( $recursion_level > wl_config_get_recursion_depth() ) { |
|
| 86 | - wl_write_log( "recursion depth limit exceeded [ level :: $recursion_level ][ max :: " . wl_config_get_recursion_depth() . " ]" ); |
|
| 85 | + if ($recursion_level > wl_config_get_recursion_depth()) { |
|
| 86 | + wl_write_log("recursion depth limit exceeded [ level :: $recursion_level ][ max :: ".wl_config_get_recursion_depth()." ]"); |
|
| 87 | 87 | |
| 88 | 88 | return ''; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | $post = Wordlift_Entity_Service::get_instance() |
| 92 | - ->get_entity_post_by_uri( $uri ); |
|
| 92 | + ->get_entity_post_by_uri($uri); |
|
| 93 | 93 | |
| 94 | 94 | // Entity not found or not published. Delete <span> tags but leave their content on page. |
| 95 | - if ( NULL === $post || $post->post_status !== 'publish' ) { |
|
| 95 | + if (NULL === $post || $post->post_status !== 'publish') { |
|
| 96 | 96 | |
| 97 | 97 | // wl_write_log( "wl_content_embed_item_microdata : entity not found or not published [ uri :: $uri ]" ); |
| 98 | 98 | |
| 99 | 99 | // Replace the original tagging with the new tagging. |
| 100 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 101 | - $content = preg_replace( $regex, '$2', $content ); |
|
| 100 | + $regex = wl_content_embed_build_regex_from_uri($uri); |
|
| 101 | + $content = preg_replace($regex, '$2', $content); |
|
| 102 | 102 | |
| 103 | 103 | return $content; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Get the entity URI and its escaped version for the regex. |
| 107 | - $entity_uri = wl_get_entity_uri( $post->ID ); |
|
| 107 | + $entity_uri = wl_get_entity_uri($post->ID); |
|
| 108 | 108 | // Get the main type. |
| 109 | - $main_type = wl_entity_type_taxonomy_get_type( $post->ID ); |
|
| 109 | + $main_type = wl_entity_type_taxonomy_get_type($post->ID); |
|
| 110 | 110 | |
| 111 | 111 | // Set the item type if available. |
| 112 | - $item_type = ( NULL === $main_type ? '' : ' itemtype="' . esc_attr( $main_type['uri'] ) . '"' ); |
|
| 112 | + $item_type = (NULL === $main_type ? '' : ' itemtype="'.esc_attr($main_type['uri']).'"'); |
|
| 113 | 113 | |
| 114 | 114 | // Define attribute itemprop if this entity is nested. |
| 115 | - if ( ! is_null( $itemprop ) ) { |
|
| 116 | - $itemprop = ' itemprop="' . $itemprop . '"'; |
|
| 115 | + if ( ! is_null($itemprop)) { |
|
| 116 | + $itemprop = ' itemprop="'.$itemprop.'"'; |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Get additional properties (this may imply a recursion of this method on a sub-entity). |
| 120 | - $additional_properties = wl_content_embed_compile_microdata_template( $post->ID, $main_type, $recursion_level ); |
|
| 120 | + $additional_properties = wl_content_embed_compile_microdata_template($post->ID, $main_type, $recursion_level); |
|
| 121 | 121 | |
| 122 | 122 | $same_as = ''; |
| 123 | 123 | // Get the array of sameAs uris. |
| 124 | - $same_as_uris = wl_schema_get_value( $post->ID, 'sameAs' ); |
|
| 124 | + $same_as_uris = wl_schema_get_value($post->ID, 'sameAs'); |
|
| 125 | 125 | // Prepare the sameAs fragment. |
| 126 | - foreach ( $same_as_uris as $same_as_uri ) { |
|
| 126 | + foreach ($same_as_uris as $same_as_uri) { |
|
| 127 | 127 | $same_as .= "<link itemprop=\"sameAs\" href=\"$same_as_uri\">"; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Get the entity URL. |
| 131 | - $permalink = get_permalink( $post->ID ); |
|
| 132 | - $url = '<link itemprop="url" href="' . $permalink . '" />'; |
|
| 131 | + $permalink = get_permalink($post->ID); |
|
| 132 | + $url = '<link itemprop="url" href="'.$permalink.'" />'; |
|
| 133 | 133 | |
| 134 | 134 | |
| 135 | 135 | // If entity is nested, we do not show a link, but a hidden meta. |
| 136 | 136 | // See https://github.com/insideout10/wordlift-plugin/issues/348 |
| 137 | - $name = ! is_null( $itemprop ) |
|
| 137 | + $name = ! is_null($itemprop) |
|
| 138 | 138 | ? "<meta itemprop='name' content='$post->post_title' /></$1>" |
| 139 | - : '<a class="wl-entity-page-link" href="' . $permalink . '" itemprop="name" content="' . $post->post_title . '">' . ( is_null( $itemprop ) ? '$2' : '' ) . '</a></$1>'; |
|
| 139 | + : '<a class="wl-entity-page-link" href="'.$permalink.'" itemprop="name" content="'.$post->post_title.'">'.(is_null($itemprop) ? '$2' : '').'</a></$1>'; |
|
| 140 | 140 | |
| 141 | 141 | // Replace the original tagging with the new tagging. |
| 142 | - $regex = wl_content_embed_build_regex_from_uri( $uri ); |
|
| 143 | - $content = preg_replace( $regex, |
|
| 144 | - '<$1' . $itemprop . ' itemscope' . $item_type . ' itemid="' . esc_attr( $entity_uri ) . '">' |
|
| 142 | + $regex = wl_content_embed_build_regex_from_uri($uri); |
|
| 143 | + $content = preg_replace($regex, |
|
| 144 | + '<$1'.$itemprop.' itemscope'.$item_type.' itemid="'.esc_attr($entity_uri).'">' |
|
| 145 | 145 | . $same_as |
| 146 | 146 | . $additional_properties |
| 147 | 147 | . $url |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | return $content; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | -add_filter( 'the_content', 'wl_content_embed_microdata' ); |
|
| 157 | +add_filter('the_content', 'wl_content_embed_microdata'); |
|
| 158 | 158 | |
| 159 | 159 | /** |
| 160 | 160 | * Fills up the microdata_template with entity's values. |
@@ -165,49 +165,49 @@ discard block |
||
| 165 | 165 | * |
| 166 | 166 | * @return string The content with embedded microdata. |
| 167 | 167 | */ |
| 168 | -function wl_content_embed_compile_microdata_template( $entity_id, $entity_type, $recursion_level = 0 ) { |
|
| 168 | +function wl_content_embed_compile_microdata_template($entity_id, $entity_type, $recursion_level = 0) { |
|
| 169 | 169 | global $wl_logger; |
| 170 | 170 | |
| 171 | - if ( WP_DEBUG ) { |
|
| 172 | - $wl_logger->trace( "Embedding microdata [ entity id :: $entity_id ][ entity type :: " . var_export( $entity_type, TRUE ) . " ][ recursion level :: $recursion_level ]" ); |
|
| 171 | + if (WP_DEBUG) { |
|
| 172 | + $wl_logger->trace("Embedding microdata [ entity id :: $entity_id ][ entity type :: ".var_export($entity_type, TRUE)." ][ recursion level :: $recursion_level ]"); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | $regex = '/{{(.*?)}}/'; |
| 176 | 176 | $matches = array(); |
| 177 | 177 | |
| 178 | - if ( NULL === $entity_type ) { |
|
| 178 | + if (NULL === $entity_type) { |
|
| 179 | 179 | return ''; |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | $template = $entity_type['microdata_template']; |
| 183 | 183 | // Return empty string if template fields have not been found. |
| 184 | - if ( FALSE === preg_match_all( $regex, $template, $matches, PREG_SET_ORDER ) ) { |
|
| 184 | + if (FALSE === preg_match_all($regex, $template, $matches, PREG_SET_ORDER)) { |
|
| 185 | 185 | return ''; |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | - foreach ( $matches as $match ) { |
|
| 188 | + foreach ($matches as $match) { |
|
| 189 | 189 | |
| 190 | 190 | $placeholder = $match[0]; |
| 191 | 191 | $field_name = $match[1]; |
| 192 | 192 | |
| 193 | 193 | // Get property value. |
| 194 | - $meta_collection = wl_schema_get_value( $entity_id, $field_name ); |
|
| 194 | + $meta_collection = wl_schema_get_value($entity_id, $field_name); |
|
| 195 | 195 | |
| 196 | 196 | // If no value is given, just remove the placeholder from the template |
| 197 | - if ( NULL == $meta_collection ) { |
|
| 198 | - $template = str_replace( $placeholder, '', $template ); |
|
| 197 | + if (NULL == $meta_collection) { |
|
| 198 | + $template = str_replace($placeholder, '', $template); |
|
| 199 | 199 | continue; |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | // What kind of value is it? |
| 203 | 203 | // TODO: Performance issue here: meta type retrieving should be centralized |
| 204 | - $expected_type = wl_get_meta_type( $field_name ); |
|
| 204 | + $expected_type = wl_get_meta_type($field_name); |
|
| 205 | 205 | |
| 206 | - if ( WP_DEBUG ) { |
|
| 207 | - $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 ]" ); |
|
| 206 | + if (WP_DEBUG) { |
|
| 207 | + $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 ]"); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - foreach ( $meta_collection as $field_value ) { |
|
| 210 | + foreach ($meta_collection as $field_value) { |
|
| 211 | 211 | |
| 212 | 212 | // Quick and dirty patch for #163: |
| 213 | 213 | // - only apply to URIs, i.e. to properties pointing to another post ( $field_value should be a post ID ), |
@@ -215,34 +215,34 @@ discard block |
||
| 215 | 215 | // - check that the referenced post is published. |
| 216 | 216 | // OR |
| 217 | 217 | // - if the value is empty then we don't display it. |
| 218 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric( $field_value ) && 'publish' !== ( $post_status = get_post_status( $field_value ) ) |
|
| 219 | - || empty( $field_value ) |
|
| 218 | + if (Wordlift_Schema_Service::DATA_TYPE_URI === $expected_type && is_numeric($field_value) && 'publish' !== ($post_status = get_post_status($field_value)) |
|
| 219 | + || empty($field_value) |
|
| 220 | 220 | ) { |
| 221 | 221 | |
| 222 | - if ( WP_DEBUG ) { |
|
| 223 | - $wl_logger->trace( "Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]" ); |
|
| 222 | + if (WP_DEBUG) { |
|
| 223 | + $wl_logger->trace("Microdata refers to a non-published post [ field value :: $field_value ][ post status :: $post_status ]"); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | // Remove the placeholder. |
| 227 | - $template = str_replace( $placeholder, '', $template ); |
|
| 227 | + $template = str_replace($placeholder, '', $template); |
|
| 228 | 228 | continue; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | - if ( Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type ) { |
|
| 231 | + if (Wordlift_Schema_Service::DATA_TYPE_URI == $expected_type) { |
|
| 232 | 232 | // If is a numeric value we assume it is an ID referencing for an internal entity. |
| 233 | - if ( is_numeric( $field_value ) ) { |
|
| 233 | + if (is_numeric($field_value)) { |
|
| 234 | 234 | // Found id, get uri. |
| 235 | - $field_value = wl_get_entity_uri( $field_value ); |
|
| 235 | + $field_value = wl_get_entity_uri($field_value); |
|
| 236 | 236 | } |
| 237 | 237 | // Just if the linked entity does exist I can go further with template compiling |
| 238 | 238 | $nested_entity = Wordlift_Entity_Service::get_instance() |
| 239 | - ->get_entity_post_by_uri( $field_value ); |
|
| 240 | - if ( ! is_null( $nested_entity ) ) { |
|
| 241 | - $content = '<span itemid="' . esc_attr( $field_value ) . '">' . $nested_entity->post_title . '</span>'; |
|
| 242 | - $compiled_template = wl_content_embed_item_microdata( $content, $field_value, $field_name, ++ $recursion_level ); |
|
| 243 | - $template = str_replace( $placeholder, $compiled_template, $template ); |
|
| 239 | + ->get_entity_post_by_uri($field_value); |
|
| 240 | + if ( ! is_null($nested_entity)) { |
|
| 241 | + $content = '<span itemid="'.esc_attr($field_value).'">'.$nested_entity->post_title.'</span>'; |
|
| 242 | + $compiled_template = wl_content_embed_item_microdata($content, $field_value, $field_name, ++ $recursion_level); |
|
| 243 | + $template = str_replace($placeholder, $compiled_template, $template); |
|
| 244 | 244 | } else { |
| 245 | - $template = str_replace( $placeholder, '', $template ); |
|
| 245 | + $template = str_replace($placeholder, '', $template); |
|
| 246 | 246 | } |
| 247 | 247 | continue; |
| 248 | 248 | } |
@@ -250,8 +250,8 @@ discard block |
||
| 250 | 250 | // Standard condition: field containing a raw value |
| 251 | 251 | // For non visible test, schema.org dictates to use the *meta* tag. |
| 252 | 252 | // see http://schema.org/docs/gs.html#advanced_missing |
| 253 | - $value = '<meta itemprop="' . esc_attr( $field_name ) . '" content="' . esc_attr( $field_value ) . '" />'; |
|
| 254 | - $template = str_replace( $placeholder, $value, $template ); |
|
| 253 | + $value = '<meta itemprop="'.esc_attr($field_name).'" content="'.esc_attr($field_value).'" />'; |
|
| 254 | + $template = str_replace($placeholder, $value, $template); |
|
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |