Website Summary Files Commits Branches @commit @diff @stats
path: root / wp-content / plugins
diff options
context:
space:
mode:
Commitac629df189f1bf36ef8737fbfcf144d5d92968c9 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2020-07-07 19:11:07 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2020-07-07 19:11:07 +0300
Filesfe87f6f59b278a5c795a7fee1d9c67229d51f8b7 /wp-content/plugins
tues-interns-2020-ac629df189f1bf36ef8737fbfcf144d5d92968c9.tar
tues-interns-2020-ac629df189f1bf36ef8737fbfcf144d5d92968c9.tar.gz
tues-interns-2020-ac629df189f1bf36ef8737fbfcf144d5d92968c9.zip
Parent3786ddd00b3091c5ed5a58f68e8ba116883f9eb9 (diff)
Implimented uploading of images. They are shown in the metabox. Updated some styling to comply with wordpress standards.
2 files changed, 55 insertions, 12 deletions
-rw-r--r--wp-content/plugins/dx-cars/car_info.php11
-rw-r--r--wp-content/plugins/dx-cars/class.cpt.php56
diff --git a/wp-content/plugins/dx-cars/car_info.php b/wp-content/plugins/dx-cars/car_info.php
index 771e67b..b8931aa 100644
--- a/wp-content/plugins/dx-cars/car_info.php
+++ b/wp-content/plugins/dx-cars/car_info.php
@@ -13,6 +13,15 @@
</tr>
<tr>
<td><?php _e( 'Photo:' ) ?></td>
- <td><input type="image" name="car-image" value="<?php echo( sanitize_text_field( get_post_meta( get_the_ID(), __( 'car-image' ), true ) ) ) ?>"></td>
+ <td><input type="file" name="car-images[]" value="" multiple></td>
+ </tr>
+ <tr>
+ <td><?php _e( 'Uploaded photo<br>(might need page refresh):' ) ?></td>
+ <?php
+ $images = ( array )json_decode( get_post_meta( get_the_ID(), __( 'car-images' ), true ) );
+ foreach ( $images as $image ) {
+ echo( '<td> <img src="' . $image . '" alt=""></td>' );
+ }
+ ?>
</tr>
</table>
diff --git a/wp-content/plugins/dx-cars/class.cpt.php b/wp-content/plugins/dx-cars/class.cpt.php
index b203205..03bdec3 100644
--- a/wp-content/plugins/dx-cars/class.cpt.php
+++ b/wp-content/plugins/dx-cars/class.cpt.php
@@ -33,7 +33,38 @@ class Vehicles {
}
function remove_message( $location ) {
- return remove_query_arg( 'message', $location);
+ return remove_query_arg( 'message', $location );
+ }
+
+ function allowed_image_types() {
+ $mime_types = array(
+ 'jpg|jpeg|jpe' => 'image/jpeg',
+ 'gif' => 'image/gif',
+ 'png' => 'image/png',
+ 'bmp' => 'image/bmp',
+ 'tiff|tif' => 'image/tiff',
+ );
+ return $mime_types;
+ }
+
+ function get_enc_uploaded_img_urls( $images ) {
+ $uploaded_urls = array();
+ for ( $i = 0; $i < sizeof( $images['name'] ); $i++ ) {
+ if ( wp_check_filetype( $images['name'][ $i ], $this->allowed_image_types() )['ext'] ) {
+ $file = array(
+ 'name' => $images['name'][ $i ],
+ 'type' => $images['type'][ $i ],
+ 'tmp_name' => $images['tmp_name'][ $i ],
+ 'error' => $images['error'][ $i ],
+ 'size' => $images['size'][ $i ]
+ );
+ $uploaded = wp_handle_upload( $file, array( 'test_form' => false ) );
+ array_push( $uploaded_urls, $uploaded['url'] );
+ } else {
+ wp_die( 'Invalid image' );
+ }
+ }
+ return json_encode( $uploaded_urls );
}
function car_info_save_meta( $post_id, $post ) {
@@ -41,12 +72,14 @@ class Vehicles {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
+
// Now that we're authenticated, time to save the data.
// This sanitizes the data from the field and saves it into an array $events_meta.
$events_meta = array(
'car-year' => sanitize_text_field( isset( $_POST['car-year'] ) ? $_POST['car-year'] : null ),
'car-millage' => sanitize_text_field( isset( $_POST['car-millage'] ) ? $_POST['car-millage'] : null ),
'car-price' => sanitize_text_field( isset( $_POST['car-price'] ) ? $_POST['car-price'] : null ),
+ 'car-images' => $this->get_enc_uploaded_img_urls( $_FILES['car-images'] ),
);
// Cycle through the $events_meta array.
@@ -57,14 +90,20 @@ class Vehicles {
return;
}
- if ($value == null) {
+ if ( $value == null ) {
return;
}
- switch( $key ) {
- case 'car-year': if( strcmp( "1769", $value ) > 0 || strcmp( "2020", $value ) < 0 ) wp_die('Invalid year'); break;
- case 'car-millage': if( strcmp( "0", $value ) > 0 ) wp_die('Invalid millage.'); break;
- case 'car-price': if( strcmp( "0", $value ) > 0 ) wp_die('Invalid price.'); break;
+ switch ( $key ) {
+ case 'car-year':
+ if( strcmp( "1769", $value ) > 0 || strcmp( "2020", $value ) < 0 ) wp_die( 'Invalid year' );
+ break;
+ case 'car-millage':
+ if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid millage.' );
+ break;
+ case 'car-price':
+ if( strcmp( "0", $value ) > 0 ) wp_die( 'Invalid price.' );
+ break;
}
if ( get_post_meta( $post_id, $key, false ) ) {
@@ -74,11 +113,6 @@ class Vehicles {
// If the custom field doesn't have a value, add it.
add_post_meta( $post_id, $key, $value );
}
-
- if ( ! $value ) {
- // Delete the meta key if there's no value
- delete_post_meta( $post_id, $key );
- }
}
}
}