PluginProbe
Panorama – Turn Photos into Immersive Virtual Tours / trunk
Panorama – Turn Photos into Immersive Virtual Tours vtrunk
1.8.0 1.7.4 1.7.3 1.7.2 1.7.1 1.7.0 1.6.1 trunk 1.0.0 1.0.1 1.0.10 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.3 1.1.4 1.1.5 1.1.6 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 All 38 releases
panorama / inc / Base / registerPostType.php

registerPostType.php in Panorama – Turn Photos into Immersive Virtual Tours trunk, at inc/Base/registerPostType.php

489 lines 20.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace BPPIV\Base;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit;
6 }
7
8 class registerPostType{
9
10 public function register(){
11 add_action( 'init', [$this, 'init']);
12 add_action( 'admin_menu', [$this, 'admin_menu']);
13 add_shortcode('panorama', [$this, 'bppiv_shortcode']);
14 add_shortcode('virtual-tour', [$this, 'bppiv_virtual_tour_shortcode']);
15 add_filter( 'manage_bppiv-image-viewer_posts_columns', [$this, 'bppiv_columns_head_only_panorama'], 10 );
16 add_action( 'manage_bppiv-image-viewer_posts_custom_column', [$this, 'bppiv_columns_content_only_panorama'], 10, 2);
17 add_filter( 'manage_virtual_tour_posts_columns', [$this, 'bppiv_columns_head_only_virtual_tour'], 10 );
18 add_action( 'manage_virtual_tour_posts_custom_column', [$this, 'bppiv_columns_content_only_virtual_tour'], 10, 2);
19 add_action( 'edit_form_after_title', [$this, 'bppiv_shortcode_area'] );
20 add_filter( 'admin_footer_text', [$this, 'bppiv_admin_footer'] );
21 add_filter( 'gettext', [$this, 'bppiv_change_publish_button'], 10, 2 );
22 add_filter( 'post_updated_messages', [$this, 'bppiv_updated_messages'] );
23 add_action( 'admin_head-post.php', [$this, 'bppiv_hide_publishing_actions'] );
24 add_action( 'admin_head-post-new.php', [$this, 'bppiv_hide_publishing_actions'] );
25 if ( is_admin() ) {
26 add_filter('post_row_actions', [$this, 'bppiv_remove_row_actions'], 10, 2 );
27 }
28
29 add_filter('template_include', [$this, 'bppiv_load_custom_template']);
30
31 }
32
33 public function bppiv_load_custom_template($template) {
34
35 if (is_singular(['bppiv-image-viewer', 'virtual_tour', 'product_spot'])) {
36
37 $plugin_template = BPPIV_PATH . 'inc/templates/single-bppiv-image-viewer.php';
38
39 if (file_exists($plugin_template)) {
40 return $plugin_template;
41 }
42 }
43
44 return $template;
45 }
46
47 public function init(){
48 $labels = array(
49 'name' => __( 'Panorama Viewer', 'panorama' ),
50 'menu_name' => __( 'Panorama Viewer', 'panorama' ),
51 'name_admin_bar' => __( 'Panorama Viewer', 'panorama' ),
52 'add_new' => __( 'Add New', 'panorama' ),
53 'add_new_item' => __( 'Add New', 'panorama' ),
54 'new_item' => __( 'New Panorama ', 'panorama' ),
55 'edit_item' => __( 'Edit Panorama ', 'panorama' ),
56 'view_item' => __( 'View Panorama ', 'panorama' ),
57 'all_items' => __( 'All Panoramas', 'panorama' ),
58 'not_found' => __( 'Sorry, we couldn\'t find the Panorama you are looking for.', 'panorama' ),
59 );
60 $args = array(
61 'labels' => $labels,
62 'description' => __( 'Panorama Options.', 'panorama' ),
63 'public' => true,
64 'show_ui' => true,
65 'show_in_menu' => true,
66 'menu_icon' => 'dashicons-welcome-view-site',
67 'query_var' => true,
68 'rewrite' => array(
69 'slug' => 'panorama',
70 ),
71 'capability_type' => 'post',
72 'has_archive' => false,
73 'hierarchical' => false,
74 'menu_position' => 20,
75 'supports' => array( 'title' ),
76 );
77 \register_post_type( 'bppiv-image-viewer', $args );
78
79
80 register_post_type('virtual_tour', [
81 'labels' => [
82 'name' => '360° Virtual Tour',
83 'singular_name' => 'Virtual Tour',
84 'add_new' => 'Add New',
85 'add_new_item' => 'Add New Tour',
86 'edit_item' => 'Edit Tour',
87 'not_found' => 'There was no tour please add one',
88 'search_items' => 'Search Tour',
89 'view_item' => 'View Tour',
90 'not_found_in_trash' => 'No Tour found in trash',
91 'item_updated' => 'Tour updated',
92 ],
93 'public' => true,
94 'publicly_queryable' => false,
95 'exclude_from_search' => true,
96 'has_archive' => false,
97 "show_in_rest" => true,
98 "template_lock" => "all",
99 "template" => [["panorama/virtual-tour"]],
100 'show_in_menu' => 'edit.php?post_type=bppiv-image-viewer',
101 ]);
102
103 register_post_type('product_spot', [
104 'labels' => [
105 'name' => 'Product Spot',
106 'singular_name' => 'Product Spot',
107 'add_new' => 'Add New',
108 'add_new_item' => 'Add New Product Spot',
109 'edit_item' => 'Edit Product Spot',
110 'not_found' => 'There was no product spot please add one',
111 'search_items' => 'Search Product Spot',
112 'view_item' => 'View Product Spot',
113 'not_found_in_trash' => 'No Product Spot found in trash',
114 'item_updated' => 'Product Spot updated',
115 ],
116 'public' => true,
117 'publicly_queryable' => false,
118 'exclude_from_search' => true,
119 'has_archive' => false,
120 'show_in_rest' => true,
121 'menu_icon' => 'dashicons-products',
122 'template' => [['psb/product-spot']],
123 'template_lock' => 'all',
124 'show_in_menu' => 'edit.php?post_type=bppiv-image-viewer',
125 ]);
126 }
127
128 function admin_menu(){
129 remove_submenu_page(
130 'edit.php?post_type=bppiv-image-viewer',
131 'post-new.php?post_type=bppiv-image-viewer'
132 );
133 }
134
135 function bppiv_virtual_tour_shortcode($atts){
136 $post_id = $atts['id'];
137 $post = get_post( $post_id );
138
139 if ( !$post ) {
140 return '';
141 }
142
143 if ( post_password_required( $post ) ) {
144 return get_the_password_form( $post );
145 }
146
147 switch ( $post->post_status ) {
148 case 'publish':
149 return $this->displayContent( $post );
150
151 case 'private':
152 if (current_user_can('read_private_posts')) {
153 return $this->displayContent( $post );
154 }
155 return '';
156
157 case 'draft':
158 case 'pending':
159 case 'future':
160 if ( current_user_can( 'edit_post', $post_id ) ) {
161 return $this->displayContent( $post );
162 }
163 return '';
164
165 default:
166 return '';
167 }
168 }
169
170 function bppiv_shortcode($atts){
171 $post_id = $atts['id'];
172 $post = get_post( $post_id );
173
174 if ( !$post ) {
175 return '';
176 }
177
178 if ( post_password_required( $post ) ) {
179 return get_the_password_form( $post );
180 }
181
182 switch ( $post->post_status ) {
183 case 'publish':
184 return $this->displayContent( $post );
185
186 case 'private':
187 if (current_user_can('read_private_posts')) {
188 return $this->displayContent( $post );
189 }
190 return '';
191
192 case 'draft':
193 case 'pending':
194 case 'future':
195 if ( current_user_can( 'edit_post', $post_id ) ) {
196 return $this->displayContent( $post );
197 }
198 return '';
199
200 default:
201 return '';
202 }
203 }
204
205 function displayContent( $post ){
206 $blocks = parse_blocks( $post->post_content );
207 if (empty($blocks)) {
208 return '';
209 }
210 return render_block( $blocks[0] );
211 }
212
213 function bppiv_columns_head_only_panorama( $defaults ){
214 unset($defaults['date']);
215 $defaults['directors_name'] = 'ShortCode';
216 $defaults['date'] = 'Date';
217 return $defaults;
218 }
219
220 function bppiv_columns_content_only_panorama( $column_name, $post_ID ){
221 if ($column_name == 'directors_name') {
222 echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr($post_ID) . '">
223 <input value="[panorama id=' . esc_attr($post_ID) . ']" onclick="copyBPlAdminShortcode(\'' . esc_attr($post_ID) . '\')" readonly>
224 <span class="tooltip">Copy To Clipboard</span>
225 </div>';
226 }
227 }
228
229 function bppiv_columns_head_only_virtual_tour( $defaults ){
230 unset($defaults['date']);
231 $defaults['shortcode'] = 'ShortCode';
232 $defaults['date'] = 'Date';
233 return $defaults;
234 }
235
236 function bppiv_columns_content_only_virtual_tour( $column_name, $post_ID ){
237 if ($column_name == 'shortcode') {
238 echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr($post_ID) . '">
239 <input value="[virtual-tour id=' . esc_attr($post_ID) . ']" onclick="copyBPlAdminShortcode(\'' . esc_attr($post_ID) . '\')" readonly>
240 <span class="tooltip">Copy To Clipboard</span>
241 </div>';
242 }
243 }
244
245 function bppiv_shortcode_area() {
246 global $post;
247
248 if ( ! $post || $post->post_type !== 'bppiv-image-viewer' ) {
249 return;
250 }
251
252
253 define('BPPIV_META_FIELDS', [
254 'id' => '_bppivimages_',
255 'title' => 'Fieds',
256 'sections' => [
257 [
258 'name' => 'first',
259 'title' => 'First Section',
260 'fields' => [
261 [
262 'id' => 'bppiv_type',
263 'title' => 'Text',
264 'field' => 'text',
265 'attributes' => [
266 'style' => ['width' => '50%']
267 ]
268 ],
269 ]
270 ]
271 ]
272 ]);
273
274 // wp_enqueue_style('bppiv-meta');
275 // wp_enqueue_script('bppiv-meta');
276
277 ?>
278
279 <div class="panorama_shortcode">
280 <p class="shortcode_desc">
281 <?php echo esc_html__( 'Use this shortcode in your post or page, or click Embed to share it on other websites:', 'panorama' ); ?>
282 </p>
283 <code
284 class="shortcode_copy"
285 data-code="[panorama id='<?php echo esc_attr($post->ID); ?>']">
286 [panorama id='<?php echo esc_attr($post->ID); ?>']
287 </code>
288
289 <button type="button" class="bppiv-embed-btn">
290 <span class="dashicons dashicons-share"></span> Embed
291 </button>
292 </div>
293
294 <div id="bppiv-embed-modal" class="bppiv-modal">
295 <div class="bppiv-modal-content">
296
297 <span class="bppiv-close">&times;</span>
298
299 <h3>Embed Panorama</h3>
300 <?php
301 $meta = get_post_meta($post->ID, '_bppivimages_', true);
302 $height = 600;
303 $type = '';
304 if (is_array($meta)) {
305 if (isset($meta['bppiv_type'])) {
306 $type = $meta['bppiv_type'];
307 }
308 }
309 if ($type !== 'video2') {
310 if (is_array($meta) && isset($meta['bppiv_image_height']['height']) && is_numeric($meta['bppiv_image_height']['height'])) {
311 $height = (int) $meta['bppiv_image_height']['height'];
312 }
313 }
314 if ($type === 'video2') {
315 $iframe_code = sprintf(
316 '<iframe id="panorama_%s" src="%s" width="100%%" loading="lazy" frameborder="0" allow="accelerometer; gyroscope; magnetometer; fullscreen; xr-spatial-tracking" allowfullscreen></iframe>',
317 esc_attr($post->ID),
318 esc_url(get_permalink($post->ID))
319 );
320 } else {
321 $iframe_code = sprintf(
322 '<iframe id="panorama_%s" src="%s" width="100%%" height="%d" loading="lazy" frameborder="0" allow="accelerometer; gyroscope; magnetometer; fullscreen; xr-spatial-tracking" allowfullscreen></iframe>',
323 esc_attr($post->ID),
324 esc_url(get_permalink($post->ID)),
325 $height
326 );
327 }
328 $embed_code = $iframe_code;
329 if ($type === 'video2') {
330 $script_url = BPPIV_PLUGIN_DIR . 'public/assets/js/iframe-resizer.js';
331 // $embed_code = sprintf(
332 // '<div>%s%s</div>',
333 // $iframe_code,
334 // "\n" . sprintf('<script src="%s"></script>', esc_url($script_url))
335 // );
336 // without div
337 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
338 $embed_code = $iframe_code . "\n" . sprintf('<script src="%s"></script>', esc_url($script_url));
339 }
340 // else {
341 // $embed_code = sprintf('<div>%s</div>', $iframe_code);
342 // }
343 ?>
344 <textarea style="height: <?php echo ($type === 'video2') ? '146px' : '66px'; ?>;" id="bppiv-iframe-code" readonly><?php echo esc_html($embed_code); ?></textarea>
345 <?php if ($type === 'video2') : ?>
346 <p class="bppiv-embed-note">
347 <strong>Note:</strong> For Video 360°, the iframe and height-resizer script should work normally.
348 If the video appears misaligned on your page, wrap both the iframe and the
349 height-resizer script inside a <code>&lt;div&gt;</code> container to ensure proper
350 alignment and automatic height adjustment.
351 </p>
352 <?php else : ?>
353 <p class="bppiv-embed-note">
354 <strong>Note:</strong> If the panorama appears misaligned on your page, wrap the iframe inside a
355 <code>&lt;div&gt;</code> container. This usually fixes alignment issues in some editors.
356 </p>
357 <?php endif; ?>
358 <p class="bppiv-embed-note">
359 <strong>Note:</strong> If you copy the embed code and the panorama does not display correctly,
360 please go to your WordPress admin dashboard Settings Permalinks and click
361 <em>Save Changes</em>. This refreshes the URLs and ensures the embed works properly.
362 </p>
363 <button type="button" id="bppiv-copy-iframe" class="bppiv-copy-embed-btn">Copy Embed Code</button>
364
365 </div>
366 </div>
367
368 <script>
369 document.addEventListener("DOMContentLoaded", function(){
370 const modal = document.getElementById("bppiv-embed-modal");
371 const btn = document.querySelector(".bppiv-embed-btn");
372 const close = document.querySelector(".bppiv-close");
373 const copyBtn = document.getElementById("bppiv-copy-iframe");
374 const textarea = document.getElementById("bppiv-iframe-code");
375
376 if(btn){
377 btn.onclick = function(){
378 modal.style.display = "block";
379 }
380 }
381
382 if(close){
383 close.onclick = function(){
384 modal.style.display = "none";
385 }
386 }
387
388 window.onclick = function(e){
389 if(e.target == modal){
390 modal.style.display = "none";
391 }
392 }
393
394 if(copyBtn){
395 copyBtn.onclick = function(){
396 textarea.select();
397 textarea.setSelectionRange(0, 99999);
398
399 if (navigator.clipboard && window.isSecureContext) {
400 navigator.clipboard.writeText(textarea.value).then(function(){
401 copyBtn.innerText = "Copied!";
402 setTimeout(function(){
403 copyBtn.innerText = "Copy Embed Code";
404 }, 1500);
405 });
406 } else {
407 try {
408 var successful = document.execCommand('copy');
409 if (successful) {
410 copyBtn.innerText = "Copied!";
411 setTimeout(function(){
412 copyBtn.innerText = "Copy Embed Code";
413 }, 1500);
414 }
415 } catch (err) {
416 console.error('Fallback: Oops, unable to copy', err);
417 }
418 }
419 }
420 }
421
422 });
423 </script>
424 <?php
425 }
426
427
428 function bppiv_admin_footer( $text ) {
429
430 if ( 'bppiv-image-viewer' == get_post_type() ) {
431 $url = 'https://wordpress.org/support/plugin/panorama/reviews/#new-post';
432 /* translators: %s: Rating URL. */
433 $text = sprintf( __( 'If you like <strong> Panorama Viewer </strong> please leave us a <a href="%s" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a> rating. Your Review is very important to us as it helps us to grow more. ', 'panorama' ), $url );
434 }
435
436 return $text;
437 }
438
439 /*-------------------------------------------------------------------------------*/
440 /* Change publish button to save.
441 /*-------------------------------------------------------------------------------*/
442 function bppiv_change_publish_button( $translation, $text ) {
443 if ( 'bppiv-image-viewer' == get_post_type() ) {
444 if ( $text == 'Publish' ) {
445 return 'Save';
446 }
447 }
448 return $translation;
449 }
450
451 /*-------------------------------------------------------------------------------*/
452 // Remove post update massage and link
453 /*-------------------------------------------------------------------------------*/
454 function bppiv_updated_messages( $messages ){
455 $messages['bppiv-image-viewer'][1] = __( 'Shortcode updated ', 'panorama' );
456 return $messages;
457 }
458
459 // HIDE everything in PUBLISH metabox except Move to Trash & PUBLISH button
460 function bppiv_hide_publishing_actions() {
461 $my_post_type = 'bppiv-image-viewer';
462 global $post;
463 if ( ! $post || $post->post_type !== $my_post_type ) {
464 return;
465 }
466 echo '
467 <style type="text/css">
468 #misc-publishing-actions,
469 #minor-publishing-actions{
470 display:none;
471 }
472 </style>
473 ';
474 }
475
476 // Hide & Disabled View, Quick Edit and Preview Button
477 function bppiv_remove_row_actions( $actions ) {
478 global $post;
479
480 if ( $post && in_array( $post->post_type, ['bppiv-image-viewer', 'virtual_tour', 'product_spot'], true ) ) {
481 unset( $actions['view'] );
482 unset( $actions['inline hide-if-no-js'] );
483 }
484
485 return $actions;
486 }
487
488 }
489