| Conditions | 3 |
| Paths | 4 |
| Total Lines | 305 |
| 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 |
||
| 47 | function post_page_metabox_assets() { |
||
| 48 | global $post; |
||
| 49 | $user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author; |
||
|
|
|||
| 50 | |||
| 51 | $default_prefix = $this->publicize->default_prefix; |
||
| 52 | $default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) ); |
||
| 53 | |||
| 54 | $default_message = $this->publicize->default_message; |
||
| 55 | $default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) ); |
||
| 56 | |||
| 57 | $default_suffix = $this->publicize->default_suffix; |
||
| 58 | $default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) ); |
||
| 59 | |||
| 60 | $max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280; |
||
| 61 | $max_length = $max_length - 24; // t.co link, space |
||
| 62 | |||
| 63 | // for deprecation tooltip |
||
| 64 | wp_enqueue_style( 'wp-pointer' ); |
||
| 65 | wp_enqueue_script( 'wp-pointer' ); |
||
| 66 | |||
| 67 | $this->google_plus_shut_down_tooltip_script(); |
||
| 68 | |||
| 69 | ?> |
||
| 70 | |||
| 71 | <script type="text/javascript"> |
||
| 72 | jQuery( function($) { |
||
| 73 | var wpasTitleCounter = $( '#wpas-title-counter' ), |
||
| 74 | wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length, |
||
| 75 | postTitle = $( '#title' ), |
||
| 76 | wpasTitle = $( '#wpas-title' ).keyup( function() { |
||
| 77 | var postTitleVal, |
||
| 78 | length = wpasTitle.val().length; |
||
| 79 | |||
| 80 | if ( ! length ) { |
||
| 81 | length = wpasTitle.attr( 'placeholder' ).length; |
||
| 82 | } |
||
| 83 | |||
| 84 | wpasTitleCounter.text( length ).trigger( 'change' ); |
||
| 85 | } ), |
||
| 86 | authClick = false; |
||
| 87 | |||
| 88 | wpasTitleCounter.on( 'change', function( e ) { |
||
| 89 | if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) { |
||
| 90 | wpasTitleCounter.addClass( 'wpas-twitter-length-limit' ); |
||
| 91 | } else { |
||
| 92 | wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' ); |
||
| 93 | } |
||
| 94 | } ); |
||
| 95 | |||
| 96 | // Keep the postTitle and the placeholder in sync |
||
| 97 | postTitle.on( 'keyup', function( e ) { |
||
| 98 | var url = $( '#sample-permalink' ).text(); |
||
| 99 | var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, 'e.currentTarget.value', 'url' ); printf( $default_suffix, 'url' ); ?>' ) |
||
| 100 | .replace( /<[^>]+>/g,''); |
||
| 101 | |||
| 102 | wpasTitle.attr( 'placeholder', defaultMessage ); |
||
| 103 | wpasTitle.trigger( 'keyup' ); |
||
| 104 | } ); |
||
| 105 | |||
| 106 | // set the initial placeholder |
||
| 107 | postTitle.trigger( 'keyup' ); |
||
| 108 | |||
| 109 | // If a custom message has been provided, open the UI so the author remembers |
||
| 110 | if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) { |
||
| 111 | $( '#publicize-form' ).show(); |
||
| 112 | $( '#publicize-defaults' ).hide(); |
||
| 113 | $( '#publicize-form-edit' ).hide(); |
||
| 114 | } |
||
| 115 | |||
| 116 | $('#publicize-disconnected-form-show').click( function() { |
||
| 117 | $('#publicize-form').slideDown( 'fast' ); |
||
| 118 | $(this).hide(); |
||
| 119 | } ); |
||
| 120 | |||
| 121 | $('#publicize-disconnected-form-hide').click( function() { |
||
| 122 | $('#publicize-form').slideUp( 'fast' ); |
||
| 123 | $('#publicize-disconnected-form-show').show(); |
||
| 124 | } ); |
||
| 125 | |||
| 126 | $('#publicize-form-edit').click( function() { |
||
| 127 | $('#publicize-form').slideDown( 'fast', function() { |
||
| 128 | var selBeg = 0, selEnd = 0; |
||
| 129 | wpasTitle.focus(); |
||
| 130 | |||
| 131 | if ( ! wpasTitle.text() ) { |
||
| 132 | wpasTitle.text( wpasTitle.attr( 'placeholder' ) ); |
||
| 133 | |||
| 134 | selBeg = wpasTitle.text().indexOf( postTitle.val() ); |
||
| 135 | if ( selBeg < 0 ) { |
||
| 136 | selBeg = 0; |
||
| 137 | } else { |
||
| 138 | selEnd = selBeg + postTitle.val().length; |
||
| 139 | } |
||
| 140 | |||
| 141 | var domObj = wpasTitle.get(0); |
||
| 142 | if ( domObj.setSelectionRange ) { |
||
| 143 | domObj.setSelectionRange( selBeg, selEnd ); |
||
| 144 | } else if ( domObj.createTextRange ) { |
||
| 145 | var r = domObj.createTextRange(); |
||
| 146 | r.moveStart( 'character', selBeg ); |
||
| 147 | r.moveEnd( 'character', selEnd ); |
||
| 148 | r.select(); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } ); |
||
| 152 | |||
| 153 | $('#publicize-defaults').hide(); |
||
| 154 | $(this).hide(); |
||
| 155 | return false; |
||
| 156 | } ); |
||
| 157 | |||
| 158 | $('#publicize-form-hide').click( function() { |
||
| 159 | var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) { |
||
| 160 | return $.trim( $(el).parent( 'label' ).text() ); |
||
| 161 | } ); |
||
| 162 | $('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show(); |
||
| 163 | $('#publicize-form-edit').show(); |
||
| 164 | return false; |
||
| 165 | } ); |
||
| 166 | |||
| 167 | $('.authorize-link').click( function() { |
||
| 168 | if ( authClick ) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | authClick = true; |
||
| 172 | $(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' ); |
||
| 173 | $.ajaxSetup( { async: false } ); |
||
| 174 | |||
| 175 | if ( window.wp && window.wp.autosave ) { |
||
| 176 | window.wp.autosave.server.triggerSave(); |
||
| 177 | } else { |
||
| 178 | autosave(); |
||
| 179 | } |
||
| 180 | |||
| 181 | return true; |
||
| 182 | } ); |
||
| 183 | |||
| 184 | $( '.pub-service' ).click( function() { |
||
| 185 | var service = $(this).data( 'service' ), |
||
| 186 | fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />'; |
||
| 187 | $( '#add-publicize-check' ).append( fakebox ); |
||
| 188 | } ); |
||
| 189 | |||
| 190 | publicizeConnTestStart = function() { |
||
| 191 | $( '#pub-connection-tests' ) |
||
| 192 | .removeClass( 'below-h2' ) |
||
| 193 | .removeClass( 'error' ) |
||
| 194 | .removeClass( 'publicize-token-refresh-message' ) |
||
| 195 | .addClass( 'test-in-progress' ) |
||
| 196 | .html( '' ); |
||
| 197 | $.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete ); |
||
| 198 | } |
||
| 199 | |||
| 200 | publicizeConnRefreshClick = function( event ) { |
||
| 201 | event.preventDefault(); |
||
| 202 | var popupURL = event.currentTarget.href; |
||
| 203 | var popupTitle = event.currentTarget.title; |
||
| 204 | // open a popup window |
||
| 205 | // when it is closed, kick off the tests again |
||
| 206 | var popupWin = window.open( popupURL, popupTitle, '' ); |
||
| 207 | var popupWinTimer= window.setInterval( function() { |
||
| 208 | if ( popupWin.closed !== false ) { |
||
| 209 | window.clearInterval( popupWinTimer ); |
||
| 210 | publicizeConnTestStart(); |
||
| 211 | } |
||
| 212 | }, 500 ); |
||
| 213 | } |
||
| 214 | |||
| 215 | publicizeConnTestComplete = function( response ) { |
||
| 216 | var testsSelector = $( '#pub-connection-tests' ); |
||
| 217 | testsSelector |
||
| 218 | .removeClass( 'test-in-progress' ) |
||
| 219 | .removeClass( 'below-h2' ) |
||
| 220 | .removeClass( 'error' ) |
||
| 221 | .removeClass( 'publicize-token-refresh-message' ) |
||
| 222 | .html( '' ); |
||
| 223 | |||
| 224 | // If any of the tests failed, show some stuff |
||
| 225 | var somethingShownAlready = false; |
||
| 226 | var facebookNotice = false; |
||
| 227 | $.each( response.data, function( index, testResult ) { |
||
| 228 | // find the li for this connection |
||
| 229 | if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) { |
||
| 230 | if ( ! somethingShownAlready ) { |
||
| 231 | testsSelector |
||
| 232 | .addClass( 'below-h2' ) |
||
| 233 | .addClass( 'error' ) |
||
| 234 | .addClass( 'publicize-token-refresh-message' ) |
||
| 235 | .append( "<p><?php echo esc_html( __( 'Before you hit Publish, please refresh the following connection(s) to make sure we can Publicize your post:', 'jetpack' ) ); ?></p>" ); |
||
| 236 | somethingShownAlready = true; |
||
| 237 | } |
||
| 238 | |||
| 239 | if ( testResult.userCanRefresh ) { |
||
| 240 | testsSelector.append( '<p/>' ); |
||
| 241 | $( '<a/>', { |
||
| 242 | 'class' : 'pub-refresh-button button', |
||
| 243 | 'title' : testResult.refreshText, |
||
| 244 | 'href' : testResult.refreshURL, |
||
| 245 | 'text' : testResult.refreshText, |
||
| 246 | 'target' : '_refresh_' + testResult.serviceName |
||
| 247 | } ) |
||
| 248 | .appendTo( testsSelector.children().last() ) |
||
| 249 | .click( publicizeConnRefreshClick ); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) { |
||
| 254 | $( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true ); |
||
| 255 | if ( ! facebookNotice ) { |
||
| 256 | var message = '<p>' |
||
| 257 | + testResult.connectionTestMessage |
||
| 258 | + '</p><p>' |
||
| 259 | + ' <a class="button" href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank">' |
||
| 260 | + '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>' |
||
| 261 | + '</a>' |
||
| 262 | + '<p>'; |
||
| 263 | |||
| 264 | testsSelector |
||
| 265 | .addClass( 'below-h2' ) |
||
| 266 | .addClass( 'error' ) |
||
| 267 | .addClass( 'publicize-token-refresh-message' ) |
||
| 268 | .append( message ); |
||
| 269 | facebookNotice = true; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } ); |
||
| 273 | } |
||
| 274 | |||
| 275 | $( document ).ready( function() { |
||
| 276 | // If we have the #pub-connection-tests div present, kick off the connection test |
||
| 277 | if ( $( '#pub-connection-tests' ).length ) { |
||
| 278 | publicizeConnTestStart(); |
||
| 279 | } |
||
| 280 | } ); |
||
| 281 | |||
| 282 | } ); |
||
| 283 | </script> |
||
| 284 | |||
| 285 | <style type="text/css"> |
||
| 286 | #publicize { |
||
| 287 | line-height: 1.5; |
||
| 288 | } |
||
| 289 | #publicize ul { |
||
| 290 | margin: 4px 0 4px 6px; |
||
| 291 | } |
||
| 292 | #publicize li { |
||
| 293 | margin: 0; |
||
| 294 | } |
||
| 295 | #publicize textarea { |
||
| 296 | margin: 4px 0 0; |
||
| 297 | width: 100% |
||
| 298 | } |
||
| 299 | #publicize ul.not-connected { |
||
| 300 | list-style: square; |
||
| 301 | padding-left: 1em; |
||
| 302 | } |
||
| 303 | .publicize__notice-warning { |
||
| 304 | display: inline-block; |
||
| 305 | padding: 7px 10px; |
||
| 306 | margin: 5px 0; |
||
| 307 | border-left-width: 4px; |
||
| 308 | border-left-style: solid; |
||
| 309 | font-size: 12px; |
||
| 310 | box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1); |
||
| 311 | } |
||
| 312 | .publicize__sharing-settings { |
||
| 313 | display: block; |
||
| 314 | text-decoration: none; |
||
| 315 | margin-top: 8px; |
||
| 316 | } |
||
| 317 | .publicize__sharing-settings:after { |
||
| 318 | content: "\f504"; |
||
| 319 | font: normal 18px/.5em dashicons; |
||
| 320 | speak: none; |
||
| 321 | margin-left: 5px; |
||
| 322 | vertical-align: middle; |
||
| 323 | } |
||
| 324 | #publicize-title:before { |
||
| 325 | content: "\f237"; |
||
| 326 | font: normal 20px/1 dashicons; |
||
| 327 | speak: none; |
||
| 328 | margin-left: -1px; |
||
| 329 | padding-right: 3px; |
||
| 330 | vertical-align: top; |
||
| 331 | -webkit-font-smoothing: antialiased; |
||
| 332 | color: #82878c; |
||
| 333 | } |
||
| 334 | .post-new-php .authorize-link, .post-php .authorize-link { |
||
| 335 | line-height: 1.5em; |
||
| 336 | } |
||
| 337 | .post-new-php .authorize-message, .post-php .authorize-message { |
||
| 338 | margin-bottom: 0; |
||
| 339 | } |
||
| 340 | #poststuff #publicize .updated p { |
||
| 341 | margin: .5em 0; |
||
| 342 | } |
||
| 343 | .wpas-twitter-length-limit { |
||
| 344 | color: red; |
||
| 345 | } |
||
| 346 | .publicize-disabled-service-message .dashicons { |
||
| 347 | font-size: 16px; |
||
| 348 | text-decoration: none; |
||
| 349 | } |
||
| 350 | </style><?php |
||
| 351 | } |
||
| 352 | |||
| 657 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.