PluginProbe
Leyka / 3.5
Leyka v3.5
3.32.3 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.6.1 2.3.7 2.3.8 2.3.9 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1 3.10 3.11 3.11.1 3.12 3.13 3.14 3.15 3.16 3.17 All 89 releases
leyka / inc / leyka-polylang.php

leyka-polylang.php in Leyka 3.5, at inc/leyka-polylang.php

181 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if( !defined('WPINC') ) die;
2
3 // Polylang plugin compatibility:
4 if(defined('POLYLANG_VERSION')) {
5
6 function leyka_pll_do_localization($slug, PLL_Language $cur_lang){
7
8 load_plugin_textdomain('leyka', false, apply_filters('leyka_l10n_mo_folder', WP_LANG_DIR.'/plugins'));
9
10 // Localize options values:
11 function leyka_localize_option_value($value, $option_name) {
12
13 if(in_array($option_name, array('success_page', 'failure_page', 'terms_of_service_page', 'pd_terms_page'))) {
14
15 // Get ID of a localized page instead of originally set:
16 $localized_page_id = empty($_POST['cur_lang']) ?
17 pll_get_post($value) : pll_get_post($value, $_POST['cur_lang']);
18
19 return $localized_page_id ? $localized_page_id : $value;
20
21 }
22
23 if(in_array(leyka_options()->get_type_of($option_name), array('text', 'textarea', 'html', 'rich_html'))) {
24 $value = pll__($value);
25 }
26
27 return $value;
28
29 }
30 add_filter('leyka_option_value', 'leyka_localize_option_value', 10, 2);
31
32 // Now donations can return their language (a language of their respective campaigns):
33 function leyka_localize_unknown_donation_field($value, $field, Leyka_Donation $donation) {
34
35 if($field == 'lang' || $field == 'campaign_lang') {
36
37 global $polylang;
38 return $polylang->model->get_post_language($donation->campaign_id)->slug;
39 }
40
41 return $value;
42
43 }
44 add_filter('leyka_get_unknown_donation_field', 'leyka_localize_unknown_donation_field', 10, 4);
45
46 // Now campaigns can return their language:
47 add_filter('leyka_get_unknown_campaign_field', 'leyka_localize_unknown_campaign_field', 10, 4);
48 function leyka_localize_unknown_campaign_field($value, $field, Leyka_Campaign $campaign) {
49
50 if($field == 'lang' || $field == 'campaign_lang') {
51
52 global $polylang;
53 return $polylang->model->get_post_language($campaign->id)->slug;
54
55 }
56
57 return $value;
58
59 }
60
61 // To make frontend ajax calls localized:
62 function leyka_localize_hidden_form_fields($fields) {
63
64 if(empty($fields['cur_lang'])) {
65 $fields['cur_lang'] = pll_current_language();
66 }
67
68 if(empty($fields['cur_locale'])) {
69 $fields['cur_locale'] = get_locale();
70 }
71
72 return $fields;
73
74 }
75 add_filter('leyka_hidden_donation_form_fields', 'leyka_localize_hidden_form_fields');
76
77 function leyka_localize_gateway_redirect_page() {
78
79 load_plugin_textdomain('leyka', false, apply_filters('leyka_l10n_mo_folder', WP_LANG_DIR.'/plugins'));
80
81 function leyka_get_current_locale($locale){
82 return $_POST['cur_locale'];
83 }
84 add_filter('locale', 'leyka_get_current_locale');
85
86 }
87 add_action('leyka_init_gateway_redirect_page', 'leyka_localize_gateway_redirect_page');
88
89 function leyka_localize_options() {
90
91 // All localization filters are in places, now create all gateways:
92 do_action('leyka_init_actions');
93
94 // Register user-defined strings:
95 foreach(leyka_options()->get_options_names() as $option) {
96
97 $option_data = leyka_options()->get_info_of($option);
98
99 if($option_data['type'] == 'text') {
100 pll_register_string($option_data['title'], $option_data['value'], 'leyka');
101 } elseif(in_array($option_data['type'], array('textarea', 'html', 'rich_html'))) {
102 pll_register_string($option_data['title'], leyka_options()->opt($option), 'leyka', true);
103 }
104
105 }
106
107 }
108 add_action('init', 'leyka_localize_options', 11);
109
110 }
111 add_action('pll_language_defined', 'leyka_pll_do_localization', 10, 2);
112
113 // Fallback to native WP language if Polylang doesn't have languages set up:
114 function leyka_pll_languages_not_set(){
115
116 global $polylang;
117 if(empty($polylang) || !pll_languages_list()) {
118
119 function leyka_pll_admin_notices_error(){
120 echo '<div class="error">
121 <p>'.sprintf(__("<strong>Leyka warning!</strong> Polylang plugin doesn't have any languages installed. Leyka may work strangely due to that. Please go to the <a href='%s'>languages settings page</a> and add at least one language.", 'leyka'), site_url('/wp-admin/options-general.php?page=mlang')).'</p>
122 </div>';
123 }
124 add_action('admin_notices', 'leyka_pll_admin_notices_error');
125
126 // $locale = get_locale();
127 // $locale = $locale ? $locale : 'ru_RU';
128
129 load_plugin_textdomain('leyka', false, apply_filters('leyka_l10n_mo_folder', WP_LANG_DIR.'/plugins'));
130
131 do_action('leyka_init_actions');
132
133 } else {
134
135 if(is_admin() && !did_action('leyka_init_actions')) {
136
137 do_action('leyka_init_actions');
138
139 if(count(pll_languages_list()) > 1) {
140 foreach(leyka_options()->get_options_names() as $option) { // Register user-defined strings
141
142 $option_data = leyka_options()->get_info_of($option);
143
144 if($option_data['type'] == 'text') {
145 pll_register_string($option_data['title'], $option_data['value'], 'leyka');
146 } elseif(in_array($option_data['type'], array('textarea', 'html', 'rich_html'))) {
147 pll_register_string($option_data['title'], leyka_options()->opt($option), 'leyka', true);
148 }
149
150 }
151 }
152
153 }
154
155 // add_action('leyka_default_success_page_created', function($page_id){
156 // /** @todo Get localized strings from PL and update success page params */
157 // });
158
159 // leyka_donation post type must not be included - there's no need to translate it:
160 $leyka_post_types = array(Leyka_Campaign_Management::$post_type);
161
162 if($leyka_post_types != $polylang->options['post_types']) {
163
164 $polylang->options['post_types'] = $polylang->options['post_types'] + $leyka_post_types;
165 update_option('polylang', $polylang->options);
166
167 }
168
169 }
170
171 }
172 add_action('init', 'leyka_pll_languages_not_set');
173
174 } else {
175
176 function leyka_init_actions(){
177 do_action('leyka_init_actions');
178 }
179 add_action('init', 'leyka_init_actions', 11);
180
181 }