/
wpcasa-polylang.php
382 lines (299 loc) · 9.85 KB
/
wpcasa-polylang.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/*
Plugin Name: WPCasa Polylang
Plugin URI: https://wpcasa.com/downloads/wpcasa-polylang
Description: Add support for Polylang to manage WPCasa property data in multiple languages.
Version: 1.1.1
Author: WPSight
Author URI: http://wpsight.com
Requires at least: 4.0
Tested up to: 5.9
Text Domain: wpcasa-polylang
Domain Path: /languages
Copyright: 2015 Simon Rimkus
License: GNU General Public License v2.0 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) )
exit;
/**
* WPSight_Polylang class
*/
class WPSight_Polylang {
/**
* Constructor
*/
public function __construct() {
// Define constants
if ( ! defined( 'WPSIGHT_NAME' ) )
define( 'WPSIGHT_NAME', 'WPCasa' );
if ( ! defined( 'WPSIGHT_DOMAIN' ) )
define( 'WPSIGHT_DOMAIN', 'wpcasa' );
define( 'WPSIGHT_POLYLANG_NAME', 'WPCasa Polylang' );
define( 'WPSIGHT_POLYLANG_DOMAIN', 'wpcasa-polylang' );
define( 'WPSIGHT_POLYLANG_VERSION', '1.1.1' );
define( 'WPSIGHT_POLYLANG_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'WPSIGHT_POLYLANG_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
if ( is_admin() ){
include( WPSIGHT_POLYLANG_PLUGIN_DIR . '/includes/admin/class-wpsight-polylang-admin.php' );
$this->admin = new WPSight_Polylang_Admin();
}
// Load text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Register text strings with Polylang
add_action( 'admin_init', array( &$this, 'details_register' ) );
add_action( 'admin_init', array( &$this, 'rental_periods_register' ) );
add_action( 'admin_init', array( &$this, 'listing_labels_register' ) );
// Filter text strings to be translated
add_filter( 'wpsight_details', array( &$this, 'details_filter' ), 25 );
add_filter( 'wpsight_rental_periods', array( &$this, 'rental_periods_filter' ), 25 );
add_filter( 'wpsight_listing_labels', array( &$this, 'listing_labels_filter' ), 25 );
// Convert some permalinks set in options (e.g. search results page)
add_filter( 'wpsight_get_option', array( &$this, 'options_pages' ), 10, 2 );
// Make some dashboard settings
add_filter( 'wpsight_get_dashboard_listings_args', array( &$this, 'dashboard_listings' ) );
add_filter( 'wpsight_dashboard_columns', array( &$this, 'dashboard_columns' ) );
add_action( 'wpsight_dashboard_column_lang', array( &$this, 'dashboard_column_lang' ) );
}
/**
* init()
*
* Initialize the plugin when WPCasa is loaded
*
* @param object $wpsight
* @uses do_action_ref_array()
* @return object
*
* @since 1.0.0
*/
public static function init( $wpsight ) {
if ( ! isset( $wpsight->polylang ) ) {
$wpsight->polylang = new self();
}
do_action_ref_array( 'wpsight_init_polylang', array( $wpsight ) );
return $wpsight->polylang;
}
/**
* load_plugin_textdomain()
*
* Set up localization for this plugin
* loading the text domain.
*
* @uses load_plugin_textdomain()
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wpcasa-polylang', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* details_register()
*
* Register listing details
* for Polylang strings translation.
*
* @uses wpsight_get_detail()
* @uses pll_register_string()
* @return array
*
* @since 1.0.0
*/
public function details_register() {
if( function_exists( 'pll_register_string' ) ) {
$details = wpsight_details();
foreach( $details as $key => $detail )
pll_register_string( 'Listing Details', wpsight_get_detail( $key, 'label' ), WPSIGHT_NAME );
}
}
/**
* details_filter()
*
* Filter listing details
* to be translated by Polylang.
*
* @uses pll__()
* @return array
*
* @since 1.0.0
*/
public function details_filter( $details ) {
if( function_exists( 'pll__' ) ) {
foreach( $details as $k => $v )
$details[ $k ]['label'] = pll__( $details[ $k ]['label'] );
}
return $details;
}
/**
* rental_periods_register()
*
* Register rental periods
* for Polylang strings translation.
*
* @uses wpsight_rental_periods()
* @uses wpsight_get_rental_period()
* @uses pll_register_string()
*
* @since 1.0.0
*/
public function rental_periods_register() {
if( function_exists( 'pll_register_string' ) ) {
$periods = wpsight_rental_periods();
foreach( $periods as $period => $label ) {
$rental_period = wpsight_get_rental_period( $period );
pll_register_string( 'Listing Periods', $rental_period, WPSIGHT_NAME );
}
}
}
/**
* rental_periods_filter()
*
* Filter rental periods
* to be translated by Polylang.
*
* @uses pll__()
*
* @since 1.0.0
*/
public function rental_periods_filter( $periods ) {
if( function_exists( 'pll__' ) ) {
foreach( $periods as $k => $v )
$periods[ $k ] = pll__( $periods[ $k ] );
}
return $periods;
}
/**
* listing_labels_register()
*
* Register listing labels
* for Polylang strings translation.
*
* @uses wpsight_listing_labels()
* @uses wpsight_get_label()
* @uses pll_register_string()
*
* @since 1.0.0
*/
public function listing_labels_register() {
if( ! function_exists( 'wpsight_listing_labels' ) || ! function_exists( 'pll_register_string' ) )
return false;
$labels = wpsight_listing_labels();
foreach( $labels as $key => $label )
pll_register_string( 'Listing Labels', wpsight_get_label( $key, 'label' ), WPSIGHT_NAME . ' Listing Labels' );
return $labels;
}
/**
* listing_labels_filter()
*
* Filter listing labels
* to be translated by Polylang.
*
* @uses pll__()
*
* @since 1.0.0
*/
public function listing_labels_filter( $labels ) {
if( ! function_exists( 'wpsight_listing_labels' ) )
return false;
if( function_exists( 'pll__' ) ) {
foreach( $labels as $k => $v )
$labels[ $k ]['label'] = pll__( $labels[ $k ]['label'] );
}
return $labels;
}
/**
* options_pages()
*
* Filter wpsight_get_option for pages
* saved in options.
*
* @uses pll_current_language()
* @uses pll_default_language()
* @uses pll_get_post()
*
* @since 1.0.0
*/
public function options_pages( $option, $name ) {
if( ! function_exists( 'pll_current_language' ) )
return get_post( $option );
$pages = array(
'listings_page',
'favorites_page',
'dashboard_page',
'dashboard_submit',
'listings_map_page'
);
if( ! in_array( $name, $pages ) )
return $option;
$current = pll_current_language();
$lang = $current ? $current : pll_default_language();
return pll_get_post( $option, $lang );
}
/**
* dashboard_listings()
*
* Make sure dashboard add-on
* shows all languages.
*
* @uses get_terms()
* @uses pll_languages_list()
*
* @since 1.0.0
*/
public function dashboard_listings( $args ) {
$args['tax_query'] = array(
'relation' => 'OR',
array(
'taxonomy' => 'language',
'terms' => get_terms( 'language', array( 'fields' => 'ids' ) ),
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'language',
'field' => 'slug',
'terms' => pll_languages_list()
)
);
return $args;
}
/**
* dashboard_columns()
*
* Make sure dashboard add-on
* shows all languages.
*
* @since 1.0.0
*/
public function dashboard_columns( $columns ) {
$columns['lang'] = __( 'Language', 'wpcasa-polylang' );
return $columns;
}
/**
* dashboard_column_lang()
*
* Display language column
* in dashboard.
*
* @uses wp_list_pluck()
* @uses pll_the_languages()
* @uses pll_get_post_language()
*
* @since 1.0.0
*/
public function dashboard_column_lang( $post ) {
$languages = wp_list_pluck( pll_the_languages( array( 'raw' => 1 ) ), 'flag', 'slug' );
$lang = pll_get_post_language( $post->ID );
echo esc_html( '<div style="text-align:center"><img src="' . $languages[ $lang ] . '" /></div>' ) ;
}
}
/**
* Check if Polylang plugin is active
* and activate our add-on if yes.
*
* @since 1.0.0
*/
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if( is_plugin_active( 'polylang/polylang.php' ) || is_plugin_active( 'polylang-pro/polylang.php' ) ) {
// Initialize plugin on wpsight_init
add_action( 'wpsight_init', array( 'WPSight_Polylang', 'init' ) );
}