Completed
Push — master ( b4100c...ed2097 )
by frank
01:49
created

admin_settings_rules.php ➔ ao_ccss_render_rules()   F

Complexity

Conditions 19
Paths 195

Size

Total Lines 198

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 195
nop 0
dl 0
loc 198
rs 2.98
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Render the rules panel.
4
 */
5
6
/**
7
 * Main function to render the rules panel.
8
 */
9
function ao_ccss_render_rules() {
10
    // Attach required arrays.
11
    global $ao_ccss_rules;
12
    global $ao_ccss_types;
13
?>
14
    <ul id="rules-panel">
15
        <li class="itemDetail">
16
            <h2 class="itemTitle"><?php _e( 'Rules', 'autoptimize' ); ?></h2>
17
18
            <!-- BEGIN Rule dialogs -->
19
            <!-- Unsaved dialog -->
20
            <div id="unSavedWarning" class="hidden updated settings-error notice notice-warning is-dismissible">
21
                <p><?php _e( "<strong>Rules or Queue changed!</strong> Don't forget to save your changes!", 'autoptimize' ); ?></p>
22
            </div>
23
24
            <!-- Create/edit rule dialog -->
25
            <div id="addEditCritCss" class="hidden">
26
                <table class="form-table rules">
27
                    <tr id="critcss_addedit_type_wrapper">
28
                        <th scope="row">
29
                            <?php _e( 'Rule Type', 'autoptimize' ); ?>
30
                        </th>
31
                        <td>
32
                            <select id="critcss_addedit_type" style="width:100%;">
33
                                <option value="paths"><?php _e( 'Path', 'autoptimize' ); ?></option>
34
                                <option value="types"><?php _e( 'Conditional Tag', 'autoptimize' ); ?></option>
35
                            </select>
36
                        </td>
37
                    </tr>
38
                    <tr id="critcss_addedit_path_wrapper">
39
                        <th scope="row">
40
                            <?php _e( 'String in Path', 'autoptimize' ); ?>
41
                        </th>
42
                        <td>
43
                            <input type="text" id="critcss_addedit_path" placeholder="<?php _e( "Enter a part of the URL that identifies the page(s) you're targetting.", 'autoptimize' ); ?>" style="width:100%;" value="">
44
                        </td>
45
                    </tr>
46
                    <tr id="critcss_addedit_pagetype_wrapper">
47
                        <th scope="row">
48
                            <?php _e( 'Conditional Tag, Custom Post Type or Page Template', 'autoptimize' ); ?>
49
                        </th>
50
                        <td>
51
                            <select id="critcss_addedit_pagetype" style="width:100%;">
52
                                <option value="" disabled selected><?php _e( 'Select from the list below...', 'autoptimize' ); ?></option>
53
                                <optgroup label="<?php _e( 'Standard Conditional Tags', 'autoptimize' ); ?>">
54
                                    <?php
55
                                    // Render grouped simple conditional tags.
56
                                    foreach ( $ao_ccss_types as $ctag ) {
57
                                        $optgrp = substr( $ctag, 0, 3 );
0 ignored issues
show
Unused Code introduced by
$optgrp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
58
                                        if ( substr( $ctag, 0, 3 ) === 'is_' ) {
59
                                            echo '<option value="' . $ctag . '">' . $ctag . '</option>';
60
                                        }
61
                                        $prevgrp = substr( $ctag, 0, 3 );
62
                                    }
63
64
                                    // Render grouped custom post types, templates and specific conditional tags.
65
                                    foreach ( $ao_ccss_types as $type ) {
66
                                        $optgrp = substr( $type, 0, 3 );
67
68
                                        // Option groups labels.
69
                                        if ( $optgrp !== $prevgrp && 'is_' !== $optgrp ) {
0 ignored issues
show
Bug introduced by
The variable $prevgrp does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
70
                                            ?>
71
                                            </optgroup>
72
                                            <?php
73
                                            if ( substr( $type, 0, 12 ) === 'custom_post_' ) {
74
                                                ?>
75
                                                <optgroup label="<?php _e( 'Custom Post Types', 'autoptimize' ); ?>">
76
                                                <?php
77
                                            } elseif ( substr( $type, 0, 9 ) === 'template_' ) {
78
                                                ?>
79
                                                <optgroup label="<?php _e( 'Page Templates', 'autoptimize' ); ?>">
80
                                                <?php
81
                                            } elseif ( substr( $type, 0, 4 ) === 'bbp_' ) {
82
                                                ?>
83
                                                <optgroup label="<?php _e( 'BBPress Conditional Tags', 'autoptimize' ); ?>">
84
                                                <?php
85
                                            } elseif ( substr( $type, 0, 3 ) === 'bp_' ) {
86
                                                ?>
87
                                                <optgroup label="<?php _e( 'BuddyPress Conditional Tags', 'autoptimize' ); ?>">
88
                                                <?php
89
                                            } elseif ( substr( $type, 0, 4 ) === 'edd_' ) {
90
                                                ?>
91
                                                <optgroup label="<?php _e( 'Easy Digital Downloads Conditional Tags', 'autoptimize' ); ?>">
92
                                                <?php
93
                                            } elseif ( substr( $type, 0, 4 ) === 'woo_' ) {
94
                                                ?>
95
                                                <optgroup label="<?php _e( 'WooCommerce Conditional Tags', 'autoptimize' ); ?>">
96
                                                <?php
97
                                            }
98
                                        }
99
100
                                        // Options.
101
                                        if ( 'is_' !== $optgrp ) {
102
                                            // Remove prefix from custom post types, templates and some specific conditional tags.
103
                                            if ( substr( $type, 0, 12 ) === 'custom_post_' ) {
104
                                                $_type = str_replace( 'custom_post_', '', $type );
105
                                            } elseif ( substr( $type, 0, 9 ) === 'template_' ) {
106
                                                $_type = str_replace( 'template_', '', $type );
107
                                            } elseif ( 'bbp_is_bbpress' == $type ) {
108
                                                $_type = str_replace( 'bbp_', '', $type );
109
                                            } elseif ( 'bp_is_buddypress' == $type ) {
110
                                                $_type = str_replace( 'bp_', '', $type );
111
                                            } elseif ( substr( $type, 0, 4 ) === 'woo_' ) {
112
                                                $_type = str_replace( 'woo_', '', $type );
113
                                            } elseif ( substr( $type, 0, 4 ) === 'edd_' ) {
114
                                                $_type = str_replace( 'edd_', '', $type );
115
                                            } else {
116
                                                $_type = $type;
117
                                            }
118
119
                                            echo '<option value="' . $type . '">' . $_type . '</option>';
120
                                            $prevgrp = $optgrp;
121
                                        }
122
                                    }
123
                                    ?>
124
                                </optgroup>
125
                            </select>
126
                        </td>
127
                    </tr>
128
                    <tr>
129
                        <th scope="row">
130
                            <?php _e( 'Custom Critical CSS', 'autoptimize' ); ?>
131
                        </th>
132
                        <td>
133
                            <textarea id="critcss_addedit_css" rows="13" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your specific critical CSS here and hit submit to save.', 'autoptimize' ); ?>"></textarea>
134
                            <input type="hidden" id="critcss_addedit_file">
135
                            <input type="hidden" id="critcss_addedit_id">
136
                        </td>
137
                    </tr>
138
                </table>
139
            </div>
140
141
            <!-- Remove dialog -->
142
            <div id="confirm-rm" title="<?php _e( 'Delete Rule', 'autoptimize' ); ?>" class="hidden">
143
                <p><?php _e( 'This Critical CSS rule will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p>
144
            </div>
145
146
            <!-- Remove All dialog -->
147
            <div id="confirm-rm-all" title="<?php _e( 'Delete all Rules and Jobs', 'autoptimize' ); ?>" class="hidden">
148
                <p><?php _e( 'All Critical CSS rules will be deleted immediately and cannot be recovered.<br /><br /><strong>Are you sure?</strong>', 'autoptimize' ); ?></p>
149
            </div>
150
151
            <!-- Add/edit default critical CSS dialog -->
152
            <div id="default_critcss_wrapper" class="hidden">
153
                <textarea id="dummyDefault" rows="19" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your MINIFIED default critical CSS here and hit submit to save. This is the critical CSS to be used for every page NOT MATCHING any rule.', 'autoptimize' ); ?>"></textarea>
154
            </div>
155
156
            <!-- Add/edit additional critical CSS dialog -->
157
            <div id="additional_critcss_wrapper" class="hidden">
158
                <textarea id="dummyAdditional" rows="19" cols="10" style="width:100%;" placeholder="<?php _e( 'Paste your MINIFIED additional critical CSS here and hit submit to save. This is the CSS to be added AT THE END of every critical CSS provided by a matching rule, or the default one.', 'autoptimize' ); ?>"></textarea>
159
            </div>
160
161
            <!-- Wrapper for in screen notices -->
162
            <div id="rules-notices"></div>
163
            <!-- END Rule add/edit dialogs -->
164
165
            <!-- BEGIN Rules UI -->
166
            <div class="howto">
167
                <div class="title-wrap">
168
                    <h4 class="title"><?php _e( 'How To Use Autoptimize CriticalCSS Power-Up Rules', 'autoptimize' ); ?></h4>
169
                    <p class="subtitle"><?php _e( 'Click the side arrow to toggle instructions', 'autoptimize' ); ?></p>
170
                </div>
171
                <button type="button" class="toggle-btn">
172
                    <span class="toggle-indicator dashicons dashicons-arrow-up dashicons-arrow-down"></span>
173
                </button>
174
                <div class="howto-wrap hidden">
175
                <p><?php _e( "TL;DR:<br />Critical CSS files from <span class='badge auto'>AUTO</span> <strong>rules are updated automatically</strong> while from <span class='badge manual'>MANUAL</span> <strong>rules are not.</strong>", 'autoptimize' ); ?></p>
176
                    <ol>
177
                        <li><?php _e( 'When a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key is in place, Autoptimize CriticalCSS Power-Up starts to operate <strong>automatically</strong>.', 'autoptimize' ); ?></li>
178
                        <li><?php _e( 'Upon a request to any of the frontend pages made by a <strong>not logged in user</strong>, it will <strong>asynchronously</strong> fetch and update the critical CSS from <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> for conditional tags you have on your site (e.g. is_page, is_single, is_archive etc.)', 'autoptimize' ); ?></li>
179
                        <li><?php _e( 'These requests also creates an <span class="badge auto">AUTO</span> rule for you. The critical CSS files from <span class="badge auto">AUTO</span> <strong>rules are updated automatically</strong> when a CSS file in your theme or frontend plugins changes.', 'autoptimize' ); ?></li>
180
                        <li><?php _e( 'If you want to make any fine tunning in the critical CSS file of an <span class="badge auto">AUTO</span> rule, click on "Edit" button of that rule, change what you need, submit and save it. The rule you\'ve just edited becomes a <span class="badge manual">MANUAL</span> rule then.', 'autoptimize' ); ?></li>
181
                        <li><?php _e( 'You can create <span class="badge manual">MANUAL</span> rules for specific page paths (URL). Longer, more specific paths have higher priority over shorter ones, which in turn have higher priority over <span class="badge auto">AUTO</span> rules. Also, critical CSS files from <span class="badge manual">MANUAL</span> <strong>rules are NEVER updated automatically.</strong>', 'autoptimize' ); ?></li>
182
                        <li><?php _e( 'You can also create an <span class="badge auto">AUTO</span> rule for a path by leaving its critical CSS content empty. The critical CSS for that path will be automatically fetched from <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> for you and updated whenever it changes.', 'autoptimize' ); ?></li>
183
                        <li><?php _e( "If you see an <span class='badge auto'>AUTO</span> rule with a <span class='badge review'>R</span> besides it (R is after REVIEW), it means that the fetched critical CSS for that rule is not 100% garanteed to work according to <a href='https://criticalcss.com/?aff=1' target='_blank'>criticalcss.com</a> analysis. It's advised that you edit and review that rule to make any required adjustments.", 'autoptimize' ); ?></li>
184
                        <li><?php _e( 'At any time you can delete an <span class="badge auto">AUTO</span> or <span class="badge manual">MANUAL</span> rule by cliking on "Remove" button of the desired rule and saving your changes.', 'autoptimize' ); ?></li>
185
                    </ol>
186
                </div>
187
            </div>
188
            <textarea id="autoptimize_css_defer_inline" name="autoptimize_css_defer_inline" rows="19" cols="10" style="width:100%;"><?php echo get_option( 'autoptimize_css_defer_inline', '' ); ?></textarea>
189
            <textarea id="autoptimize_ccss_additional" name="autoptimize_ccss_additional" rows="19" cols="10" style="width:100%;"><?php echo get_option( 'autoptimize_ccss_additional', '' ); ?></textarea>
190
            <table class="rules-list" cellspacing="0"><tbody id="rules-list"></tbody></table>
191
            <input class="hidden" type="text" id="critCssOrigin" name="autoptimize_ccss_rules" value='<?php echo ( json_encode( $ao_ccss_rules, JSON_FORCE_OBJECT ) ); ?>'>
192
            <div class="submit rules-btn">
193
                <div class="alignleft">
194
                    <span id="addCritCssButton" class="button-secondary"><?php _e( 'Add New Rule', 'autoptimize' ); ?></span>
195
                    <span id="editDefaultButton" class="button-secondary"><?php _e( 'Edit Default Rule CSS', 'autoptimize' ); ?></span>
196
                    <span id="editAdditionalButton" class="button-secondary"><?php _e( 'Add CSS To All Rules', 'autoptimize' ); ?></span>
197
                </div>
198
                <div class="alignright">
199
                    <span id="removeAllRules" class="button-secondary" style="color:red;"><?php _e( 'Remove all rules', 'autoptimize' ); ?></span>
200
                </div>
201
            </div>
202
            <!-- END Rules UI -->
203
        </li>
204
    </ul>
205
<?php
206
}
207
?>
208