Completed
Pull Request — master (#14)
by Gorka
12:44 queued 10:15
created

Acf   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 36
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
wysiwygToolbars() 0 1 ?
A __construct() 0 8 1
A acfGetFields() 0 8 2
A serializeFeaturedImage() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015-present LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace LIN3S\WPFoundation;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 * @author Jon Torrado <[email protected]>
19
 */
20
abstract class Acf
21
{
22
    abstract public function wysiwygToolbars() : array;
23
24
    public function __construct()
25
    {
26
        $customToolbars = $this->wysiwygToolbars();
27
        add_filter('acf/rest_api/post/get_fields', [$this, 'acfGetFields']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_filter() seems unnecessary as the function has no side-effects.
Loading history...
28
        add_filter('acf/fields/wysiwyg/toolbars', function (array $toolbars) use ($customToolbars) {
0 ignored issues
show
Unused Code introduced by
The call to the function add_filter() seems unnecessary as the function has no side-effects.
Loading history...
29
            return array_merge($toolbars, $customToolbars);
30
        });
31
    }
32
33
    public function acfGetFields(array $data) : array
34
    {
35
        if (!empty($data)) {
36
            array_walk_recursive($data, ['self', 'serializeFeaturedImage']);
37
        }
38
39
        return $data;
40
    }
41
42
    public function serializeFeaturedImage(&$item) : void
43
    {
44
        if (!is_object($item)) {
45
            return null;
46
        }
47
48
        $item->featured = [
49
            'thumbnail' => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'thumbnail'),
50
            'medium'    => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'medium'),
51
            'large'     => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'full'),
52
            'xlarge'    => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'full'),
53
        ];
54
    }
55
}
56