Completed
Push — master ( 6f8a68...2f55bf )
by Askupa
02:00
created

Manager.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Amarkal\Metabox;
4
5
/**
6
 * Metabox Manager adds metaboxes to WordPress posts
7
 */
8
class Manager
9
{
10
    /**
11
     * @var Singleton The reference to *Singleton* instance of this class
12
     */
13
    private static $instance;
14
    
15
    /**
16
     * @var Array Stores all the registered metaboxes
17
     */
18
    private $metaboxes = array();
19
    
20
    /**
21
     * Returns the *Singleton* instance of this class.
22
     *
23
     * @return Singleton The *Singleton* instance.
24
     */
25
    public static function get_instance()
26
    {
27
        if( null === static::$instance ) 
28
        {
29
            static::$instance = new static();
30
            static::$instance->init();
31
        }
32
        return static::$instance;
33
    }
34
    
35
    public function add( $id, $args )
36
    {
37
        if( !in_array($id, $this->metaboxes) )
38
        {
39
            $this->metaboxes[$id] = array_merge($this->default_args(), $args);
40
        }
41
        else throw new \RuntimeException("A metabox with id '$id' has already been registered.");
42
    }
43
    
44
    public function render( $post, $args )
45
    {
46
        foreach( $this->metaboxes[$args['id']]['fields'] as $field )
47
        {
48
            $field_template = new Field($field);
49
            echo $field_template->render();
50
        }
51
    }
52
    
53
    public function add_meta_boxes()
54
    {
55
        foreach( $this->metaboxes as $id => $args )
56
        {
57
            \add_meta_box(
58
                $id,
59
                $args['title'],
60
                array($this, 'render'),
61
                $args['screen'],
62
                $args['context'],
63
                $args['priority']
64
            );
65
        }
66
    }
67
    
68
    public function save_meta_boxes()
69
    {
70
        
71
    }
72
    
73
    function print_style() 
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
74
    {
75
        $cs = get_current_screen();
76
        
77
        foreach( $this->metaboxes as $metabox )
78
        {
79
            if( $metabox['screen'] === $cs->id )
80
            {
81
                echo '<style>';
82
                include 'metabox.css';
83
                echo '</style>';
84
                return;
85
            }
86
        }
87
    }
88
    
89
    private function init()
90
    {
91
        \add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
92
        \add_action( 'save_post', array( $this, 'save_meta_boxes' ) );
93
        \add_action( 'admin_footer', array( $this, 'print_style' ) );
94
    }
95
    
96
    private function default_args()
97
    {
98
        return array(
99
            'id'       => null,
100
            'title'    => null,
101
            'screen'   => null,
102
            'context'  => 'advanced',
103
            'priority' => 'default',
104
            'fields'   => array()
105
        );
106
    }
107
}