| Conditions | 17 |
| Paths | 50 |
| Total Lines | 74 |
| Code Lines | 44 |
| 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 |
||
| 174 | protected static function add($funcType, $options) |
||
| 175 | { |
||
| 176 | $options = array_merge(self::DEFAULT_OPTIONS, $options); |
||
| 177 | |||
| 178 | if (!array_key_exists('name', $options)) { |
||
| 179 | trigger_error('Cannot add asset: Name not provided!', E_USER_WARNING); |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | |||
| 183 | if (!array_key_exists('path', $options)) { |
||
| 184 | trigger_error('Cannot add asset: Path not provided!', E_USER_WARNING); |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | |||
| 188 | $funcName = "wp_{$funcType}_{$options['type']}"; |
||
| 189 | $lastVar = $options['type'] === 'script' ? $options['inFooter'] : $options['media']; |
||
| 190 | |||
| 191 | // allow external urls |
||
| 192 | $path = $options['path']; |
||
| 193 | if (!(StringHelpers::startsWith('http://', $path)) |
||
| 194 | && !(StringHelpers::startsWith('https://', $path)) |
||
| 195 | && !(StringHelpers::startsWith('//', $path)) |
||
| 196 | ) { |
||
| 197 | $path = Asset::requireUrl($path); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ('script' === $options['type'] |
||
| 201 | && true === self::$loadFromCdn |
||
| 202 | && !empty($options['cdn']) |
||
| 203 | && !empty($options['cdn']['check']) |
||
| 204 | && !empty($options['cdn']['url']) |
||
| 205 | ) { |
||
| 206 | // if the script isn't registered or enqueued yet |
||
| 207 | if (!wp_script_is($options['name'], 'registered') |
||
| 208 | && !wp_script_is($options['name'], 'enqueued') |
||
| 209 | ) { |
||
| 210 | $localPath = $path; |
||
| 211 | $path = $options['cdn']['url']; |
||
| 212 | } else { |
||
| 213 | // script already registered / enqueued |
||
| 214 | // get registered script and compare paths |
||
| 215 | $scriptPath = wp_scripts()->registered[$options['name']]->src; |
||
| 216 | |||
| 217 | // deregister script and set cdn options to re-register down below |
||
| 218 | if ($options['cdn']['url'] !== $scriptPath) { |
||
| 219 | wp_deregister_script($options['name']); |
||
| 220 | $localPath = $path; |
||
| 221 | $path = $options['cdn']['url']; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | if (function_exists($funcName)) { |
||
| 227 | $funcName( |
||
| 228 | $options['name'], |
||
| 229 | $path, |
||
| 230 | $options['dependencies'], |
||
| 231 | $options['version'], |
||
| 232 | $lastVar |
||
| 233 | ); |
||
| 234 | |||
| 235 | if (isset($localPath)) { |
||
| 236 | $cdnCheck = $options['cdn']['check']; |
||
| 237 | wp_add_inline_script( |
||
| 238 | $options['name'], |
||
| 239 | "${cdnCheck}||document.write(\"<script src=\\\"${localPath}\\\"><\/script>\")" |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | return true; |
||
| 244 | } |
||
| 245 | |||
| 246 | return false; |
||
| 247 | } |
||
| 248 | } |
||
| 249 |