Completed
Branch dev (1e869a)
by
unknown
05:31
created

AdminPageFramework_Factory_Model   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 364
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 364
rs 10
c 0
b 0
f 0
wmc 27
lcom 3
cbo 1

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A _setUp() 0 3 1
A _replyToFieldsetResourceRegistration() 0 17 2
A _replyToFilterFieldTypeDefinitions() 0 12 2
A _replyToModifySectionsets() 0 9 1
B _replyToModifyFieldsets() 0 27 3
A _replyToModifyFieldsetsDefinitions() 0 7 1
A _replyToModifyFieldsetDefinitionAfterFormatting() 0 8 1
A _replyToModifyFieldsetDefinitionBeforeFormatting() 0 7 1
A _getHookNameByFieldsetAndPrefix() 0 12 1
A _replyToHandleSubmittedFormData() 0 3 1
A _replyToFormatFieldsetDefinition() 0 3 1
A _replyToFormatSectionsetDefinition() 0 15 2
A _replyToDetermineWhetherToProcessFormRegistration() 0 3 1
A _replyToGetCapabilityForForm() 0 3 1
A _replyToGetSavedFormData() 0 11 1
A _replyToDetermineWhetherToShowDebugInfo() 0 3 1
A getSavedOptions() 0 3 1
A getFieldErrors() 0 3 1
A _getFieldErrors() 0 3 1
A setLastInputs() 0 3 1
A _setLastInput() 0 3 1
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 models.
12
 * 
13
 * @abstract
14
 * @since       3.0.4
15
 * @package     AdminPageFramework
16
 * @subpackage  Common/Factory
17
 * @transient   apf_field_erros_{user id}   stores the user-set fields error array.
18
 * @transient   apf_notices_{user id}       stores the user-set admin notification messages.
19
 * @internal
20
 */
21
abstract class AdminPageFramework_Factory_Model extends AdminPageFramework_Factory_Router {
22
    
23
    /**
24
     * Sets up hooks and properties.
25
     * 
26
     * @since       3.7.0
27
     * @internal
28
     */
29
    public function __construct( $oProp ) {
30
        
31
        parent::__construct( $oProp );
32
        
33
        add_filter(
34
            // 'field_types_admin_page_framework',
35
            'field_types_' . $oProp->sClassName,
36
            array( $this, '_replyToFilterFieldTypeDefinitions' )
37
        );
38
        
39
    }       
40
    
41
    /**
42
     * Calls the setUp() method. 
43
     * 
44
     * @since       3.1.0
45
     * @todo        Deprecate this method. This method was intended to be used in a user defined abstract class 
46
     * but it requires to call the setUp() method in the overridden method so it's not that useful.
47
     * @internal
48
     */
49
    protected function _setUp() { 
50
        $this->setUp();
0 ignored issues
show
Bug introduced by
The method setUp() does not exist on AdminPageFramework_Factory_Model. Did you maybe mean _setUp()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
51
    }    
52
    
53
    /**
54
     * Called upon fieldset resource registration.
55
     * 
56
     * A contextual help pane item associated with this fieldset will be added.
57
     * 
58
     * @since       3.7.0
59
     * @return      void
60
     */
61
    public function _replyToFieldsetResourceRegistration( $aFieldset ) {
62
        
63
        $aFieldset = $aFieldset + array(
64
            'help'       => null,
65
            'title'      => null,
66
            'help_aside' => null,
67
        );
68
        if ( ! $aFieldset[ 'help' ] ) {
69
            return;
70
        }
71
        $this->oHelpPane->_addHelpTextForFormFields( 
72
            $aFieldset[ 'title' ], 
73
            $aFieldset[ 'help' ], 
74
            $aFieldset[ 'help_aside' ] 
75
        );
76
                   
77
    }    
78
    
79
    /**
80
     * Filters field type definitions array.
81
     * @callback    filter      field_types_{class name}
82
     * @since       3.7.0
83
     * @since       3.7.1       Changed the callback from `field_types_admin_page_framework`.
84
     */
85
    public function _replyToFilterFieldTypeDefinitions( $aFieldTypeDefinitions ) {
86
        
87
        // Not triggering `__call()` as the filter is fired manually in the form class.
88
        if ( method_exists( $this, 'field_types_' . $this->oProp->sClassName ) ) {
89
            return call_user_func_array(
90
                array( $this, 'field_types_' . $this->oProp->sClassName ),
91
                array( $aFieldTypeDefinitions )
92
            );
93
        }
94
        return $aFieldTypeDefinitions;
95
                     
96
    }
97
    
98
    /**
99
     * Modifies registered section-sets definition array.
100
     * 
101
     * This lets third party scripts to set their own sections 
102
     * before the framework registered field resource (assets) files.
103
     * 
104
     * @remark      Called prior to field resource registrations.
105
     * @since       3.7.0
106
     * @return      array       The modified section-sets definition array.
107
     */    
108
    public function _replyToModifySectionsets( $aSectionsets ) {    
109
        
110
        return $this->oUtil->addAndApplyFilter( 
111
            $this,  // caller factory object
112
            "sections_{$this->oProp->sClassName}", 
113
            $aSectionsets
114
        );
115
        
116
    }
117
118
    /**
119
     * Modifies registered field-sets definition array.
120
     * 
121
     * This lets third party scripts to set their own sections 
122
     * before the framework registered field resource (assets) files.
123
     * 
124
     * @remark      Called prior to field resource registrations.
125
     * @since       3.7.0
126
     * @return      array       The modified field-sets definition array.
127
     */
128
    public function _replyToModifyFieldsets( $aFieldsets, $aSectionsets ) {
0 ignored issues
show
Unused Code introduced by
The parameter $aSectionsets 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...
129
130
        // Apply filters to added field-sets
131
        foreach( $aFieldsets as $_sSectionPath => $_aFields ) {
132
            $_aSectionPath  = explode( '|', $_sSectionPath );
133
            $_sFilterSuffix = implode( '_', $_aSectionPath );
134
            $aFieldsets[ $_sSectionPath ] = $this->oUtil->addAndApplyFilter(
135
                $this,
136
                "fields_{$this->oProp->sClassName}_{$_sFilterSuffix}",
137
                $_aFields
138
            ); 
139
        }
140
        $aFieldsets =  $this->oUtil->addAndApplyFilter( 
141
            $this,
142
            "fields_{$this->oProp->sClassName}",
143
            $aFieldsets
144
        );         
145
        
146
        // If at lease one filed is added, set the flag to enable the form.
147
        // Do not set `false` when there is no field because page meta boxes may add form fields.
148
        if ( count( $aFieldsets ) ) {
149
            $this->oProp->bEnableForm = true;
150
        }
151
        
152
        return $aFieldsets;
153
        
154
    }
155
    
156
    /**
157
     * Applies filters to all the conditioned field definitions array.
158
     * @since       3.7.0
159
     * @return      array   
160
     */
161
    public function _replyToModifyFieldsetsDefinitions( $aFieldsets /*, $aSectionsets */ ) {
162
        return $this->oUtil->addAndApplyFilter(
163
            $this,
164
            "field_definition_{$this->oProp->sClassName}",
165
            $aFieldsets
166
        );    
167
    }
168
    
169
    /**
170
     * Applies filters to each conditioned (formatted) field definition array.
171
     * 
172
     * @since       3.0.2
173
     * @since       3.1.1      Made it reformat the fields after applying filters.
174
     * @since       3.7.0      Renamed from `applyFiltersToFieldsets()`.
175
     * @since       3.8.4      Ranamed from `_replyToModifyFieldsetDefinition()`.  
176
     * Moved from `AdminPageFramework_FormDefinition_Base`.
177
     * @return      array      
178
     */
179
    public function _replyToModifyFieldsetDefinitionAfterFormatting( $aFieldset /*, $aSectionsets */ ) {
180
        return $this->oUtil->addAndApplyFilter(
181
            $this,
182
            $this->_getHookNameByFieldsetAndPrefix( 'field_definition_', $aFieldset ),
183
            $aFieldset,
184
            $aFieldset[ '_subsection_index' ]
185
        ); 
186
    }    
187
    
188
    /**
189
     * Applies the filter to the passed filed-set definition array.
190
     * @since       3.8.4
191
     * @return      array
192
     * @deprecated  3.8.4
193
     */    
194
    public function _replyToModifyFieldsetDefinitionBeforeFormatting( $aFieldset ) {
195
        return $this->oUtil->addAndApplyFilter(
196
            $this,
197
            $this->_getHookNameByFieldsetAndPrefix( 'field_definition_before_formatting_', $aFieldset ),
198
            $aFieldset
199
        );
200
    }
201
    
202
        /**
203
         * Constructs a WordPress filter hook name.
204
         * @internal
205
         * @since       3.8.4
206
         * @return      string
207
         */
208
        private function _getHookNameByFieldsetAndPrefix( $sPrefix, $aFieldset ) {
209
            
210
            $_sFieldPart    = '_' . implode( '_', $aFieldset[ '_field_path_array' ] );
211
            $_sSectionPart  = implode( '_', $aFieldset[ '_section_path_array' ] );
212
            $_sSectionPart  = $this->oUtil->getAOrB(
213
                '_default' === $_sSectionPart,
214
                '',
215
                '_' . $_sSectionPart
216
            );
217
            return $sPrefix . $this->oProp->sClassName . $_sSectionPart . $_sFieldPart;
218
            
219
        }
220
               
221
    /**
222
     * Gets called after the form element registration is done.
223
     * 
224
     * @since       3.7.0
225
     */
226
    public function _replyToHandleSubmittedFormData( $aSavedData, $aArguments, $aSectionsets, $aFieldsets ) {
227
        // Do validation and saving data 
228
    }
229
        
230
    /**
231
     * @since       3.7.0
232
     * @return      array
233
     */
234
    public function _replyToFormatFieldsetDefinition( $aFieldset, $aSectionsets ) {
235
        return $aFieldset;
236
    }
237
        
238
    /**
239
     * @since       3.7.0
240
     * @return      array
241
     */
242
    public function _replyToFormatSectionsetDefinition( $aSectionset ) {
243
        
244
        if ( empty( $aSectionset ) ) {
245
            return $aSectionset;
246
        }
247
        
248
        $aSectionset = $aSectionset
249
            + array( 
250
                '_fields_type'      => $this->oProp->_sPropertyType, // backward compatibility
251
                '_structure_type'   => $this->oProp->_sPropertyType,
252
            );
253
254
        return $aSectionset;
255
        
256
    }
257
    
258
    /**
259
     * @since       3.7.0
260
     * @return      boolean     Whether or not the form registration should be allowed in the current screen.
261
     */
262
    public function _replyToDetermineWhetherToProcessFormRegistration( $bAllowed ) {
0 ignored issues
show
Unused Code introduced by
The parameter $bAllowed 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...
263
        return $this->_isInThePage();
264
    }
265
    
266
    /**
267
     * Returns the inherited capability value from the page and in-page tab for form elements.
268
     * 
269
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Page`.
270
     * @return      string
271
     */    
272
    public function _replyToGetCapabilityForForm( $sCapability ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sCapability 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...
273
        return $this->oProp->sCapability;         
274
    }    
275
276
    /**
277
     * Called when the form object tries to set the form data from the database.
278
     * 
279
     * @callback    form        `saved_data`    
280
     * @remark      The `oOptions` property will be automatically set with the overload method.
281
     * @return      array       The saved form data.
282
     * @since       3.7.0
283
     */
284
    public function _replyToGetSavedFormData() {
285
        
286
        // Must update the property with the filtered value.
287
        $this->oProp->aOptions =  $this->oUtil->addAndApplyFilter(
288
            $this, // the caller factory object
289
            'options_' . $this->oProp->sClassName,
290
            $this->oProp->aOptions      // subject value to be filtered
291
        );
292
        return $this->oProp->aOptions;
293
        
294
    }
295
    
296
    /**
297
     * Determines whether to show debug information.
298
     * @callback    form        show_debug_info
299
     * @return      boolean
300
     * @since       3.8.5
301
     */
302
    public function _replyToDetermineWhetherToShowDebugInfo() {
303
        return $this->oProp->bShowDebugInfo;
304
    }
305
    
306
    /**
307
     * Returns the saved options array.
308
     * 
309
     * The scope public it is accessed from the outside. This is mainly for field callback methods to create inner nested or different type of fields
310
     * as instantiating a field object requires this value.
311
     * 
312
     * This method is used from inside field classes especially for the 'revealer' custom field type that needs to create a field object
313
     * while processing the revealer field output. For that, the saved option array needs to be passed and accessing the property object was somewhat indirect 
314
     * so there needs to be a direct method to retrieve the options. 
315
     * 
316
     * As of 3.7.0, the form object will store the saved options by itself. And the revealer field type shuold use the form object method.
317
     * 
318
     * @remark      When the confirmation URL query key is set, it will merger the saved options with the last form input array, used for contact forms.
319
     * @since       3.3.0
320
     * @since       3.3.1       Moved from `AdminPageFramework_Setting_Base`. Changed the visibility scope to `protected` as the caller method has moved to the view class.
321
     * @since       3.4.0       Changed the visibility scope to public.
322
     * @since       3.4.1       Changed the name from '_getSavedOptions()'.
323
     * @since       3.7.0      Moved from `AdminPageFramework_Model_Form`.
324
     * @remark      assumes the `aSavedData` property is already set. 
325
     * This is set when the form fields are registered.
326
     * @deprecated  3.7.0      Kept for backward compatibility. 
327
     */
328
    public function getSavedOptions() {
329
        return $this->oForm->aSavedData;
330
    }
331
332
    /**
333
     * Returns the settings error array set by the user in the validation callback.
334
     * 
335
     * The scope is public because it is accessed from outside ofo the class. This is mainly for field callback methods to create inner nested or different type of fields
336
     * as instantiating a field object requires this value.
337
     * 
338
     * @since       3.4.0
339
     */
340
    public function getFieldErrors() {
341
        return $this->oForm->getFieldErrors();
342
    }
343
344
    /**
345
     * Retrieves the settings error array set by the user in the validation callback.
346
     * 
347
     * @since       3.0.4    
348
     * @since       3.6.3       Changed the visibility scope to public as a delegation class needs to access this method.
349
     * @since       3.7.0      Changed back the visibility scope to protected as there is the `getFieldErrors()` public method.
350
     * @access      protected
351
     * @internal
352
     * @param       string      $sID        deprecated
0 ignored issues
show
Bug introduced by
There is no parameter named $sID. 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...
353
     * @param       boolean     $bDelete    whether or not the transient should be deleted after retrieving it. 
0 ignored issues
show
Bug introduced by
There is no parameter named $bDelete. 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...
354
     * @return      array
355
     * @deprecated  3.7.0      Use `getFieldErrors()` instead. Kept for backward compatibility.
356
     */
357
    protected function _getFieldErrors( /* $sID='deprecated', $bDelete=true */ ) {
358
        return $this->oForm->getFieldErrors();        
359
    }    
360
361
    /**
362
     * Saves user last input in the database as a transient.
363
     * 
364
     * To get the set input, call `$this->oProp->aLastInput`.
365
     * 
366
     * @since       3.4.1
367
     * @since       3.7.0      Changed the name from `_setLastInput()`.
368
     * @since       3.7.8      Changed the return value to void.
369
     * @return      void
370
     * @internal
371
     */
372
    public function setLastInputs( array $aLastInputs ) {
373
        return $this->oForm->setLastInputs( $aLastInputs );
374
    }
375
        /**
376
         * An alias of `_setLastInputs()`.
377
         * @deprecated      3.7.0
378
         */
379
        public function _setLastInput( $aLastInputs )  {
380
            return $this->setLastInputs( $aLastInputs );
381
        }    
382
383
     
384
}
385