Completed
Pull Request — master (#14)
by Beñat
07:49
created

Acf   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A acfGetFields() 0 8 2
A serializeFeaturedImage() 0 13 2
A removeSectionsForNotAdminUsers() 0 9 1
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
namespace LIN3S\WPFoundation\Configuration\Acf;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 * @author Jon Torrado <[email protected]>
17
 */
18
abstract class Acf implements AcfInterface
19
{
20
    public function __construct()
21
    {
22
        $customToolbars = $this->wyswygToolbars();
23
        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...
24
        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...
25
            return array_merge($toolbars, $customToolbars);
26
        });
27
        add_filter('admin_menu', function () {
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
            if (!acf_current_user_can_admin()) {
29
                $this->removeSectionsForNotAdminUsers();
30
            }
31
        });
32
    }
33
34
    public function acfGetFields(array $data) : array
35
    {
36
        if (!empty($data)) {
37
            array_walk_recursive($data, ['self', 'serializeFeaturedImage']);
38
        }
39
40
        return $data;
41
    }
42
43
    public function serializeFeaturedImage(&$item) : void
44
    {
45
        if (!is_object($item)) {
46
            return null;
47
        }
48
49
        $item->featured = [
50
            'thumbnail' => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'thumbnail'),
51
            'medium'    => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'medium'),
52
            'large'     => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'full'),
53
            'xlarge'    => wp_get_attachment_image_src(get_post_thumbnail_id($item->ID), 'full'),
54
        ];
55
    }
56
57
    private function removeSectionsForNotAdminUsers() : void
58
    {
59
        remove_menu_page('profile.php');
60
        remove_menu_page('tools.php');
61
        remove_menu_page('options-general.php');
62
        remove_menu_page('edit.php');
63
        remove_menu_page('admin.php');
64
        remove_submenu_page('users.php', 'profile.php');
65
    }
66
}
67