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 |
||
| 20 | */ |
||
| 21 | private $_data; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | */ |
||
| 26 | public function __construct() { |
||
| 27 | // Generate data... |
||
| 28 | add_action( 'woocommerce_before_main_content', array( $this, 'generate_website_data' ), 30, 0 ); |
||
| 29 | add_action( 'woocommerce_breadcrumb', array( $this, 'generate_breadcrumblist_data' ), 10, 1 ); |
||
| 30 | add_action( 'woocommerce_shop_loop', array( $this, 'generate_product_data' ), 10, 0 ); |
||
| 31 | add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60, 0 ); |
||
| 32 | add_action( 'woocommerce_review_meta', array( $this, 'generate_review_data' ), 20, 1 ); |
||
| 33 | add_action( 'woocommerce_email_order_details', array( $this, 'generate_order_data' ), 20, 3 ); |
||
| 34 | |||
| 35 | // Output structured data... |
||
| 36 | add_action( 'woocommerce_email_order_details', array( $this, 'output_structured_data' ), 30, 0 ); |
||
| 37 | add_action( 'wp_footer', array( $this, 'output_structured_data' ), 10, 0 ); |
||
| 38 | |||
| 39 | // Filters... |
||
| 40 | add_filter( 'woocommerce_structured_data_product_limit', array( $this, 'limit_product_data' ), 10, 1 ); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Sets `$this->_data`. |
||
| 45 | * |
||
| 46 | * @param array $data |
||
| 47 | * @param bool $reset (default: false) |
||
| 48 | * @return bool |
||
| 49 | */ |
||
| 50 | public function set_data( $data, $reset = false ) { |
||
| 51 | if ( ! isset( $data['@type'] ) ) { |
||
| 52 | return false; |
||
| 53 | } elseif ( ! is_string( $data['@type'] ) ) { |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | if ( $reset && isset( $this->_data ) ) { |
||
| 58 | unset( $this->_data ); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->_data[] = $data; |
||
| 62 | |||
| 63 | return true; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Gets `$this->_data`. |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function get_data() { |
||
| 72 | return isset( $this->_data ) ? $this->_data : array(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Structures and returns data. |
||
| 77 | * |
||
| 78 | * List of types available by default for specific request: |
||
| 79 | * |
||
| 80 | * 'product', |
||
| 81 | * 'review', |
||
| 82 | * 'breadcrumblist', |
||
| 83 | * 'website', |
||
| 84 | * 'order', |
||
| 85 | * |
||
| 86 | * @param bool|array|string $requested_types (default: false) |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function get_structured_data( $requested_types = false ) { |
||
| 90 | $data = $this->get_data(); |
||
| 91 | |||
| 92 | if ( empty( $data ) || ( $requested_types && ! is_array( $requested_types ) && ! is_string( $requested_types ) || is_null( $requested_types ) ) ) { |
||
| 93 | return array(); |
||
| 94 | } elseif ( $requested_types && is_string( $requested_types ) ) { |
||
| 95 | $requested_types = array( $requested_types ); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Puts together the values of same type of structured data. |
||
| 99 | foreach ( $data as $value ) { |
||
| 100 | $structured_data[ strtolower( $value['@type'] ) ][] = $value; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Wraps the multiple values of each type of structured data inside a graph. Then adds context to each type of value. |
||
| 104 | foreach ( $structured_data as $type => $value ) { |
||
| 105 | $structured_data[ $type ] = count( $value ) > 1 ? array( '@graph' => $value ) : $value[0]; |
||
| 106 | $structured_data[ $type ] = apply_filters( 'woocommerce_structured_data_context', array( '@context' => 'http://schema.org/' ), $structured_data, $type, $value ) + $structured_data[ $type ]; |
||
| 107 | } |
||
| 108 | |||
| 109 | // If requested types, picks them up. Finally changes the associative array to an indexed one. |
||
| 110 | $structured_data = $requested_types ? array_values( array_intersect_key( $structured_data, array_flip( $requested_types ) ) ) : array_values( $structured_data ); |
||
| 111 | |||
| 112 | if ( ! empty( $structured_data ) ) { |
||
| 113 | $structured_data = count( $structured_data ) > 1 ? array( '@graph' => $structured_data ) : $structured_data[0]; |
||
| 114 | } |
||
| 115 | |||
| 116 | return $structured_data; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sanitizes, encodes and outputs structured data. |
||
| 121 | * |
||
| 122 | * @uses `wp_footer` action hook |
||
| 123 | * @uses `woocommerce_email_order_details` action hook |
||
| 124 | * @param bool|array|string $requested_types (default: true) |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function output_structured_data( $requested_types = true ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Sanitizes data. |
||
| 149 | * |
||
| 150 | * @param array $data |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function sanitize_data( $data ) { |
||
| 154 | if ( ! $data || ! is_array( $data ) ) { |
||
|
|
|||
| 155 | return array(); |
||
| 156 | } |
||
| 157 | |||
| 158 | foreach ( $data as $key => $value ) { |
||
| 159 | $sanitized_data[ sanitize_text_field( $key ) ] = is_array( $value ) ? $this->sanitize_data( $value ) : sanitize_text_field( $value ); |
||
| 160 | } |
||
| 161 | |||
| 162 | return $sanitized_data; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Generates, sanitizes, encodes and outputs specific structured data type. |
||
| 167 | * |
||
| 168 | * @param string $type |
||
| 169 | * @param mixed $object (default: null) |
||
| 170 | * @param mixed $param_1 (default: null) |
||
| 171 | * @param mixed $param_2 (default: null) |
||
| 172 | * @param mixed $param_3 (default: null) |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | public function generate_output_structured_data( $type, $object = null, $param_1 = null, $param_2 = null, $param_3 = null ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Limit Product structured data. |
||
| 191 | * |
||
| 192 | * @param bool $limit_data |
||
| 193 | * @return bool $limit_data |
||
| 194 | */ |
||
| 195 | public function limit_product_data( $limit_data ) { |
||
| 198 | |||
| 199 | /* |
||
| 200 | |-------------------------------------------------------------------------- |
||
| 201 | | Generators |
||
| 202 | |-------------------------------------------------------------------------- |
||
| 203 | | |
||
| 204 | | Methods for generating specific structured data types: |
||
| 205 | | |
||
| 206 | | - Product |
||
| 207 | | - Review |
||
| 208 | | - BreadcrumbList |
||
| 209 | | - WebSite |
||
| 210 | | - Order |
||
| 211 | | |
||
| 212 | | The generated data is stored into `$this->_data`. |
||
| 213 | | See the methods above for handling `$this->_data`. |
||
| 214 | | |
||
| 215 | */ |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Generates Product structured data. |
||
| 219 | * |
||
| 220 | * @uses `woocommerce_single_product_summary` action hook |
||
| 221 | * @uses `woocommerce_shop_loop` action hook |
||
| 222 | * @param bool|object $product (default: false) |
||
| 223 | * @param bool $limit_data (default: false) |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function generate_product_data( $product = false, $limit_data = false ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Generates Review structured data. |
||
| 290 | * |
||
| 291 | * @uses `woocommerce_review_meta` action hook |
||
| 292 | * @param object $comment |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | public function generate_review_data( $comment ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Generates BreadcrumbList structured data. |
||
| 322 | * |
||
| 323 | * @uses `woocommerce_breadcrumb` action hook |
||
| 324 | * @param object $breadcrumbs |
||
| 325 | * @return bool|void |
||
| 326 | */ |
||
| 327 | public function generate_breadcrumblist_data( $breadcrumbs ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Generates WebSite structured data. |
||
| 356 | * |
||
| 357 | * @uses `woocommerce_before_main_content` action hook |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function generate_website_data() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Generates Order structured data. |
||
| 375 | * |
||
| 376 | * @uses `woocommerce_email_order_details` action hook |
||
| 377 | * @param object $order |
||
| 378 | * @param bool $sent_to_admin (default: false) |
||
| 379 | * @param bool $plain_text (default: false) |
||
| 380 | * @return bool|void |
||
| 381 | */ |
||
| 382 | public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) { |
||
| 494 | } |
||
| 495 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.