Conditions | 104 |
Paths | 0 |
Total Lines | 317 |
Code Lines | 178 |
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 |
||
247 | public function upgrade_from_yoast() { |
||
248 | // Do Yoast's Old Routines |
||
249 | $options = get_option( 'yst_ga', array() ); |
||
250 | if ( ! empty( $options['ga_general'] ) ) { |
||
251 | $options = $options['ga_general']; |
||
252 | } |
||
253 | |||
254 | $tracking_code = null; |
||
255 | if ( ! empty( $options['analytics_profile'] ) && ! empty( $options['analytics_profile_code'] ) ) { |
||
256 | $tracking_code = $options['analytics_profile_code']; |
||
257 | } else if ( ! empty( $options['analytics_profile'] ) && empty( $options['analytics_profile_code'] ) ) { |
||
258 | // Analytics profile is still holding the UA code |
||
259 | $tracking_code = $options['analytics_profile']; |
||
260 | } |
||
261 | |||
262 | if ( ! empty( $options['manual_ua_code_field'] ) && ! empty( $options['manual_ua_code'] ) ) { |
||
263 | $tracking_code = $options['manual_ua_code_field']; |
||
264 | } |
||
265 | |||
266 | if ( ! isset( $options['version'] ) && is_null( $tracking_code ) ) { |
||
267 | $old_options = get_option( 'Yoast_Google_Analytics' ); |
||
268 | if ( isset( $old_options ) && is_array( $old_options ) ) { |
||
269 | if ( isset( $old_options['uastring'] ) && '' !== trim( $old_options['uastring'] ) ) { |
||
270 | // Save UA as manual UA, instead of saving all the old GA crap |
||
271 | $options['manual_ua_code'] = 1; |
||
272 | $options['manual_ua_code_field'] = $old_options['uastring']; |
||
273 | } |
||
274 | // Other settings |
||
275 | $options['allow_anchor'] = $old_options['allowanchor']; |
||
276 | $options['add_allow_linker'] = $old_options['allowlinker']; |
||
277 | $options['anonymous_data'] = $old_options['anonymizeip']; |
||
278 | $options['track_outbound'] = $old_options['trackoutbound']; |
||
279 | $options['track_internal_as_outbound'] = $old_options['internallink']; |
||
280 | $options['track_internal_as_label'] = $old_options['internallinklabel']; |
||
281 | $options['extensions_of_files'] = $old_options['dlextensions']; |
||
282 | } |
||
283 | delete_option( 'Yoast_Google_Analytics' ); |
||
284 | } |
||
285 | // 5.0.0 to 5.0.1 fix of ignore users array |
||
286 | if ( ! isset( $options['version'] ) || version_compare( $options['version'], '5.0.1', '<' ) ) { |
||
287 | if ( isset( $options['ignore_users'] ) && ! is_array( $options['ignore_users'] ) ) { |
||
288 | $options['ignore_users'] = (array) $options['ignore_users']; |
||
289 | } |
||
290 | } |
||
291 | // 5.1.2+ Remove firebug_lite from options, if set |
||
292 | if ( ! isset ( $options['version'] ) || version_compare( $options['version'], '5.1.2', '<' ) ) { |
||
293 | if ( isset( $options['firebug_lite'] ) ) { |
||
294 | unset( $options['firebug_lite'] ); |
||
295 | } |
||
296 | } |
||
297 | // 5.2.8+ Add disabled dashboards option |
||
298 | if ( ! isset ( $options['dashboards_disabled'] ) || version_compare( $options['version'], '5.2.8', '>' ) ) { |
||
299 | $options['dashboards_disabled'] = 0; |
||
300 | } |
||
301 | // Check is API option already exists - if not add it |
||
302 | $yst_ga_api = get_option( 'yst_ga_api' ); |
||
303 | if ( $yst_ga_api === false ) { |
||
304 | add_option( 'yst_ga_api', array(), '', 'no' ); |
||
305 | } |
||
306 | // Fallback to make sure every default option has a value |
||
307 | $defaults = $this->get_yoast_default_values(); |
||
308 | if ( is_array( $defaults ) ) { |
||
309 | foreach ( $defaults[ 'ga_general' ] as $key => $value ) { |
||
310 | if ( ! isset( $options[ $key ] ) ) { |
||
311 | $options[ $key ] = $value; |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | // Set to the current version now that we've done all needed upgrades |
||
317 | $options['version'] = '5.5.3'; // Last Yoast codebase version |
||
318 | $saved_options = get_option( 'yst_ga' ); |
||
319 | $saved_options[ 'ga_general' ] = $options; |
||
320 | update_option( 'yst_ga', $saved_options ); |
||
321 | |||
322 | |||
323 | // Do license key switchover |
||
324 | $key = ''; |
||
325 | $found = false; |
||
326 | $network = false; |
||
327 | // Try network active Premium |
||
328 | $is_key = get_site_option( 'google-analytics-by-yoast-premium_license', array() ); |
||
329 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){ |
||
330 | $key = $is_key['key']; |
||
331 | $found = true; |
||
332 | $network = true; |
||
333 | } |
||
334 | |||
335 | // Try single site Premium |
||
336 | if ( ! $found ) { |
||
337 | $is_key = get_option( 'google-analytics-by-yoast-premium_license', array() ); |
||
338 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){ |
||
339 | $key = $is_key['key']; |
||
340 | $found = true; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | // Try network active Premium |
||
345 | if ( ! $found ) { |
||
346 | $is_key = get_site_option( 'monsterinsights-pro_license', array() ); |
||
347 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){ |
||
348 | $key = $is_key['key']; |
||
349 | $found = true; |
||
350 | $network = true; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | // Try single site Premium |
||
355 | if ( ! $found ) { |
||
356 | $is_key = get_option( 'monsterinsights-pro_license', array() ); |
||
357 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){ |
||
358 | $key = $is_key['key']; |
||
359 | $found = true; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | // Try network active ecommmerce |
||
364 | if ( ! $found ) { |
||
365 | $is_key = get_site_option( 'ecommerce-addon_license', array() ); |
||
366 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) && is_multisite() ){ |
||
367 | $key = $is_key['key']; |
||
368 | $found = true; |
||
369 | $network = true; |
||
370 | } |
||
371 | } |
||
372 | // Try single site ecommerce |
||
373 | if ( ! $found ) { |
||
374 | $is_key = get_option( 'ecommerce-addon_license', array() ); |
||
375 | if ( $is_key && ! empty( $is_key ) && is_array( $is_key ) && ! empty( $is_key['key'] ) ){ |
||
376 | $key = $is_key['key']; |
||
377 | $found = true; |
||
378 | } |
||
379 | } |
||
380 | |||
381 | // set as new key for monsterinsights |
||
382 | if ( $found && ! empty( $key ) ) { |
||
383 | // In pro, install custom dimensions + ads. In lite, just save the key |
||
384 | do_action( 'monsterinsights_upgrade_from_yoast', $key, $network ); |
||
385 | } |
||
386 | |||
387 | // Next up: Settings Migration |
||
388 | |||
389 | $options = get_option( 'yst_ga', array() ); |
||
390 | if ( ! empty( $options['ga_general'] ) ) { |
||
391 | $options = $options['ga_general']; |
||
392 | } |
||
393 | |||
394 | |||
395 | // Let's remove the defaults |
||
396 | if ( isset( $options['ga_general'] ) ) { |
||
397 | unset( $options['ga_general'] ); |
||
398 | } |
||
399 | |||
400 | // Let's remove unused options |
||
401 | if ( isset( $options['yoast_ga_nonce'] ) ) { |
||
402 | unset( $options['yoast_ga_nonce'] ); |
||
403 | } |
||
404 | if ( isset( $options['ga-form-settings'] ) ) { |
||
405 | unset( $options['ga-form-settings'] ); |
||
406 | } |
||
407 | if ( isset( $options['string_error_custom_dimensions'] ) ) { |
||
408 | unset( $options['string_error_custom_dimensions'] ); |
||
409 | } |
||
410 | if ( isset( $options['custom_metrics'] ) ) { |
||
411 | unset( $options['custom_metrics'] ); |
||
412 | } |
||
413 | if ( isset( $options['track_full_url'] ) ) { |
||
414 | unset( $options['track_full_url'] ); |
||
415 | } |
||
416 | if ( isset( $options['version'] ) ) { |
||
417 | unset( $options['version'] ); |
||
418 | } |
||
419 | |||
420 | // Migrate universal to tracking_mode |
||
421 | if ( isset( $options['enable_universal'] ) ) { |
||
422 | unset( $options['enable_universal'] ); |
||
423 | $options['tracking_mode'] = 'analytics'; |
||
424 | } else { |
||
425 | $options['tracking_mode'] = 'ga'; |
||
426 | } |
||
427 | |||
428 | // Migrate events tracking |
||
429 | if ( isset( $options['track_outbound'] ) ) { |
||
430 | unset( $options['track_outbound'] ); |
||
431 | $options['events_mode'] = 'php'; |
||
432 | } else { |
||
433 | $options['events_mode'] = 'none'; |
||
434 | } |
||
435 | |||
436 | // Migrate anonymous_data to allow tracking |
||
437 | if ( isset( $options['anonymous_data'] ) ) { |
||
438 | unset( $options['anonymous_data'] ); |
||
439 | $options['allow_tracking'] = 1; |
||
440 | } else { |
||
441 | $options['allow_tracking'] = 0; |
||
442 | } |
||
443 | |||
444 | |||
445 | // Migrate GA profile data if there |
||
446 | // first let's try to salvage the current profile |
||
447 | $access_token = get_option( 'yoast-ga-access_token', array() ); |
||
448 | $refresh_token = get_option( 'yoast-ga-refresh_token', array() ); |
||
449 | $profiles = get_option( 'yst_ga_api', array() ); |
||
450 | |||
451 | $profile_name = ! empty( $options['analytics_profile'] ) ? $options['analytics_profile'] : ''; |
||
452 | |||
453 | if ( empty( $refresh_token ) && ! empty( $access_token['refresh_token'] ) ) { |
||
454 | $refresh_token = $access_token['refresh_token']; |
||
455 | } |
||
456 | |||
457 | // We need a name and a profile |
||
458 | if ( ! empty( $refresh_token ) && ! empty( $options['analytics_profile'] ) && ! empty( $profiles['ga_api_response_accounts'] ) ) { |
||
459 | // See if we have an access token |
||
460 | if ( ! empty( $access_token['access_token'] ) ) { |
||
461 | if ( monsterinsights_is_pro_version() ) { |
||
462 | update_option( 'monsterinsights_pro_access_token', $access_token['access_token'] ); |
||
463 | } else { |
||
464 | update_option( 'monsterinsights_lite_access_token', $access_token['access_token'] ); |
||
465 | } |
||
466 | } |
||
467 | |||
468 | // We need a refresh token |
||
469 | if ( monsterinsights_is_pro_version() ) { |
||
470 | update_option( 'monsterinsights_pro_refresh_token', $refresh_token ); |
||
471 | } else { |
||
472 | update_option( 'monsterinsights_lite_refresh_token', $refresh_token ); |
||
473 | } |
||
474 | |||
475 | // If we can find the profile in the response save the name |
||
476 | if ( ! empty( $profiles['ga_api_response_accounts'] ) && is_array( $profiles['ga_api_response_accounts'] ) ) { |
||
477 | foreach ( $profiles['ga_api_response_accounts'] as $account ) { |
||
478 | foreach ( $account['items'] as $profile ) { |
||
479 | foreach ( $profile['items'] as $subprofile ) { |
||
480 | if ( isset( $subprofile['id'] ) && $subprofile['id'] == $profile_name ) { |
||
481 | $options['analytics_profile_name'] = $subprofile['name']; |
||
482 | if ( empty( $options['analytics_profile_code'] ) ) { |
||
483 | $options['analytics_profile_code'] = $subprofile['ua_code']; |
||
484 | } |
||
485 | break 3; |
||
486 | } |
||
487 | } |
||
488 | } |
||
489 | } |
||
490 | } |
||
491 | $options['cron_last_run'] = strtotime("-25 hours"); |
||
492 | } else { |
||
493 | // if UA in manual code field, remove analytics profile fields if set |
||
494 | if ( ! empty( $options['manual_ua_code_field' ] ) ) { |
||
495 | if ( isset( $options['analytics_profile_code'] ) ) { |
||
496 | unset( $options['analytics_profile_code'] ); |
||
497 | } |
||
498 | if ( isset( $options['analytics_profile'] ) ) { |
||
499 | unset( $options['analytics_profile'] ); |
||
500 | } |
||
501 | $options['manual_ua_code'] = $options['manual_ua_code_field']; |
||
502 | delete_option( 'yoast-ga-access_token' ); |
||
503 | delete_option( 'yoast-ga-refresh_token' ); |
||
504 | delete_option( 'yst_ga_api' ); |
||
505 | } else if ( ! empty( $options['analytics_profile_code' ] ) ) { |
||
506 | // if UA in profile fields, remove others and use that |
||
507 | $options['manual_ua_code'] = $options['analytics_profile_code']; |
||
508 | if ( isset( $options['analytics_profile_code'] ) ) { |
||
509 | unset( $options['analytics_profile_code'] ); |
||
510 | } |
||
511 | if ( isset( $options['analytics_profile'] ) ) { |
||
512 | unset( $options['analytics_profile'] ); |
||
513 | } |
||
514 | delete_option( 'yoast-ga-access_token' ); |
||
515 | delete_option( 'yoast-ga-refresh_token' ); |
||
516 | delete_option( 'yst_ga_api' ); |
||
517 | } else { |
||
518 | // if UA in profile profiles, remove others and use that |
||
519 | if ( ! empty( $options['analytics_profile' ] ) && ! empty( $profiles['ga_api_response_accounts'] ) && is_array( $profiles['ga_api_response_accounts'] ) ) { |
||
520 | foreach ( $profiles as $account ) { |
||
521 | foreach ( $account['items'] as $profile ) { |
||
522 | foreach ( $profile['items'] as $subprofile ) { |
||
523 | if ( isset( $subprofile['id'] ) && $subprofile['id'] == $options['analytics_profile' ] ) { |
||
524 | $options['manual_ua_code'] = $subprofile['ua_code']; |
||
525 | break 3; |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | } |
||
530 | } |
||
531 | delete_option( 'yoast-ga-access_token' ); |
||
532 | delete_option( 'yoast-ga-refresh_token' ); |
||
533 | delete_option( 'yst_ga_api' ); |
||
534 | } |
||
535 | } |
||
536 | |||
537 | if ( isset( $options['manual_ua_code_field'] ) ) { |
||
538 | unset( $options['manual_ua_code_field'] ); |
||
539 | } |
||
540 | |||
541 | // oAuth Stir Data Tank |
||
542 | // Will happen automatically as cron_last_run set to 25 hours ago |
||
543 | |||
544 | // Add oAuth version |
||
545 | $options['oauth_version'] = '1.0.0'; |
||
546 | |||
547 | $data = array( |
||
548 | 'installed_version' => MONSTERINSIGHTS_VERSION, |
||
549 | 'installed_date' => time(), |
||
550 | 'installed_pro' => monsterinsights_is_pro_version(), |
||
551 | ); |
||
552 | |||
553 | update_option( 'monsterinsights_over_time', $data ); |
||
554 | |||
555 | // Add the cron job |
||
556 | //if ( ! wp_next_scheduled( 'monsterinsights_daily_cron' ) ) { |
||
557 | // Set the next event of fetching data |
||
558 | //wp_schedule_event( strtotime( date( 'Y-m-d', strtotime( 'tomorrow' ) ) . ' 00:05:00 ' ), 'daily', 'monsterinsights_daily_cron' ); |
||
559 | //} |
||
560 | |||
561 | // Finish up |
||
562 | // Save the new settings |
||
563 | $this->new_settings = $options; |
||
564 | } |
||
811 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.