Last active
January 5, 2018 09:10
-
-
Save mauryaratan/e3e3a5fa9bc11bd1385c94c4037e8b66 to your computer and use it in GitHub Desktop.
Insert a post manually to put correct data back in its place.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Embassy Suites Amarillo,"550 Buchanan Street, Amarillo, TX 79101","Amarillo, TX",Hilton,Open,226,,,35.207672,-101.8324899 | |
async function fetchAsync(string) { | |
const response = await fetch(ajaxurl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' | |
}, | |
credentials: 'same-origin', | |
body: `action=crest_insert_post&string=${string}` | |
}); | |
return await response.json(); | |
} | |
const insertPost = (string) => { | |
fetchAsync(string).then((data) => { | |
if ( data.success === 'false' ) { | |
console.log(data.data); | |
} else { | |
console.log( 'Post created: ', data.data.data); | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Insert a post manually to Crest portfolio. | |
* Can only be run inside admin area. | |
* | |
* @return void | |
*/ | |
function crest_insert_post() { | |
// Bail early if not admin or if current user doesn't have administration capabilities. | |
if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) { | |
wp_send_json_error(); | |
} | |
// A comma separated value from, exported from Google Drive excel sheet. | |
$string = $_POST['string']; // @codingStandardsIgnoreLine | |
// Safely extract comma separated values into an array. Escapes commas between double quotes. | |
$hotel_data = str_getcsv( $string, ',' ); | |
// Destructure hotel data from referenced array. | |
[ $title, $address, $address_label, $brand, $status, $rooms, $featured_image, $url, $lat, $long ] = $hotel_data; | |
// Check if post_exists() function exists. | |
if ( ! function_exists( 'post_exists' ) ) { | |
require_once ABSPATH . 'wp-admin/includes/post.php'; | |
} | |
if ( post_exists( $title ) ) { | |
wp_send_json( [ | |
'success' => 'false', | |
'data' => 'Post already exists.', | |
] ); | |
exit; | |
} | |
$post_args = [ | |
'post_type' => 'crest-portfolio', | |
'post_title' => $title, | |
'post_status' => 'publish', | |
'meta_input' => [ | |
'hotel_details_rooms' => $rooms, | |
'hotel_details_address_label' => $address_label, | |
'hotel_details_link' => $url, | |
'hotel_details_lat' => $lat, | |
'hotel_details_long' => $long, | |
], | |
]; | |
$post_id = wp_insert_post( $post_args ); | |
$post = get_post( $post_id ); | |
wp_send_json_success( [ | |
'success' => true, | |
'data' => $post, | |
] ); | |
} | |
add_action( 'wp_ajax_crest_insert_post', 'crest_insert_post' ); | |
function crest_admin_enqueue( $hook ) { | |
wp_enqueue_script( 'insert_post_script', get_template_directory_uri() . '/js/insertPost.js' ); | |
} | |
add_action( 'admin_enqueue_scripts', 'crest_admin_enqueue' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment