plugin_name = $plugin_name;
$this->version = $version;
$this->post_type = $post_type;
// Register the post type
add_action('init', array($this, 'register'));
// Admin set post columns
add_filter('manage_edit-' . $this->post_type . '_columns', array($this, 'set_columns')) ;
// Set messages
add_filter('post_updated_messages', array($this, 'wpvr_post_updated_messages') );
// Admin edit post columns
add_filter('manage_' . $this->post_type . '_posts_custom_column', array($this, 'edit_columns'));
add_filter('admin_body_class',array($this, 'add_body_class'));
add_filter('admin_head',array($this, 'add_javascript_in_wpvr_admin_page'));
// add_action('admin_footer',array($this, 'add_new_tour_modal'));
}
/**
* Adds a modal for creating a new tour.
*
* This function checks if the current screen is the post type edit screen and
* if so, it displays a modal for creating a new tour. The modal includes options
* for selecting the tour type, entering the tour name, and submitting the form.
*
* @return void
*/
public function add_new_tour_modal()
{
$current_screen = get_current_screen();
if(!empty($current_screen->post_type) && $current_screen->post_type == $this->post_type && $current_screen->base == 'edit'){
$radio_icon = '';
?>
post_type) && $current_screen->post_type == $this->post_type && $current_screen->base == 'edit'){
?>
post_type) && $current_screen->post_type == $this->post_type && $current_screen->base == 'post' && $current_screen->action == 'add') {
// $tour_title = $_GET['tour_title'];
?>
post_type) && $current_screen->post_type == $this->post_type && $current_screen->base == 'edit') {
$classes .= 'wpvr-tour-list';
}
return $classes;
}
/**
* Register the custom post type
*
* @since 8.0.0
*/
public function register()
{
$labels = array(
'name' => __('Tours', $this->plugin_name),
'singular_name' => __('Tours', $this->plugin_name),
'add_new' => __('Add New Tour', $this->plugin_name),
'add_new_item' => __('Add New Tour', $this->plugin_name),
'edit_item' => __('Edit Tour', $this->plugin_name),
'new_item' => __('New Tour', $this->plugin_name),
'view_item' => __('View Tour', $this->plugin_name),
'search_items' => __('Search WP VR Tour', $this->plugin_name),
'not_found' => __('No WP VR Tour found', $this->plugin_name),
'not_found_in_trash'=> __('No WP VR Tour found in Trash', $this->plugin_name),
'parent_item_colon' => '',
'all_items' => __('All Tours', $this->plugin_name),
'menu_name' => __('WP VR', $this->plugin_name),
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 100,
'supports' => array( 'title' ),
'menu_icon' => plugins_url(). '/wpvr/images/icon.png',
'capabilities' => array(
'edit_post' => 'edit_wpvr_tour',
'edit_posts' => 'edit_wpvr_tours',
'edit_others_posts' => 'edit_other_wpvr_tours',
'publish_posts' => 'publish_wpvr_tours',
'read_post' => 'read_wpvr_tour',
'read_private_posts' => 'read_private_wpvr_tours',
'delete_post' => 'delete_wpvr_tour'
),
'map_meta_cap' => true,
);
/**
* Documentation : https://codex.wordpress.org/Function_Reference/register_post_type
*/
register_post_type($this->post_type, $args);
}
public function copy_shortcode_html()
{
$copy = 'Copied';
return $copy;
}
/**
* @param $columns
* @return mixed
*
* Choose the columns you want in
* the admin table for this post
* @since 8.0.0
*/
public function set_columns($columns) {
// Set/unset post type table columns
$columns = array(
'cb' => '',
'title' => __('Title', $this->plugin_name),
'thumbnail' => __('Thumbnail', $this->plugin_name),
'shortcode' => __('Shortcodes', $this->plugin_name),
'type' => __('Type', $this->plugin_name),
'author' => __('Author', $this->plugin_name),
'date' => __('Date', $this->plugin_name)
);
return $columns;
}
/**
* @param $column
* @param $post_id
*
* Edit the contents of each column in
* the admin table for this post
* @since 8.0.0
*/
public function edit_columns($column) {
// Post type table column content
$post = get_post();
$postdata = get_post_meta($post->ID, 'panodata', true);
$image_tour_icon = '';
$video_tour_icon = '';
$streetview_icon = '';
switch ($column) {
case 'shortcode':
echo '
';
break;
case 'thumbnail':
$scene_image = !empty($postdata['preview']) ? $postdata['preview'] : $this->get_scene_image($postdata);
if ($scene_image) {
echo '';
}else {
echo '';
}
break;
case 'type':
if (isset($postdata['streetviewdata'])) {
echo ''. $streetview_icon .'Street View';
} elseif (isset($postdata['vidid'])) {
echo ''. $video_tour_icon .'360 Video';
} else {
echo ''. $image_tour_icon .' 360 Image';
}
break;
default:
break;
}
}
/**
* Get the scene image for the post
*
* @param $postdata array The post meta data
* @return string
* @since 8.5.22
*/
private function get_scene_image($postdata) {
if (!empty($postdata['panodata']['scene-list'][1]['scene-type']) && $postdata['panodata']['scene-list'][1]['scene-type'] === 'equirectangular') {
$image_src = $postdata['panodata']['scene-list'][1]['scene-attachment-url'] ?? '';
$thumbnail = $this->wpvr_fetch_thumbnail($image_src);
if ($thumbnail) {
return $thumbnail;
}
}
$image_src_face = $postdata['panodata']['scene-list'][1]['scene-attachment-url-face0'] ?? '';
return $this->wpvr_fetch_thumbnail($image_src_face);
}
/**
* Fetches the thumbnail for the given image URL
*
* @param string $image_url The URL of the image
* @return string The URL of the thumbnail
* @since 8.5.22
*/
private function wpvr_fetch_thumbnail($image_url) {
if (empty($image_url)) {
return '';
}
$attachment_id = attachment_url_to_postid($image_url);
if ($attachment_id) {
$thumb_url = wp_get_attachment_image_url($attachment_id, 'thumbnail');
if ($thumb_url) {
return $thumb_url;
}
}
$attachment_id = $this->wpvr_get_attachment_id_by_url($image_url);
if ($attachment_id) {
$thumb_url = wp_get_attachment_image_url($attachment_id, 'thumbnail');
if ($thumb_url) {
return $thumb_url;
}
}
return $this->wpvr_generate_thumbnail($image_url);
}
/**
* Get the attachment ID by URL
*
* @param string $image_url The URL of the image
* @return int The ID of the attachment
* @since 8.5.22
*/
private function wpvr_get_attachment_id_by_url($image_url) {
global $wpdb;
return $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid=%s AND post_type='attachment'",
$image_url
));
}
/**
* Generates a thumbnail for the given image URL
*
* @param string $image_url The URL of the image
* @return string The URL of the thumbnail
* @since 8.5.22
*/
private function wpvr_generate_thumbnail($image_url) {
$uploads_dir = wp_upload_dir();
$image_path = str_replace($uploads_dir['baseurl'], $uploads_dir['basedir'], $image_url);
if (file_exists($image_path)) {
$image = wp_get_image_editor($image_path);
if (!is_wp_error($image)) {
$thumb_path = dirname($image_path) . '/thumb-' . basename($image_path);
$image->resize(150, 150, true);
$image->save($thumb_path);
return str_replace($uploads_dir['basedir'], $uploads_dir['baseurl'], $thumb_path);
}
}
return $image_url;
}
/**
* Sets the messages for the custom post type
*
* @since 8.0.0
*/
public function wpvr_post_updated_messages($messages)
{
$messages[$this->post_type][1] = __('WP VR item updated.', $this->plugin_name);
$messages[$this->post_type][4] = __('WP VR item updated.', $this->plugin_name);
return $messages;
}
}