| 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 |
||
| 110 | function post_page_metabox_assets() { |
||
| 111 | global $post; |
||
| 112 | $user_id = empty( $post->post_author ) ? $GLOBALS['user_ID'] : $post->post_author; |
||
| 113 | |||
| 114 | $default_prefix = $this->publicize->default_prefix; |
||
| 115 | $default_prefix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_prefix ) ); |
||
| 116 | |||
| 117 | $default_message = $this->publicize->default_message; |
||
| 118 | $default_message = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_message ) ); |
||
| 119 | |||
| 120 | $default_suffix = $this->publicize->default_suffix; |
||
| 121 | $default_suffix = preg_replace( '/%([0-9])\$s/', "' + %\\1\$s + '", esc_js( $default_suffix ) ); |
||
| 122 | |||
| 123 | $max_length = defined( 'JETPACK_PUBLICIZE_TWITTER_LENGTH' ) ? JETPACK_PUBLICIZE_TWITTER_LENGTH : 280; |
||
| 124 | $max_length = $max_length - 24; // t.co link, space |
||
| 125 | |||
| 126 | // for deprecation tooltip |
||
| 127 | wp_enqueue_style( 'wp-pointer' ); |
||
| 128 | wp_enqueue_script( 'wp-pointer' ); |
||
| 129 | |||
| 130 | $this->google_plus_shut_down_tooltip_script(); |
||
| 131 | |||
| 132 | ?> |
||
| 133 | |||
| 134 | <script type="text/javascript"> |
||
| 135 | jQuery( function($) { |
||
| 136 | var wpasTitleCounter = $( '#wpas-title-counter' ), |
||
| 137 | wpasTwitterCheckbox = $( '.wpas-submit-twitter' ).length, |
||
| 138 | postTitle = $( '#title' ), |
||
| 139 | wpasTitle = $( '#wpas-title' ).keyup( function() { |
||
| 140 | var postTitleVal, |
||
| 141 | length = wpasTitle.val().length; |
||
| 142 | |||
| 143 | if ( ! length ) { |
||
| 144 | length = wpasTitle.attr( 'placeholder' ).length; |
||
| 145 | } |
||
| 146 | |||
| 147 | wpasTitleCounter.text( length ).trigger( 'change' ); |
||
| 148 | } ), |
||
| 149 | authClick = false; |
||
| 150 | |||
| 151 | wpasTitleCounter.on( 'change', function( e ) { |
||
| 152 | if ( wpasTwitterCheckbox && parseInt( $( e.currentTarget ).text(), 10 ) > <?php echo (int) $max_length; ?> ) { |
||
| 153 | wpasTitleCounter.addClass( 'wpas-twitter-length-limit' ); |
||
| 154 | } else { |
||
| 155 | wpasTitleCounter.removeClass( 'wpas-twitter-length-limit' ); |
||
| 156 | } |
||
| 157 | } ); |
||
| 158 | |||
| 159 | // Keep the postTitle and the placeholder in sync |
||
| 160 | postTitle.on( 'keyup', function( e ) { |
||
| 161 | var url = $( '#sample-permalink' ).text(); |
||
| 162 | var defaultMessage = $.trim( '<?php printf( $default_prefix, 'url' ); printf( $default_message, 'e.currentTarget.value', 'url' ); printf( $default_suffix, 'url' ); ?>' ) |
||
| 163 | .replace( /<[^>]+>/g,''); |
||
| 164 | |||
| 165 | wpasTitle.attr( 'placeholder', defaultMessage ); |
||
| 166 | wpasTitle.trigger( 'keyup' ); |
||
| 167 | } ); |
||
| 168 | |||
| 169 | // set the initial placeholder |
||
| 170 | postTitle.trigger( 'keyup' ); |
||
| 171 | |||
| 172 | // If a custom message has been provided, open the UI so the author remembers |
||
| 173 | if ( wpasTitle.val() && ! wpasTitle.prop( 'disabled' ) && wpasTitle.attr( 'placeholder' ) !== wpasTitle.val() ) { |
||
| 174 | $( '#publicize-form' ).show(); |
||
| 175 | $( '#publicize-defaults' ).hide(); |
||
| 176 | $( '#publicize-form-edit' ).hide(); |
||
| 177 | } |
||
| 178 | |||
| 179 | $('#publicize-disconnected-form-show').click( function() { |
||
| 180 | $('#publicize-form').slideDown( 'fast' ); |
||
| 181 | $(this).hide(); |
||
| 182 | } ); |
||
| 183 | |||
| 184 | $('#publicize-disconnected-form-hide').click( function() { |
||
| 185 | $('#publicize-form').slideUp( 'fast' ); |
||
| 186 | $('#publicize-disconnected-form-show').show(); |
||
| 187 | } ); |
||
| 188 | |||
| 189 | $('#publicize-form-edit').click( function() { |
||
| 190 | $('#publicize-form').slideDown( 'fast', function() { |
||
| 191 | var selBeg = 0, selEnd = 0; |
||
| 192 | wpasTitle.focus(); |
||
| 193 | |||
| 194 | if ( ! wpasTitle.text() ) { |
||
| 195 | wpasTitle.text( wpasTitle.attr( 'placeholder' ) ); |
||
| 196 | |||
| 197 | selBeg = wpasTitle.text().indexOf( postTitle.val() ); |
||
| 198 | if ( selBeg < 0 ) { |
||
| 199 | selBeg = 0; |
||
| 200 | } else { |
||
| 201 | selEnd = selBeg + postTitle.val().length; |
||
| 202 | } |
||
| 203 | |||
| 204 | var domObj = wpasTitle.get(0); |
||
| 205 | if ( domObj.setSelectionRange ) { |
||
| 206 | domObj.setSelectionRange( selBeg, selEnd ); |
||
| 207 | } else if ( domObj.createTextRange ) { |
||
| 208 | var r = domObj.createTextRange(); |
||
| 209 | r.moveStart( 'character', selBeg ); |
||
| 210 | r.moveEnd( 'character', selEnd ); |
||
| 211 | r.select(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } ); |
||
| 215 | |||
| 216 | $('#publicize-defaults').hide(); |
||
| 217 | $(this).hide(); |
||
| 218 | return false; |
||
| 219 | } ); |
||
| 220 | |||
| 221 | $('#publicize-form-hide').click( function() { |
||
| 222 | var newList = $.map( $('#publicize-form').slideUp( 'fast' ).find( ':checked' ), function( el ) { |
||
| 223 | return $.trim( $(el).parent( 'label' ).text() ); |
||
| 224 | } ); |
||
| 225 | $('#publicize-defaults').html( '<strong>' + newList.join( '</strong>, <strong>' ) + '</strong>' ).show(); |
||
| 226 | $('#publicize-form-edit').show(); |
||
| 227 | return false; |
||
| 228 | } ); |
||
| 229 | |||
| 230 | $('.authorize-link').click( function() { |
||
| 231 | if ( authClick ) { |
||
| 232 | return false; |
||
| 233 | } |
||
| 234 | authClick = true; |
||
| 235 | $(this).after( '<img src="images/loading.gif" class="alignleft" style="margin: 0 .5em" />' ); |
||
| 236 | $.ajaxSetup( { async: false } ); |
||
| 237 | |||
| 238 | if ( window.wp && window.wp.autosave ) { |
||
| 239 | window.wp.autosave.server.triggerSave(); |
||
| 240 | } else { |
||
| 241 | autosave(); |
||
| 242 | } |
||
| 243 | |||
| 244 | return true; |
||
| 245 | } ); |
||
| 246 | |||
| 247 | $( '.pub-service' ).click( function() { |
||
| 248 | var service = $(this).data( 'service' ), |
||
| 249 | fakebox = '<input id="wpas-submit-' + service + '" type="hidden" value="1" name="wpas[submit][' + service + ']" />'; |
||
| 250 | $( '#add-publicize-check' ).append( fakebox ); |
||
| 251 | } ); |
||
| 252 | |||
| 253 | publicizeConnTestStart = function() { |
||
| 254 | $( '#pub-connection-tests' ) |
||
| 255 | .removeClass( 'below-h2' ) |
||
| 256 | .removeClass( 'error' ) |
||
| 257 | .removeClass( 'publicize-token-refresh-message' ) |
||
| 258 | .addClass( 'test-in-progress' ) |
||
| 259 | .html( '' ); |
||
| 260 | $.post( ajaxurl, { action: 'test_publicize_conns' }, publicizeConnTestComplete ); |
||
| 261 | } |
||
| 262 | |||
| 263 | publicizeConnRefreshClick = function( event ) { |
||
| 264 | event.preventDefault(); |
||
| 265 | var popupURL = event.currentTarget.href; |
||
| 266 | var popupTitle = event.currentTarget.title; |
||
| 267 | // open a popup window |
||
| 268 | // when it is closed, kick off the tests again |
||
| 269 | var popupWin = window.open( popupURL, popupTitle, '' ); |
||
| 270 | var popupWinTimer= window.setInterval( function() { |
||
| 271 | if ( popupWin.closed !== false ) { |
||
| 272 | window.clearInterval( popupWinTimer ); |
||
| 273 | publicizeConnTestStart(); |
||
| 274 | } |
||
| 275 | }, 500 ); |
||
| 276 | } |
||
| 277 | |||
| 278 | publicizeConnTestComplete = function( response ) { |
||
| 279 | var testsSelector = $( '#pub-connection-tests' ); |
||
| 280 | testsSelector |
||
| 281 | .removeClass( 'test-in-progress' ) |
||
| 282 | .removeClass( 'below-h2' ) |
||
| 283 | .removeClass( 'error' ) |
||
| 284 | .removeClass( 'publicize-token-refresh-message' ) |
||
| 285 | .html( '' ); |
||
| 286 | |||
| 287 | // If any of the tests failed, show some stuff |
||
| 288 | var somethingShownAlready = false; |
||
| 289 | var facebookNotice = false; |
||
| 290 | $.each( response.data, function( index, testResult ) { |
||
| 291 | // find the li for this connection |
||
| 292 | if ( ! testResult.connectionTestPassed && testResult.userCanRefresh ) { |
||
| 293 | if ( ! somethingShownAlready ) { |
||
| 294 | testsSelector |
||
| 295 | .addClass( 'below-h2' ) |
||
| 296 | .addClass( 'error' ) |
||
| 297 | .addClass( 'publicize-token-refresh-message' ) |
||
| 298 | .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>" ); |
||
| 299 | somethingShownAlready = true; |
||
| 300 | } |
||
| 301 | |||
| 302 | if ( testResult.userCanRefresh ) { |
||
| 303 | testsSelector.append( '<p/>' ); |
||
| 304 | $( '<a/>', { |
||
| 305 | 'class' : 'pub-refresh-button button', |
||
| 306 | 'title' : testResult.refreshText, |
||
| 307 | 'href' : testResult.refreshURL, |
||
| 308 | 'text' : testResult.refreshText, |
||
| 309 | 'target' : '_refresh_' + testResult.serviceName |
||
| 310 | } ) |
||
| 311 | .appendTo( testsSelector.children().last() ) |
||
| 312 | .click( publicizeConnRefreshClick ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | if( ! testResult.connectionTestPassed && ! testResult.userCanRefresh ) { |
||
| 317 | $( '#wpas-submit-' + testResult.unique_id ).prop( "checked", false ).prop( "disabled", true ); |
||
| 318 | if ( ! facebookNotice ) { |
||
| 319 | var message = '<p>' |
||
| 320 | + testResult.connectionTestMessage |
||
| 321 | + '</p><p>' |
||
| 322 | + ' <a class="button" href="<?php echo esc_url( $this->publicize_settings_url ); ?>" rel="noopener noreferrer" target="_blank">' |
||
| 323 | + '<?php echo esc_html( __( 'Update Your Sharing Settings' ,'jetpack' ) ); ?>' |
||
| 324 | + '</a>' |
||
| 325 | + '<p>'; |
||
| 326 | |||
| 327 | testsSelector |
||
| 328 | .addClass( 'below-h2' ) |
||
| 329 | .addClass( 'error' ) |
||
| 330 | .addClass( 'publicize-token-refresh-message' ) |
||
| 331 | .append( message ); |
||
| 332 | facebookNotice = true; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } ); |
||
| 336 | } |
||
| 337 | |||
| 338 | $( document ).ready( function() { |
||
| 339 | // If we have the #pub-connection-tests div present, kick off the connection test |
||
| 340 | if ( $( '#pub-connection-tests' ).length ) { |
||
| 341 | publicizeConnTestStart(); |
||
| 342 | } |
||
| 343 | } ); |
||
| 344 | |||
| 345 | } ); |
||
| 346 | </script> |
||
| 347 | |||
| 348 | <style type="text/css"> |
||
| 349 | #publicize { |
||
| 350 | line-height: 1.5; |
||
| 351 | } |
||
| 352 | #publicize ul { |
||
| 353 | margin: 4px 0 4px 6px; |
||
| 354 | } |
||
| 355 | #publicize li { |
||
| 356 | margin: 0; |
||
| 357 | } |
||
| 358 | #publicize textarea { |
||
| 359 | margin: 4px 0 0; |
||
| 360 | width: 100% |
||
| 361 | } |
||
| 362 | #publicize ul.not-connected { |
||
| 363 | list-style: square; |
||
| 364 | padding-left: 1em; |
||
| 365 | } |
||
| 366 | .publicize__notice-warning { |
||
| 367 | display: inline-block; |
||
| 368 | padding: 7px 10px; |
||
| 369 | margin: 5px 0; |
||
| 370 | border-left-width: 4px; |
||
| 371 | border-left-style: solid; |
||
| 372 | font-size: 12px; |
||
| 373 | box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1); |
||
| 374 | } |
||
| 375 | .publicize__sharing-settings { |
||
| 376 | display: block; |
||
| 377 | text-decoration: none; |
||
| 378 | margin-top: 8px; |
||
| 379 | } |
||
| 380 | .publicize__sharing-settings:after { |
||
| 381 | content: "\f504"; |
||
| 382 | font: normal 18px/.5em dashicons; |
||
| 383 | speak: none; |
||
| 384 | margin-left: 5px; |
||
| 385 | vertical-align: middle; |
||
| 386 | } |
||
| 387 | #publicize-title:before { |
||
| 388 | content: "\f237"; |
||
| 389 | font: normal 20px/1 dashicons; |
||
| 390 | speak: none; |
||
| 391 | margin-left: -1px; |
||
| 392 | padding-right: 3px; |
||
| 393 | vertical-align: top; |
||
| 394 | -webkit-font-smoothing: antialiased; |
||
| 395 | color: #82878c; |
||
| 396 | } |
||
| 397 | .post-new-php .authorize-link, .post-php .authorize-link { |
||
| 398 | line-height: 1.5em; |
||
| 399 | } |
||
| 400 | .post-new-php .authorize-message, .post-php .authorize-message { |
||
| 401 | margin-bottom: 0; |
||
| 402 | } |
||
| 403 | #poststuff #publicize .updated p { |
||
| 404 | margin: .5em 0; |
||
| 405 | } |
||
| 406 | .wpas-twitter-length-limit { |
||
| 407 | color: red; |
||
| 408 | } |
||
| 409 | .publicize-disabled-service-message .dashicons { |
||
| 410 | font-size: 16px; |
||
| 411 | text-decoration: none; |
||
| 412 | } |
||
| 413 | </style><?php |
||
| 414 | } |
||
| 415 | |||
| 720 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: