MatchImageBySKU.php
81 lines
| 1 | <?php |
| 2 | /** |
| 3 | * MatchImageBySKU class file. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\ProductImage; |
| 7 | |
| 8 | use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Class for the product image matching by SKU. |
| 14 | */ |
| 15 | class MatchImageBySKU { |
| 16 | |
| 17 | use AccessiblePrivateMethods; |
| 18 | |
| 19 | /** |
| 20 | * The name of the setting for this feature. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private $setting_name = 'woocommerce_product_match_featured_image_by_sku'; |
| 25 | |
| 26 | /** |
| 27 | * MatchImageBySKU constructor. |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | $this->init_hooks(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Initialize the hooks used by the class. |
| 35 | */ |
| 36 | private function init_hooks() { |
| 37 | self::add_filter( 'woocommerce_get_settings_products', array( $this, 'add_product_image_sku_setting' ), 110, 2 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Is this feature enabled. |
| 42 | * |
| 43 | * @since 8.3.0 |
| 44 | * @return bool |
| 45 | */ |
| 46 | public function is_enabled() { |
| 47 | return wc_string_to_bool( get_option( $this->setting_name ) ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Handler for 'woocommerce_get_settings_products', adds the settings related to the product image SKU matching table. |
| 52 | * |
| 53 | * @param array $settings Original settings configuration array. |
| 54 | * @param string $section_id Settings section identifier. |
| 55 | * @return array New settings configuration array. |
| 56 | */ |
| 57 | private function add_product_image_sku_setting( array $settings, string $section_id ): array { |
| 58 | if ( 'advanced' !== $section_id ) { |
| 59 | return $settings; |
| 60 | } |
| 61 | |
| 62 | $settings[] = array( |
| 63 | 'title' => __( 'Product image matching by SKU', 'woocommerce' ), |
| 64 | 'type' => 'title', |
| 65 | ); |
| 66 | |
| 67 | $settings[] = array( |
| 68 | 'title' => __( 'Match images', 'woocommerce' ), |
| 69 | 'desc' => __( 'Set product featured image when uploaded image file name matches product SKU.', 'woocommerce' ), |
| 70 | 'id' => $this->setting_name, |
| 71 | 'default' => 'no', |
| 72 | 'type' => 'checkbox', |
| 73 | 'checkboxgroup' => 'start', |
| 74 | ); |
| 75 | |
| 76 | $settings[] = array( 'type' => 'sectionend' ); |
| 77 | |
| 78 | return $settings; |
| 79 | } |
| 80 | } |
| 81 |