| @@ -1,0 +1,304 @@ | ||
| 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 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 117 | + return apply_filters('nx_wpml_translate_field', $meta, $post); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public function localize_moment($nx_ids = null, $return_url = false){ | |
| 121 | + return null; | |
| 122 | + } | |
| 123 | + | |
| 124 | + public function generate_package($post, $nx_id){ | |
| 125 | + return array( | |
| 126 | + 'kind' => 'NotificationX', | |
| 127 | + 'name' => "$nx_id", | |
| 128 | + 'title' => isset($post['title']) ? $post['title'] : '', // ($nx_id) | |
| 129 | + 'edit_link' => PostType::get_instance()->get_edit_link($nx_id), | |
| 130 | + 'view_link' => PostType::get_instance()->get_edit_link($nx_id), | |
| 131 | + ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + public function register_package($post, $data, $nx_id){ | |
| 135 | + $data = array_merge($post, $data); | |
| 136 | + if(empty($data['is_translated'])){ | |
| 137 | + return; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $package = $this->generate_package($data, $nx_id); | |
| 141 | + | |
| 142 | + $settings = new UsabilityDynamicsSettings(['data' => $post]); | |
| 143 | + | |
| 144 | + | |
| 145 | + foreach ($this->get_meta($data) as $key => $param) { | |
| 146 | + $_key = isset($param[2]) ? $param[2] : $key; | |
| 147 | + if($value = $settings->get($_key)){ | |
| 148 | + $title = $param[0]; | |
| 149 | + $type = $param[1]; | |
| 150 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 151 | + do_action('wpml_register_string', $value, $key, $package, $title, $type); | |
| 152 | + } | |
| 153 | + } | |
| 154 | + | |
| 155 | + if($post['source'] != 'google'){ | |
| 156 | + unset($this->template['ga_fourth_param']); | |
| 157 | + unset($this->template['ga_fifth_param']); | |
| 158 | + } | |
| 159 | + if(strpos($data['themes'], 'maps_theme') === false){ | |
| 160 | + unset($this->template['map_fourth_param']); | |
| 161 | + } | |
| 162 | + | |
| 163 | + if($post['source'] != 'press_bar' && $post['source'] != 'flashing_tab'){ | |
| 164 | + // @todo when theme template have all the params. | |
| 165 | + // $source = $data['source']; | |
| 166 | + // $theme = $data['themes']; | |
| 167 | + // $ext = ExtensionFactory::get_instance()->get($source); | |
| 168 | + // $themes = $ext ? $ext->get_themes() : null; | |
| 169 | + // if(!empty($themes[$theme]['template'])){ | |
| 170 | + // } | |
| 171 | + | |
| 172 | + foreach ($this->template as $key => $param) { | |
| 173 | + $_key = "notification-template.$key"; | |
| 174 | + if($value = $settings->get($_key)){ | |
| 175 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 176 | + do_action('wpml_register_string', $value, $key, $package, $param, 'LINE'); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + } | |
| 182 | + | |
| 183 | + public function translate_values($post){ | |
| 184 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 185 | + if(empty($_GET['frontend']) && !did_action( "nx_inline" )){ | |
| 186 | + // checking if request came from frontend. | |
| 187 | + return $post; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $settings = new UsabilityDynamicsSettings(['data' => $post]); | |
| 191 | + | |
| 192 | + $package = $this->generate_package($post, $post['nx_id']); | |
| 193 | + | |
| 194 | + foreach ($this->get_meta($post) as $key => $param) { | |
| 195 | + $_key = isset($param[2]) ? $param[2] : $key; | |
| 196 | + if($value = $settings->get($_key)){ | |
| 197 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 198 | + $value = apply_filters( 'wpml_translate_string', $value, $key, $package ); | |
| 199 | + $settings->set($_key, $value); | |
| 200 | + } | |
| 201 | + } | |
| 202 | + | |
| 203 | + if($post['source'] != 'press_bar'){ | |
| 204 | + foreach ($this->template as $key => $param) { | |
| 205 | + $_key = "notification-template.$key"; | |
| 206 | + if($value = $settings->get($_key)){ | |
| 207 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 208 | + $value = apply_filters( 'wpml_translate_string', $value, $key, $package ); | |
| 209 | + $settings->set($_key, $value); | |
| 210 | + } | |
| 211 | + } | |
| 212 | + } | |
| 213 | + return $settings->get(); | |
| 214 | + } | |
| 215 | + | |
| 216 | + public function delete_translation($nx_id, $post){ | |
| 217 | + $package = $this->generate_package($post, $nx_id); | |
| 218 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 219 | + do_action( 'wpml_delete_package', $package['name'], $package['kind'] ); | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 223 | + * We are only translating when user click translate button. | |
| 224 | + * | |
| 225 | + * @param [type] $request | |
| 226 | + * @return void | |
| 227 | + */ | |
| 228 | + public function translate($request){ | |
| 229 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 230 | + if(!empty($_GET['id'])){ | |
| 231 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 232 | + $nx_id = sanitize_text_field( wp_unslash( $_GET['id'] ) ); | |
| 233 | + $post = PostType::get_instance()->get_post($nx_id); | |
| 234 | + if($post['source'] == 'press_bar' && !empty($post['elementor_id'])){ | |
| 235 | + $cookie = new \WPML_Cookie(); | |
| 236 | + $cookie_data = filter_var( http_build_query( ['type' => 'nx_bar'] ), FILTER_SANITIZE_URL ); | |
| 237 | + $cookie->set_cookie( 'wp-translation_dashboard_filter', $cookie_data, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); | |
| 238 | + | |
| 239 | + wp_safe_redirect(admin_url("admin.php?page=wpml-translation-management/menu/main.php&sm=dashboard")); | |
| 240 | + die; | |
| 241 | + } | |
| 242 | + else if($post){ | |
| 243 | + $post['is_translated'] = true; | |
| 244 | + PostType::get_instance()->update_post([ | |
| 245 | + 'data' => $post, | |
| 246 | + ], $nx_id); | |
| 247 | + | |
| 248 | + $this->register_package($post, [], $nx_id); | |
| 249 | + wp_safe_redirect(admin_url("admin.php?page=wpml-string-translation/menu/string-translation.php&context=notificationx-$nx_id")); | |
| 250 | + die; | |
| 251 | + } | |
| 252 | + } | |
| 253 | + return new WP_Error(); | |
| 254 | + } | |
| 255 | + public function can_translate( $request ) { | |
| 256 | + return current_user_can('wpml_manage_string_translation'); | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Frontend append lang param. | |
| 261 | + * | |
| 262 | + * @param [type] $rest | |
| 263 | + * @return void | |
| 264 | + */ | |
| 265 | + public function rest_data($rest){ | |
| 266 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 267 | + $my_default_lang = apply_filters('wpml_default_language', NULL ); | |
| 268 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 269 | + $my_current_lang = apply_filters( 'wpml_current_language', NULL ); | |
| 270 | + if($my_default_lang != $my_current_lang){ | |
| 271 | + $rest['lang'] = $my_current_lang; | |
| 272 | + } | |
| 273 | + return $rest; | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * Backend check if translation is enabled. | |
| 278 | + * | |
| 279 | + * @param [type] $rest | |
| 280 | + * @return void | |
| 281 | + */ | |
| 282 | + public function builder_configs($tabs){ | |
| 283 | + $tabs['can_translate'] = true; | |
| 284 | + return $tabs; | |
| 285 | + } | |
| 286 | + | |
| 287 | + public function check_location($check_location, $custom_ids, $show_on = []){ | |
| 288 | + if(!$check_location && $custom_ids){ | |
| 289 | + global $post; | |
| 290 | + if( empty( $custom_ids ) || empty($post) ) { | |
| 291 | + return false; | |
| 292 | + } | |
| 293 | + $custom_ids = explode(',', $custom_ids); | |
| 294 | + $custom_ids = array_map('trim', $custom_ids); | |
| 295 | + if( is_post_type_archive( 'product' ) ) { | |
| 296 | + if( in_array( get_option( 'woocommerce_shop_page_id' ), $custom_ids ) ) { | |
| 297 | + return true; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + return in_array( $post->ID, $custom_ids ) ? true : false; | |
| 301 | + } | |
| 302 | + return $check_location; | |
| 303 | + } | |
| 304 | +} | |