@@ -4,177 +4,177 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Duplicate_Markup_Remover { |
| 6 | 6 | |
| 7 | - private $types_to_properties_map = array( |
|
| 8 | - 'HowTo' => array( |
|
| 9 | - 'estimatedCost', |
|
| 10 | - 'totalTime', |
|
| 11 | - 'supply', |
|
| 12 | - 'tool', |
|
| 13 | - 'step' |
|
| 14 | - ), |
|
| 15 | - 'FAQPage' => array( 'mainEntity' ), |
|
| 16 | - 'Recipe' => array( |
|
| 17 | - 'cookTime', |
|
| 18 | - 'cookingMethod', |
|
| 19 | - 'nutrition', |
|
| 20 | - 'recipeCategory', |
|
| 21 | - 'recipeCuisine', |
|
| 22 | - 'recipeIngredient', |
|
| 23 | - 'recipeInstructions', |
|
| 24 | - 'recipeYield', |
|
| 25 | - 'suitableForDiet' |
|
| 26 | - ), |
|
| 27 | - 'Product' => array( |
|
| 28 | - 'additionalProperty', |
|
| 29 | - 'aggregateRating', |
|
| 30 | - 'audience', |
|
| 31 | - 'award', |
|
| 32 | - 'brand', |
|
| 33 | - 'category', |
|
| 34 | - 'color', |
|
| 35 | - 'countryOfAssembly', |
|
| 36 | - 'countryOfLastProcessing', |
|
| 37 | - 'countryOfOrigin', |
|
| 38 | - 'depth', |
|
| 39 | - 'funding', |
|
| 40 | - 'gtin', |
|
| 41 | - 'gtin12', |
|
| 42 | - 'gtin13', |
|
| 43 | - 'gtin14', |
|
| 44 | - 'gtin8', |
|
| 45 | - 'hasAdultConsideration', |
|
| 46 | - 'hasEnergyConsumptionDetails', |
|
| 47 | - 'hasMeasurement', |
|
| 48 | - 'hasMerchantReturnPolicy', |
|
| 49 | - 'height', |
|
| 50 | - 'inProductGroupWithID', |
|
| 51 | - 'isAccessoryOrSparePartFor', |
|
| 52 | - 'isConsumableFor', |
|
| 53 | - 'isFamilyFriendly', |
|
| 54 | - 'isRelatedTo', |
|
| 55 | - 'isSimilarTo', |
|
| 56 | - 'isVariantOf', |
|
| 57 | - 'itemCondition', |
|
| 58 | - 'keywords', |
|
| 59 | - 'logo', |
|
| 60 | - 'manufacturer', |
|
| 61 | - 'material', |
|
| 62 | - 'model', |
|
| 63 | - 'mpn', |
|
| 64 | - 'nsn', |
|
| 65 | - 'offers', |
|
| 66 | - 'pattern', |
|
| 67 | - 'productID', |
|
| 68 | - 'productionDate', |
|
| 69 | - 'purchaseDate', |
|
| 70 | - 'releaseDate', |
|
| 71 | - 'review', |
|
| 72 | - 'size', |
|
| 73 | - 'sku', |
|
| 74 | - 'slogan', |
|
| 75 | - 'weight', |
|
| 76 | - 'width' |
|
| 77 | - ) |
|
| 78 | - ); |
|
| 79 | - |
|
| 80 | - public function __construct() { |
|
| 81 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param $jsonld array The final jsonld. |
|
| 87 | - * @param $post_id int The post id. |
|
| 88 | - * |
|
| 89 | - * @return array Filtered jsonld. |
|
| 90 | - */ |
|
| 91 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 92 | - |
|
| 93 | - foreach ( $this->types_to_properties_map as $type_to_remove => $properties_to_remove ) { |
|
| 94 | - $jsonld = $this->remove_type( $jsonld, $type_to_remove, $properties_to_remove ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - return $jsonld; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param array $jsonld |
|
| 103 | - * |
|
| 104 | - * @return bool |
|
| 105 | - */ |
|
| 106 | - protected function should_alter_jsonld( $jsonld, $type_to_remove ) { |
|
| 107 | - return ! is_array( $jsonld ) |
|
| 108 | - || ! count( $jsonld ) > 1 |
|
| 109 | - || ! array_key_exists( 0, $jsonld ) |
|
| 110 | - || ! $this->schema_type_matches_post( $jsonld, $type_to_remove ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param array $jsonld |
|
| 115 | - * |
|
| 116 | - * @return array |
|
| 117 | - */ |
|
| 118 | - private function remove_type( $jsonld, $type_to_remove, $properties_to_remove ) { |
|
| 119 | - |
|
| 120 | - |
|
| 121 | - if ( $this->should_alter_jsonld( $jsonld, $type_to_remove ) ) { |
|
| 122 | - // Return early if there are no referenced entities. |
|
| 123 | - return $jsonld; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $post_jsonld = array_shift( $jsonld ); |
|
| 127 | - |
|
| 128 | - // we need to loop through all the items and remove the faq markup. |
|
| 129 | - foreach ( $jsonld as $key => &$value ) { |
|
| 130 | - if ( ! array_key_exists( '@type', $value ) ) { |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - $type = $value['@type']; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Two possibilities: |
|
| 137 | - * 1. The referenced entity has only supplied SchemaType markup, in that case remove the complete entity. |
|
| 138 | - * 2. The referenced entity has multiple types, in that case completely remove the supplied SchemaType markup, but |
|
| 139 | - * retain the other entity data. |
|
| 140 | - */ |
|
| 141 | - // If the referenced entity is purely supplied SchemaType markup, then remove it. |
|
| 142 | - |
|
| 143 | - if ( is_string( $type ) && $type === $type_to_remove ) { |
|
| 144 | - // Remove the entity completely. |
|
| 145 | - unset( $jsonld[ $key ] ); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if ( is_array( $type ) && in_array( $type_to_remove, $type ) ) { |
|
| 149 | - // Remove the supplied SchemaType markup. |
|
| 150 | - $position = array_search( $type_to_remove, $type ); |
|
| 151 | - // Also update the type. |
|
| 152 | - if ( $position !== false ) { |
|
| 153 | - unset( $type[ $position ] ); |
|
| 154 | - $value['@type'] = array_values( $type ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - foreach ( $properties_to_remove as $property ) { |
|
| 158 | - // Remove keys of supplied SchemaType. |
|
| 159 | - unset( $value[ $property ] ); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - // Add the post jsonld to front of jsonld array. |
|
| 166 | - array_unshift( $jsonld, $post_jsonld ); |
|
| 167 | - |
|
| 168 | - return $jsonld; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - private function schema_type_matches_post( $jsonld, $type_to_remove ) { |
|
| 172 | - $type = isset( $jsonld[0]['@type'] ) ? $jsonld[0]['@type'] : array(); |
|
| 173 | - if ( is_string( $type ) ) { |
|
| 174 | - $type = array( $type ); |
|
| 175 | - } |
|
| 176 | - return in_array( $type_to_remove, $type ); |
|
| 177 | - } |
|
| 7 | + private $types_to_properties_map = array( |
|
| 8 | + 'HowTo' => array( |
|
| 9 | + 'estimatedCost', |
|
| 10 | + 'totalTime', |
|
| 11 | + 'supply', |
|
| 12 | + 'tool', |
|
| 13 | + 'step' |
|
| 14 | + ), |
|
| 15 | + 'FAQPage' => array( 'mainEntity' ), |
|
| 16 | + 'Recipe' => array( |
|
| 17 | + 'cookTime', |
|
| 18 | + 'cookingMethod', |
|
| 19 | + 'nutrition', |
|
| 20 | + 'recipeCategory', |
|
| 21 | + 'recipeCuisine', |
|
| 22 | + 'recipeIngredient', |
|
| 23 | + 'recipeInstructions', |
|
| 24 | + 'recipeYield', |
|
| 25 | + 'suitableForDiet' |
|
| 26 | + ), |
|
| 27 | + 'Product' => array( |
|
| 28 | + 'additionalProperty', |
|
| 29 | + 'aggregateRating', |
|
| 30 | + 'audience', |
|
| 31 | + 'award', |
|
| 32 | + 'brand', |
|
| 33 | + 'category', |
|
| 34 | + 'color', |
|
| 35 | + 'countryOfAssembly', |
|
| 36 | + 'countryOfLastProcessing', |
|
| 37 | + 'countryOfOrigin', |
|
| 38 | + 'depth', |
|
| 39 | + 'funding', |
|
| 40 | + 'gtin', |
|
| 41 | + 'gtin12', |
|
| 42 | + 'gtin13', |
|
| 43 | + 'gtin14', |
|
| 44 | + 'gtin8', |
|
| 45 | + 'hasAdultConsideration', |
|
| 46 | + 'hasEnergyConsumptionDetails', |
|
| 47 | + 'hasMeasurement', |
|
| 48 | + 'hasMerchantReturnPolicy', |
|
| 49 | + 'height', |
|
| 50 | + 'inProductGroupWithID', |
|
| 51 | + 'isAccessoryOrSparePartFor', |
|
| 52 | + 'isConsumableFor', |
|
| 53 | + 'isFamilyFriendly', |
|
| 54 | + 'isRelatedTo', |
|
| 55 | + 'isSimilarTo', |
|
| 56 | + 'isVariantOf', |
|
| 57 | + 'itemCondition', |
|
| 58 | + 'keywords', |
|
| 59 | + 'logo', |
|
| 60 | + 'manufacturer', |
|
| 61 | + 'material', |
|
| 62 | + 'model', |
|
| 63 | + 'mpn', |
|
| 64 | + 'nsn', |
|
| 65 | + 'offers', |
|
| 66 | + 'pattern', |
|
| 67 | + 'productID', |
|
| 68 | + 'productionDate', |
|
| 69 | + 'purchaseDate', |
|
| 70 | + 'releaseDate', |
|
| 71 | + 'review', |
|
| 72 | + 'size', |
|
| 73 | + 'sku', |
|
| 74 | + 'slogan', |
|
| 75 | + 'weight', |
|
| 76 | + 'width' |
|
| 77 | + ) |
|
| 78 | + ); |
|
| 79 | + |
|
| 80 | + public function __construct() { |
|
| 81 | + add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param $jsonld array The final jsonld. |
|
| 87 | + * @param $post_id int The post id. |
|
| 88 | + * |
|
| 89 | + * @return array Filtered jsonld. |
|
| 90 | + */ |
|
| 91 | + public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 92 | + |
|
| 93 | + foreach ( $this->types_to_properties_map as $type_to_remove => $properties_to_remove ) { |
|
| 94 | + $jsonld = $this->remove_type( $jsonld, $type_to_remove, $properties_to_remove ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + return $jsonld; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param array $jsonld |
|
| 103 | + * |
|
| 104 | + * @return bool |
|
| 105 | + */ |
|
| 106 | + protected function should_alter_jsonld( $jsonld, $type_to_remove ) { |
|
| 107 | + return ! is_array( $jsonld ) |
|
| 108 | + || ! count( $jsonld ) > 1 |
|
| 109 | + || ! array_key_exists( 0, $jsonld ) |
|
| 110 | + || ! $this->schema_type_matches_post( $jsonld, $type_to_remove ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param array $jsonld |
|
| 115 | + * |
|
| 116 | + * @return array |
|
| 117 | + */ |
|
| 118 | + private function remove_type( $jsonld, $type_to_remove, $properties_to_remove ) { |
|
| 119 | + |
|
| 120 | + |
|
| 121 | + if ( $this->should_alter_jsonld( $jsonld, $type_to_remove ) ) { |
|
| 122 | + // Return early if there are no referenced entities. |
|
| 123 | + return $jsonld; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $post_jsonld = array_shift( $jsonld ); |
|
| 127 | + |
|
| 128 | + // we need to loop through all the items and remove the faq markup. |
|
| 129 | + foreach ( $jsonld as $key => &$value ) { |
|
| 130 | + if ( ! array_key_exists( '@type', $value ) ) { |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + $type = $value['@type']; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Two possibilities: |
|
| 137 | + * 1. The referenced entity has only supplied SchemaType markup, in that case remove the complete entity. |
|
| 138 | + * 2. The referenced entity has multiple types, in that case completely remove the supplied SchemaType markup, but |
|
| 139 | + * retain the other entity data. |
|
| 140 | + */ |
|
| 141 | + // If the referenced entity is purely supplied SchemaType markup, then remove it. |
|
| 142 | + |
|
| 143 | + if ( is_string( $type ) && $type === $type_to_remove ) { |
|
| 144 | + // Remove the entity completely. |
|
| 145 | + unset( $jsonld[ $key ] ); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if ( is_array( $type ) && in_array( $type_to_remove, $type ) ) { |
|
| 149 | + // Remove the supplied SchemaType markup. |
|
| 150 | + $position = array_search( $type_to_remove, $type ); |
|
| 151 | + // Also update the type. |
|
| 152 | + if ( $position !== false ) { |
|
| 153 | + unset( $type[ $position ] ); |
|
| 154 | + $value['@type'] = array_values( $type ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + foreach ( $properties_to_remove as $property ) { |
|
| 158 | + // Remove keys of supplied SchemaType. |
|
| 159 | + unset( $value[ $property ] ); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + // Add the post jsonld to front of jsonld array. |
|
| 166 | + array_unshift( $jsonld, $post_jsonld ); |
|
| 167 | + |
|
| 168 | + return $jsonld; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + private function schema_type_matches_post( $jsonld, $type_to_remove ) { |
|
| 172 | + $type = isset( $jsonld[0]['@type'] ) ? $jsonld[0]['@type'] : array(); |
|
| 173 | + if ( is_string( $type ) ) { |
|
| 174 | + $type = array( $type ); |
|
| 175 | + } |
|
| 176 | + return in_array( $type_to_remove, $type ); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | 179 | |
| 180 | 180 | } |
| 181 | 181 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | 'tool', |
| 13 | 13 | 'step' |
| 14 | 14 | ), |
| 15 | - 'FAQPage' => array( 'mainEntity' ), |
|
| 15 | + 'FAQPage' => array('mainEntity'), |
|
| 16 | 16 | 'Recipe' => array( |
| 17 | 17 | 'cookTime', |
| 18 | 18 | 'cookingMethod', |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | ); |
| 79 | 79 | |
| 80 | 80 | public function __construct() { |
| 81 | - add_filter( 'wl_after_get_jsonld', array( $this, 'wl_after_get_jsonld' ), 10, 2 ); |
|
| 81 | + add_filter('wl_after_get_jsonld', array($this, 'wl_after_get_jsonld'), 10, 2); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | |
@@ -88,10 +88,10 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return array Filtered jsonld. |
| 90 | 90 | */ |
| 91 | - public function wl_after_get_jsonld( $jsonld, $post_id ) { |
|
| 91 | + public function wl_after_get_jsonld($jsonld, $post_id) { |
|
| 92 | 92 | |
| 93 | - foreach ( $this->types_to_properties_map as $type_to_remove => $properties_to_remove ) { |
|
| 94 | - $jsonld = $this->remove_type( $jsonld, $type_to_remove, $properties_to_remove ); |
|
| 93 | + foreach ($this->types_to_properties_map as $type_to_remove => $properties_to_remove) { |
|
| 94 | + $jsonld = $this->remove_type($jsonld, $type_to_remove, $properties_to_remove); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | return $jsonld; |
@@ -103,11 +103,11 @@ discard block |
||
| 103 | 103 | * |
| 104 | 104 | * @return bool |
| 105 | 105 | */ |
| 106 | - protected function should_alter_jsonld( $jsonld, $type_to_remove ) { |
|
| 107 | - return ! is_array( $jsonld ) |
|
| 108 | - || ! count( $jsonld ) > 1 |
|
| 109 | - || ! array_key_exists( 0, $jsonld ) |
|
| 110 | - || ! $this->schema_type_matches_post( $jsonld, $type_to_remove ); |
|
| 106 | + protected function should_alter_jsonld($jsonld, $type_to_remove) { |
|
| 107 | + return ! is_array($jsonld) |
|
| 108 | + || ! count($jsonld) > 1 |
|
| 109 | + || ! array_key_exists(0, $jsonld) |
|
| 110 | + || ! $this->schema_type_matches_post($jsonld, $type_to_remove); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -115,19 +115,19 @@ discard block |
||
| 115 | 115 | * |
| 116 | 116 | * @return array |
| 117 | 117 | */ |
| 118 | - private function remove_type( $jsonld, $type_to_remove, $properties_to_remove ) { |
|
| 118 | + private function remove_type($jsonld, $type_to_remove, $properties_to_remove) { |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | - if ( $this->should_alter_jsonld( $jsonld, $type_to_remove ) ) { |
|
| 121 | + if ($this->should_alter_jsonld($jsonld, $type_to_remove)) { |
|
| 122 | 122 | // Return early if there are no referenced entities. |
| 123 | 123 | return $jsonld; |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - $post_jsonld = array_shift( $jsonld ); |
|
| 126 | + $post_jsonld = array_shift($jsonld); |
|
| 127 | 127 | |
| 128 | 128 | // we need to loop through all the items and remove the faq markup. |
| 129 | - foreach ( $jsonld as $key => &$value ) { |
|
| 130 | - if ( ! array_key_exists( '@type', $value ) ) { |
|
| 129 | + foreach ($jsonld as $key => &$value) { |
|
| 130 | + if ( ! array_key_exists('@type', $value)) { |
|
| 131 | 131 | continue; |
| 132 | 132 | } |
| 133 | 133 | $type = $value['@type']; |
@@ -140,40 +140,40 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | // If the referenced entity is purely supplied SchemaType markup, then remove it. |
| 142 | 142 | |
| 143 | - if ( is_string( $type ) && $type === $type_to_remove ) { |
|
| 143 | + if (is_string($type) && $type === $type_to_remove) { |
|
| 144 | 144 | // Remove the entity completely. |
| 145 | - unset( $jsonld[ $key ] ); |
|
| 145 | + unset($jsonld[$key]); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - if ( is_array( $type ) && in_array( $type_to_remove, $type ) ) { |
|
| 148 | + if (is_array($type) && in_array($type_to_remove, $type)) { |
|
| 149 | 149 | // Remove the supplied SchemaType markup. |
| 150 | - $position = array_search( $type_to_remove, $type ); |
|
| 150 | + $position = array_search($type_to_remove, $type); |
|
| 151 | 151 | // Also update the type. |
| 152 | - if ( $position !== false ) { |
|
| 153 | - unset( $type[ $position ] ); |
|
| 154 | - $value['@type'] = array_values( $type ); |
|
| 152 | + if ($position !== false) { |
|
| 153 | + unset($type[$position]); |
|
| 154 | + $value['@type'] = array_values($type); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - foreach ( $properties_to_remove as $property ) { |
|
| 157 | + foreach ($properties_to_remove as $property) { |
|
| 158 | 158 | // Remove keys of supplied SchemaType. |
| 159 | - unset( $value[ $property ] ); |
|
| 159 | + unset($value[$property]); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | // Add the post jsonld to front of jsonld array. |
| 166 | - array_unshift( $jsonld, $post_jsonld ); |
|
| 166 | + array_unshift($jsonld, $post_jsonld); |
|
| 167 | 167 | |
| 168 | 168 | return $jsonld; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - private function schema_type_matches_post( $jsonld, $type_to_remove ) { |
|
| 172 | - $type = isset( $jsonld[0]['@type'] ) ? $jsonld[0]['@type'] : array(); |
|
| 173 | - if ( is_string( $type ) ) { |
|
| 174 | - $type = array( $type ); |
|
| 171 | + private function schema_type_matches_post($jsonld, $type_to_remove) { |
|
| 172 | + $type = isset($jsonld[0]['@type']) ? $jsonld[0]['@type'] : array(); |
|
| 173 | + if (is_string($type)) { |
|
| 174 | + $type = array($type); |
|
| 175 | 175 | } |
| 176 | - return in_array( $type_to_remove, $type ); |
|
| 176 | + return in_array($type_to_remove, $type); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | |