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_in_loops' ), 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() { |
||
| 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 ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sanitizes, encodes and outputs structured data. |
||
| 121 | * |
||
| 122 | * Hooked into `wp_footer` action hook. |
||
| 123 | * Hooked into `woocommerce_email_order_details` action hook. |
||
| 124 | * |
||
| 125 | * @param bool|array|string $requested_types (default: true) |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function output_structured_data( $requested_types = true ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Sanitizes data. |
||
| 150 | * |
||
| 151 | * @param array $data |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function sanitize_data( $data ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Generates, sanitizes, encodes and outputs specific structured data type. |
||
| 168 | * |
||
| 169 | * @param string $type |
||
| 170 | * @param mixed $object (default: null) |
||
| 171 | * @param mixed $param_1 (default: null) |
||
| 172 | * @param mixed $param_2 (default: null) |
||
| 173 | * @param mixed $param_3 (default: null) |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function generate_output_structured_data( $type, $object = null, $param_1 = null, $param_2 = null, $param_3 = null ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Limits Product structured data on taxonomies and shop page. |
||
| 192 | * |
||
| 193 | * Hooked into `woocommerce_structured_data_product_limit` filter hook. |
||
| 194 | * |
||
| 195 | * @param bool $limit_data |
||
| 196 | * @return bool $limit_data |
||
| 197 | */ |
||
| 198 | public function limit_product_data_in_loops( $limit_data ) { |
||
| 201 | |||
| 202 | /* |
||
| 203 | |-------------------------------------------------------------------------- |
||
| 204 | | Generators |
||
| 205 | |-------------------------------------------------------------------------- |
||
| 206 | | |
||
| 207 | | Methods for generating specific structured data types: |
||
| 208 | | |
||
| 209 | | - Product |
||
| 210 | | - Review |
||
| 211 | | - BreadcrumbList |
||
| 212 | | - WebSite |
||
| 213 | | - Order |
||
| 214 | | |
||
| 215 | | The generated data is stored into `$this->_data`. |
||
| 216 | | See the methods above for handling `$this->_data`. |
||
| 217 | | |
||
| 218 | */ |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Generates Product structured data. |
||
| 222 | * |
||
| 223 | * Hooked into `woocommerce_single_product_summary` action hook. |
||
| 224 | * Hooked into `woocommerce_shop_loop` action hook. |
||
| 225 | * |
||
| 226 | * @param bool|object $product (default: false) |
||
| 227 | * @param bool $limit_data (default: false) |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public function generate_product_data( $product = false, $limit_data = false ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Generates Review structured data. |
||
| 294 | * |
||
| 295 | * Hooked into `woocommerce_review_meta` action hook. |
||
| 296 | * |
||
| 297 | * @param object $comment |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function generate_review_data( $comment ) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Generates BreadcrumbList structured data. |
||
| 327 | * |
||
| 328 | * Hooked into `woocommerce_breadcrumb` action hook. |
||
| 329 | * |
||
| 330 | * @param object $breadcrumbs |
||
| 331 | * @return bool|void |
||
| 332 | */ |
||
| 333 | public function generate_breadcrumblist_data( $breadcrumbs ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Generates WebSite structured data. |
||
| 362 | * |
||
| 363 | * Hooked into `woocommerce_before_main_content` action hook. |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function generate_website_data() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Generates Order structured data. |
||
| 382 | * |
||
| 383 | * Hooked into `woocommerce_email_order_details` action hook. |
||
| 384 | * |
||
| 385 | * @param object $order |
||
| 386 | * @param bool $sent_to_admin (default: false) |
||
| 387 | * @param bool $plain_text (default: false) |
||
| 388 | * @return bool|void |
||
| 389 | */ |
||
| 390 | public function generate_order_data( $order, $sent_to_admin = false, $plain_text = false ) { |
||
| 502 | } |
||
| 503 |
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.