| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_woocommerce |
| 5 |
* |
| 6 |
* @reason Adding flags for ignoring the lazyloaded tags. |
| 7 |
*/ |
| 8 |
class Optml_woocommerce extends Optml_compatibility { |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Should we load the integration logic. |
| 13 |
* |
| 14 |
* @return bool Should we load. |
| 15 |
*/ |
| 16 |
public function should_load() { |
| 17 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 18 |
return is_plugin_active( 'woocommerce/woocommerce.php' ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register integration details. |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
if ( Optml_Main::instance()->admin->settings->use_lazyload() ) { |
| 26 |
add_filter( 'optml_lazyload_early_flags', [ $this, 'add_lazyload_early_flag' ], PHP_INT_MAX, 1 ); |
| 27 |
} |
| 28 |
} |
| 29 |
/** |
| 30 |
* Add ignore lazyload flag. |
| 31 |
* |
| 32 |
* @param array $flags Old flags. |
| 33 |
* |
| 34 |
* @return array New flags. |
| 35 |
*/ |
| 36 |
public function add_lazyload_early_flag( $flags = [] ) { |
| 37 |
$flags[] = 'data-large_image'; |
| 38 |
|
| 39 |
return $flags; |
| 40 |
} |
| 41 |
/** |
| 42 |
* Ads the product pages to the list of posts parents when querying images for offload. |
| 43 |
* |
| 44 |
* @param array $parents Default post parents. |
| 45 |
* |
| 46 |
* @return array New post parents that include product pages. |
| 47 |
*/ |
| 48 |
public function add_product_pages_to_image_query( $parents = [ 0 ] ) { |
| 49 |
$paged = 1; |
| 50 |
$query_args = [ |
| 51 |
'post_type' => 'product', |
| 52 |
'fields' => 'ids', |
| 53 |
'posts_per_page' => 150, |
| 54 |
'post_status' => 'publish', |
| 55 |
'paged' => $paged, |
| 56 |
]; |
| 57 |
$products = new \WP_Query( $query_args ); |
| 58 |
$ids = $products->get_posts(); |
| 59 |
while ( ! empty( $ids ) ) { |
| 60 |
++$paged; |
| 61 |
$parents = array_merge( $parents, $ids ); |
| 62 |
$query_args['paged'] = $paged; |
| 63 |
$products = new \WP_Query( $query_args ); |
| 64 |
$ids = $products->get_posts(); |
| 65 |
} |
| 66 |
|
| 67 |
return $parents; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Should we early load the compatibility? |
| 71 |
* |
| 72 |
* @return bool Whether to load the compatibility or not. |
| 73 |
*/ |
| 74 |
public function should_load_early() { |
| 75 |
if ( Optml_Main::instance()->admin->settings->get( 'offload_media' ) === 'enabled' ) { |
| 76 |
return true; |
| 77 |
} |
| 78 |
return false; |
| 79 |
} |
| 80 |
} |
| 81 |
|