| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPVR Sample Tour Class |
| 4 |
* |
| 5 |
* This class handles the creation of a sample virtual tour for the WPVR plugin. |
| 6 |
* It includes methods to copy necessary images to the WordPress uploads folder |
| 7 |
* and generate a sample tour post with predefined panorama data. |
| 8 |
* |
| 9 |
* @since 8.5.21 |
| 10 |
*/ |
| 11 |
|
| 12 |
class WPVR_Sample_Tour { |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Check if running in WordPress Playground. |
| 17 |
* |
| 18 |
* @return bool True if in Playground, false otherwise. |
| 19 |
* @since 8.5.22 |
| 20 |
*/ |
| 21 |
public function wpvr_is_playground() { |
| 22 |
|
| 23 |
if (get_option('wpvr_is_playground') === '1') { |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
// Check if HTTP_HOST is set |
| 28 |
$host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; |
| 29 |
|
| 30 |
// Check for Playground domains |
| 31 |
$playground_domains = [ |
| 32 |
'playground.wordpress.net', // Online Playground |
| 33 |
'.wordpress.net', // Any WordPress.net subdomain |
| 34 |
'localhost:5400', // Local Playground default port |
| 35 |
]; |
| 36 |
|
| 37 |
foreach ($playground_domains as $domain) { |
| 38 |
if (strpos($host, $domain) !== false) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Check for specific environment variables that might be set in Playground |
| 44 |
if (defined('IS_WORDPRESS_PLAYGROUND') && IS_WORDPRESS_PLAYGROUND) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
// Check for specific file structures that might indicate Playground |
| 49 |
if (file_exists('/wordpress-playground') || file_exists('/wp-playground')) { |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Create a sample virtual tour for the WPVR plugin. |
| 58 |
* |
| 59 |
* This method copies the necessary images to the WordPress uploads folder |
| 60 |
* and generates a sample tour post with predefined panorama data. |
| 61 |
* |
| 62 |
* @since 8.5.21 |
| 63 |
*/ |
| 64 |
public function create_sample_tour() { |
| 65 |
|
| 66 |
if (!function_exists('WP_Filesystem')) { |
| 67 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 68 |
} |
| 69 |
|
| 70 |
// Initialize the Filesystem API |
| 71 |
global $wp_filesystem; |
| 72 |
if (!isset($wp_filesystem)) { |
| 73 |
$filesystem_initialized = WP_Filesystem(); |
| 74 |
if (!$filesystem_initialized) { |
| 75 |
wp_send_json_error(__('Failed to initialize WP_Filesystem', 'wpvr')); |
| 76 |
return; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
$file_save_url = wp_upload_dir(); |
| 81 |
$sample_data_path = WPVR_PLUGIN_DIR_PATH . 'sample-data/wpvr_11852.json'; |
| 82 |
$tour_json = $sample_data_path; |
| 83 |
$arrContextOptions = array( |
| 84 |
"ssl" => array( |
| 85 |
"verify_peer" => false, |
| 86 |
"verify_peer_name" => false, |
| 87 |
), |
| 88 |
); |
| 89 |
$getfile = file_get_contents($tour_json, false, stream_context_create($arrContextOptions)); |
| 90 |
|
| 91 |
$file_content = json_decode($getfile, true); |
| 92 |
|
| 93 |
$new_title = $file_content['title']; |
| 94 |
$new_data = $file_content['data']; |
| 95 |
|
| 96 |
if(empty($new_data)){ |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
$new_post_id = wp_insert_post(array( |
| 101 |
'post_title' => $new_title, |
| 102 |
'post_type' => 'wpvr_item', |
| 103 |
'post_status' => 'publish', |
| 104 |
)); |
| 105 |
if ($new_post_id) { |
| 106 |
// Mark this as a demo tour |
| 107 |
update_post_meta($new_post_id, 'wpvr_is_demo_tour', '1'); |
| 108 |
|
| 109 |
if ($new_data['panoid']) { |
| 110 |
$new_data['panoid'] = 'pano' . $new_post_id; |
| 111 |
} |
| 112 |
if ($new_data['preview']) { |
| 113 |
$new_data['preview'] = WPVR_Sample_Tour_Import::prepare_preview_file_to_import($file_save_url, $new_post_id, $new_data); |
| 114 |
} |
| 115 |
|
| 116 |
if ($new_data['cpLogoImg']) { |
| 117 |
$new_data['cpLogoImg'] = WPVR_Sample_Tour_Import::prepare_company_log_image_to_import($new_data['cpLogoImg'], $file_save_url, $new_post_id, $new_data); |
| 118 |
} |
| 119 |
|
| 120 |
if ($new_data['bg_music_url']) { |
| 121 |
$new_data['bg_music_url'] = WPVR_Sample_Tour_Import::prepare_bg_music_url_to_import($new_data, $file_save_url, $new_post_id); |
| 122 |
} |
| 123 |
if ($new_data['panodata']) { |
| 124 |
|
| 125 |
if ($new_data['panodata']["scene-list"]) { |
| 126 |
|
| 127 |
foreach ($new_data['panodata']["scene-list"] as $key => $panoscenes) { |
| 128 |
|
| 129 |
if ($panoscenes['scene-type'] == 'cubemap') { |
| 130 |
|
| 131 |
// face 0 |
| 132 |
if ($panoscenes["scene-attachment-url-face0"]) { |
| 133 |
$cube_name = '_face0.jpg'; |
| 134 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 135 |
} |
| 136 |
|
| 137 |
// face 1 |
| 138 |
if ($panoscenes["scene-attachment-url-face1"]) { |
| 139 |
$cube_name = '_face1.jpg'; |
| 140 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 141 |
} |
| 142 |
|
| 143 |
// face 2 |
| 144 |
if ($panoscenes["scene-attachment-url-face2"]) { |
| 145 |
$cube_name = '_face2.jpg'; |
| 146 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 147 |
} |
| 148 |
|
| 149 |
// face 3 |
| 150 |
if ($panoscenes["scene-attachment-url-face0"]) { |
| 151 |
$cube_name = '_face3.jpg'; |
| 152 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 153 |
} |
| 154 |
|
| 155 |
// face 4 |
| 156 |
if ($panoscenes["scene-attachment-url-face4"]) { |
| 157 |
$cube_name = '_face4.jpg'; |
| 158 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 159 |
} |
| 160 |
|
| 161 |
// face 5 |
| 162 |
if ($panoscenes["scene-attachment-url-face5"]) { |
| 163 |
$cube_name = '_face5.jpg'; |
| 164 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 165 |
} |
| 166 |
} else { |
| 167 |
if ($panoscenes["scene-attachment-url"]) { |
| 168 |
$cube_name = '.jpg'; |
| 169 |
$new_data['panodata']["scene-list"][$key]['scene-attachment-url'] = WPVR_Sample_Tour_Import::prepare_scene_attachment_url_to_import($panoscenes, $cube_name, $file_save_url, $new_post_id, $key); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
update_post_meta($new_post_id, 'panodata', $new_data); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|