PATH:
home
/
lab2454c
/
archswipe.com
/
wp-content
/
plugins
/
user-registration-advanced-fields
/
includes
<?php /** * URAF_AJAX * * AJAX Event Handler * * @class URAF_AJAX * @since 1.3.0 * @package UserRegistrationAdvancedFields/Classes * @category Class * @author WPEverest */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * URAF_AJAX Class */ class URAF_AJAX { /** * Hooks in ajax handlers. */ public static function init() { self::add_ajax_events(); } /** * Hook in methods - uses WordPress ajax handlers (admin-ajax) */ public static function add_ajax_events() { $ajax_events = array( 'method_upload' => true, 'method_remove' => true, ); foreach ( $ajax_events as $ajax_event => $nopriv ) { add_action( 'wp_ajax_uraf_profile_picture_upload_' . $ajax_event, array( __CLASS__, $ajax_event ) ); if ( $nopriv ) { add_action( 'wp_ajax_nopriv_uraf_profile_picture_upload_' . $ajax_event, array( __CLASS__, $ajax_event, ) ); } } } /** * User input dropped function. */ public static function method_upload() { check_ajax_referer( 'uraf_profile_picture_upload_nonce', 'security' ); $nonce = isset( $_REQUEST['security'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['security'] ) ) : false; $flag = wp_verify_nonce( $nonce, 'uraf_profile_picture_upload_nonce' ); if ( true != $flag || is_wp_error( $flag ) ) { wp_send_json_error( array( 'message' => __( 'Nonce error, please reload.', 'user-registration-advanced-fields' ), ) ); } $upload = isset( $_FILES['file'] ) ? $_FILES['file'] : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized // valid extension for image. $valid_extensions = $_REQUEST['valid_extension']; $valid_extension_type = explode( ',', $valid_extensions ); $valid_ext = array(); foreach ( $valid_extension_type as $key => $value ) { $image_extension = explode( '/', $value ); $valid_ext[ $key ] = $image_extension[1]; } $src_file_name = isset( $upload['name'] ) ? $upload['name'] : ''; $file_extension = strtolower( pathinfo( $src_file_name, PATHINFO_EXTENSION ) ); // Validates if the uploaded file has the acceptable extension. if ( ! in_array( $file_extension, $valid_ext ) ) { wp_send_json_error( array( 'message' => __( 'Invalid file type, please contact with site administrator.', 'user-registration-advanced-fields' ), ) ); } $max_size = wp_max_upload_size(); // Retrieves cropped picture dimensions from ajax request. $value = $_REQUEST['cropped_image']; $cropped_image_size = json_decode( stripslashes( $value ), true ); $max_uploaded_size_option_value = $_REQUEST['max_uploaded_size']; if ( isset( $max_uploaded_size_option_value ) && '' !== $max_uploaded_size_option_value ) { $max_upload_size_options_value = $max_uploaded_size_option_value * 1024; } else { $max_upload_size_options_value = $max_size; } if ( ! isset( $upload['size'] ) || ( isset( $upload['size'] ) && $upload['size'] < 1 ) ) { wp_send_json_error( array( /* translators: %s - Max Size */ 'message' => sprintf( __( 'Please upload a picture with size less than %s', 'user-registration-advanced-fields' ), size_format( $max_size ) ), ) ); } else if ( $upload['size'] > $max_upload_size_options_value ) { wp_send_json_error( array( /* translators: %s - Max Size */ 'message' => sprintf( __( 'Please upload a picture with size less than %s', 'user-registration-advanced-fields' ), size_format( $max_upload_size_options_value ) ), ) ); } $upload_dir = wp_upload_dir(); $upload_path = apply_filters( 'user_registration_profile_pic_upload_url', $upload_dir['basedir'] . '/user_registration_uploads/profile-pictures' ); /*Get path of upload dir of WordPress*/ if ( ! is_writable( $upload_path ) ) { /*Check if upload dir is writable*/ wp_send_json_error( array( 'message' => __( 'Upload path permission deny.', 'user-registration-advanced-fields' ), ) ); } $upload_path = $upload_path . '/'; $file_ext = strtolower( pathinfo( $upload['name'], PATHINFO_EXTENSION ) ); $file_name = user_registration_incremental_file_name( $upload_path, $upload ); $file_path = $upload_path . sanitize_file_name( $file_name ); if ( move_uploaded_file( $upload['tmp_name'], $file_path ) ) { $attachment_id = wp_insert_attachment( array( 'guid' => $file_path, 'post_mime_type' => $file_ext, 'post_title' => preg_replace( '/\.[^.]+$/', '', sanitize_file_name( $file_name ) ), 'post_content' => '', 'post_status' => 'inherit', ), $file_path ); if ( is_wp_error( $attachment_id ) ) { wp_send_json_error( array( 'message' => $attachment_id->get_error_message(), ) ); } include_once ABSPATH . 'wp-admin/includes/image.php'; // Generate and save the attachment metas into the database. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file_path ) ); $url = wp_get_attachment_url( $attachment_id ); // Retrieves original picture height and width. list( $original_image_width, $original_image_height ) = getimagesize( $file_path ); // Determines the type of uploaded picture and treats them differently. switch ( $upload['type'] ) { case 'image/png': $img_r = imagecreatefrompng( $file_path ); break; case 'image/gif': $img_r = imagecreatefromgif( $file_path ); break; default: $img_r = imagecreatefromjpeg( $file_path ); } $cropped_image_holder_width = rtrim( $cropped_image_size['holder_width'], 'px' ); $cropped_image_holder_height = rtrim( $cropped_image_size['holder_height'], 'px' ); // Calculates the actual portion of original picture where the cropping is applied. $cropped_image_width = absint( $cropped_image_size['w'] * $original_image_width / $cropped_image_holder_width ); $cropped_image_left = absint( $cropped_image_size['x'] * $original_image_width / $cropped_image_holder_width ); $cropped_image_height = absint( $cropped_image_size['h'] * $original_image_height / $cropped_image_holder_height ); $cropped_image_right = absint( $cropped_image_size['y'] * $original_image_height / $cropped_image_holder_height ); // Creates a frame of original height and width and copies the cropped picture portion to the frame. $dst_r = wp_imageCreateTrueColor( $original_image_width, $original_image_height ); imagecopyresampled( $dst_r, $img_r, 0, 0, $cropped_image_left, $cropped_image_right, $original_image_width, $original_image_height, $cropped_image_width, $cropped_image_height ); // Retrieves and Resizes the cropped picture to a size defined by user in filter or default of 150 by 150. list( $image_width, $image_height ) = apply_filters( 'user_registration_cropped_image_size', array( 150, 150 ) ); $dest_r = wp_imageCreateTrueColor( $image_width, $image_height ); imagecopyresampled( $dest_r, $dst_r, 0, 0, 0, 0, $image_width, $image_height, $original_image_width, $original_image_height ); // Replaces the original picture with the cropped picture. $img_r = imagejpeg( $dest_r, $file_path ); if ( empty( $url ) ) { $url = home_url() . '/wp-includes/images/media/text.png'; } wp_send_json_success( array( 'attachment_id' => $attachment_id, 'profile_picture_url' => $url, ) ); } else { wp_send_json_error( array( 'message' => __( 'File cannot be uploaded.', 'user-registration-advanced-fields' ), ) ); } } /** * User profile picture remove function. */ public static function method_remove() { check_ajax_referer( 'uraf_profile_picture_remove_nonce', 'security' ); $nonce = isset( $_REQUEST['security'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['security'] ) ) : false; $flag = wp_verify_nonce( $nonce, 'uraf_profile_picture_remove_nonce' ); if ( true != $flag || is_wp_error( $flag ) ) { wp_send_json_error( array( 'message' => __( 'Nonce error, please reload.', 'user-registration-advanced-fields' ), ) ); } $attachment_id = isset( $_POST['attachment_id'] ) ? intval( wp_unslash( $_POST['attachment_id'] ) ) : ''; if ( file_exists( get_attached_file( $attachment_id ) ) && ! unlink( get_attached_file( $attachment_id ) ) ) { wp_send_json_error( array( 'message' => esc_html__( 'File cannot be removed', 'user-registration-advanced-fields' ), ) ); } $user_id = get_current_user_id(); if ( $user_id > 0 ) { update_user_meta( $user_id, 'user_registration_profile_pic_url', '' ); } wp_send_json_success( array( 'message' => __( 'User profile picture removed successfully', 'user-registration-advanced-fields' ), ) ); } } URAF_AJAX::init();
[+]
admin
[+]
..
[-] class-uraf-frontend.php
[edit]
[-] functions-uraf-core.php
[edit]
[-] class-uraf-ajax.php
[edit]
[-] class-uraf-autoloader.php
[edit]
[-] .htaccess
[edit]
[-] class-uraf-install.php
[edit]
[+]
form