PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.10
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.10
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / ThirdParty / WPML.php

WPML.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.10, at includes/ThirdParty/WPML.php

294 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace NotificationX\ThirdParty;
4
5 use NotificationX\Admin\Settings;
6 use NotificationX\Core\Helper;
7 use NotificationX\Core\PostType;
8 use NotificationX\Core\REST;
9 use NotificationX\Extensions\ExtensionFactory;
10 use NotificationX\GetInstance;
11 use WP_Error;
12 use WP_REST_Server;
13 use UsabilityDynamics\Settings as UsabilityDynamicsSettings;
14
15 /**
16 * @method static WPML get_instance($args = null)
17 */
18 class WPML {
19 /**
20 * Instance of WPML
21 *
22 * @var WPML
23 */
24 use GetInstance;
25
26 protected $inclued_entry_key = [
27 'name',
28 'first_name',
29 'last_name',
30 // 'link', // link is automatically translated.
31 'title',
32 'city',
33 'state',
34 'country',
35 'city_country',
36 ];
37
38 private $template = [
39 'custom_first_param' => "Custom First Parameter",
40 'second_param' => "Second Param",
41 'custom_third_param' => "Custom Third Param",
42 'custom_fourth_param' => "Custom Fourth Parameter",
43 'custom_fifth_param' => "Custom Fifth Parameter",
44 'custom_sixth_param' => "Custom Sixth Parameter",
45 'map_fourth_param' => "Map Fourth Parameter",
46 'ga_fourth_param' => "Google Analytics Fourth Parameter",
47 'ga_fifth_param' => "Google Analytics Fifth Parameter",
48 'review_fourth_param' => "Review Fourth Parameter",
49 ];
50
51 /**
52 * Constructor.
53 *
54 */
55 public function __construct() {
56 add_action('wpml_st_loaded', [$this, 'st_loaded'], 10);
57 // localize moment even without wpml;
58 // can't load moment locale in admin. it cause problem in date picker.
59 // add_action('notificationx_admin_scripts', [$this, 'localize_moment'], 10);
60 }
61
62 /**
63 * This method is reponsible for Admin Menu of
64 * NotificationX
65 *
66 * @return void
67 */
68 public function st_loaded() {
69
70 add_action('init', [$this, 'init'], 10);
71
72 add_action('nx_saved_post', [$this, 'register_package'], 10, 3);
73 add_action('nx_delete_post', [$this, 'delete_translation'], 10, 2);
74 add_filter('nx_get_post', [$this, 'translate_values'], 10);
75
76 add_filter('nx_rest_data', [$this, 'rest_data']);
77 add_filter('nx_builder_configs', [$this, 'builder_configs']);
78 add_filter('nx_check_location', [$this, 'check_location'], 10, 3);
79 add_action( 'wp_ajax_nx-translate', [$this, 'translate'] );
80
81 }
82
83 public function init(){
84 // load translated version of the settings.
85 Settings::get_instance()->_load();
86
87 }
88
89 public function get_meta($post){
90 $meta = [];
91 $meta['title'] = ['Title', 'LINE'];
92 if($post['source'] == 'press_bar'){
93 if(empty($post['elementor_id'])){
94 $meta['press_content'] = ['Notification Bar Content', 'VISUAL'];
95 $meta['button_text'] = ['Button Text', 'LINE'];
96 $meta['button_url'] = ['Button URL', 'LINE'];
97 $meta['countdown_expired_text'] = ['Countdown Expired Text', 'LINE'];
98 $meta['countdown_text'] = ['Countdown Text', 'LINE'];
99 $meta['coupon_text'] = ['Coupon Text', 'LINE'];
100 }
101 }
102 else{
103 if(!empty($post['link_type'])){
104 $meta['custom_url'] = ['Custom URL', 'LINE'];
105 }
106 if(!empty($post['template_adv'])){
107 $meta['advanced_template'] = ['Advance Template', 'VISUAL'];
108 }
109 if(($post['source'] == 'edd' || $post['source'] == 'woocommerce') && !empty($post['combine_multiorder'])){
110 $meta['combine_multiorder_text'] = ['Combine Multi Order Text', 'LINE'];
111 }
112 if(($post['type'] == 'conversions') && !empty($post['link_button'])){
113 $meta['link_button_text'] = ['Link Button Text', 'LINE'];
114 }
115 }
116 return apply_filters('nx_wpml_translate_field', $meta, $post);
117 }
118
119 public function localize_moment($nx_ids = null, $return_url = false){
120 return null;
121 }
122
123 public function generate_package($post, $nx_id){
124 return array(
125 'kind' => 'NotificationX',
126 'name' => "$nx_id",
127 'title' => isset($post['title']) ? $post['title'] : '', // ($nx_id)
128 'edit_link' => PostType::get_instance()->get_edit_link($nx_id),
129 'view_link' => PostType::get_instance()->get_edit_link($nx_id),
130 );
131 }
132
133 public function register_package($post, $data, $nx_id){
134 $data = array_merge($post, $data);
135 if(empty($data['is_translated'])){
136 return;
137 }
138
139 $package = $this->generate_package($data, $nx_id);
140
141 $settings = new UsabilityDynamicsSettings(['data' => $post]);
142
143
144 foreach ($this->get_meta($data) as $key => $param) {
145 $_key = isset($param[2]) ? $param[2] : $key;
146 if($value = $settings->get($_key)){
147 $title = $param[0];
148 $type = $param[1];
149 do_action('wpml_register_string', $value, $key, $package, $title, $type);
150 }
151 }
152
153 if($post['source'] != 'google'){
154 unset($this->template['ga_fourth_param']);
155 unset($this->template['ga_fifth_param']);
156 }
157 if(strpos($data['themes'], 'maps_theme') === false){
158 unset($this->template['map_fourth_param']);
159 }
160
161 if($post['source'] != 'press_bar' && $post['source'] != 'flashing_tab'){
162 // @todo when theme template have all the params.
163 // $source = $data['source'];
164 // $theme = $data['themes'];
165 // $ext = ExtensionFactory::get_instance()->get($source);
166 // $themes = $ext ? $ext->get_themes() : null;
167 // if(!empty($themes[$theme]['template'])){
168 // }
169
170 foreach ($this->template as $key => $param) {
171 $_key = "notification-template.$key";
172 if($value = $settings->get($_key)){
173 do_action('wpml_register_string', $value, $key, $package, $param, 'LINE');
174 }
175 }
176 }
177
178 }
179
180 public function translate_values($post){
181 if(empty($_GET['frontend']) && !did_action( "nx_inline" )){
182 // checking if request came from frontend.
183 return $post;
184 }
185
186 $settings = new UsabilityDynamicsSettings(['data' => $post]);
187
188 $package = $this->generate_package($post, $post['nx_id']);
189
190 foreach ($this->get_meta($post) as $key => $param) {
191 $_key = isset($param[2]) ? $param[2] : $key;
192 if($value = $settings->get($_key)){
193 $value = apply_filters( 'wpml_translate_string', $value, $key, $package );
194 $settings->set($_key, $value);
195 }
196 }
197
198 if($post['source'] != 'press_bar'){
199 foreach ($this->template as $key => $param) {
200 $_key = "notification-template.$key";
201 if($value = $settings->get($_key)){
202 $value = apply_filters( 'wpml_translate_string', $value, $key, $package );
203 $settings->set($_key, $value);
204 }
205 }
206 }
207 return $settings->get();
208 }
209
210 public function delete_translation($nx_id, $post){
211 $package = $this->generate_package($post, $nx_id);
212 do_action( 'wpml_delete_package', $package['name'], $package['kind'] );
213 }
214
215 /**
216 * We are only translating when user click translate button.
217 *
218 * @param [type] $request
219 * @return void
220 */
221 public function translate($request){
222 if(!empty($_GET['id'])){
223 $nx_id = sanitize_text_field( $_GET['id'] );
224 $post = PostType::get_instance()->get_post($nx_id);
225 if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){
226 $cookie = new \WPML_Cookie();
227 $cookie_data = filter_var( http_build_query( ['type' => 'nx_bar'] ), FILTER_SANITIZE_URL );
228 $cookie->set_cookie( 'wp-translation_dashboard_filter', $cookie_data, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
229
230 wp_redirect(admin_url("admin.php?page=wpml-translation-management/menu/main.php&sm=dashboard"));
231 die;
232 }
233 else if($post){
234 $post['is_translated'] = true;
235 PostType::get_instance()->update_post([
236 'data' => $post,
237 ], $nx_id);
238
239 $this->register_package($post, [], $nx_id);
240 wp_redirect(admin_url("admin.php?page=wpml-string-translation/menu/string-translation.php&context=notificationx-$nx_id"));
241 die;
242 }
243 }
244 return new WP_Error();
245 }
246 public function can_translate( $request ) {
247 return current_user_can('wpml_manage_string_translation');
248 }
249
250 /**
251 * Frontend append lang param.
252 *
253 * @param [type] $rest
254 * @return void
255 */
256 public function rest_data($rest){
257 $my_default_lang = apply_filters('wpml_default_language', NULL );
258 $my_current_lang = apply_filters( 'wpml_current_language', NULL );
259 if($my_default_lang != $my_current_lang){
260 $rest['lang'] = $my_current_lang;
261 }
262 return $rest;
263 }
264
265 /**
266 * Backend check if translation is enabled.
267 *
268 * @param [type] $rest
269 * @return void
270 */
271 public function builder_configs($tabs){
272 $tabs['can_translate'] = true;
273 return $tabs;
274 }
275
276 public function check_location($check_location, $custom_ids, $show_on = []){
277 if(!$check_location && $custom_ids){
278 global $post;
279 if( empty( $custom_ids ) || empty($post) ) {
280 return false;
281 }
282 $custom_ids = explode(',', $custom_ids);
283 $custom_ids = array_map('trim', $custom_ids);
284 if( is_post_type_archive( 'product' ) ) {
285 if( in_array( get_option( 'woocommerce_shop_page_id' ), $custom_ids ) ) {
286 return true;
287 }
288 }
289 return in_array( $post->ID, $custom_ids ) ? true : false;
290 }
291 return $check_location;
292 }
293 }
294