| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets up translations for Charitable. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_i18n |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.1.2 |
| 10 |
* @version 1.6.35 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_i18n' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_i18n |
| 22 |
* |
| 23 |
* @since 1.1.2 |
| 24 |
*/ |
| 25 |
class Charitable_i18n { |
| 26 |
|
| 27 |
/** |
| 28 |
* The single instance of this class. |
| 29 |
* |
| 30 |
* @since 1.1.2 |
| 31 |
* |
| 32 |
* @var Charitable_i18n|null |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Plugin's textdomain. |
| 38 |
* |
| 39 |
* @since 1.1.2 |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $textdomain = 'charitable'; |
| 44 |
|
| 45 |
/** |
| 46 |
* The path to the languages directory. |
| 47 |
* |
| 48 |
* @since 1.1.2 |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
protected $languages_directory; |
| 53 |
|
| 54 |
/** |
| 55 |
* The site locale. |
| 56 |
* |
| 57 |
* @since 1.1.2 |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
protected $locale; |
| 62 |
|
| 63 |
/** |
| 64 |
* The MO filename. |
| 65 |
* |
| 66 |
* @since 1.1.2 |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
protected $mofile; |
| 71 |
|
| 72 |
/** |
| 73 |
* Whether decline months names is on. |
| 74 |
* |
| 75 |
* @since 1.6.35 |
| 76 |
* |
| 77 |
* @var boolean |
| 78 |
*/ |
| 79 |
private $decline_months; |
| 80 |
|
| 81 |
/** |
| 82 |
* Set up the class. |
| 83 |
* |
| 84 |
* @since 1.1.2 |
| 85 |
*/ |
| 86 |
private function __construct() { |
| 87 |
/** |
| 88 |
* Customize the directory to use for translation files. |
| 89 |
* |
| 90 |
* @since 1.0.0 |
| 91 |
* |
| 92 |
* @param string $directory The directory, relative to the WP_PLUGIN_DIR directory. |
| 93 |
*/ |
| 94 |
$this->languages_directory = apply_filters( 'charitable_languages_directory', 'charitable/i18n/languages' ); |
| 95 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- plugin_locale is a WordPress core hook used for plugin localization. |
| 96 |
$this->locale = apply_filters( 'plugin_locale', get_locale(), $this->textdomain ); |
| 97 |
$this->mofile = sprintf( '%1$s-%2$s.mo', $this->textdomain, $this->locale ); |
| 98 |
|
| 99 |
add_action( 'change_locale', array( $this, 'load_textdomain' ) ); |
| 100 |
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ), 20 ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns and/or create the single instance of this class. |
| 105 |
* |
| 106 |
* @since 1.2.0 |
| 107 |
* |
| 108 |
* @return Charitable_i18n |
| 109 |
*/ |
| 110 |
public static function get_instance() { |
| 111 |
if ( is_null( self::$instance ) ) { |
| 112 |
self::$instance = new self(); |
| 113 |
} |
| 114 |
|
| 115 |
return self::$instance; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create class object. |
| 120 |
* |
| 121 |
* @since 1.1.2 |
| 122 |
* @version 1.8.9.1 |
| 123 |
* |
| 124 |
* @return void |
| 125 |
*/ |
| 126 |
public function load_textdomain() { |
| 127 |
foreach ( array( 'global', 'local' ) as $source ) { |
| 128 |
$mofile_path = $this->get_mofile_path( $source ); |
| 129 |
|
| 130 |
if ( ! file_exists( $mofile_path ) ) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
load_textdomain( $this->textdomain, $mofile_path ); |
| 135 |
} |
| 136 |
|
| 137 |
// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound |
| 138 |
load_plugin_textdomain( $this->textdomain, false, $this->languages_directory ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Whether decline months is on or off. |
| 143 |
* |
| 144 |
* @since 1.6.35 |
| 145 |
* |
| 146 |
* @return boolean |
| 147 |
*/ |
| 148 |
public function decline_months() { |
| 149 |
if ( ! isset( $this->decline_months ) ) { |
| 150 |
$this->decline_months = 'on' === _x( 'off', 'decline months names: on or off', 'charitable' ); |
| 151 |
} |
| 152 |
|
| 153 |
return $this->decline_months; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Return the date format to use for datepickers, based |
| 158 |
* on whether decline months is on. |
| 159 |
* |
| 160 |
* @since 1.6.35 |
| 161 |
* |
| 162 |
* @param string $default The format to use when decline months is off. |
| 163 |
* @return string |
| 164 |
*/ |
| 165 |
public function get_datepicker_format( $default = 'F d, Y' ) { // phpcs:ignore |
| 166 |
if ( ! $this->decline_months() ) { |
| 167 |
return $default; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Filter the date format used for datepickers |
| 172 |
* when decline months is on. |
| 173 |
* |
| 174 |
* @since 1.6.35 |
| 175 |
* |
| 176 |
* @param string $format The date format to use. |
| 177 |
* @param string $default The format that would be used if decline months is off. |
| 178 |
*/ |
| 179 |
return apply_filters( 'charitable_datepicker_date_format_decline_months', 'Y-m-d', $default ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Return the Javascript date format to use for datepickers, |
| 184 |
* based on whether decline months is on. |
| 185 |
* |
| 186 |
* @since 1.6.35 |
| 187 |
* |
| 188 |
* @param string $default The format to use when decline months is off. |
| 189 |
* @return string |
| 190 |
*/ |
| 191 |
public function get_js_datepicker_format( $default = 'MM d, yy' ) { // phpcs:ignore |
| 192 |
if ( ! $this->decline_months() ) { |
| 193 |
return $default; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Filter the Javascript date format used for datepickers |
| 198 |
* when decline months is on. |
| 199 |
* |
| 200 |
* @since 1.6.35 |
| 201 |
* |
| 202 |
* @param string $format The date format to use. |
| 203 |
* @param string $default The format that would be used if decline months is off. |
| 204 |
*/ |
| 205 |
return apply_filters( 'charitable_datepicker_date_format_decline_months', 'yy-mm-dd', $default ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get the path to the MO file. |
| 210 |
* |
| 211 |
* @since 1.1.2 |
| 212 |
* |
| 213 |
* @param string $source Either 'local' or 'global'. |
| 214 |
* @return string |
| 215 |
*/ |
| 216 |
private function get_mofile_path( $source = 'local' ) { // phpcs:ignore |
| 217 |
if ( 'global' == $source ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 218 |
return WP_LANG_DIR . '/' . $this->textdomain . '/' . $this->mofile; |
| 219 |
} |
| 220 |
|
| 221 |
return trailingslashit( $this->languages_directory ) . $this->mofile; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
endif; |
| 226 |
|