getOtherTabOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 21
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
/**
3
 * Admin Page Framework
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed MIT
7
 *
8
 */
9
10
/**
11
 * Provides methods to build forms of the `admin_page` structure type.
12
 *
13
 * The suffix of `_admin_page` represents the structure type of the form.
14
 *
15
 * @package     AdminPageFramework/Factory/AdminPage/Form
16
 * @since       3.7.0
17
 * @extends     AdminPageFramework_Form
18
 * @internal
19
 */
20
class AdminPageFramework_Form_admin_page extends AdminPageFramework_Form {
21
22
    /**
23
     * Retrieves the stored options of the given page slug.
24
     *
25
     * The other pages' option data will not be contained in the returning array.
26
     * This is used to pass the old option array to the validation callback method.
27
     *
28
     * @since       2.0.0
29
     * @since       3.0.0       Moved from the settings class.
30
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
31
     * @remark      Consider the possibility that page meta box's values are included in the `$aOptions` array.
32
     * So rather than storing the page-matching elements, drop the unmatched elements
33
     * so that the externally injected options will be respected.
34
     * @return      array     the stored options of the given page slug. If not found, an empty array will be returned.
35
     */
36
    public function getPageOptions( $aOptions, $sPageSlug ) {
37
        $_aOtherPageOptions = $this->getOtherPageOptions( $aOptions, $sPageSlug );
38
        return $this->invertCastArrayContents( $aOptions, $_aOtherPageOptions );
39
    }
40
41
    /**
42
     * Retrieves the saved options of the given page slug.
43
     *
44
     * This is a stricter version of the `getPageOptions()` method.
45
     * This method does not respect the injected elements by the page meta box class.
46
     *
47
     * @since       3.0.0
48
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
49
     * @return      array
50
     */
51
    public function getPageOnlyOptions( $aOptions, $sPageSlug ) {
52
53
        $_aStoredOptionsOfThePage = array();
54
        foreach( $this->aFieldsets as $_sSectionID => $_aSubSectionsOrFields ) {
55
56
            // Check the section
57
            if ( ! $this->_isThisSectionSetToThisPage( $_sSectionID, $sPageSlug ) ) {
58
                continue;
59
            }
60
61
            // At this point, the element belongs the given page slug as the section is of the given page slug's.
62
            $this->_setPageOnlyOptions(
63
                $_aStoredOptionsOfThePage,  // by reference - gets updated in the method.
64
                $aOptions,
65
                $_aSubSectionsOrFields,
66
                $sPageSlug,
67
                $_sSectionID
68
            );
69
70
        }
71
        return $_aStoredOptionsOfThePage;
72
73
    }
74
        /**
75
         * Updates the first parameter array holding page only options.
76
         *
77
         * @since       3.5.3
78
         * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
79
         * @return      void
80
         * @internal
81
         */
82
        private function _setPageOnlyOptions( array &$_aStoredOptionsOfThePage, array $aOptions, array $_aSubSectionsOrFields, $sPageSlug, $_sSectionID ) {
83
            foreach( $_aSubSectionsOrFields as $_sFieldID => $_aFieldset ) {
84
85
                // If it's a sub-section array,
86
                if ( $this->isNumericInteger( $_sFieldID ) ) {
87
88
                    $this->_setOptionValue(
89
                        $_aStoredOptionsOfThePage,
90
                        $_sSectionID,
91
                        $aOptions
92
                    );
93
94
                    // @deprecated
95
                    // if ( array_key_exists( $_sSectionID, $aOptions ) ) {
96
                        // $_aStoredOptionsOfThePage[ $_sSectionID ] = $aOptions[ $_sSectionID ];
97
                    // }
98
                    continue;
99
                }
100
101
                // At this point, a section is set.
102
                $_aFieldset = $_aFieldset + array(
103
                    'section_id' => null,
104
                    'field_id'   => null,
105
                    'page_slug'  => null,
106
                );
107
                // @todo Examine whether this check can be removed
108
                // as the section that hods this field is already checked above outside this loop.
109
                if ( $sPageSlug !== $_aFieldset[ 'page_slug' ] ) {
110
                    continue;
111
                }
112
113
                if ( '_default' !== $_aFieldset[ 'section_id' ] ) {
114
115
                    $this->_setOptionValue(
116
                        $_aStoredOptionsOfThePage,
117
                        $_aFieldset[ 'section_id' ],
118
                        $aOptions
119
                    );
120
121
                    // @deprecated
122
                    // if ( array_key_exists( $_aFieldset[ 'section_id' ], $aOptions ) ) {
123
                        // $_aStoredOptionsOfThePage[ $_aFieldset[ 'section_id' ] ] = $aOptions[ $_aFieldset[ 'section_id' ] ];
124
                    // }
125
                    continue;
126
                }
127
128
                // It does not have a section so set the field id as its key.
129
                $this->_setOptionValue(
130
                    $_aStoredOptionsOfThePage,
131
                    $_aFieldset[ 'field_id' ],
132
                    $aOptions
133
                );
134
                // @deprecated
135
                // if ( array_key_exists( $_aFieldset[ 'field_id' ], $aOptions ) ) {
136
                    // $_aStoredOptionsOfThePage[ $_aFieldset[ 'field_id' ] ] = $aOptions[ $_aFieldset[ 'field_id' ] ];
137
                // }
138
139
            }
140
        }
141
142
    /**
143
     * Retrieves the stored options excluding the key of the given page slug.
144
     *
145
     * This is used to merge the submitted form input data with the previously stored option data except the given page.
146
     *
147
     * @since       2.0.0
148
     * @since       3.0.0     Moved from the settings class.
149
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
150
     * @return      array     An array storing the options excluding the key of the given page slug.
151
     */
152
    public function getOtherPageOptions( $aOptions, $sPageSlug ) {
153
154
        $_aStoredOptionsNotOfThePage = array();
155
        foreach( $this->aFieldsets as $_sSectionID => $_aSubSectionsOrFields ) {
156
157
            // Check the section
158
            if ( $this->_isThisSectionSetToThisPage( $_sSectionID, $sPageSlug ) ) {
159
                continue;
160
            }
161
162
            // At this point, the parsing element does not belong to the given page slug as the section does not ( as it is checked above ).
163
            $this->_setOtherPageOptions(
164
                $_aStoredOptionsNotOfThePage,
165
                $aOptions,
166
                $_aSubSectionsOrFields,
167
                $sPageSlug
168
            );
169
170
        }
171
172
        return $_aStoredOptionsNotOfThePage;
173
174
    }
175
        /**
176
         * Updates the first parameter array holding the other page options.
177
         *
178
         * @since       3.5.3
179
         * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
180
         * @return      void
181
         * @internal
182
         */
183
        private function _setOtherPageOptions( array &$_aStoredOptionsNotOfThePage, array $aOptions, array $_aSubSectionsOrFields, $sPageSlug ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sPageSlug is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

183
        private function _setOtherPageOptions( array &$_aStoredOptionsNotOfThePage, array $aOptions, array $_aSubSectionsOrFields, /** @scrutinizer ignore-unused */ $sPageSlug ) {

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

Loading history...
184
            foreach( $_aSubSectionsOrFields as $_sFieldID => $_aFieldset ) {
185
186
                // It's a sub-section array.
187
                if ( $this->isNumericInteger( $_sFieldID ) ) {
188
                    continue;
189
                }
190
191
                // @todo Examine whether this check can be removed
192
                // as the section that holds this field is already checked above outside the loop.
193
                // if ( $sPageSlug === $_aFieldset[ 'page_slug' ] ) {
194
                    // continue;
195
                // }
196
197
                // If a section is set,
198
                if ( '_default' !== $_aFieldset[ 'section_id' ] ) {
199
200
                    $this->_setOptionValue(
201
                        $_aStoredOptionsNotOfThePage,
202
                        $_aFieldset[ 'section_id' ],
203
                        $aOptions
204
                    );
205
206
                    // @deprecated
207
                    // if ( array_key_exists( $_aFieldset[ 'section_id' ], $aOptions ) ) {
208
                        // $_aStoredOptionsNotOfThePage[ $_aFieldset[ 'section_id' ] ] = $aOptions[ $_aFieldset[ 'section_id' ] ];
209
                    // }
210
                    continue;
211
                }
212
213
                // It does not have a section
214
                $this->_setOptionValue(
215
                    $_aStoredOptionsNotOfThePage,
216
                    $_aFieldset[ 'field_id' ],
217
                    $aOptions
218
                );
219
                // @deprecated
220
                // if ( array_key_exists( $_aFieldset[ 'field_id' ], $aOptions ) ) {
221
                    // $_aStoredOptionsNotOfThePage[ $_aFieldset[ 'field_id' ] ] = $aOptions[ $_aFieldset[ 'field_id' ] ];
222
                // }
223
224
            }
225
        }
226
    /**
227
     * Returns the options excluding the currently specified tab's sections and their fields.
228
     *
229
     * This is used to merge the submitted form data with the previously stored option data of the form elements
230
     * that belong to the in-page tab of the given page.
231
     *
232
     * @remark      Note that this method will return the other pages' option elements as well.
233
     *
234
     * @since       2.0.0
235
     * @since       3.0.0       The second parameter was changed to a tab slug. Moved from the settings class.
236
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
237
     * @param       array       $aOptions      the options array. Note that the options array structure are very similar to the aFieldsets array.
238
     * However, it does not have the `_default` section key.
239
     * @param       string      $sPageSlug     the page slug to check
240
     * @param       string      $sTabSlug      the tab slug to check
241
     * @return      array       the stored options excluding the currently specified tab's sections and their fields.
242
     * If not found, an empty array will be returned.
243
     */
244
    public function getOtherTabOptions( $aOptions, $sPageSlug, $sTabSlug ) {
245
246
        $_aStoredOptionsNotOfTheTab = array();
247
        foreach( $this->aFieldsets as $_sSectionPath => $_aSubSectionsOrFields ) {
248
249
            // If the section is of the given page and the given tab, skip.
250
            if ( $this->_isThisSectionSetToThisTab( $_sSectionPath, $sPageSlug, $sTabSlug ) ) {
251
                continue;
252
            }
253
254
            // At this point, the passed element belongs to the other tabs since the section of the given tab is skipped.
255
            $this->_setOtherTabOptions(
256
                $_aStoredOptionsNotOfTheTab,
257
                $aOptions,
258
                $_aSubSectionsOrFields,
259
                $_sSectionPath
260
            );
261
262
        }
263
264
        return $_aStoredOptionsNotOfTheTab;
265
266
    }
267
        /**
268
         * Updates the first parameter array holding the other tab options.
269
         *
270
         * @since       3.5.3
271
         * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
272
         * @return      void
273
         * @internal
274
         */
275
        private function _setOtherTabOptions( array &$_aStoredOptionsNotOfTheTab, array $aOptions, array $_aSubSectionsOrFields, $sSectionPath ) {
276
277
            // At this point, the passed element belongs to the other tabs since the section of the given tab is skipped.
278
            foreach ( $_aSubSectionsOrFields as $_isSubSectionIndexOrFieldID => $_aSubSectionOrField  ) {
279
280
                // If it's a sub section
281
                if ( $this->isNumericInteger( $_isSubSectionIndexOrFieldID ) ) {
282
283
                    // Store the entire section
284
                    $this->_setOptionValue(
285
                        $_aStoredOptionsNotOfTheTab,
286
                        $sSectionPath,
287
                        $aOptions
288
                    );
289
                    // @deprecated
290
                    // if ( array_key_exists( $sSectionPath, $aOptions ) ) {
291
                        // $_aStoredOptionsNotOfTheTab[ $sSectionPath ] = $aOptions[ $sSectionPath ];
292
                    // }
293
                    continue;
294
295
                }
296
297
                // Otherwise,
298
                $_aFieldset = $_aSubSectionOrField;
299
300
                // If a section is set,
301
                if ( $_aFieldset[ 'section_id' ] !== '_default' ) {
302
                    $this->_setOptionValue(
303
                        $_aStoredOptionsNotOfTheTab,
304
                        $_aFieldset[ 'section_id' ],
305
                        $aOptions
306
                    );
307
                    // @deprecated
308
                    // if ( array_key_exists( $_aFieldset[ 'section_id' ], $aOptions ) ) {
309
                        // $_aStoredOptionsNotOfTheTab[ $_aFieldset[ 'section_id' ] ] = $aOptions[ $_aFieldset[ 'section_id' ] ];
310
                    // }
311
                    continue;
312
                }
313
                // So it's a field
314
                $this->_setOptionValue(
315
                    $_aStoredOptionsNotOfTheTab,
316
                    $_aFieldset[ 'field_id' ],
317
                    $aOptions
318
                );
319
                // if ( array_key_exists( $_aFieldset[ 'field_id' ], $aOptions ) ) {
320
                    // $_aStoredOptionsNotOfTheTab[ $_aFieldset[ 'field_id' ] ] = $aOptions[ $_aFieldset[ 'field_id' ] ];
321
                // }
322
323
            }
324
325
        }
326
    /**
327
     * Retrieves the stored options of the given tab slug.
328
     *
329
     * @remark      Consider the possibility that the values of page meta boxes are included in the `$aOptions` array.
330
     * So rather than storing the page-tab-matching elements, drop the unmatched elements
331
     * so that the externally injected options will be respected.
332
     * @since       3.0.0
333
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
334
     * @return      array
335
     */
336
    public function getTabOptions( $aOptions, $sPageSlug, $sTabSlug='' ) {
337
338
        $_aOtherTabOptions = $this->getOtherTabOptions( $aOptions, $sPageSlug, $sTabSlug );
339
        $_aTabOptions      = $this->invertCastArrayContents( $aOptions, $_aOtherTabOptions );
340
        return $_aTabOptions;
341
342
    }
343
344
    /**
345
     * Retrieves the stored options of the given tab slug.
346
     *
347
     * This is stricter version of the `getTabOptions()` method.
348
     * This method does not respect injected elements such as page meta box fields.
349
     *
350
     * @since       3.0.0
351
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
352
     * @return      array
353
     */
354
    public function getTabOnlyOptions( array $aOptions, $sPageSlug, $sTabSlug='' ) {
355
356
        $_aStoredOptionsOfTheTab = array();
357
        if ( ! $sTabSlug ) {
358
            return $_aStoredOptionsOfTheTab;
359
        }
360
361
        foreach( $this->aFieldsets as $_sSectionID => $_aSubSectionsOrFields ) {
362
363
            // Check the section
364
            if ( ! $this->_isThisSectionSetToThisTab( $_sSectionID, $sPageSlug, $sTabSlug ) ) {
365
                continue;
366
            }
367
368
            // At this point, the parsing element is of the given page and the tab.
369
            $this->_setTabOnlyOptions(
370
                $_aStoredOptionsOfTheTab, // by reference, gets updated in the method
371
                $aOptions,
372
                $_aSubSectionsOrFields,
373
                $_sSectionID
374
            );
375
376
        }
377
        return $_aStoredOptionsOfTheTab;
378
379
    }
380
        /**
381
         * Updates the first parameter array holding tab only options.
382
         *
383
         * @since       3.5.3
384
         * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
385
         * @return      void
386
         * @internal
387
         */
388
        private function _setTabOnlyOptions( array &$_aStoredOptionsOfTheTab, array $aOptions, array $_aSubSectionsOrFields, $_sSectionID ) {
389
390
            foreach( $_aSubSectionsOrFields as $_sFieldID => $_aFieldset ) {
391
392
                // if it's a sub-section array.
393
                if ( $this->isNumericInteger( $_sFieldID ) ) {
394
                    $this->_setOptionValue(
395
                        $_aStoredOptionsOfTheTab,
396
                        $_sSectionID,
397
                        $aOptions
398
                    );
399
                    // @deprecated
400
                    // if ( array_key_exists( $_sSectionID, $aOptions ) ) {
401
                        // $_aStoredOptionsOfTheTab[ $_sSectionID ] = $aOptions[ $_sSectionID ];
402
                    // }
403
                    continue;
404
                }
405
406
                // if a section is set,
407
                if ( '_default' !== $_aFieldset[ 'section_id' ] ) {
408
409
                    $this->_setOptionValue(
410
                        $_aStoredOptionsOfTheTab, // by reference
411
                        $_aFieldset[ 'section_id' ],
412
                        $aOptions
413
                    );
414
                    // @deprecated
415
                    // if ( array_key_exists( $_aFieldset[ 'section_id' ], $aOptions ) ) {
416
                        // $_aStoredOptionsOfTheTab[ $_aFieldset[ 'section_id' ] ] = $aOptions[ $_aFieldset[ 'section_id' ] ];
417
                    // }
418
                    continue;
419
                }
420
421
                // It does not have a section so set the field id as its key.
422
                $this->_setOptionValue(
423
                    $_aStoredOptionsOfTheTab,
424
                    $_aFieldset[ 'field_id' ],
425
                    $aOptions
426
                );
427
                // @deprecated      3.7.0
428
                // if ( array_key_exists( $_aFieldset[ 'field_id' ], $aOptions ) ) {
429
                    // $_aStoredOptionsOfTheTab[ $_aFieldset[ 'field_id' ] ] = $aOptions[ $_aFieldset[ 'field_id' ] ];
430
                    // continue;
431
                // }
432
433
            }
434
435
        }
436
437
438
    /**
439
     * Checks if the given section added to the given page.
440
     *
441
     * @since       3.5.3
442
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
443
     * @return      boolean
444
     */
445
    private function _isThisSectionSetToThisPage( $sSectionPath, $sPageSlug ) {
446
447
        if ( ! isset( $this->aSectionsets[ $sSectionPath ][ 'page_slug' ] ) ) {
448
            return false;
449
        }
450
        return (
451
            $sPageSlug === $this->aSectionsets[ $sSectionPath ][ 'page_slug' ]
452
        );
453
    }
454
455
    /**
456
     * Checks if a form section is set for the given section ID, page slug, and tab slug.
457
     *
458
     * @internal
459
     * @since       3.5.3
460
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
461
     * @return      boolean
462
     */
463
    private function _isThisSectionSetToThisTab( $sSectionPath, $sPageSlug, $sTabSlug ) {
464
465
        if ( ! $this->_isThisSectionSetToThisPage( $sSectionPath, $sPageSlug ) ) {
466
            return false;
467
        }
468
        if ( ! isset( $this->aSectionsets[ $sSectionPath ][ 'tab_slug' ] ) ) {
469
            return false;
470
        }
471
        return (
472
            $sTabSlug === $this->aSectionsets[ $sSectionPath ][ 'tab_slug' ]
473
        );
474
475
    }
476
477
478
    /**
479
     * Sets a value of a section of the given option array to the subject array.
480
     * @since       3.7.0
481
     * @return      void
482
     */
483
    private function _setOptionValue( &$aSubject, $asDimensionalPath, $aOptions ) {
484
        $_aDimensionalPath = $this->getAsArray( $asDimensionalPath );
485
        $_mValue     = $this->getElement(
486
            $aOptions,
487
            $_aDimensionalPath,    // as of 3.7.0, it can be an array or string
488
            null
489
        );
490
        if ( isset( $_mValue ) ) {
491
            $this->setMultiDimensionalArray(
492
                $aSubject,
493
                $_aDimensionalPath,
494
                $_mValue
495
            );
496
        }
497
    }
498
499
}
500