| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
/** |
| 4 |
* WP VR Tour Element for Oxygen Builder |
| 5 |
* |
| 6 |
* Creates a custom element to display virtual reality tours created with WP VR plugin |
| 7 |
* within the Oxygen Builder interface. |
| 8 |
* |
| 9 |
* @package WPVR |
| 10 |
* @version 8.5.21 |
| 11 |
* @subpackage Oxygen Integration |
| 12 |
*/ |
| 13 |
|
| 14 |
class Wpvr_Tour_Element extends WPVR_CUSTOM_OXY_ELEMENT { |
| 15 |
|
| 16 |
/** |
| 17 |
* Returns the name of the element for Oxygen Builder |
| 18 |
* |
| 19 |
* @return string Element name |
| 20 |
*/ |
| 21 |
public function name() { |
| 22 |
return esc_html__('WP VR Tour', 'wpvr'); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Defines the UI controls for this element |
| 27 |
* |
| 28 |
* Sets up all configuration options available to the user |
| 29 |
*/ |
| 30 |
public function controls() { |
| 31 |
// Get all published VR tours |
| 32 |
$posts = get_posts([ |
| 33 |
'post_type' => 'wpvr_item', |
| 34 |
'post_status' => 'publish', |
| 35 |
'orderby' => 'ID', |
| 36 |
'order' => 'DESC', |
| 37 |
'numberposts' => -1, |
| 38 |
]); |
| 39 |
|
| 40 |
// Build the tour selection array |
| 41 |
$tour_options = [0 => __("None", "wpvr")]; |
| 42 |
foreach ($posts as $post) { |
| 43 |
$id = $post->ID; |
| 44 |
$title = $post->post_title ? $post->post_title : __("No title", "wpvr"); |
| 45 |
$tour_options[$id] = "{$id} : {$title}"; |
| 46 |
} |
| 47 |
|
| 48 |
// Tour selection control |
| 49 |
$this->addOptionControl([ |
| 50 |
"type" => "dropdown", |
| 51 |
"name" => __("Select Tour", "wpvr"), |
| 52 |
"slug" => "tour_id", |
| 53 |
"value" => $tour_options |
| 54 |
])->rebuildElementOnChange(); |
| 55 |
|
| 56 |
// Width controls |
| 57 |
$this->addOptionControl([ |
| 58 |
"type" => "textfield", |
| 59 |
"name" => __("Tour Width", "wpvr"), |
| 60 |
"slug" => "tour_width", |
| 61 |
"value" => "600", |
| 62 |
"condition" => "tour_width_fullwidth=off", |
| 63 |
]); |
| 64 |
|
| 65 |
$this->addOptionControl([ |
| 66 |
"type" => "dropdown", |
| 67 |
"name" => __("Tour Width Unit", "wpvr"), |
| 68 |
"slug" => "tour_width_unit", |
| 69 |
"default" => "px", |
| 70 |
"value" => [ |
| 71 |
'px' => __('px', "wpvr"), |
| 72 |
'%' => __('%', "wpvr"), |
| 73 |
'vw' => __('vw', "wpvr"), |
| 74 |
], |
| 75 |
"condition" => "tour_width_fullwidth=off", |
| 76 |
])->rebuildElementOnChange(); |
| 77 |
|
| 78 |
$this->addOptionControl([ |
| 79 |
"type" => "dropdown", |
| 80 |
"name" => __("Tour Width Fullwidth", "wpvr"), |
| 81 |
"slug" => "tour_width_fullwidth", |
| 82 |
"default" => "off", |
| 83 |
"value" => [ |
| 84 |
'off' => __('OFF', "wpvr"), |
| 85 |
'on' => __('ON', "wpvr"), |
| 86 |
] |
| 87 |
])->rebuildElementOnChange(); |
| 88 |
|
| 89 |
// Height controls |
| 90 |
$this->addOptionControl([ |
| 91 |
"type" => "textfield", |
| 92 |
"name" => __("Tour Height", "wpvr"), |
| 93 |
"slug" => "tour_height", |
| 94 |
"value" => "400" |
| 95 |
]); |
| 96 |
|
| 97 |
$this->addOptionControl([ |
| 98 |
"type" => "dropdown", |
| 99 |
"name" => __("Tour Height Unit", "wpvr"), |
| 100 |
"slug" => "tour_height_unit", |
| 101 |
"default" => "px", |
| 102 |
"value" => [ |
| 103 |
'px' => __('px', "wpvr"), |
| 104 |
'vh' => __('vh', "wpvr"), |
| 105 |
] |
| 106 |
])->rebuildElementOnChange(); |
| 107 |
|
| 108 |
// Mobile-specific height controls |
| 109 |
$this->addOptionControl([ |
| 110 |
"type" => "textfield", |
| 111 |
"name" => __("Tour Mobile Height", "wpvr"), |
| 112 |
"slug" => "tour_mobile_height", |
| 113 |
"value" => "300" |
| 114 |
])->setParam('description', __('Height on mobile devices. Leave empty to use the main height.', 'wpvr')); |
| 115 |
|
| 116 |
$this->addOptionControl([ |
| 117 |
"type" => "dropdown", |
| 118 |
"name" => __("Tour Mobile Height Unit", "wpvr"), |
| 119 |
"slug" => "tour_mobile_height_unit", |
| 120 |
"default" => "px", |
| 121 |
"value" => [ |
| 122 |
'px' => __('px', "wpvr"), |
| 123 |
'vh' => __('vh', "wpvr"), |
| 124 |
] |
| 125 |
])->rebuildElementOnChange(); |
| 126 |
|
| 127 |
// Border radius control |
| 128 |
$this->addOptionControl([ |
| 129 |
"type" => "textfield", |
| 130 |
"name" => __("Tour Radius", "wpvr"), |
| 131 |
"slug" => "tour_radius", |
| 132 |
"value" => "0" |
| 133 |
]); |
| 134 |
|
| 135 |
$this->addOptionControl([ |
| 136 |
"type" => "dropdown", |
| 137 |
"name" => __("Tour Radius Unit", "wpvr"), |
| 138 |
"slug" => "tour_radius_unit", |
| 139 |
"default" => "px", |
| 140 |
"value" => [ |
| 141 |
'px' => __('px', "wpvr"), |
| 142 |
] |
| 143 |
])->rebuildElementOnChange(); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Renders the element on the frontend |
| 148 |
* |
| 149 |
* @param array $options Values set in the controls |
| 150 |
* @param array $defaults Default values for all controls |
| 151 |
* @param array $content Shortcode content for nested elements |
| 152 |
*/ |
| 153 |
public function render($options, $defaults, $content) { |
| 154 |
// Get and validate tour ID |
| 155 |
$id = isset($options['tour_id']) ? absint($options['tour_id']) : 0; |
| 156 |
|
| 157 |
if (!$id) { |
| 158 |
echo '<div class="wpvr-no-tour-selected">'; |
| 159 |
echo esc_html__('Please select a WP VR Tour from the dropdown in the sidebar.', 'wpvr'); |
| 160 |
echo '</div>'; |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
// Process dimension settings |
| 165 |
$width = !empty($options['tour_width']) ? esc_attr($options['tour_width'] . $options['tour_width_unit']) : '600px'; |
| 166 |
$height = !empty($options['tour_height']) ? esc_attr($options['tour_height'] . $options['tour_height_unit']) : '400px'; |
| 167 |
$radius = !empty($options['tour_radius']) ? esc_attr($options['tour_radius'] . $options['tour_radius_unit']) : '0px'; |
| 168 |
|
| 169 |
// Handle full width setting |
| 170 |
if (isset($options['tour_width_fullwidth']) && $options['tour_width_fullwidth'] === 'on') { |
| 171 |
$width = 'fullwidth'; |
| 172 |
} |
| 173 |
|
| 174 |
// Process mobile height settings |
| 175 |
$mobile_height = !empty($options['tour_mobile_height']) ? |
| 176 |
esc_attr($options['tour_mobile_height'] . $options['tour_mobile_height_unit']) : |
| 177 |
'300px'; |
| 178 |
|
| 179 |
// Construct and output the shortcode |
| 180 |
$shortcode = sprintf( |
| 181 |
'[wpvr id="%d" width="%s" height="%s" radius="%s" mobile_height="%s"]', |
| 182 |
$id, |
| 183 |
esc_attr($width), |
| 184 |
esc_attr($height), |
| 185 |
esc_attr($radius), |
| 186 |
esc_attr($mobile_height) |
| 187 |
); |
| 188 |
|
| 189 |
echo do_shortcode(shortcode_unautop($shortcode)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Initialize the element |
| 194 |
new Wpvr_Tour_Element(); |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Modify the text of specific translatable strings in WordPress. |
| 199 |
* |
| 200 |
* This function replaces "Apply Params" with "Save Changes" anywhere it appears |
| 201 |
* in WordPress translations. It uses the `gettext` filter to intercept and modify |
| 202 |
* translatable strings dynamically. |
| 203 |
* |
| 204 |
* @param string $translated_text The translated text. |
| 205 |
* @param string $text The original text. |
| 206 |
* @version 8.5.21 |
| 207 |
* @param string $domain The text domain (used to identify different plugins/themes). |
| 208 |
* @return string Modified text if it matches; otherwise, returns the original text. |
| 209 |
*/ |
| 210 |
function modify_wpvr_save_param_text($translated_text, $text, $domain) { |
| 211 |
if ($text === 'Apply Params') { // Check if the text matches the target phrase |
| 212 |
return 'Save Changes'; // Replace it with the desired text |
| 213 |
} |
| 214 |
return $translated_text; // Return unchanged text if no match is found |
| 215 |
} |
| 216 |
|
| 217 |
// Hook the function into the 'gettext' filter to modify translatable strings dynamically |
| 218 |
add_filter('gettext', 'modify_wpvr_save_param_text', 10, 3); |
| 219 |
|
| 220 |
|
| 221 |
|