1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace LSX\Classes; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* All the image functions for the theme. |
6
|
|
|
* |
7
|
|
|
* @package LSX |
8
|
|
|
* @author LightSpeed |
9
|
|
|
* @license GPL3 |
10
|
|
|
* @link |
11
|
|
|
* @copyright 2023 LightSpeed |
12
|
|
|
*/ |
13
|
|
|
class Images { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Contructor |
17
|
|
|
*/ |
18
|
|
|
public function __construct() { |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Initiate our class. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function init() { |
27
|
|
|
add_action( 'after_setup_theme', array( $this, 'register_image_sizes' ), 10 ); |
28
|
|
|
add_filter( 'image_size_names_choose', array( $this, 'register_media_editor_sizes' ), 10, 1 ); |
29
|
|
|
add_filter( 'render_block_data', array( $this, 'render_post_image_data' ), 10, 3 ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Register our image size |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function register_image_sizes() { |
38
|
|
|
add_image_size( 'lsx-blog-thumbnail', 640, 480, array( 'center', 'center' ) ); |
39
|
|
|
add_image_size( 'lsx-product-thumbnail', 480, 480, array( 'center', 'center' ) ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add ing the image sizes to the media editor. |
44
|
|
|
* |
45
|
|
|
* @param array $sizes |
|
|
|
|
46
|
|
|
* @return void |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function register_media_editor_sizes( $sizes = array() ) { |
49
|
|
|
return array_merge( $sizes, array( |
|
|
|
|
50
|
|
|
'lsx-blog-thumbnail' => __( 'LSX Blog Thumbnail', 'lsx' ), |
51
|
|
|
) ); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the post image fields |
56
|
|
|
* |
57
|
|
|
* @param array $parsed_block The block being rendered. |
58
|
|
|
* @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content. |
59
|
|
|
* @param WP_Block|null $parent_block If this is a nested block, a reference to the parent block. |
|
|
|
|
60
|
|
|
* @return array |
61
|
|
|
*/ |
|
|
|
|
62
|
|
|
|
63
|
|
|
public function render_post_image_data( $parsed_block, $source_block, $parent_block ) { |
|
|
|
|
64
|
|
|
if ( ! is_home() || !is_front_page() || is_archive() && 'core/post-featured-image' === $parsed_block['blockName'] ) { |
|
|
|
|
65
|
|
|
$parsed_block['attrs']['sizeSlug'] = 'lsx-blog-thumbnail'; |
66
|
|
|
} |
67
|
|
|
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() && 'woocommerce/product-image' === $parsed_block['blockName'] ) { |
68
|
|
|
$parsed_block['attrs']['sizeSlug'] = 'lsx-product-thumbnail'; |
69
|
|
|
} |
70
|
|
|
return $parsed_block; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|