Переопределите класс плагина WordPress, чтобы изменить размер загружаемого изображения - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь изменить функцию внутри класса обработчика для загрузки изображения пользователя из внешнего интерфейса, используя код, который в основном работает в таких ситуациях:

class Custom_Class extends Main_Class {
  function __construct() {
    remove_action('default_action', array($this));
    add_action('new_action', array($this));
  }
  /* custom code from here on */
}
new Custom_Class

Цель состоит в том, чтобы изменить $image->resize значение 320 пикселей, но я не могу даже удалить действие, а исходный код выглядит так:

class UR_Form_Handler {
/**
 * Hook in methods.
 */
public static function init() {
    add_action( 'template_redirect', array( __CLASS__, 'redirect_reset_password_link' ) );
    add_action( 'template_redirect', array( __CLASS__, 'save_profile_details' ) );
    add_action( 'template_redirect', array( __CLASS__, 'save_change_password' ) );
    add_action( 'wp_loaded', array( __CLASS__, 'process_login' ), 20 );
    add_action( 'wp_loaded', array( __CLASS__, 'process_lost_password' ), 20 );
    add_action( 'wp_loaded', array( __CLASS__, 'process_reset_password' ), 20 );
    add_action( 'user_registration_before_customer_login_form', array( __CLASS__, 'export_confirmation_request' ) );
}

/**
 * Save/update a profile fields if the form was submitted through the user account page.
 *
 * @return mixed
 */
public function save_profile_details() {
    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
        return;
    }

    if ( empty( $_POST['action'] ) || 'save_profile_details' !== $_POST['action'] || empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'save_profile_details' ) ) {
        return;
    }

    $user_id = get_current_user_id();

    if ( $user_id <= 0 ) {
        return;
    }
    if ( has_action( 'uraf_profile_picture_buttons' ) ) {
        if ( isset( $_POST['profile_pic_url'] ) && ! empty( $_POST['profile_pic_url'] ) ) {
            update_user_meta( $user_id, 'user_registration_profile_pic_url', $_POST['profile_pic_url'] );
        }
    } else {
        if ( isset( $_FILES['profile-pic'] ) && $_FILES['profile-pic']['size'] ) {

            if ( ! function_exists( 'wp_handle_upload' ) ) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
            }

            $upload           = $_FILES['profile-pic'];
            $upload_overrides = array(
                'action' => 'save_profile_details',
            );
            $uploaded         = wp_handle_upload( $upload, $upload_overrides );

            if ( $uploaded && ! isset( $uploaded['error'] ) ) {
                $image = wp_get_image_editor( $uploaded['file'] );

                if ( ! is_wp_error( $image ) ) {
                    $image->resize( 150, 150, true );
                    $image->save( $uploaded['file'] );
                }
                update_user_meta( $user_id, 'user_registration_profile_pic_url', $uploaded['url'] );
            } else {
                ur_add_notice( $uploaded['error'], 'error' );
            }
        } elseif ( UPLOAD_ERR_NO_FILE !== $_FILES['profile-pic']['error'] ) {

            switch ( $_FILES['profile-pic']['error'] ) {
                case UPLOAD_ERR_INI_SIZE:
                    ur_add_notice( __( 'File size exceed, please check your file size.', 'user-registration' ), 'error' );
                    break;
                default:
                    ur_add_notice( __( 'Something went wrong while uploading, please contact your site administrator.', 'user-registration' ), 'error' );
                    break;
            }
        } elseif ( empty( $_POST['profile-pic-url'] ) ) {
            $upload_dir  = wp_upload_dir();
            $profile_url = get_user_meta( $user_id, 'user_registration_profile_pic_url', true );

            // Check if profile already set?
            if ( $profile_url ) {

                // Then delete file and user meta.
                $profile_url = $upload_dir['basedir'] . explode( '/uploads', $profile_url )[1];

                if ( ! empty( $profile_url ) && file_exists( $profile_url ) ) {
                    @unlink( $profile_url );
                }
                delete_user_meta( $user_id, 'user_registration_profile_pic_url' );
            }
        }
    }

    $form_id_array = get_user_meta( $user_id, 'ur_form_id' );
    $form_id       = 0;

    if ( isset( $form_id_array[0] ) ) {
        $form_id = $form_id_array[0];
    }

    $profile = user_registration_form_data( $user_id, $form_id );

    foreach ( $profile as $key => $field ) {
        if ( ! isset( $field['type'] ) ) {
            $field['type'] = 'text';
        }
        // Get Value.
        switch ( $field['type'] ) {
            case 'checkbox':
                if ( isset( $_POST[ $key ] ) && is_array( $_POST[ $key ] ) ) {
                    $_POST[ $key ] = $_POST[ $key ];
                } else {
                    $_POST[ $key ] = (int) isset( $_POST[ $key ] );
                }
                break;
            default:
                $_POST[ $key ] = isset( $_POST[ $key ] ) ? ur_clean( $_POST[ $key ] ) : '';
                break;
        }

        // Hook to allow modification of value.
        $_POST[ $key ] = apply_filters( 'user_registration_process_myaccount_field_' . $key, $_POST[ $key ] );

        $disabled = false;
        if ( isset( $field['custom_attributes'] ) && isset( $field['custom_attributes']['readonly'] ) && isset( $field['custom_attributes']['disabled'] ) ) {
            if ( 'readonly' === $field['custom_attributes']['readonly'] || 'disabled' === $field['custom_attributes']['disabled'] ) {
                $disabled = true;
            }
        }

        if ( ! empty( $_POST[ $key ] ) ) {

            // Validation rules.
            if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
                foreach ( $field['validate'] as $rule ) {
                    switch ( $rule ) {
                        case 'email':
                            $_POST[ $key ] = strtolower( $_POST[ $key ] );

                            if ( ! is_email( $_POST[ $key ] ) ) {
                                ur_add_notice( sprintf( __( '%s is not a valid email address.', 'user-registration' ), '<strong>' . $field['label'] . '</strong>' ), 'error' );
                            }
                            break;
                    }
                }
            }
        }
    }// End foreach().

    do_action( 'user_registration_after_save_profile_validation', $user_id, $profile );

    if ( 0 === ur_notice_count( 'error' ) ) {
        $user_data = array();

        foreach ( $profile as $key => $field ) {
            $new_key = str_replace( 'user_registration_', '', $key );

            if ( in_array( $new_key, ur_get_user_table_fields() ) ) {

                if ( $new_key === 'display_name' ) {
                    $user_data['display_name'] = $_POST[ $key ];
                } else {
                    $user_data[ $new_key ] = $_POST[ $key ];
                }
            } else {
                $update_key = $key;

                if ( in_array( $new_key, ur_get_registered_user_meta_fields() ) ) {
                    $update_key = str_replace( 'user_', '', $new_key );
                }
                $disabled = isset( $field['custom_attributes']['disabled'] ) ? $field['custom_attributes']['disabled'] : '';

                if ( $disabled !== 'disabled' ) {
                    update_user_meta( $user_id, $update_key, $_POST[ $key ] );
                }
            }
        }

        if ( count( $user_data ) > 0 ) {
            $user_data['ID'] = get_current_user_id();
            wp_update_user( $user_data );
        }

        do_action( 'user_registration_save_profile_details', $user_id, $form_id );

        wp_safe_redirect( ur_get_endpoint_url( 'edit-profile', '', ur_get_page_permalink( 'myaccount' ) ) );
        exit;
    }
  }
}

Либо я получаю фатальную ошибку: не удается сделать stati c метод UR_Form_Handler :: save_profile_details () non stati c в классе Custom_UR_Form_Handler, или переопределение не работает вообще, я не уверен, что я вообще делаю это правильно, возможно, нет необходимости переопределять всю функцию, но я ' я застрял с этим, любая помощь или идея были бы очень признательны, спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...