Completed
Push — master ( 45c44e...c3b4db )
by Beñat
02:59
created

Fields::fields()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.2
cc 4
eloc 7
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WPFoundation library.
5
 *
6
 * Copyright (c) 2015 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\PostTypes\Fields;
13
14
/**
15
 * Abstract class of base custom fields that implements the interface.
16
 * This class avoids the redundant task of create the same Fields constructor.
17
 *
18
 * @author Beñat Espiña <[email protected]>
19
 */
20
abstract class Fields implements FieldsInterface
21
{
22
    /**
23
     * The template name.
24
     *
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * Array that contains the fully qualified
31
     * namespaces of the field components.
32
     *
33
     * @var array
34
     */
35
    protected $components;
36
37
    /**
38
     * Constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->components = [];
43
44
        $this->fields();
45
        $this->connector();
46
47
        if (false === is_admin()) {
48
            return;
49
        }
50
51
        $newPostId = filter_input(INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT);
52
        $updatePostId = filter_input(INPUT_POST, 'post_ID', FILTER_SANITIZE_NUMBER_INT);
53
54
        if (isset($newPostId)) {
55
            $postId = absint($newPostId);
56
        } else if (isset($updatePostId)) {
57
            $postId = absint($updatePostId);
58
        } else {
59
            return;
60
        }
61
62
        if (isset($postId)) {
63
            if ($this->name === get_post_meta($postId, '_wp_page_template', true)) {
64
                add_action('admin_init', [$this, 'removeScreenAttributes']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
65
            }
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function fields()
73
    {
74
        foreach ($this->components as $component) {
75
            if (false === class_exists($component)) {
76
                throw new \Exception(sprintf('The %s class does not exist', $component));
77
            }
78
            if ($component instanceof FieldComponent) {
79
                throw new \Exception('The %s class must be extend the FieldComponent', $component);
80
            }
81
            $component::register($this->connector(), $this->name);
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function removeScreenAttributes()
89
    {
90
    }
91
}
92