mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-07-02 01:01:18 +03:00
Move where image handling happens so we can pass back errors
This commit is contained in:
parent
1094dc3e89
commit
66772080a5
|
@ -108,6 +108,34 @@ function process_form_new() {
|
||||||
Contributor\create_new_contributor( $wporg_username, $new_pledge_id );
|
Contributor\create_new_contributor( $wporg_username, $new_pledge_id );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Process image.
|
||||||
|
if ( ! function_exists('media_handle_upload') ) {
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
||||||
|
require_once( ABSPATH . 'wp-admin/includes/media.php' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$logo = isset( $_FILES['org-logo'] ) ? $_FILES['org-logo'] : false;
|
||||||
|
if ( $logo ) {
|
||||||
|
if ( ! in_array( $logo['type'], [ 'image/png', 'image/jpg' ] ) ) {
|
||||||
|
return new WP_Error(
|
||||||
|
'invalid_image_type',
|
||||||
|
__( 'Logo file must be a png or jpg.', 'wporg' )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if ( ( $logo['size'] > 5 * MB_IN_BYTES ) ) {
|
||||||
|
return new WP_Error(
|
||||||
|
'invalid_image_size',
|
||||||
|
__( 'Logo file must be less than 5MB.', 'wporg' )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = \media_handle_sideload( $logo, $new_pledge_id );
|
||||||
|
if ( ! is_wp_error( $result ) ) {
|
||||||
|
set_post_thumbnail( $new_pledge_id, $result );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 'success';
|
return 'success';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,12 +69,6 @@ function get_pledge_meta_config( $context = '' ) {
|
||||||
'sanitize_callback' => 'sanitize_text_field',
|
'sanitize_callback' => 'sanitize_text_field',
|
||||||
'show_in_rest' => false,
|
'show_in_rest' => false,
|
||||||
),
|
),
|
||||||
'org-logo' => array(
|
|
||||||
'single' => true,
|
|
||||||
'sanitize_callback' => 'esc_url_raw',
|
|
||||||
'show_in_rest' => true,
|
|
||||||
'php_filter' => FILTER_VALIDATE_URL,
|
|
||||||
),
|
|
||||||
'pledge-email-confirmed' => array(
|
'pledge-email-confirmed' => array(
|
||||||
'single' => true,
|
'single' => true,
|
||||||
'sanitize_callback' => 'wp_validate_boolean',
|
'sanitize_callback' => 'wp_validate_boolean',
|
||||||
|
@ -230,27 +224,6 @@ function save_pledge( $pledge_id, $pledge ) {
|
||||||
function save_pledge_meta( $pledge_id, $new_values ) {
|
function save_pledge_meta( $pledge_id, $new_values ) {
|
||||||
$config = get_pledge_meta_config();
|
$config = get_pledge_meta_config();
|
||||||
|
|
||||||
// Process image.
|
|
||||||
if ( ! function_exists('media_handle_upload') ) {
|
|
||||||
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
|
||||||
require_once( ABSPATH . 'wp-admin/includes/file.php' );
|
|
||||||
require_once( ABSPATH . 'wp-admin/includes/media.php' );
|
|
||||||
}
|
|
||||||
|
|
||||||
$logo = isset( $_FILES['org-logo'] ) ? $_FILES['org-logo'] : false;
|
|
||||||
if (
|
|
||||||
$logo &&
|
|
||||||
in_array( $logo['type'], [ 'image/png', 'image/jpg' ] ) &&
|
|
||||||
( $logo['size'] < 5 * MB_IN_BYTES )
|
|
||||||
) {
|
|
||||||
$result = \media_handle_sideload( $logo, $pledge_id );
|
|
||||||
|
|
||||||
if ( ! is_wp_error( $result ) ) {
|
|
||||||
$new_values['org-logo'] = wp_get_attachment_url( $result );
|
|
||||||
set_post_thumbnail( $pledge_id, $result );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ( $new_values as $key => $value ) {
|
foreach ( $new_values as $key => $value ) {
|
||||||
if ( array_key_exists( $key, $config ) ) {
|
if ( array_key_exists( $key, $config ) ) {
|
||||||
$meta_key = META_PREFIX . $key;
|
$meta_key = META_PREFIX . $key;
|
||||||
|
|
Loading…
Reference in a new issue