Gallery   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
eloc 25
c 4
b 0
f 0
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A saveGalleryIds() 0 22 3
A __construct() 0 4 1
1
<?php # -*- coding: utf-8 -*-
2
declare(strict_types=1);
3
4
namespace MultisiteGlobalMedia\WooCommerce;
5
6
use MultisiteGlobalMedia\Helper;
7
use MultisiteGlobalMedia\SingleSwitcher;
8
use MultisiteGlobalMedia\Site;
9
10
/**
11
 * Class Gallery
12
 *
13
 * TODO May be we want to split admin from the frontend?
14
 */
15
class Gallery
16
{
17
    use Helper;
18
19
    const FILTER_ATTACHMENT_IMAGE_SRC = 'wp_get_attachment_image_src';
20
21
    const DEFAULT_PRODUCT_TYPE = 'simple';
22
    const POST_PRODUCT_TYPE = 'product-type';
23
    const POST_PRODUCT_IMAGE_GALLERY = 'product_image_gallery';
24
25
    const META_KEY_PRODUCT_GALLERY = '_product_image_gallery';
26
27
    /**
28
     * @var Site
29
     */
30
    private $site;
31
32
    /**
33
     * @var SingleSwitcher
34
     */
35
    private $siteSwitcher;
36
37
    /**
38
     * Gallery constructor
39
     *
40
     * @param Site $site
41
     * @param SingleSwitcher $siteSwitcher
42
     */
43
    public function __construct(Site $site, SingleSwitcher $siteSwitcher)
44
    {
45
        $this->site = $site;
46
        $this->siteSwitcher = $siteSwitcher;
47
    }
48
49
    /**
50
     * Save Gallery Ids into product meta
51
     *
52
     * @param int $productId
53
     */
54
    public function saveGalleryIds(int $productId)
55
    {
56
        $productType = \WC_Product_Factory::get_product_type($productId);
57
        $requestProductType = filter_input(
58
            INPUT_POST,
59
            self::POST_PRODUCT_TYPE,
60
            FILTER_SANITIZE_STRING
61
        );
62
63
        $requestProductType and $productType = sanitize_title(stripslashes($requestProductType));
64
65
        $productType = $productType ?: self::DEFAULT_PRODUCT_TYPE;
66
        $classname = \WC_Product_Factory::get_product_classname($productId, $productType);
67
        /** @var \WC_Product $product */
68
        $product = new $classname($productId);
69
        $attachmentIds = filter_input(
70
            INPUT_POST,
71
            self::POST_PRODUCT_IMAGE_GALLERY,
72
            FILTER_SANITIZE_STRING
73
        );
74
75
        update_post_meta($product->get_id(), self::META_KEY_PRODUCT_GALLERY, $attachmentIds);
76
    }
77
}
78