Complex classes like WC_Structured_Data often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_Structured_Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class WC_Structured_Data { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var null|array $_data Partially structured data from `generate_*` methods |
||
| 20 | */ |
||
| 21 | private $_data; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null|array $_structured_data |
||
| 25 | */ |
||
| 26 | private $_structured_data; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor. |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 32 | // Generate data... |
||
| 33 | add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30, 0 ); |
||
| 34 | add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumb_data' ), 10, 1 ); |
||
| 35 | add_action( 'woocommerce_shop_loop', array( $this, 'generate_product_data' ), 10, 0 ); |
||
| 36 | add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60, 0 ); |
||
| 37 | add_action( 'woocommerce_review_meta', array( $this, 'generate_product_review_data' ), 20, 1 ); |
||
| 38 | add_action( 'woocommerce_email_order_details', array( $this, 'generate_email_order_data' ), 20, 3 ); |
||
| 39 | // Enqueue structured data... |
||
| 40 | add_action( 'woocommerce_email_order_details', array( $this, 'enqueue_data' ), 30, 0 ); |
||
| 41 | add_action( 'wp_footer', array( $this, 'enqueue_data_type_for_page' ), 10, 0 ); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sets `$this->_data`. |
||
| 46 | * |
||
| 47 | * @param array $data |
||
| 48 | * @param bool $reset (default: false) |
||
| 49 | * @return bool |
||
| 50 | */ |
||
| 51 | public function set_data( $data, $reset = false ) { |
||
| 52 | if ( ! is_array( $data ) || ! array_key_exists( '@type', $data ) ) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ( $reset && isset( $this->_data ) ) { |
||
| 57 | unset( $this->_data ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->_data[] = $data; |
||
| 61 | |||
| 62 | return true; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Gets `$this->_data`. |
||
| 67 | * |
||
| 68 | * @return array $data |
||
| 69 | */ |
||
| 70 | public function get_data() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Sets `$this->_structured_data`. |
||
| 76 | * |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | public function set_structured_data() { |
||
| 80 | if ( empty( $this->get_data() ) ) { |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | foreach ( $this->get_data() as $value ) { |
||
| 85 | switch ( $type = $value['@type'] ) { |
||
| 86 | case 'MusicAlbum': |
||
| 87 | case 'SoftwareApplication': |
||
| 88 | $type = 'Product'; |
||
| 89 | break; |
||
| 90 | } |
||
| 91 | |||
| 92 | $structured_data[ $type ][] = $value; |
||
| 93 | } |
||
| 94 | |||
| 95 | foreach ( $structured_data as $type => $value ) { |
||
| 96 | if ( count( $value ) > 1 ) { |
||
| 97 | $structured_data[ $type ] = array( '@graph' => $value ); |
||
| 98 | } else { |
||
| 99 | $structured_data[ $type ] = $value[0]; |
||
| 100 | } |
||
| 101 | |||
| 102 | $structured_data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'http://schema.org/' ), $structured_data, $type, $value ) + $structured_data[ $type ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->_structured_data = $structured_data; |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Gets `$this->_structured_data`. |
||
| 112 | * |
||
| 113 | * @return array $structured_data |
||
| 114 | */ |
||
| 115 | public function get_structured_data() { |
||
| 116 | $this->set_structured_data(); |
||
| 117 | |||
| 118 | return $structured_data = isset( $this->_structured_data ) ? $this->_structured_data : array(); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Sanitizes, encodes and echoes structured data. |
||
| 123 | * |
||
| 124 | * List of the types available by default for specific request: |
||
| 125 | * 'Product', |
||
| 126 | * 'Review', |
||
| 127 | * 'BreadcrumbList', |
||
| 128 | * 'WebSite', |
||
| 129 | * 'Order', |
||
| 130 | * |
||
| 131 | * @uses `woocommerce_email_order_details` action hook |
||
| 132 | * @param bool|array $requested_types (default: false) |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function enqueue_data( $requested_types = false ) { |
||
| 136 | if ( ! $structured_data = $this->get_structured_data() ) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( $requested_types ) { |
||
| 141 | if ( ! is_array( $requested_types ) ) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | foreach ( $structured_data as $type => $value ) { |
||
| 146 | foreach ( $requested_types as $requested_type ) { |
||
| 147 | if ( $requested_type === $type ) { |
||
| 148 | $json[] = $value; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if ( ! isset( $json ) ) { |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | foreach ( $structured_data as $value ) { |
||
| 158 | $json[] = $value; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | if ( count( $json ) > 1 ) { |
||
| 163 | $json = array( '@graph' => $json ); |
||
| 164 | } else { |
||
| 165 | $json = $json[0]; |
||
| 166 | } |
||
| 167 | |||
| 168 | if ( $json = $this->sanitize_data( $json ) ) { |
||
| 169 | // Testing/Debugging |
||
| 170 | //echo json_encode( $json, JSON_UNESCAPED_SLASHES ); |
||
| 171 | echo '<script type="application/ld+json">' . wp_json_encode( $json ) . '</script>'; |
||
| 172 | |||
| 173 | return true; |
||
| 174 | } |
||
| 175 | |||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Sanitizes, encodes and echoes specific structured data type on scpecific page. |
||
| 181 | * |
||
| 182 | * @uses `wp_footer` action hook |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function enqueue_data_type_for_page() { |
||
| 186 | $requested_types = apply_filters( 'woocommerce_structured_data_type_for_page', array( |
||
| 187 | is_shop() && is_front_page() ? 'WebSite' : null, |
||
| 188 | 'BreadcrumbList', |
||
| 189 | 'Product', |
||
| 190 | 'Review', |
||
| 191 | ) ); |
||
| 192 | |||
| 193 | return $this->enqueue_data( $requested_types ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Sanitizes data. |
||
| 198 | * |
||
| 199 | * @param array $data |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function sanitize_data( $data ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Generates Product structured data. |
||
| 216 | * |
||
| 217 | * @uses `woocommerce_single_product_summary` action hook |
||
| 218 | * @uses `woocommerce_shop_loop` action hook |
||
| 219 | * @param bool|object $product (default: false) |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function generate_product_data( $product = false ) { |
||
| 223 | if ( ! $product ) { |
||
| 224 | global $product; |
||
| 225 | } |
||
| 226 | |||
| 227 | if ( ! is_object( $product ) ) { |
||
| 228 | return false; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ( $is_multi_variation = count( $product->get_children() ) > 1 ? true : false ) { |
||
| 232 | $variations = $product->get_available_variations(); |
||
| 233 | } else { |
||
| 234 | $variations = array( null ); |
||
| 235 | } |
||
| 236 | |||
| 237 | foreach ( $variations as $variation ) { |
||
| 238 | $product_variation = $is_multi_variation ? wc_get_product( $variation['variation_id'] ) : $product; |
||
| 239 | |||
| 240 | $markup_offers[] = array( |
||
| 241 | '@type' => 'Offer', |
||
| 242 | 'priceCurrency' => get_woocommerce_currency(), |
||
| 243 | 'price' => $product_variation->get_price(), |
||
| 244 | 'availability' => 'http://schema.org/' . $stock = ( $product_variation->is_in_stock() ? 'InStock' : 'OutOfStock' ), |
||
| 245 | 'sku' => $product_variation->get_sku(), |
||
| 246 | 'image' => wp_get_attachment_url( $product_variation->get_image_id() ), |
||
| 247 | 'description' => $is_multi_variation ? $product_variation->get_variation_description() : '', |
||
| 248 | 'seller' => array( |
||
| 249 | '@type' => 'Organization', |
||
| 250 | 'name' => get_bloginfo( 'name' ), |
||
| 251 | 'url' => get_bloginfo( 'url' ), |
||
| 252 | ), |
||
| 253 | ); |
||
| 254 | } |
||
| 255 | |||
| 256 | if ( $product->is_downloadable() ) { |
||
| 257 | switch ( $product->download_type ) { |
||
| 258 | case 'application' : |
||
| 259 | $type = "SoftwareApplication"; |
||
| 260 | break; |
||
| 261 | case 'music' : |
||
| 262 | $type = "MusicAlbum"; |
||
| 263 | break; |
||
| 264 | default : |
||
| 265 | $type = "Product"; |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | } else { |
||
| 269 | $type = "Product"; |
||
| 270 | } |
||
| 271 | |||
| 272 | $markup['@type'] = $type; |
||
| 273 | $markup['@id'] = get_the_permalink(); |
||
| 274 | $markup['name'] = get_the_title(); |
||
| 275 | $markup['description'] = get_the_excerpt(); |
||
| 276 | $markup['url'] = get_the_permalink(); |
||
| 277 | $markup['offers'] = $markup_offers; |
||
| 278 | |||
| 279 | if ( $product->get_rating_count() ) { |
||
| 280 | $markup['aggregateRating'] = array( |
||
| 281 | '@type' => 'AggregateRating', |
||
| 282 | 'ratingValue' => $product->get_average_rating(), |
||
| 283 | 'ratingCount' => $product->get_rating_count(), |
||
| 284 | 'reviewCount' => $product->get_review_count(), |
||
| 285 | ); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) ); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Generates Product Review structured data. |
||
| 293 | * |
||
| 294 | * @uses `woocommerce_review_meta` action hook |
||
| 295 | * @param object $comment |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | public function generate_product_review_data( $comment ) { |
||
| 299 | if ( ! is_object( $comment ) ) { |
||
| 300 | return false; |
||
| 301 | } |
||
| 302 | |||
| 303 | $markup['@type'] = 'Review'; |
||
| 304 | $markup['@id'] = get_the_permalink() . '#li-comment-' . get_comment_ID(); |
||
| 305 | $markup['datePublished'] = get_comment_date( 'c' ); |
||
| 306 | $markup['description'] = get_comment_text(); |
||
| 307 | $markup['itemReviewed'] = array( |
||
| 308 | '@type' => 'Product', |
||
| 309 | 'name' => get_the_title(), |
||
| 310 | ); |
||
| 311 | $markup['reviewRating'] = array( |
||
| 312 | '@type' => 'rating', |
||
| 313 | 'ratingValue' => intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ), |
||
| 314 | ); |
||
| 315 | $markup['author'] = array( |
||
| 316 | '@type' => 'Person', |
||
| 317 | 'name' => get_comment_author(), |
||
| 318 | ); |
||
| 319 | |||
| 320 | return $this->set_data( apply_filters( 'woocommerce_structured_data_product_review', $markup, $comment ) ); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Generates BreadcrumbList structured data. |
||
| 325 | * |
||
| 326 | * @uses `woocommerce_breadcrumb` action hook |
||
| 327 | * @param array $breadcrumb |
||
| 328 | * @return bool|void |
||
| 329 | */ |
||
| 330 | public function generate_breadcrumb_data( $breadcrumb ) { |
||
| 331 | if ( ! is_array( $breadcrumb ) ) { |
||
| 332 | return false; |
||
| 333 | } |
||
| 334 | |||
| 335 | if ( empty( $breadcrumb = $breadcrumb['breadcrumb'] ) ) { |
||
| 336 | return; |
||
| 337 | } |
||
| 338 | |||
| 339 | $position = 1; |
||
| 340 | |||
| 341 | foreach ( $breadcrumb as $key => $value ) { |
||
| 342 | $markup_crumbs[] = array( |
||
| 343 | '@type' => 'ListItem', |
||
| 344 | 'position' => $position ++, |
||
| 345 | 'item' => array( |
||
| 346 | '@id' => ! empty( $value[1] ) && sizeof( $breadcrumb ) !== $key + 1 ? $value[1] : '#', |
||
| 347 | 'name' => $value[0], |
||
| 348 | ), |
||
| 349 | ); |
||
| 350 | } |
||
| 351 | |||
| 352 | $markup['@type'] = 'BreadcrumbList'; |
||
| 353 | $markup['itemListElement'] = $markup_crumbs; |
||
| 354 | |||
| 355 | return $this->set_data( apply_filters( 'woocommerce_structured_data_breadcrumb', $markup, $breadcrumb ) ); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Generates WebSite structured data. |
||
| 360 | * |
||
| 361 | * @uses `woocommerce_before_main_content` action hook |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public function generate_website_data() { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Generates Email Order structured data. |
||
| 379 | * |
||
| 380 | * @uses `woocommerce_email_order_details` action hook |
||
| 381 | * @param object $order |
||
| 382 | * @param bool $sent_to_admin (default: false) |
||
| 383 | * @param bool $plain_text (default: false) |
||
| 384 | * @return bool|void |
||
| 385 | */ |
||
| 386 | public function generate_email_order_data( $order, $sent_to_admin = false, $plain_text = false ) { |
||
| 500 | } |
||
| 501 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.