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