Completed
Branch dev (869f5d)
by
unknown
04:33
created

getFieldValue()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 8
nop 2
dl 0
loc 25
rs 5.3846
c 0
b 0
f 0
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 public methods to add form elements. 
12
 *
13
 * @abstract
14
 * @since           2.0.0
15
 * @since           3.3.1       Changed the name from `AdminPageFramework_Setting`.
16
 * @since           3.6.3       Changed the name from `AdminPageFramework_Form_Controller`.
17
 * @extends         AdminPageFramework_View_Form
18
 * @package         AdminPageFramework
19
 * @subpackage      AdminPage
20
 */
21
abstract class AdminPageFramework_Controller_Form extends AdminPageFramework_View_Form {
22
                                    
23
    /**
24
     * {@inheritdoc}
25
     * 
26
     * {@inheritdoc}
27
     * 
28
     * <h4>Example</h4>
29
     * <code>$this->addSettingSections(
30
     *      array(
31
     *          'section_id'    => 'text_fields',
32
     *          'page_slug'     => 'first_page',
33
     *          'tab_slug'      => 'textfields',
34
     *          'title'         => 'Text Fields',
35
     *          'description'   => 'These are text type fields.',
36
     *          'order'         => 10,
37
     *      ),    
38
     *      array(
39
     *          'section_id'    => 'selectors',
40
     *          'page_slug'     => 'first_page',
41
     *          'tab_slug'      => 'selectors',
42
     *          'title'         => 'Selectors',
43
     *          'description'   => 'These are selector type options such as dropdown lists, radio buttons, and checkboxes',
44
     *      )
45
     * );</code>
46
     *
47
     * @since       2.0.0
48
     * @since       3.0.0           Changed the scope to public from protected.
49
     * @since       3.5.3           Removed the parameter declarations as they are caught with the func_get_args().
50
     * @access      public
51
     * @remark      Accepts variadic parameters; the number of accepted parameters are not limited to three.
52
     * @remark      The actual registration will be performed in the <em>_replyToRegisterSettings()</em> method with the <em>admin_menu</em> hook.
53
     * @remark      The target section tab slug and the target tab slug will be reset once the method returns.
54
     * @param       array|string    the section array or the target page slug. If the target page slug is set, the next section array can omit the page slug key.
55
     * <h4>Section Array</h4>
56
     * <ul>
57
     *      <li>**section_id** - (required, string) the section ID. Avoid using non-alphabetic characters except underscore and numbers.</li>
58
     *      <li>**page_slug** - (optional, string) the page slug that the section belongs to. If the target page slug is set, it can be omitted.</li>
59
     *      <li>**tab_slug** - (optional, string) the tab slug that the section belongs to. The tab here refers to in-page tabs.</li>
60
     *      <li>**section_tab_slug** - (optional, string) [3.0.0+] the section tab slug that the section are grouped into. The tab here refers to section tabs.</li>
61
     *      <li>**title** - (optional, string) the title of the section.</li>
62
     *      <li>**capability** - (optional, string) the <a href="http://codex.wordpress.org/Roles_and_Capabilities">access level</a> of the section. If the page visitor does not have sufficient capability, the section will be invisible to them.</li>
63
     *      <li>**if** - (optional, boolean) if the passed value is false, the section will not be registered.</li>
64
     *      <li>**order** - (optional, integer) the order number of the section. The higher the number is, the lower the position it gets.</li>
65
     *      <li>**help** - (optional, string) the help description added to the contextual help tab.</li>
66
     *      <li>**help_aside** - (optional, string) the additional help description for the side bar of the contextual help tab.</li>
67
     *      <li>**repeatable** - (optional, boolean|array) [3.0.0+] Indicates whether or not the section is repeatable. To set a minimum/maximum number of sections, pass an array with the key, `min`, and `max`. e.g. `array( 'min' => 3, 'max' => 10 )`</li>
68
     * </ul>
69
     * @param       array           (optional) another section array.
70
     * @param       array           (optional) add more section array to the next parameters as many as necessary.
71
     * @return      void
72
     */     
73
    public function addSettingSections( /* $aSection1, $aSection2=null, $_and_more=null */ ) {
74
        
75
        foreach( func_get_args() as $asSection ) { 
76
            $this->addSettingSection( $asSection ); 
77
        }
78
        
79
        // reset the stored target tab slug and the target section tab slug
80
        $this->_sTargetTabSlug          = null;
81
        $this->_sTargetSectionTabSlug   = null;
82
        
83
    }
84
    
85
    /**
86
     * A singular form of the adSettingSections() method which takes only a single parameter.
87
     * 
88
     * This is useful when adding section arrays in loops.
89
     * 
90
     * @since       2.1.2
91
     * @since       3.0.0           Changed the scope to public from protected.
92
     * @access      public
93
     * @param       array|string    the section array. If a string is passed, it is considered as a target page slug that will be used as a page slug element from the next call so that the element can be omitted.
94
     * @return      void
95
     */
96
    public function addSettingSection( $asSection ) {
97
                
98
        if ( ! is_array( $asSection ) ) {
99
            $this->_sTargetPageSlug = is_string( $asSection )
100
                ? $asSection 
101
                : $this->_sTargetPageSlug;
102
            return;
103
        } 
104
        
105
        $aSection = $asSection;
106
        $this->_sTargetPageSlug         = $this->_getTargetPageSlug( $aSection );
107
        $this->_sTargetTabSlug          = $this->_getTargetTabSlug( $aSection );
108
        $this->_sTargetSectionTabSlug   = $this->oUtil->getElement( $aSection, 'section_tab_slug', $this->_sTargetSectionTabSlug );
109
        
110
        // Pre-format - avoid undefined index warnings.
111
        $aSection = $this->oUtil->uniteArrays( 
112
            $aSection, 
113
            array( 
114
                'page_slug'         => $this->_sTargetPageSlug, 
115
                'tab_slug'          => $this->_sTargetTabSlug, 
116
                'section_tab_slug'  => $this->_sTargetSectionTabSlug, 
117
            )
118
        ); 
119
        
120
        $aSection[ 'section_tab_slug' ]   = $this->oUtil->sanitizeSlug( $aSection[ 'section_tab_slug' ] );
121
        
122
        // A page slug is required.
123
        if ( ! $aSection[ 'page_slug' ] ) {
124
            return; 
125
        }
126
        $this->oForm->addSection( $aSection );
127
        
128
    }
129
        /**
130
         * Sets the target page slug of the section.
131
         * If a page slug is not set, set the currently loading one from the registered items.
132
         * Do not simply assign the currently loaded page but the one added by the user because the method can be called from anyware.
133
         * @since       3.7.2
134
         * @return      string
135
         */
136
        private function _getTargetPageSlug( $aSection ) {
137
            
138
            $_sTargetPageSlug = $this->oUtil->getElement( 
139
                $aSection,      // subject
140
                'page_slug',    // key
141
                $this->_sTargetPageSlug     // default
142
            );
143
         
144
            $_sTargetPageSlug = $_sTargetPageSlug
145
                ? $this->oUtil->sanitizeSlug( $_sTargetPageSlug ) 
146
                : $this->oProp->getCurrentPageSlugIfAdded();
147
            return $_sTargetPageSlug;
148
            
149
        }
150
            
151
        /**
152
         * Sets the target tab slug of the section.
153
         * If a tab slug is not set, set the currently loading one from the registered items.
154
         * Do not simply assign the currently loaded page but the one added by the user because the method can be called from anyware.
155
         * @return      string
156
         * @since       3.7.2
157
         */
158
        private function _getTargetTabSlug( $aSection ) {
159
            $_sTargetTabSlug = $this->oUtil->getElement( 
160
                $aSection,              // subject
161
                'tab_slug',             // key
162
                $this->_sTargetTabSlug  // default
163
            );
164
            $_sTargetTabSlug = $_sTargetTabSlug
165
                ? $this->oUtil->sanitizeSlug( $aSection[ 'tab_slug' ] )
166
                : $this->oProp->getCurrentInPageTabSlugIfAdded();
167
            return $_sTargetTabSlug;
168
        }    
169
    
170
    /**
171
    * Removes the given section(s) by section ID.
172
    * 
173
    * This accesses the property storing the added section arrays and removes the specified ones.
174
    * 
175
    * <h4>Example</h4>
176
    * <code>$this->removeSettingSections( 'text_fields', 'selectors', 'another_section', 'yet_another_section' );
177
    * </code>
178
    * 
179
    * @since        2.0.0
180
    * @since        3.0.0       Changed the scope to public from protected.
181
    * @since        3.5.3       Removed the parameter declarations as they are caught with the func_get_args().
182
    * @access       public
183
    * @remark       Accepts variadic parameters; the number of accepted parameters are not limited to three.
184
    * @param        string      $sSectionID1        the section ID to remove.
0 ignored issues
show
Bug introduced by
There is no parameter named $sSectionID1. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
185
    * @param        string      $sSectionID2        (optional) another section ID to remove.
0 ignored issues
show
Bug introduced by
There is no parameter named $sSectionID2. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
186
    * @param        string      $_and_more          (optional) add more section IDs to the next parameters as many as necessary.
0 ignored issues
show
Bug introduced by
There is no parameter named $_and_more. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
    * @return       void
188
    */    
189
    public function removeSettingSections( /* $sSectionID1=null, $sSectionID2=null, $_and_more=null */ ) {    
190
        foreach( func_get_args() as $_sSectionID ) {
191
            $this->oForm->removeSection( $_sSectionID );
192
        }        
193
    }
194
    
195
    /**
196
     * {@inheritdoc}
197
     * 
198
     * {@inheritdoc}
199
     * 
200
     * <h4>Example</h4>
201
     * <code>$this->addSettingFields(
202
     *      array(
203
     *          'field_id'      => 'text',
204
     *          'section_id'    => 'text_fields',
205
     *          'title'         => __( 'Text', 'my-text-domain' ),
206
     *          'description'   => __( 'Type something here.', 'my-text-domain' ),
207
     *          'type'          => 'text',
208
     *          'order'         => 1,
209
     *          'default'       => 123456,
210
     *      ),    
211
     *      array(
212
     *          'field_id'      => 'text_multiple',
213
     *          'section_id'    => 'text_fields',
214
     *          'title'         => __( 'Multiple Text Fields', 'my-text-domain' ),
215
     *          'description'   => __( 'These are multiple text fields.', 'my-text-domain' ),
216
     *          'type'          => 'text',
217
     *          'order'         => 2,
218
     *          'default'       => __( 'Hello World', 'my-text-domain' ),
219
     *          'label'         => __( 'First Item', 'my-text-domain' ),
220
     *          'attributes' => array(
221
     *              'size' => 30
222
     *          ),
223
     *          array(
224
     *              'label'         => __( 'Second Item', 'my-text-domain' ),
225
     *              'default'       => __( 'Foo bar', 'my-text-domain' ),
226
     *              'attributes'    => array(
227
     *                  'size' => 60,
228
     *              ),
229
     *          ),
230
     *          array(
231
     *              'label'         => __( 'Third Item', 'my-text-domain' ),
232
     *              'default'       => __( 'Yes, we can.', 'my-text-domain' ),
233
     *              'attributes' => array(
234
     *                  'size' => 90,
235
     *              ),
236
     *          ),
237
     *      )
238
     * );</code>
239
     * 
240
     * @since       2.0.0
241
     * @since       3.0.0       Changed the scope to public from protected.
242
     * @since       3.5.3       Removed the parameter declarations as they are caught with the func_get_args().
243
     * @access      public
244
     * @remark      Accepts variadic parameters; the number of accepted parameters are not limited to three.
245
     * @remark      The actual registration will be performed in the <em>_replyToRegisterSettings()</em> method with the <em>admin_menu</em> hook.
246
     */     
247
    public function addSettingFields( /* $aField1, $aField2=null, $_and_more=null */ ) {    
248
        foreach( func_get_args() as $aField ) { 
249
            $this->addSettingField( $aField ); 
250
        }
251
    }
252
    /**
253
    * Adds the given field array items into the field array property.
254
    * 
255
    * Identical to the `addSettingFields()` method except that this method does not accept enumerated parameters. 
256
    * 
257
    * @since        2.1.2
258
    * @since        3.0.0           Changed the scope to public from protected.
259
    * @access       public
260
    * @param        array|string    $asField        the field array or the target section ID. If the target section ID is set, the section_id key can be omitted from the next passing field array.
261
    * @return       void
262
    */    
263
    public function addSettingField( $asField ) {
264
        $this->oForm->addField( $asField );    
265
    }    
266
    
267
    /**
268
    * Removes the given field(s) by field ID.
269
    * 
270
    * This accesses the property storing the added field arrays and removes the specified ones.
271
    * 
272
    * <h4>Example</h4>
273
    * <code>$this->removeSettingFields( 'fieldID_A', 'fieldID_B', 'fieldID_C', 'fieldID_D' );
274
    * </code>
275
    * 
276
    * @since        2.0.0
277
    * @since        3.0.0       Changed the scope to public from protected.
278
    * @access       public
279
    * @remark       Accepts variadic parameters; the number of accepted parameters are not limited to three.
280
    * @param        string      $sFieldID1      the field ID to remove.
281
    * @param        string      $sFieldID2      (optional) another field ID to remove.
282
    * @param        string      $_and_more      (optional) add more field IDs to the next parameters as many as necessary.
283
    * @return void
284
    */    
285
    public function removeSettingFields( $sFieldID1, $sFieldID2=null, $_and_more ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sFieldID1 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $sFieldID2 is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $_and_more is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
286
        foreach( func_get_args() as $_sFieldID ) { 
287
            $this->oForm->removeField( $_sFieldID ); 
288
        }
289
    }    
290
            
291
    /**
292
     * Retrieves the specified field value stored in the options by field ID.
293
     * 
294
     * <h4>Example</h4>
295
     * <code>
296
     *  $this->addSettingFields(
297
     *      'number_section',  // section id
298
     *      array( 
299
     *          'field_id'          => 'preset_field',
300
     *          'title'             => __( 'Preset', 'admin-page-framework-demo' ),
301
     *          'type'              => 'number',
302
     *      ),
303
     *      array( 
304
     *          'field_id'          => 'value_based_on_preset',
305
     *          'title'             => __( 'Value Based on Preset', 'admin-page-framework-demo' ),
306
     *          'type'              => 'number',
307
     *          'default'           => 10 + ( int ) $this->getValue( 
308
     *              'number_section',   // section id
309
     *              'preset_field'      // field id
310
     *          ),
311
     *      ),    
312
     *  );
313
     * </code>
314
     * 
315
     * @since       3.3.0
316
     * @since       3.3.5       Made it respect last input arrays.
317
     * @since       3.5.3       When a parameter is not set, it returns the entire options.
318
     * @param       The key that points the dimensional array key of the options array.
319
     */
320
    public function getValue( /* $sDimensionalKey1, $sDimensionalKey2 ... or $aDimensionalKeys, $mDefault */ ) {
321
        
322
        $_aParams            = func_get_args();
323
        $_aDimensionalKeys   = $_aParams + array( null, null );
324
        $_mDefault           = null;
325
        if ( is_array( $_aDimensionalKeys[ 0 ] ) ) {
326
            $_mDefault         = $_aDimensionalKeys[ 1 ];
327
            $_aDimensionalKeys = $_aDimensionalKeys[ 0 ];
328
        }
329
        return AdminPageFramework_WPUtility::getOption( 
330
            $this->oProp->sOptionKey, 
331
            empty( $_aParams ) 
332
                ? null                  // will return the entire options array
333
                : $_aDimensionalKeys,   // dimensional keys
334
            $_mDefault, // default
335
            $this->getSavedOptions() + $this->oForm->getDefaultFormValues()
0 ignored issues
show
Deprecated Code introduced by
The method AdminPageFramework_Facto...odel::getSavedOptions() has been deprecated with message: 3.7.0 Kept for backward compatibility.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
336
        );
337
        
338
    }
339
            
340
    /**
341
     * Retrieves the specified field value stored in the options by field ID.
342
     *  
343
     * @since       2.1.2
344
     * @since       3.0.0       Changed the scope to public from protected. Dropped the sections. Made it return a default value even if it's not saved in the database.
345
     * @access      public
346
     * @param       string      $sFieldID         The field ID.
347
     * @param       string      $sSectionID       The section ID.
348
     * @return      array|string|null       If the field ID is not set in the saved option array, it will return null. Otherwise, the set value.
349
     * If the user has not submitted the form, the framework will try to return the default value set in the field definition array.
350
     * @deprecated  3.3.0
351
     */
352
    public function getFieldValue( $sFieldID, $sSectionID='' ) {
353
                               
354
        trigger_error( 'Admin Page Framework: ' . ' : ' . sprintf( __( 'The method is deprecated: %1$s. Use %2$s instead.', $this->oProp->sTextDomain ), __METHOD__, 'getValue()' ), E_USER_NOTICE );
355
    
356
        $_aOptions = $this->oUtil->uniteArrays( $this->oProp->aOptions, $this->oForm->getDefaultFormValues() );
357
        /* If it's saved, return it */
358
        if ( ! $sSectionID ) {
359
            if ( array_key_exists( $sFieldID, $_aOptions ) ) {
360
                return $_aOptions[ $sFieldID ];
361
            }    
362
            // loop through section elements
363
            foreach( $_aOptions as $aOptions ) {
364
                if ( array_key_exists( $sFieldID, $aOptions ) ) {
365
                    return $aOptions[ $sFieldID ];
366
                }
367
            }
368
        }
369
        if ( $sSectionID ) {
370
            if ( array_key_exists( $sSectionID, $_aOptions ) && array_key_exists( $sFieldID, $_aOptions[ $sSectionID ] ) ) {
371
                return $_aOptions[ $sSectionID ][ $sFieldID ];
372
            }
373
        }
374
        return null;
375
                    
376
    }
377
            
378
}
379