PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/data/plugins/translations/inc/hacks/missing-php-functions.php

https://bitbucket.org/lpservice-it/ljmc
PHP | 305 lines | 205 code | 41 blank | 59 comment | 73 complexity | d7de63dfd3d091b5891e4aa950307de0 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, MIT
  1. <?php
  2. // json_decode
  3. if ( !function_exists('json_decode') ){
  4. include_once ICL_PLUGIN_PATH . '/lib/JSON.php';
  5. function json_decode($data, $bool) {
  6. if ($bool) {
  7. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  8. } else {
  9. $json = new Services_JSON();
  10. }
  11. return( $json->decode($data) );
  12. }
  13. }
  14. if(!function_exists('_cleanup_header_comment')){
  15. function _cleanup_header_comment($str) {
  16. return trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $str));
  17. }
  18. }
  19. /* remove this when we stop supporting LJMC versions below 3.0 */
  20. if(!function_exists('ljmc_get_mu_plugins')){
  21. function ljmc_get_mu_plugins() {
  22. $mu_plugins = array();
  23. if ( !is_dir( LJMCMU_PLUGIN_DIR ) )
  24. return $mu_plugins;
  25. if ( ! $dh = opendir( LJMCMU_PLUGIN_DIR ) )
  26. return $mu_plugins;
  27. while ( ( $plugin = readdir( $dh ) ) !== false ) {
  28. if ( substr( $plugin, -4 ) == '.php' )
  29. $mu_plugins[] = LJMCMU_PLUGIN_DIR . '/' . $plugin;
  30. }
  31. closedir( $dh );
  32. sort( $mu_plugins );
  33. return $mu_plugins;
  34. }
  35. }
  36. if(!function_exists('money_format')):
  37. /*
  38. That it is an implementation of the function money_format for the
  39. platforms that do not it bear.
  40. The function accepts to same string of format accepts for the
  41. original function of the PHP.
  42. (Sorry. my writing in English is very bad)
  43. The function is tested using PHP 5.1.4 in Windows XP
  44. and Apache WebServer.
  45. */
  46. function money_format($format, $number)
  47. {
  48. $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
  49. '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
  50. if (setlocale(LC_MONETARY, 0) == 'C') {
  51. setlocale(LC_MONETARY, '');
  52. }
  53. $locale = localeconv();
  54. preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
  55. foreach ($matches as $fmatch) {
  56. $value = floatval($number);
  57. $flags = array(
  58. 'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
  59. $match[1] : ' ',
  60. 'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
  61. 'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
  62. $match[0] : '+',
  63. 'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
  64. 'isleft' => preg_match('/\-/', $fmatch[1]) > 0
  65. );
  66. $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
  67. $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
  68. $right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
  69. $conversion = $fmatch[5];
  70. $positive = true;
  71. if ($value < 0) {
  72. $positive = false;
  73. $value *= -1;
  74. }
  75. $letter = $positive ? 'p' : 'n';
  76. $prefix = $suffix = $cprefix = $csuffix = $signal = '';
  77. $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
  78. switch (true) {
  79. case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
  80. $prefix = $signal;
  81. break;
  82. case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
  83. $suffix = $signal;
  84. break;
  85. case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
  86. $cprefix = $signal;
  87. break;
  88. case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
  89. $csuffix = $signal;
  90. break;
  91. case $flags['usesignal'] == '(':
  92. case $locale["{$letter}_sign_posn"] == 0:
  93. $prefix = '(';
  94. $suffix = ')';
  95. break;
  96. }
  97. if (!$flags['nosimbol']) {
  98. $currency = $cprefix .
  99. ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
  100. $csuffix;
  101. } else {
  102. $currency = '';
  103. }
  104. $space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
  105. $value = number_format($value, $right, $locale['mon_decimal_point'],
  106. $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
  107. $value = @explode($locale['mon_decimal_point'], $value);
  108. $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
  109. if ($left > 0 && $left > $n) {
  110. $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
  111. }
  112. $value = implode($locale['mon_decimal_point'], $value);
  113. if ($locale["{$letter}_cs_precedes"]) {
  114. $value = $prefix . $currency . $space . $value . $suffix;
  115. } else {
  116. $value = $prefix . $value . $space . $currency . $suffix;
  117. }
  118. if ($width > 0) {
  119. $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
  120. STR_PAD_RIGHT : STR_PAD_LEFT);
  121. }
  122. $format = str_replace($fmatch[0], $value, $format);
  123. }
  124. return $format;
  125. }
  126. endif;
  127. if(!defined('E_DEPRECATED')){ define('E_DEPRECATED', 8192); }
  128. if(!function_exists('esc_textarea')):
  129. function esc_textarea( $text ) {
  130. $safe_text = esc_html( $text );
  131. return apply_filters( 'esc_textarea', $safe_text, $text );
  132. }
  133. endif;
  134. if ( ! function_exists( 'ljmcml_is_ajax' ) ) {
  135. /**
  136. * ljmcml_is_ajax - Returns true when the page is loaded via ajax.
  137. *
  138. * @since 3.1.5
  139. *
  140. * @return bool
  141. */
  142. function ljmcml_is_ajax() {
  143. if ( defined( 'DOING_AJAX' ) ) {
  144. return true;
  145. }
  146. return ( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && mb_strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) == 'xmlhttprequest' ) ? true : false;
  147. }
  148. }
  149. if ( ! function_exists( 'is_ajax' ) ) {
  150. /**
  151. * is_ajax - Returns true when the page is loaded via ajax.
  152. *
  153. * @deprecated Deprecated since 3.1.5
  154. *
  155. * @return bool
  156. */
  157. function is_ajax() {
  158. // Deprecation notice will be added in a next release
  159. // _deprecated_function( "Languages " . __FUNCTION__, "3.1.5", "ljmcml_is_ajax" );
  160. if ( defined( 'DOING_AJAX' ) ) {
  161. return true;
  162. }
  163. return ( isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && mb_strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) == 'xmlhttprequest' ) ? true : false;
  164. }
  165. }
  166. /**
  167. * This file is part of the array_column library
  168. *
  169. * For the full copyright and license information, please view the LICENSE
  170. * file that was distributed with this source code.
  171. *
  172. * @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
  173. * @license http://opensource.org/licenses/MIT MIT
  174. */
  175. if (!function_exists('array_column')) {
  176. /**
  177. * Returns the values from a single column of the input array, identified by
  178. * the $columnKey.
  179. *
  180. * Optionally, you may provide an $indexKey to index the values in the returned
  181. * array by the values from the $indexKey column in the input array.
  182. *
  183. * @param array $input A multi-dimensional array (record set) from which to pull
  184. * a column of values.
  185. * @param mixed $columnKey The column of values to return. This value may be the
  186. * integer key of the column you wish to retrieve, or it
  187. * may be the string key name for an associative array.
  188. * @param mixed $indexKey (Optional.) The column to use as the index/keys for
  189. * the returned array. This value may be the integer key
  190. * of the column, or it may be the string key name.
  191. * @return array
  192. */
  193. function array_column($input = null, $columnKey = null, $indexKey = null)
  194. {
  195. // Using func_get_args() in order to check for proper number of
  196. // parameters and trigger errors exactly as the built-in array_column()
  197. // does in PHP 5.5.
  198. $argc = func_num_args();
  199. $params = func_get_args();
  200. if ($argc < 2) {
  201. trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
  202. return null;
  203. }
  204. if (!is_array($params[0])) {
  205. trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
  206. return null;
  207. }
  208. if (!is_int($params[1])
  209. && !is_float($params[1])
  210. && !is_string($params[1])
  211. && $params[1] !== null
  212. && !(is_object($params[1]) && method_exists($params[1], '__toString'))
  213. ) {
  214. trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
  215. return false;
  216. }
  217. if (isset($params[2])
  218. && !is_int($params[2])
  219. && !is_float($params[2])
  220. && !is_string($params[2])
  221. && !(is_object($params[2]) && method_exists($params[2], '__toString'))
  222. ) {
  223. trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
  224. return false;
  225. }
  226. $paramsInput = $params[0];
  227. $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
  228. $paramsIndexKey = null;
  229. if (isset($params[2])) {
  230. if (is_float($params[2]) || is_int($params[2])) {
  231. $paramsIndexKey = (int) $params[2];
  232. } else {
  233. $paramsIndexKey = (string) $params[2];
  234. }
  235. }
  236. $resultArray = array();
  237. foreach ($paramsInput as $row) {
  238. $key = $value = null;
  239. $keySet = $valueSet = false;
  240. if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
  241. $keySet = true;
  242. $key = (string) $row[$paramsIndexKey];
  243. }
  244. if ($paramsColumnKey === null) {
  245. $valueSet = true;
  246. $value = $row;
  247. } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
  248. $valueSet = true;
  249. $value = $row[$paramsColumnKey];
  250. }
  251. if ($valueSet) {
  252. if ($keySet) {
  253. $resultArray[$key] = $value;
  254. } else {
  255. $resultArray[] = $value;
  256. }
  257. }
  258. }
  259. return $resultArray;
  260. }
  261. }