From 7c0759eeefc2a402b9c82650b41621d94ea95acc Mon Sep 17 00:00:00 2001 From: Stefan Bohacek Date: Sat, 4 Feb 2023 11:30:16 -0500 Subject: [PATCH] Code cleanup. --- archive/template-converter.php | 28 +++------------------------- functions.php | 2 +- includes/helpers.php | 30 ++++++++++++++++++++++++++++++ template-submit-resource.php | 26 +------------------------- template-submit-your-bot.php | 32 +------------------------------- 5 files changed, 36 insertions(+), 82 deletions(-) diff --git a/archive/template-converter.php b/archive/template-converter.php index 19662eda..896cf764 100644 --- a/archive/template-converter.php +++ b/archive/template-converter.php @@ -37,30 +37,6 @@ function make_slug($string) { return $string; } - - function add_post_thumbnail( $post_id, $image_path, $description ){ - $upload_dir = wp_upload_dir(); - $image_data = file_get_contents($image_path); - $filename = basename($image_path); - if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; - else $file = $upload_dir['basedir'] . '/' . $filename; - file_put_contents($file, $image_data); - - $wp_filetype = wp_check_filetype($filename, null ); - $attachment = array( - 'post_mime_type' => $wp_filetype['type'], - 'post_title' => $description, - 'post_content' => '', - 'post_status' => 'inherit' - ); - $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); - require_once(ABSPATH . 'wp-admin/includes/image.php'); - $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); - $res1 = wp_update_attachment_metadata( $attach_id, $attach_data ); - $res2 = set_post_thumbnail( $post_id, $attach_id ); - } - - $current = 0; foreach ($files as $file) { @@ -254,7 +230,9 @@ function add_post_thumbnail( $post_id, $image_path, $description ){ // noop } - add_post_thumbnail($post_id, $image_path, $file_meta['Description']); + global $helpers; + + $helpers->add_post_thumbnail($post_id, $image_path, $file_meta['Description']); } echo "
"; diff --git a/functions.php b/functions.php index 794c6c7d..daeceb66 100644 --- a/functions.php +++ b/functions.php @@ -908,7 +908,7 @@ function bw_import_bots(\WP_REST_Request $request){ /* noop */ } } - add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); + $helpers->add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); } catch ( Exception $e ) { // TODO: Proper error handling. log_this($e->getMessage()); diff --git a/includes/helpers.php b/includes/helpers.php index 033ef9ec..0a03e531 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -301,6 +301,36 @@ function get_screenshotable_url( $urls ){ return $screenshotable_url; } + function add_post_thumbnail( $post_id, $image_path, $description ){ + $upload_dir = wp_upload_dir(); + $image_data = file_get_contents( $image_path ); + $filename = basename( $image_path ); + + if ( wp_mkdir_p( $upload_dir['path'] ) ){ + $file = $upload_dir['path'] . '/' . $filename; + } + else{ + $file = $upload_dir['basedir'] . '/' . $filename; + } + + file_put_contents( $file, $image_data ); + $wp_filetype = wp_check_filetype( $filename, null ); + + $attachment = array( + 'post_mime_type' => $wp_filetype['type'], + // 'post_title' => $description, + 'post_title' => $_POST['bot-name'], + 'post_content' => '', + 'post_status' => 'inherit' + ); + + $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); + require_once( ABSPATH . 'wp-admin/includes/image.php' ); + $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); + $res1 = wp_update_attachment_metadata( $attach_id, $attach_data ); + $res2 = set_post_thumbnail( $post_id, $attach_id ); + } + function get_fediverse_url( $handle ){ $handle_array = explode('@', $handle); $fediverse_url = 'https://' . diff --git a/template-submit-resource.php b/template-submit-resource.php index 978588b0..a3912a33 100644 --- a/template-submit-resource.php +++ b/template-submit-resource.php @@ -22,30 +22,6 @@ ( isset( $_POST['resource-tags'] ) && !empty( $_POST['resource-tags'] ) ) ) { - function add_post_thumbnail( $post_id, $image_path, $description ){ - $upload_dir = wp_upload_dir(); - $image_data = file_get_contents($image_path); - $filename = basename($image_path); - if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename; - else $file = $upload_dir['basedir'] . '/' . $filename; - file_put_contents($file, $image_data); - - $wp_filetype = wp_check_filetype($filename, null ); - $attachment = array( - 'post_mime_type' => $wp_filetype['type'], - // 'post_title' => $description, - 'post_title' => $_POST['resource-name'], - 'post_content' => '', - 'post_status' => 'inherit' - ); - $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); - require_once(ABSPATH . 'wp-admin/includes/image.php'); - $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); - $res1 = wp_update_attachment_metadata( $attach_id, $attach_data ); - $res2 = set_post_thumbnail( $post_id, $attach_id ); - } - - $resource_authors = array(); if ( !empty( $_POST['author-names'] ) ){ @@ -178,7 +154,7 @@ function add_post_thumbnail( $post_id, $image_path, $description ){ /* noop */ } - add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); + $helpers->add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); if ( !is_user_logged_in() || $_POST['disassociate-author-input'] !== 'false' ){ global $wpdb; diff --git a/template-submit-your-bot.php b/template-submit-your-bot.php index 7da67a07..51c56b94 100644 --- a/template-submit-your-bot.php +++ b/template-submit-your-bot.php @@ -19,36 +19,6 @@ function get_network_name( $network_term_slug ){ return get_term_by( 'slug', $network_term_slug, 'network' )->name; } - function add_post_thumbnail( $post_id, $image_path, $description ){ - $upload_dir = wp_upload_dir(); - $image_data = file_get_contents( $image_path ); - $filename = basename( $image_path ); - - if ( wp_mkdir_p( $upload_dir['path'] ) ){ - $file = $upload_dir['path'] . '/' . $filename; - } - else{ - $file = $upload_dir['basedir'] . '/' . $filename; - } - - file_put_contents( $file, $image_data ); - $wp_filetype = wp_check_filetype( $filename, null ); - - $attachment = array( - 'post_mime_type' => $wp_filetype['type'], - // 'post_title' => $description, - 'post_title' => $_POST['bot-name'], - 'post_content' => '', - 'post_status' => 'inherit' - ); - - $attach_id = wp_insert_attachment( $attachment, $file, $post_id ); - require_once( ABSPATH . 'wp-admin/includes/image.php' ); - $attach_data = wp_generate_attachment_metadata( $attach_id, $file ); - $res1 = wp_update_attachment_metadata( $attach_id, $attach_data ); - $res2 = set_post_thumbnail( $post_id, $attach_id ); - } - if ( !empty( $_POST ) ){ // error_log( print_r( $_POST, true ) ); @@ -246,7 +216,7 @@ function add_post_thumbnail( $post_id, $image_path, $description ){ /* noop */ } } - add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); + $helpers->add_post_thumbnail( $new_post_id, $screenshot['image_path'], $bot_description ); if ( !is_user_logged_in() || $_POST['disassociate-author-input'] !== 'false' ){ global $wpdb;