Completed
Pull Request — 2.x (#3830)
by Scott Kingsley
17:13 queued 11:36
created

PodsBuilderModuleList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package Pods\Components
4
 * @subpackage Builder
5
 */
6
if ( !class_exists( 'LayoutModule' ) )
7
    return;
8
9
if ( !class_exists( 'PodsBuilderModuleList' ) ) {
10
    class PodsBuilderModuleList extends LayoutModule {
11
12
        var $_name = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
        var $_var = 'pods-builder-list';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_var.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
14
        var $_description = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_description.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
        var $_editor_width = 500;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_editor_width.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
        var $_can_remove_wrappers = true;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_can_remove_wrappers.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
        /**
19
         * Register the Module
20
         */
21 View Code Duplication
        public function __construct () {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $this->_name = __( 'Pods - List Items', 'pods' );
23
            $this->_description = __( 'Display multiple Pod items', 'pods' );
24
            $this->module_path = dirname( __FILE__ );
25
26
            $this->LayoutModule();
27
        }
28
29
        /**
30
         * Set default variables
31
         *
32
         * @param $defaults
33
         *
34
         * @return mixed
35
         */
36
        function _get_defaults ( $defaults ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
37
            $new_defaults = array(
38
                'pod_type' => '',
39
                'template' => '',
40
                'template_custom' => '',
41
                'limit' => 15,
42
                'orderby' => '',
43
                'where' => '',
44
                'expires' => ( 60 * 5 ),
45
                'cache_mode' => 'transient',
46
                'sidebar' => 'none'
47
            );
48
49
            return ITUtility::merge_defaults( $new_defaults, $defaults );
50
        }
51
52
        /**
53
         * Output something before the table form
54
         *
55
         * @param object $form Form class
56
         * @param bool $results
57
         */
58
        function _before_table_edit ( $form, $results = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
?>
60
    <p><?php echo $this->_description; ?></p>
61
<?php
62
        }
63
64
        /**
65
         * Output something at the start of the table form
66
         *
67
         * @param object $form Form class
68
         * @param bool $results
69
         */
70
        function _start_table_edit ( $form, $results = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
71
            $api = pods_api();
72
            $all_pods = $api->load_pods( array( 'names' => true ) );
73
74
            $pod_types = array();
75
76
            foreach ( $all_pods as $pod_name => $pod_label ) {
0 ignored issues
show
Bug introduced by
The expression $all_pods of type array|object|integer|double|string|null|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
                $pod_types[ $pod_name ] = $pod_label . ' (' . $pod_name . ')';
78
            }
79
?>
80
    <tr>
81
        <td valign="top">
82
            <label for="pod_type"><?php _e( 'Pod', 'pods' ); ?></label>
83
        </td>
84
        <td>
85
            <?php
86 View Code Duplication
                if ( 0 < count( $all_pods ) )
87
                    $form->add_drop_down( 'pod_type', $pod_types );
88
                else
89
                    echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>';
90
            ?>
91
        </td>
92
    </tr>
93
94
    <?php
95 View Code Duplication
        if ( class_exists( 'Pods_Templates' ) ) {
96
            $all_templates = (array) $api->load_templates( array() );
97
98
            $templates = array(
99
                '' => '- ' . __( 'Custom Template', 'pods' ) . ' -'
100
            );
101
102
            foreach ( $all_templates as $template ) {
103
                $templates[ $template[ 'name' ] ] = $template[ 'name' ];
104
            }
105
    ?>
106
        <tr>
107
            <td valign="top">
108
                <label for="template"><?php _e( 'Template', 'pods' ); ?></label>
109
            </td>
110
            <td>
111
                <?php
112
                    if ( 0 < count( $all_templates ) )
113
                        $form->add_drop_down( 'template', $templates );
114
                    else
115
                        echo '<strong class="red">' . __( 'None Found', 'pods' ) . '</strong>';
116
                ?>
117
            </td>
118
        </tr>
119
    <?php
120
        }
121
        else {
122
    ?>
123
        <tr>
124
            <td valign="top">
125
                <label for="template"><?php _e( 'Template', 'pods' ); ?></label>
126
            </td>
127
            <td>
128
                <?php $form->add_text_box( 'template' ); ?>
129
            </td>
130
        </tr>
131
    <?php
132
        }
133
    ?>
134
135
    <tr>
136
        <td valign="top">
137
            <label for="template_custom"><?php _e( 'Custom Template', 'pods' ); ?></label>
138
        </td>
139
        <td>
140
            <?php $form->add_text_area( 'template_custom', array( 'style' => 'width:90%; max-width:100%; min-height:100px;', 'rows' => '8' ) ); ?>
141
        </td>
142
    </tr>
143
    <tr>
144
        <td valign="top">
145
            <label for="limit"><?php _e( 'Limit', 'pods' ); ?></label>
146
        </td>
147
        <td>
148
            <?php $form->add_text_box( 'limit' ); ?>
149
        </td>
150
    </tr>
151
    <tr>
152
        <td valign="top">
153
            <label for="orderby"><?php _e( 'Order By', 'pods' ); ?></label>
154
        </td>
155
        <td>
156
            <?php $form->add_text_box( 'orderby' ); ?>
157
        </td>
158
    </tr>
159
    <tr>
160
        <td valign="top">
161
            <label for="where"><?php _e( 'Where', 'pods' ); ?></label>
162
        </td>
163
        <td>
164
            <?php $form->add_text_box( 'where' ); ?>
165
        </td>
166
    </tr>
167
    <tr>
168
        <td valign="top">
169
            <label for="cache_mode"><?php _e( 'Cache Type', 'pods' ); ?></label>
170
        </td>
171
        <td>
172
            <?php
173
                $cache_modes = array(
174
                    'none' => __( 'Disable Caching', 'pods' ),
175
                    'cache' => __( 'Object Cache', 'pods' ),
176
                    'transient' => __( 'Transient', 'pods' ),
177
                    'site-transient' => __( 'Site Transient', 'pods' )
178
                );
179
180
                $form->add_drop_down( 'cache_mode', $cache_modes );
181
            ?>
182
        </td>
183
    </tr>
184
    <tr>
185
        <td valign="top">
186
            <label for="expires"><?php _e( 'Cache Expiration (in seconds)', 'pods' ); ?></label>
187
        </td>
188
        <td>
189
            <?php $form->add_text_box( 'expires' ); ?>
190
        </td>
191
    </tr>
192
<?php
193
        }
194
195
        /**
196
         * Module Output
197
         */
198
        function _render ( $fields ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
199
            $args = array(
200
                'name' => trim( pods_var_raw( 'pod_type', $fields[ 'data' ], '' ) ),
201
                'template' => trim( pods_var_raw( 'template', $fields[ 'data' ], '' ) ),
202
                'limit' => (int) pods_var_raw( 'limit', $fields[ 'data' ], 15, null, true ),
203
                'orderby' => trim( pods_var_raw( 'orderby', $fields[ 'data' ], '' ) ),
204
                'where' => trim( pods_var_raw( 'where', $fields[ 'data' ], '' ) ),
205
                'expires' => (int) trim( pods_var_raw( 'expires', $fields[ 'data' ], ( 60 * 5 ) ) ),
206
                'cache_mode' => trim( pods_var_raw( 'cache_mode', $fields[ 'data' ], 'transient', null, true ) )
207
            );
208
209
            $content = trim( pods_var_raw( 'template_custom', $fields[ 'data' ], '' ) );
210
211 View Code Duplication
            if ( 0 < strlen( $args[ 'name' ] ) && ( 0 < strlen( $args[ 'template' ] ) || 0 < strlen( $content ) ) )
212
                echo pods_shortcode( $args, ( isset( $content ) ? $content : null ) );
213
        }
214
215
    }
216
}
217
218
new PodsBuilderModuleList();