| Conditions | 6 |
| Paths | 8 |
| Total Lines | 54 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 97 | public function insert_code( $output = true ) { |
||
| 98 | // If $output is not a boolean false, set it to true (default). |
||
| 99 | $output = ( false !== $output); |
||
| 100 | |||
| 101 | $tracking_id = $this->_get_tracking_code(); |
||
| 102 | if ( empty( $tracking_id ) ) { |
||
| 103 | return $this->_output_or_return( '<!-- Your Google Analytics Plugin is missing the tracking ID -->', $output ); |
||
| 104 | } |
||
| 105 | |||
| 106 | // If we're in the admin_area, return without inserting code. |
||
| 107 | if ( is_admin() ) { |
||
| 108 | return $this->_output_or_return( '<!-- Your Google Analytics Plugin is set to ignore Admin area -->', $output ); |
||
| 109 | } |
||
| 110 | |||
| 111 | $custom_vars = array( |
||
| 112 | "_gaq.push(['_setAccount', '{$tracking_id}']);", |
||
| 113 | ); |
||
| 114 | |||
| 115 | $track = array(); |
||
| 116 | if ( is_404() ) { |
||
| 117 | // This is a 404 and we are supposed to track them. |
||
| 118 | $custom_vars[] = "_gaq.push( [ '_trackEvent', '404', document.location.href, document.referrer ] );"; |
||
| 119 | } elseif ( is_search() ) { |
||
| 120 | // Set track for searches, if it's a search, and we are supposed to. |
||
| 121 | $track['data'] = sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); // Input var okay. |
||
| 122 | $track['code'] = 'search'; |
||
| 123 | } |
||
| 124 | |||
| 125 | if ( ! empty( $track ) ) { |
||
| 126 | $track['url'] = $this->_get_url( $track ); |
||
| 127 | // adjust the code that we output, account for both types of tracking. |
||
| 128 | $track['url'] = esc_js( str_replace( '&', '&', $track['url'] ) ); |
||
| 129 | $custom_vars[] = "_gaq.push(['_trackPageview','{$track['url']}']);"; |
||
| 130 | } else { |
||
| 131 | $custom_vars[] = "_gaq.push(['_trackPageview']);"; |
||
| 132 | } |
||
| 133 | |||
| 134 | $async_code = "<!-- Jetpack Google Analytics --> |
||
| 135 | <script type='text/javascript'> |
||
| 136 | var _gaq = _gaq || []; |
||
| 137 | %custom_vars% |
||
| 138 | |||
| 139 | (function() { |
||
| 140 | var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
||
| 141 | ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
||
| 142 | var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
||
| 143 | })(); |
||
| 144 | </script>"; |
||
| 145 | |||
| 146 | $custom_vars_string = implode( "\r\n", $custom_vars ); |
||
| 147 | $async_code = str_replace( '%custom_vars%', $custom_vars_string, $async_code ); |
||
| 148 | |||
| 149 | return $this->_output_or_return( $async_code, $output ); |
||
| 150 | } |
||
| 151 | |||
| 170 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.