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