//raw это просто объект с кастомными полями
public function save(){
if(empty($this->raw))
// $raw = $this->sampledata();
$raws = $this->getJson();
else
$raws = $this->raw;
$ids = [];
foreach ($raws as $raw)
$ids[] = $raw->id;
$exist = $this->get_exists_ids($ids);
$posts = [];
foreach ($raws as $raw) {
if(in_array($raw->id, $exist)) continue;
//https://developer.wordpress.org/reference/functions/wp_insert_post/
$post = array(
'post_title' => wp_strip_all_tags($raw->title),
'post_date' => $raw->time,
'post_content' => wp_slash($raw->text),
'post_excerpt' => wp_strip_all_tags($raw->descr),
'post_status' => 'publish',
'post_type' => 'post_smi',
'comment_status' => 'closed',
'guid' => 'smi-'.$raw->id,
'tax_input' => [
'post_smi_tags' => [
$raw->source,
]
],
'meta_input' => [
'source' => $raw->source,
'attitude' => $raw->attitude,
'region' => $raw->region,
'speech' => $raw->speech,
'time' => $raw->time,
'time_parsed' => $raw->time_parsed,
'stat_read' => $raw->stat_read,
'stat_saved' => $raw->stat_saved,
'source_id' => $raw->id,
'image' => $raw->image,
'url' => $raw->url,
],
);
//replace thumb suffix size to original size
$image_url = $raw->image;
$reg = '/\d+x\d+\.(\w+)$/';
$image_url = preg_replace($reg, 'text-1.$1',$image_url);
$post_id = wp_insert_post($post);
$this->set_post_image($post_id, $image_url);
}
}
public function set_post_image($post_id, $image_url){
// $image_url = 'http://s.wordpress.org/style/images/wp-header-logo.png'; // Define the image URL here
$image_name = basename($image_url);
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$unique_file_name = wp_unique_filename( $upload_dir['path'], $image_name ); // Generate unique name
$filename = basename( $unique_file_name ); // Create image file name
// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
// Create the image file on the server
file_put_contents( $file, $image_data );
// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );
// Set attachment data
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );
// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );
}
public function get_exists_ids($ids){
$args = [
'posts_per_page' => 50,
'order' => 'DESC',
'post_type' => 'post_smi',
'meta_query' => [
'relation' => 'AND',
[
'key' => 'source_id',
'value' => $ids,
'compare' => 'IN',
],
],
];
$query = new WP_Query($args);
if(count($query->posts)<1)
return [];
$posts_ids = [];
foreach ($query->posts as $post) {
$posts_ids[] = $post->source_id;
}
return $posts_ids;
}