Conditions | 74 |
Paths | > 20000 |
Total Lines | 256 |
Code Lines | 151 |
Lines | 70 |
Ratio | 27.34 % |
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 |
||
546 | public function admin_ajax_upload () { |
||
547 | pods_session_start(); |
||
548 | |||
549 | // Sanitize input |
||
550 | $params = pods_unslash( (array) $_POST ); |
||
551 | |||
552 | View Code Duplication | foreach ( $params as $key => $value ) { |
|
553 | if ( 'action' == $key ) |
||
554 | continue; |
||
555 | |||
556 | unset( $params[ $key ] ); |
||
557 | |||
558 | $params[ str_replace( '_podsfix_', '', $key ) ] = $value; |
||
559 | } |
||
560 | |||
561 | $params = (object) $params; |
||
562 | |||
563 | $methods = array( |
||
564 | 'upload', |
||
565 | ); |
||
566 | |||
567 | if ( !isset( $params->method ) || !in_array( $params->method, $methods ) || !isset( $params->pod ) || !isset( $params->field ) || !isset( $params->uri ) || empty( $params->uri ) ) |
||
568 | pods_error( 'Invalid AJAX request', PodsInit::$admin ); |
||
569 | elseif ( !empty( $params->pod ) && empty( $params->field ) ) |
||
570 | pods_error( 'Invalid AJAX request', PodsInit::$admin ); |
||
571 | elseif ( empty( $params->pod ) && !current_user_can( 'upload_files' ) ) |
||
572 | pods_error( 'Invalid AJAX request', PodsInit::$admin ); |
||
573 | |||
574 | // Flash often fails to send cookies with the POST or upload, so we need to pass it in GET or POST instead |
||
575 | if ( is_ssl() && empty( $_COOKIE[ SECURE_AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) ) |
||
576 | $_COOKIE[ SECURE_AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ]; |
||
577 | elseif ( empty( $_COOKIE[ AUTH_COOKIE ] ) && !empty( $_REQUEST[ 'auth_cookie' ] ) ) |
||
578 | $_COOKIE[ AUTH_COOKIE ] = $_REQUEST[ 'auth_cookie' ]; |
||
579 | |||
580 | if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) && !empty( $_REQUEST[ 'logged_in_cookie' ] ) ) |
||
581 | $_COOKIE[ LOGGED_IN_COOKIE ] = $_REQUEST[ 'logged_in_cookie' ]; |
||
582 | |||
583 | global $current_user; |
||
584 | unset( $current_user ); |
||
585 | |||
586 | /** |
||
587 | * Access Checking |
||
588 | */ |
||
589 | $upload_disabled = false; |
||
590 | |||
591 | if ( defined( 'PODS_DISABLE_FILE_UPLOAD' ) && true === PODS_DISABLE_FILE_UPLOAD ) |
||
592 | $upload_disabled = true; |
||
593 | View Code Duplication | elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && true === PODS_UPLOAD_REQUIRE_LOGIN && !is_user_logged_in() ) |
|
594 | $upload_disabled = true; |
||
595 | View Code Duplication | elseif ( defined( 'PODS_UPLOAD_REQUIRE_LOGIN' ) && !is_bool( PODS_UPLOAD_REQUIRE_LOGIN ) && ( !is_user_logged_in() || !current_user_can( PODS_UPLOAD_REQUIRE_LOGIN ) ) ) |
|
596 | $upload_disabled = true; |
||
597 | |||
598 | $uid = @session_id(); |
||
599 | |||
600 | if ( is_user_logged_in() ) |
||
601 | $uid = 'user_' . get_current_user_id(); |
||
602 | |||
603 | $nonce_check = 'pods_upload_' . (int) $params->pod . '_' . $uid . '_' . $params->uri . '_' . (int) $params->field; |
||
604 | |||
605 | View Code Duplication | if ( true === $upload_disabled || !isset( $params->_wpnonce ) || false === wp_verify_nonce( $params->_wpnonce, $nonce_check ) ) |
|
606 | pods_error( __( 'Unauthorized request', 'pods' ), PodsInit::$admin ); |
||
607 | |||
608 | $pod = array(); |
||
609 | $field = array( |
||
610 | 'type' => 'file', |
||
611 | 'options' => array() |
||
612 | ); |
||
613 | |||
614 | $api = pods_api(); |
||
615 | |||
616 | $api->display_errors = false; |
||
617 | |||
618 | if ( !empty( $params->pod ) ) { |
||
619 | $pod = $api->load_pod( array( 'id' => (int) $params->pod ) ); |
||
620 | $field = $api->load_field( array( 'id' => (int) $params->field ) ); |
||
621 | |||
622 | View Code Duplication | if ( empty( $pod ) || empty( $field ) || $pod[ 'id' ] != $field[ 'pod_id' ] || !isset( $pod[ 'fields' ][ $field[ 'name' ] ] ) ) |
|
623 | pods_error( __( 'Invalid field request', 'pods' ), PodsInit::$admin ); |
||
624 | |||
625 | if ( !in_array( $field[ 'type' ], PodsForm::file_field_types() ) ) |
||
626 | pods_error( __( 'Invalid field', 'pods' ), PodsInit::$admin ); |
||
627 | } |
||
628 | |||
629 | $method = $params->method; |
||
630 | |||
631 | // Cleaning up $params |
||
632 | unset( $params->action ); |
||
633 | unset( $params->method ); |
||
634 | unset( $params->_wpnonce ); |
||
635 | |||
636 | $params->post_id = pods_var( 'post_id', $params, 0, null, true ); |
||
637 | |||
638 | /** |
||
639 | * Upload a new file (advanced - returns URL and ID) |
||
640 | */ |
||
641 | if ( 'upload' == $method ) { |
||
642 | $file = $_FILES[ 'Filedata' ]; |
||
643 | |||
644 | $limit_size = pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] ); |
||
645 | |||
646 | if ( !empty( $limit_size ) ) { |
||
647 | if ( false !== stripos( $limit_size, 'MB' ) ) { |
||
648 | $limit_size = (float) trim( str_ireplace( 'MB', '', $limit_size ) ); |
||
649 | $limit_size = $limit_size * 1025 * 1025; // convert to KB to B |
||
650 | } |
||
651 | View Code Duplication | elseif ( false !== stripos( $limit_size, 'KB' ) ) { |
|
652 | $limit_size = (float) trim( str_ireplace( 'KB', '', $limit_size ) ); |
||
653 | $limit_size = $limit_size * 1025 * 1025; // convert to B |
||
654 | } |
||
655 | View Code Duplication | elseif ( false !== stripos( $limit_size, 'GB' ) ) { |
|
656 | $limit_size = (float) trim( str_ireplace( 'GB', '', $limit_size ) ); |
||
657 | $limit_size = $limit_size * 1025 * 1025 * 1025; // convert to MB to KB to B |
||
658 | } |
||
659 | elseif ( false !== stripos( $limit_size, 'B' ) ) |
||
660 | $limit_size = (float) trim( str_ireplace( 'B', '', $limit_size ) ); |
||
661 | else |
||
662 | $limit_size = wp_max_upload_size(); |
||
663 | |||
664 | if ( 0 < $limit_size && $limit_size < $file[ 'size' ] ) { |
||
665 | $error = __( 'File size too large, max size is %s', 'pods' ); |
||
666 | $error = sprintf( $error, pods_var( $field[ 'type' ] . '_restrict_filesize', $field[ 'options' ] ) ); |
||
667 | |||
668 | pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' ); |
||
669 | } |
||
670 | } |
||
671 | |||
672 | $limit_file_type = pods_var( $field[ 'type' ] . '_type', $field[ 'options' ], 'images' ); |
||
673 | |||
674 | if ( 'images' == $limit_file_type ) |
||
675 | $limit_types = 'jpg,jpeg,png,gif'; |
||
676 | elseif ( 'video' == $limit_file_type ) |
||
677 | $limit_types = 'mpg,mov,flv,mp4'; |
||
678 | elseif ( 'audio' == $limit_file_type ) |
||
679 | $limit_types = 'mp3,m4a,wav,wma'; |
||
680 | elseif ( 'text' == $limit_file_type ) |
||
681 | $limit_types = 'txt,rtx,csv,tsv'; |
||
682 | elseif ( 'any' == $limit_file_type ) |
||
683 | $limit_types = ''; |
||
684 | else |
||
685 | $limit_types = pods_var( $field[ 'type' ] . '_allowed_extensions', $field[ 'options' ], '', null, true ); |
||
686 | |||
687 | $limit_types = trim( str_replace( array( ' ', '.', "\n", "\t", ';' ), array( '', ',', ',', ',' ), $limit_types ), ',' ); |
||
688 | |||
689 | View Code Duplication | if ( pods_version_check( 'wp', '3.5' ) ) { |
|
690 | $mime_types = wp_get_mime_types(); |
||
691 | |||
692 | if ( in_array( $limit_file_type, array( 'images', 'audio', 'video' ) ) ) { |
||
693 | $new_limit_types = array(); |
||
694 | |||
695 | foreach ( $mime_types as $type => $mime ) { |
||
696 | if ( 0 === strpos( $mime, $limit_file_type ) ) { |
||
697 | $type = explode( '|', $type ); |
||
698 | |||
699 | $new_limit_types = array_merge( $new_limit_types, $type ); |
||
700 | } |
||
701 | } |
||
702 | |||
703 | if ( !empty( $new_limit_types ) ) |
||
704 | $limit_types = implode( ',', $new_limit_types ); |
||
705 | } |
||
706 | elseif ( 'any' != $limit_file_type ) { |
||
707 | $new_limit_types = array(); |
||
708 | |||
709 | $limit_types = explode( ',', $limit_types ); |
||
710 | |||
711 | foreach ( $limit_types as $k => $limit_type ) { |
||
712 | $found = false; |
||
713 | |||
714 | foreach ( $mime_types as $type => $mime ) { |
||
715 | if ( 0 === strpos( $mime, $limit_type ) ) { |
||
716 | $type = explode( '|', $type ); |
||
717 | |||
718 | foreach ( $type as $t ) { |
||
719 | if ( !in_array( $t, $new_limit_types ) ) |
||
720 | $new_limit_types[] = $t; |
||
721 | } |
||
722 | |||
723 | $found = true; |
||
724 | } |
||
725 | } |
||
726 | |||
727 | if ( !$found ) |
||
728 | $new_limit_types[] = $limit_type; |
||
729 | } |
||
730 | |||
731 | if ( !empty( $new_limit_types ) ) |
||
732 | $limit_types = implode( ',', $new_limit_types ); |
||
733 | } |
||
734 | } |
||
735 | |||
736 | $limit_types = explode( ',', $limit_types ); |
||
737 | |||
738 | $limit_types = array_filter( array_unique( $limit_types ) ); |
||
739 | |||
740 | if ( !empty( $limit_types ) ) { |
||
741 | $ok = false; |
||
742 | |||
743 | foreach ( $limit_types as $limit_type ) { |
||
744 | $limit_type = '.' . trim( $limit_type, ' .' ); |
||
745 | |||
746 | $pos = ( strlen( $file[ 'name' ] ) - strlen( $limit_type ) ); |
||
747 | |||
748 | if ( $pos === stripos( $file[ 'name' ], $limit_type ) ) { |
||
749 | $ok = true; |
||
750 | |||
751 | break; |
||
752 | } |
||
753 | } |
||
754 | |||
755 | if ( false === $ok ) { |
||
756 | $error = __( 'File type not allowed, please use one of the following: %s', 'pods' ); |
||
757 | $error = sprintf( $error, '.' . implode( ', .', $limit_types ) ); |
||
758 | |||
759 | pods_error( '<div style="color:#FF0000">Error: ' . $error . '</div>' ); |
||
760 | } |
||
761 | } |
||
762 | |||
763 | $custom_handler = apply_filters( 'pods_upload_handle', null, 'Filedata', $params->post_id, $params, $field ); |
||
764 | |||
765 | if ( null === $custom_handler ) { |
||
766 | $linked = pods_var( $field[ 'type' ] . '_linked', $field[ 'options' ], 0 ); |
||
767 | |||
768 | $attachment_id = media_handle_upload( 'Filedata', $params->post_id ); |
||
769 | |||
770 | if ( is_object( $attachment_id ) ) { |
||
771 | $errors = array(); |
||
772 | |||
773 | foreach ( $attachment_id->errors[ 'upload_error' ] as $error_code => $error_message ) { |
||
774 | $errors[] = '[' . $error_code . '] ' . $error_message; |
||
775 | } |
||
776 | |||
777 | pods_error( '<div style="color:#FF0000">Error: ' . implode( '</div><div>', $errors ) . '</div>' ); |
||
778 | } |
||
779 | else { |
||
780 | $attachment = get_post( $attachment_id, ARRAY_A ); |
||
781 | |||
782 | $attachment[ 'filename' ] = basename( $attachment[ 'guid' ] ); |
||
783 | |||
784 | $thumb = wp_get_attachment_image_src( $attachment[ 'ID' ], 'thumbnail', true ); |
||
785 | $attachment[ 'thumbnail' ] = $thumb[ 0 ]; |
||
786 | |||
787 | $attachment[ 'link' ] = ''; |
||
788 | |||
789 | if ( $linked ) { |
||
790 | $attachment[ 'link' ] = wp_get_attachment_url( $attachment[ 'ID' ] ); |
||
791 | } |
||
792 | |||
793 | $attachment = apply_filters( 'pods_upload_attachment', $attachment, $params->post_id ); |
||
794 | |||
795 | wp_send_json( $attachment ); |
||
796 | } |
||
797 | } |
||
798 | } |
||
799 | |||
800 | die(); // KBAI! |
||
801 | } |
||
802 | } |
||
803 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.