forked from dereckson/wp-cli-polylang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolylangHelperFunctions.php
More file actions
129 lines (114 loc) · 3.63 KB
/
PolylangHelperFunctions.php
File metadata and controls
129 lines (114 loc) · 3.63 KB
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
<?php
/**
* Polylang community package for WP-CLI
*
* Helper global functions
*
* This is a temporary file, pending integration to Polylang API (api.php).
* As this API uses global functions starting with pll_, we follow the convention.
*
* @package WP-CLI
* @subpackage Polylang
* @author Sébastien Santoro aka Dereckson <dereckson@espace-win.org>
* @license http://www.opensource.org/licenses/bsd-license.php BSD
* @filesource
*/
/**
* gets the list of installed languages as PLL_Language objects, as procided by the polylang's model interface function 'get_languages_list()'
* @return boolean|array
*/
function pll_installed_language_list() {
global $polylang;
return isset($polylang) ? $polylang->model->get_languages_list() : false;
}
/**
* Gets default language information
*
* @param string $language_code ISO 639 or locale code
* @return array|null the default information for the the specified language, or null if it doesn't exist
*/
function pll_get_default_language_information($language_code) {
global $polylang;
require(PLL_SETTINGS_INC.'/languages.php');
foreach ($languages as $language) {
if ($language[0] == $language_code || $language[1] == $language_code) {
$rtl = (count($language) > 3) && ($language[3] == 'rtl');
return array(
'code' => $language[0],
'locale' => $language[1],
'name' => $language[2],
'rtl' => $rtl
);
}
}
return null;
}
/**
* Determines if the specified language code is a valid one.
*
* @param string $language_code ISO 639 or locale code
* @return bool true if the language code is valid; otherwise, false.
*/
function pll_is_valid_language_code($language_code) {
return pll_get_default_language_information($language_code) !== null;
}
/**
* Adds a language with the default locale, name and direction.
*
* @param string $language_code ISO 639 or locale code
* @param int $language_order language order [optional]
* @param int &$error_code the error code, or 0 if the operation is succesful
* @return bool true if the language has been added; false if an error has occured
*/
function pll_add_language($language_code, $language_order = 0) {
global $polylang;
$info = pll_get_default_language_information($language_code);
$args = array(
name => $info['name'],
slug => $info['code'],
locale => $info['locale'],
rtl => $info['rtl'] ? 1 : 0,
term_group => $language_order
);
return $polylang->model->add_language($args);
}
/**
* Delete a language with the default locale.
*
* @param string $language_code ISO 639 or locale code
* @return bool true if the language has been deleted; false if an error has occured
*/
function pll_del_language($language_code) {
global $polylang;
$languages = pll_installed_language_list();
// are any languages available
if( !$languages) {
return false;
}
foreach ($languages as $language) {
if ($language->slug == $language_code || $language->locale == $language_code) {
$polylang->model->delete_language((int) $language->term_id);
return true;
}
}
return false;
}
/**
* Determines whether a language is currently installed.
*
* @param string $language_code The language slug
* @return bool true if the language is installed; otherwise, false.
*/
function pll_is_language_installed($language_code) {
$languages = pll_installed_language_list();
// are any languages available
if( !$languages) {
return false;
}
foreach ($languages as $language) {
if ($language->slug == $language_code || $language->locale == $language_code) {
return true;
}
}
return false;
}