Completed
Branch master (163d15)
by
unknown
08:22
created

AdminPageFramework_PageMetaBox::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 38
rs 8.439
cc 5
eloc 25
nc 6
nop 7
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://en.michaeluno.jp/admin-page-framework/
6
 * Copyright (c) 2013-2016 Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods for creating meta boxes in pages added by the framework.
12
 * 
13
 * @abstract
14
 * @since       3.0.0
15
 * @package     AdminPageFramework
16
 * @subpackage  PageMetaBox
17
 */
18
abstract class AdminPageFramework_PageMetaBox extends AdminPageFramework_PageMetaBox_Controller {
19
    
20
    /**
21
     * Defines the class object structure type.
22
     * 
23
     * This is used to create a property object as well as to define the form element structure.
24
     * 
25
     * @since       3.0.0
26
     * @since       3.7.0      Changed the name from `$_sPropertyType`.
27
     * @since       3.7.12     Moved from `AdminPageFramework_PageMetaBox_Model`.
28
     * @internal
29
     */
30
    protected $_sStructureType = 'page_meta_box';    
31
    
32
    /**
33
     * Registers necessary hooks and internal properties.
34
     * 
35
     * <h4>Examples</h4>
36
     * <code>
37
     *     new APF_MetaBox_For_Pages_Normal(
38
     *         'apf_metabox_for_pages_normal', // meta box id
39
     *         __( 'Sample Meta Box For Admin Pages Inserted in Normal Area' ), // title
40
     *         'apf_first_page', // page slugs
41
     *         'normal', // context
42
     *         'default' // priority
43
     *     );
44
     *     include( APFDEMO_DIRNAME . '/example/APF_MetaBox_For_Pages_Advanced.php' );
45
     *     new APF_MetaBox_For_Pages_Advanced(
46
     *         'apf_metabox_for_pages_advanced', // meta box id
47
     *         __( 'Sample Meta Box For Admin Pages Inserted in Advanced Area' ), // title
48
     *         'apf_first_page', // page slugs
49
     *         'advanced', // context
50
     *         'default' // priority
51
     *     );    
52
     *     include( APFDEMO_DIRNAME . '/example/APF_MetaBox_For_Pages_Side.php' );
53
     *     new APF_MetaBox_For_Pages_Side(
54
     *         'apf_metabox_for_pages_side', // meta box id
55
     *         __( 'Sample Meta Box For Admin Pages Inserted in Advanced Area' ), // title
56
     *         array( 'apf_first_page', 'apf_second_page' ), // page slugs - setting multiple slugs is possible
57
     *         'side', // context
58
     *         'default' // priority
59
     *     );     
60
     * </code>
61
     * @since       3.0.0
62
     * 
63
     * @param       string          $sMetaBoxID     The meta box ID to be created.
64
     * @param       string          $sTitle         The meta box title.
65
     * @param       array|string    $asPageSlugs    the page slug(s) that the meta box belongs to. If the element is an array, it will be considered as a tab array.
66
     * <code>
67
     *  $asPageSlugs = array(     
68
     *      'settings' => array(     // if the key is not numeric and the value is an array, it will be considered as a tab array.
69
     *          'help',         // enabled in the tab whose slug is 'help' which belongs to the page whose slug is 'settings'
70
     *          'about',        // enabled in the tab whose slug is 'about' which belongs to the page whose slug is 'settings'
71
     *          'general',      // enabled in the tab whose slug is 'general' which belongs to the page whose slug is 'settings'
72
     *      ),
73
     *      'manage', // if the numeric key with a string value is given, the condition applies to the page slug of this string value.
74
     *  );
75
     * </code>
76
     * @param       string          $sContext       The context, either `normal`, `advanced`, or `side`.
77
     * @param       string          $sPriority      The priority, either `high`, `core`, `default` or `low`.
78
     * @param       string          $sCapability    The capability. See <a href="https://codex.wordpress.org/Roles_and_Capabilities" target="_blank">Roles and Capabilities</a>.
79
     */
80
    public function __construct( $sMetaBoxID, $sTitle, $asPageSlugs=array(), $sContext='normal', $sPriority='default', $sCapability='manage_options', $sTextDomain='admin-page-framework' ) {
81
        
82
        if ( empty( $asPageSlugs ) ) { 
83
            return; 
84
        }
85
        
86
        if ( ! $this->_isInstantiatable() ) { 
87
            return; 
88
        }
89
                
90
        // The property object needs to be done first before the parent constructor.
91
        $_sProprtyClassName = isset( $this->aSubClassNames[ 'oProp' ] )
92
            ? $this->aSubClassNames[ 'oProp' ]
93
            : 'AdminPageFramework_Property_' . $this->_sStructureType;        
94
        $this->oProp             = new $_sProprtyClassName( 
95
            $this, 
96
            get_class( $this ), 
97
            $sCapability, 
98
            $sTextDomain, 
99
            $this->_sStructureType 
100
        );
101
        
102
        // This property item must be set before the isInThePage() method is used.
103
        $this->oProp->aPageSlugs = is_string( $asPageSlugs ) 
104
            ? array( $asPageSlugs ) 
105
            : $asPageSlugs;                 
106
                
107
        parent::__construct( 
108
            $sMetaBoxID, 
109
            $sTitle, 
110
            $asPageSlugs, 
0 ignored issues
show
Bug introduced by
It seems like $asPageSlugs defined by parameter $asPageSlugs on line 80 can also be of type string; however, AdminPageFramework_MetaBox_Model::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
111
            $sContext, 
112
            $sPriority, 
113
            $sCapability,
114
            $sTextDomain 
115
        );
116
                    
117
    }
118
                
119
}
120