Skip to content

Instantly share code, notes, and snippets.

@platoosom
Created April 17, 2020 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save platoosom/b9913a8ff8ca00bb93726ab435a05b7f to your computer and use it in GitHub Desktop.
Save platoosom/b9913a8ff8ca00bb93726ab435a05b7f to your computer and use it in GitHub Desktop.
wpml insert post in english language ให้โฟกัสที่บรรทัด 105-117 มันใช้ hook wpml_set_element_language_details ในการยัดภาษาเข้าไป ถ้าไม่มี hook นี้เวลาสร้างโพสต์มันจะเซฟเป็นภาษา default
function topgear_midnight_cron_task()
{
$action = isset($_GET['action'])? $_GET['action']: null;
if($action !== 'cron'){ return ;}
// Find Author
$user = get_user_by( 'login', 'Topgear' );
$userID = ($user->ID)? $user->ID : 4;
// Find Category
$category = get_category_by_slug( 'car-news' );
$categoryID = ($category->term_id)? $category->term_id: null;
// Get the contents of the JSON file
$strJsonFileContents = file_get_contents('https://www.topgear.com/uk/car-news/json');
// Convert to object
$j = json_decode($strJsonFileContents);
foreach($j->nodes as $node){
$item = $node->node;
// Check content is exists, skip if doesn't exists.
if(empty($item->BodyText)){
continue;
}
$LinkUrls = explode('/', $item->LinkUrl);
$slug = end($LinkUrls);
if (topgear_post_exists_by_LinkUrl($item->LinkUrl)) {
continue;
}
// If not exists insert it. Create post object
$article = array(
'post_title' => $item->PageTitle,
'post_content' => $item->BodyText,
'post_excerpt' => $item->Summary,
'post_status' => 'draft',
'post_author' => $userID,
'post_category' => array($categoryID),
'post_name' => $slug,
);
// Insert the post into the database
$ID = wp_insert_post( $article );
echo $ID, ', ';
add_post_meta($ID, 'LinkUrl', $item->LinkUrl);
// Insert tags
if($item->Keywords != ''){
$tags = explode(',', $item->Keywords);
wp_set_post_tags( $ID, $tags); // Set tags to Post
}
// Insert thumbnail
$image_url = 'https://www.topgear.com'. $item->ImageUrl;
$r = explode('/', $image_url);
$image_name = end($r);
$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, $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($ID, $attach_id);
// It is English language
if ( is_plugin_active('sitepress-multilingual-cms/sitepress.php') ) {
$get_language_args = array('element_id' => $ID, 'element_type' => 'post' );
$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
$args = array(
'element_id' => $ID,
'element_type' => 'post_post',
'language_code' => 'en',
'trid' => $original_post_language_info->trid,
'source_language_code' => $original_post_language_info->language_code,
);
do_action( 'wpml_set_element_language_details', $args );
}
}
die('CRON Finish.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment