addFields()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 134
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 102
dl 0
loc 134
rs 8
c 0
b 0
f 0
cc 3
ccs 0
cts 128
cp 0
nc 1
nop 2
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Admin Page Framework Loader
4
 *
5
 * http://admin-page-framework.michaeluno.jp/
6
 * Copyright (c) 2013-2022, Michael Uno; Licensed GPLv2
7
 */
8
9
/**
10
 * Adds the 'Report' form section to the 'Report' tab.
11
 *
12
 * @since       3.5.4           Moved some methods from `AdminPageFrameworkLoader_AdminPage_Help_Repor`.
13
 */
14
class AdminPageFrameworkLoader_AdminPage_Help_Report_Report extends AdminPageFrameworkLoader_AdminPage_Section_Base {
15
16
    /**
17
     * Adds form fields.
18
     *
19
     * @since       3.5.4
20
     */
21
    public function addFields( $oFactory, $sSectionID ) {
22
23
        $_oCurrentUser = wp_get_current_user();
24
25
        $oFactory->addSettingFields(
26
            'report',
27
            array(
28
                'field_id'          => 'name',
29
                'title'             => __( 'Your Name', 'admin-page-framework-loader' ),
30
                'type'              => 'text',
31
                'default'           => $_oCurrentUser->user_lastname || $_oCurrentUser->user_firstname
32
                    ? $_oCurrentUser->user_lastname . ' ' .  $_oCurrentUser->user_lastname
33
                    : '',
34
                'attributes'        => array(
35
                    'required'      => 'required',
36
                    'placeholder'   => __( 'Type your name.', 'admin-page-framewrok-demo' ),
37
                ),
38
            ),
39
            array(
40
                'field_id'          => 'from',
41
                'title'             => __( 'Your Email Address', 'admin-page-framework-loader' ),
42
                'type'              => 'text',
43
                'default'           => $_oCurrentUser->user_email,
44
                'attributes'        => array(
45
                    'required'      => 'required',
46
                    'placeholder'   =>  __( 'Type your email that the developer replies backt to.', 'admin-page-framework-loader' )
47
                ),
48
            ),
49
            array(
50
                'field_id'          => 'expected_result',
51
                'title'             => __( 'Expected Behavior', 'admin-page-framework-loader' ),
52
                'type'              => 'textarea',
53
                'description'       => __( 'Tell how the framework should work.', 'admin-page-framework-loader' ),
54
                'attributes'        => array(
55
                    'required'  => 'required',
56
                ),
57
            ),
58
            array(
59
                'field_id'          => 'actual_result',
60
                'title'             => __( 'Actual Behavior', 'admin-page-framework-loader' ),
61
                'type'              => 'textarea',
62
                'description'      => __( 'Describe the behavior of the framework.', 'admin-page-framework-loader' ),
63
                'attributes'        => array(
64
                    'required'  => 'required',
65
                ),
66
            ),
67
            array(
68
                'field_id'          => 'attachments',
69
                'title'             => __( 'Screenshots', 'admin-page-framework-loader' ),
70
                'type'              => 'image',
71
                'repeatable'        => true,
72
                'attributes'        => array(
73
                    'size'  => 40,
74
                    'preview' => array(
75
                        'style' => 'max-width: 200px;'
76
                    ),
77
                ),
78
            ),
79
            array(
80
                'field_id'      => 'system_information',
81
                'type'          => 'system',
82
                'title'         => __( 'System Information', 'admin-page-framework-loader' ),
83
                'data'          => array(
84
                    __( 'Custom Data', 'admin-page-framework-loader' )  => __( 'This is custom data inserted with the data argument.', 'admin-page-framework-loader' ),
85
                    __( 'Current Time', 'admin-page-framework' )        => '', // Removes the Current Time Section.
86
                ),
87
                'attributes'    => array(
88
                    'rows'          =>  10,
89
                ),
90
            ),
91
            array(
92
                'field_id'      => 'saved_options',
93
                'type'          => 'system',
94
                'title'         => __( 'Saved Options', 'admin-page-framework-loader' ),
95
                'data'          => array(
96
                    // Removes the default data by passing an empty value below.
97
                    'WordPress'             => '',
98
                    'Admin Page Framework'  => '',
99
                    'Server'                => '',
100
                    'PHP'                   => '',
101
                    'PHP Error Log'         => '',
102
                    'MySQL'                 => '',
103
                    'MySQL Error Log'       => '',
104
                    'Browser'               => '',
105
                )
106
                + $this->oFactory->oProp->aOptions, // the stored options of the main demo class
107
                'attributes'    => array(
108
                    'rows'          =>  10,
109
                ),
110
                'hidden'        => true,
111
            ),
112
            array(
113
                'field_id'          => 'allow_sending_system_information',
114
                'title'             => __( 'Confirmation', 'admin-page-framework-loader' )
115
                    . ' (' . __( 'required', 'admin-page-framework-loader' ) . ')',
116
                'type'              => 'checkbox',
117
                'label'             => __( 'I understand that the system information including a PHP version and WordPress version etc. will be sent along with the messages to help developer trouble-shoot the problem.', 'admin-page-framework-loader' ),
118
                'attributes'        => array(
119
                    'required'  => 'required',
120
                ),
121
            ),
122
            array(
123
                'field_id'          => 'send',
124
                'type'              => 'submit',
125
                'label_min_width'   => 0,
126
                'value'             => $oFactory->oUtil->getAOrB(
127
                    'email' === $oFactory->oUtil->getElement( $_GET, 'confirmation' ),
128
                    __( 'Send', 'adimn-page-framework-demo' ),
129
                    __( 'Preview', 'adimn-page-framework-demo' )
130
                ),
131
                'attributes'        => array(
132
                    'field' => array(
133
                        'style' => 'float:right; clear:none; display: inline;',
134
                    ),
135
                    'class' => $oFactory->oUtil->getAOrB(
136
                        'email' === $oFactory->oUtil->getElement( $_GET, 'confirmation' ),
137
                        null,
138
                        'button-secondary'
139
                    ),
140
                ),
141
                'email'             => array(
142
                    // Each argument can accept a string or an array representing the dimensional array key.
143
                    // For example, if there is a field for the email title, and its section id is 'my_section'  and  the field id is 'my_field', pass an array, array( 'my_section', 'my_field' )
144
                    'to'            => '[email protected]',
145
                    'subject'       => 'Reporting Issue',
146
                    'message'       => array( 'report' ), // the section name enclosed in an array. If it is a field, set it to the second element like array( 'seciton id', 'field id' ).
147
                    'headers'       => '',
148
                    'attachments'   => '', // the file path(s)
149
                    'name'          => '', // The email sender name. If the 'name' argument is empty, the field named 'name' in this section will be applied
150
                    'from'          => '', // The sender email address. If the 'from' argument is empty, the field named 'from' in this section will be applied.
151
                    // 'is_html'       => true,
152
                ),
153
            ),
154
            array()
155
        );
156
157
    }
158
159
    /**
160
     * Validates the submitted form data.
161
     *
162
     * @since       3.5.4
163
     */
164
    public function validate( $aInput, $aOldInput, $oFactory, $aSubmit ) {
165
166
       // Local variables
167
        $_bIsValid = true;
168
        $_aErrors  = array();
169
170
        if ( ! $aInput[ 'allow_sending_system_information' ] ) {
171
            $_bIsValid = false;
172
            $_aErrors[ 'allow_sending_system_information' ] = __( 'We need necessary information to help you.', 'admin-page-framework-loader' );
173
        }
174
175
        if ( ! $_bIsValid ) {
176
177
            $oFactory->setFieldErrors( $_aErrors );
178
            $oFactory->setSettingNotice( __( 'Please help us to help you.', 'admin-page-framework-loader' ) );
179
            return $aOldInput;
180
181
        }
182
183
        // Otherwise, process the data.
184
        return $aInput;
185
186
    }
187
188
189
}
190