Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbohacek committed Feb 4, 2023
1 parent 1ce85dd commit 7c0759e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 82 deletions.
28 changes: 3 additions & 25 deletions archive/template-converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 "<hr/>";

Expand Down
2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
30 changes: 30 additions & 0 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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://' .
Expand Down
26 changes: 1 addition & 25 deletions template-submit-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ) ){
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 1 addition & 31 deletions template-submit-your-bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7c0759e

Please sign in to comment.