Load image into media library, attach to the submitted pledge as a featured image.

This commit is contained in:
Kelly Dwan 2019-10-11 12:07:28 -04:00
parent b3ea42b84d
commit 1094dc3e89
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
2 changed files with 26 additions and 1 deletions

View file

@ -230,6 +230,27 @@ function save_pledge( $pledge_id, $pledge ) {
function save_pledge_meta( $pledge_id, $new_values ) {
$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 ) {
if ( array_key_exists( $key, $config ) ) {
$meta_key = META_PREFIX . $key;

View file

@ -24,7 +24,11 @@ namespace WordPressDotOrg\FiveForTheFuture\View;
<?php esc_html_e( 'Logo', 'wordpressorg' ); ?>
</label>
<br />
<?php /* @todo Display existing logo here */ ?>
<?php if ( is_admin() && has_post_thumbnail() ) : ?>
<div class="form-field__logo-display">
<?php the_post_thumbnail(); ?>
</div>
<?php endif; ?>
<input
type="file"
id="5ftf-org-logo"