| 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 |
||
| 69 | protected static function add($funcType, $options) |
||
| 70 | { |
||
| 71 | $options = array_merge(self::DEFAULT_OPTIONS, $options); |
||
| 72 | |||
| 73 | if (!array_key_exists('name', $options)) { |
||
| 74 | trigger_error('Cannot add asset: Name not provided!', E_USER_WARNING); |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (!array_key_exists('path', $options)) { |
||
| 79 | trigger_error('Cannot add asset: Path not provided!', E_USER_WARNING); |
||
| 80 | return false; |
||
| 81 | } |
||
| 82 | |||
| 83 | $funcName = "wp_{$funcType}_{$options['type']}"; |
||
| 84 | $lastVar = $options['type'] === 'script' ? $options['inFooter'] : $options['media']; |
||
| 85 | |||
| 86 | // allow external urls |
||
| 87 | $path = $options['path']; |
||
| 88 | if (!(StringHelpers::startsWith('http://', $path)) |
||
| 89 | && !(StringHelpers::startsWith('https://', $path)) |
||
| 90 | && !(StringHelpers::startsWith('//', $path)) |
||
| 91 | ) { |
||
| 92 | $path = Asset::requireUrl($path); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ('script' === $options['type'] |
||
| 96 | && true === self::$loadFromCdn |
||
| 97 | && !empty($options['cdn']) |
||
| 98 | && !empty($options['cdn']['check']) |
||
| 99 | && !empty($options['cdn']['url']) |
||
| 100 | ) { |
||
| 101 | // if the script isn't registered or enqueued yet |
||
| 102 | if (!wp_script_is($options['name'], 'registered') |
||
| 103 | && !wp_script_is($options['name'], 'enqueued') |
||
| 104 | ) { |
||
| 105 | $localPath = $path; |
||
| 106 | $path = $options['cdn']['url']; |
||
| 107 | } else { |
||
| 108 | // script already registered / enqueued |
||
| 109 | // get registered script and compare paths |
||
| 110 | $scriptPath = wp_scripts()->registered[$options['name']]->src; |
||
| 111 | |||
| 112 | // deregister script and set cdn options to re-register down below |
||
| 113 | if ($options['cdn']['url'] !== $scriptPath) { |
||
| 114 | wp_deregister_script($options['name']); |
||
| 115 | $localPath = $path; |
||
| 116 | $path = $options['cdn']['url']; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | if (function_exists($funcName)) { |
||
| 122 | $funcName( |
||
| 123 | $options['name'], |
||
| 124 | $path, |
||
| 125 | $options['dependencies'], |
||
| 126 | $options['version'], |
||
| 127 | $lastVar |
||
| 128 | ); |
||
| 129 | |||
| 130 | if (isset($localPath)) { |
||
| 131 | $cdnCheck = $options['cdn']['check']; |
||
| 132 | wp_add_inline_script( |
||
| 133 | $options['name'], |
||
| 134 | "${cdnCheck}||document.write(\"<script src=\\\"${localPath}\\\"><\/script>\")" |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | return true; |
||
| 139 | } |
||
| 140 | |||
| 141 | return false; |
||
| 142 | } |
||
| 143 | |||
| 157 |