Completed
Branch dev (87c2e5)
by
unknown
03:52
created

_updateMetaDatumByFunctionName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 5
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
/**
3
 * Admin Page Framework
4
 * 
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2018, Michael Uno; Licensed MIT
7
 * 
8
 */
9
10
/**
11
 * Provides methods to build forms for the user meta and post meta structure type.
12
 *
13
 * This is used by taxonomy meta, user meta, and post meta form classes.
14
 *
15
 * @package     AdminPageFramework/Common/Form
16
 * @since       3.7.0      
17
 * @extends     AdminPageFramework_Form
18
 * @internal
19
 */
20
class AdminPageFramework_Form_Meta extends AdminPageFramework_Form {
21
        
22
    /**
23
     * Saves the meta data with the given object ID with the given type with the user submit data.
24
     * 
25
     * Used by the post meta box and user meta classes.
26
     * 
27
     * @since       3.5.3
28
     * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Meta`.
29
     * @param       integer     $iObjectID      The ID that is associated with the meta data such as user ID and post ID.
30
     * @param       array       $aInput         The user submit form input data.
31
     * @param       array       $aSavedMeta     The stored form data (old input data).
32
     * @param       string      $sStructureType    The type of object. Currently 'post_meta_box' or 'user_meta' is accepted.
33
     * @return      void
34
     */
35
    public function updateMetaDataByType( $iObjectID, array $aInput, array $aSavedMeta, $sStructureType='post_meta_box' ) {
36
37
        if ( ! $iObjectID ) {
38
            return;
39
        }
40
           
41
        $_aFunctionNameMapByFieldsType = array(
42
            'post_meta_box'     => 'update_post_meta',
43
            'user_meta'         => 'update_user_meta',               
44
            'term_meta'         => 'update_term_meta',               
45
        );
46
        if ( ! in_array( $sStructureType, array_keys( $_aFunctionNameMapByFieldsType ) ) ) {
47
            return;
48
        }
49
        $_sFunctionName = $this->getElement( $_aFunctionNameMapByFieldsType, $sStructureType );
50
        
51
        // 3.6.0+ Unset field elements that the 'save' argument is false.
52
        $aInput = $this->getInputsUnset( $aInput, $this->sStructureType );
53
54
        // Loop through sections/fields and save the data.
55
        foreach ( $aInput as $_sSectionOrFieldID => $_vValue ) {
56
            $this->_updateMetaDatumByFunctionName( 
57
                $iObjectID,
58
                $_vValue, 
59
                $aSavedMeta, 
60
                $_sSectionOrFieldID, 
61
                $_sFunctionName
62
            );
63
        }
64
        
65
    }
66
    
67
        /**
68
         * Saves an individual meta datum with the given section or field ID with the given function name.
69
         * 
70
         * @internal
71
         * @since       3.5.3
72
         * @since       3.7.0      Moved from `AdminPageFramework_FormDefinition_Meta`.
73
         * @return      void
74
         */
75
        private function _updateMetaDatumByFunctionName( $iObjectID, $_vValue, array $aSavedMeta, $_sSectionOrFieldID, $_sFunctionName ) {
76
            
77
            if ( is_null( $_vValue ) ) { 
78
                return;
79
            }
80
81
            $_vSavedValue = $this->getElement(
82
                $aSavedMeta, // subject
83
                $_sSectionOrFieldID,    // dimensional keys
84
                null   // default value
85
            );
86
                
87
            // PHP can compare even array contents with the == operator. See http://www.php.net/manual/en/language.operators.array.php
88
            // if the input value and the saved meta value are the same, no need to update it.
89
            if ( $_vValue == $_vSavedValue ) { 
90
                return; 
91
            }
92
            
93
            // Currently either 'update_post_meta' or 'update_user_meta'
94
            $_sFunctionName( $iObjectID, $_sSectionOrFieldID, $_vValue );             
95
            
96
        }    
97
    
98
}
99