Completed
Pull Request — master (#1082)
by Devin
19:08
created

Give_i18n_Banner::translation_details()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 306.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Class Give_i18n
5
 */
6
class Give_i18n_Banner {
7
8
	/**
9
	 * Your translation site's URL.
10
	 *
11
	 * @var string
12
	 */
13
	private $glotpress_url;
14
15
	/**
16
	 * Hook where you want to show the promo box.
17
	 *
18
	 * @var string
19
	 */
20
	private $hook;
21
22
	/**
23
	 * Will contain the site's locale.
24
	 *
25
	 * @access private
26
	 * @var string
27
	 */
28
	private $locale;
29
30
	/**
31
	 * Will contain the locale's name, obtained from your translation site.
32
	 *
33
	 * @access private
34
	 * @var string
35
	 */
36
	private $locale_name;
37
38
	/**
39
	 * Will contain the percentage translated for the plugin translation project in the locale.
40
	 *
41
	 * @access private
42
	 * @var int
43
	 */
44
	private $percent_translated;
45
46
47
	/**
48
	 * Indicates whether there's a translation available at all.
49
	 *
50
	 * @access private
51
	 * @var bool
52
	 */
53
	private $translation_exists;
54
55
	/**
56
	 * Indicates whether the translation's loaded.
57
	 *
58
	 * @access private
59
	 * @var bool
60
	 */
61
	private $translation_loaded;
62
63
	/**
64
	 * Give_i18n constructor.
65
	 *
66
	 * @param $args
67
	 *
68
	 */
69
	public function __construct( $args ) {
70
		if ( ! is_admin() ) {
71
			return;
72
		}
73
74
		//This plugin is en_US native.
75
		$this->locale = get_locale();
76
		if ( 'en_US' === $this->locale ) {
77
			return;
78
		}
79
80
		$this->init( $args );
81
82
		if ( ! $this->hide_promo() ) {
83
			add_action( $this->hook, array( $this, 'promo' ) );
84
		}
85
	}
86
87
	/**
88
	 * This is where you decide where to display the messages and where you set the plugin specific variables.
89
	 *
90
	 * @access private
91
	 *
92
	 * @param array $args
93
	 */
94
	private function init( $args ) {
95
		foreach ( $args as $key => $arg ) {
96
			$this->$key = $arg;
97
		}
98
99
	}
100
101
	/**
102
	 * Check whether the promo should be hidden or not.
103
	 *
104
	 * @access private
105
	 *
106
	 * @return bool
107
	 */
108
	private function hide_promo() {
109
		$hide_promo = get_transient( 'give_i18n_give_promo_hide' );
110
		if ( ! $hide_promo ) {
111
			if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) {
112
				// No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc.
113
				set_transient( 'give_i18n_give_promo_hide', true );
114
				$hide_promo = true;
115
			}
116
		}
117
118
		return $hide_promo;
119
	}
120
121
	/**
122
	 * Generates a promo message.
123
	 *
124
	 * @access private
125
	 *
126
	 * @return bool|string $message
127
	 */
128
	private function promo_message() {
129
		$message = false;
130
131
		if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) {
132
			$message = __( 'As you can see, there is a translation of this plugin in %1$s. This translation is currently %3$d%% complete. We need your help to make it complete and to fix any errors. Please register at %4$s to help complete the translation to %1$s!', 'give' );
133
		} else if ( ! $this->translation_loaded && $this->translation_exists ) {
134
			$message = __( 'You\'re using WordPress in %1$s. While %2$s has been translated to %1$s for %3$d%%, it\'s not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', 'give' );
135
		} else if ( ! $this->translation_exists ) {
136
			$message = __( 'You\'re using WordPress in a language we don\'t support yet. We\'d love for %2$s to be translated in that language too, but unfortunately, it isn\'t right now. You can change that! Register at %4$s to help translate it!', 'give' );
137
		}
138
139
		$registration_link = sprintf( '<a href="%1$s">%2$s</a>', 'https://wordpress.org/support/register.php', esc_html__( 'WordPress.org', 'give' ) );
140
		$message           = sprintf( $message, esc_html( $this->locale_name ), esc_html( 'Give' ), $this->percent_translated, $registration_link );
141
142
		return $message;
143
	}
144
145
	/**
146
	 * Outputs a promo box
147
	 */
148
	public function promo() {
149
150
		$this->translation_details();
151
		$message = $this->promo_message();
152
153
		if ( $message ) { ?>
154
155
			<style>
156
				/* Banner specific styles */
157
				div.give-addon-alert.updated {
158
					padding: 10px 20px;
159
					position: relative;
160
					border-color: #66BB6A;
161
				}
162
163
				div.give-addon-alert a {
164
					color: #66BB6A;
165
				}
166
167
				#give-i18n-notice > .give-i18n-icon {
168
					overflow: hidden;
169
				}
170
171
				#give-i18n-notice > .give-i18n-icon .dashicons {
172
					width: 110px;
173
					height: 110px;
174
				}
175
176
				#give-i18n-notice > .give-i18n-icon:focus {
177
					box-shadow: none;
178
				}
179
180
				.give-i18n-notice-content {
181
					margin: 0 30px 0 125px;
182
				}
183
184
				div.give-addon-alert .dismiss {
185
					position: absolute;
186
					right: 20px;
187
					height: 100%;
188
					top: 50%;
189
					margin-top: -10px;
190
					outline: none;
191
					box-shadow: none;
192
					text-decoration: none;
193
					color: #AAA;
194
				}
195
196
				div.give-addon-alert .dismiss:hover {
197
					color: #333;
198
				}
199
			</style>
200
			<div id="give-i18n-notice" class="give-addon-alert updated" style="">
201
202
				<a href="https://wordpress.org/support/register.php" class="alignleft give-i18n-icon" style="margin:0" target="_blank"><span class="dashicons dashicons-translation" style="font-size: 110px; text-decoration: none;"></span></a>
203
204
				<div class="give-i18n-notice-content">
205
					<a href="<?php echo esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ); ?>" class="dismiss"><span class="dashicons dashicons-dismiss"></span></a>
206
207
					<h2 style="margin: 10px 0;"><?php printf( __( 'Help Translate Give to %s', 'give' ), $this->locale_name ); ?></h2>
208
					<p><?php echo $message; ?></p>
209
					<p>
210
						<a href="https://wordpress.org/support/register.php" target="_blank"><?php _e( 'Register now &raquo;', 'give' ); ?></a>
211
					</p>
212
				</div>
213
			</div>
214
			<?php
215
		}
216
	}
217
218
	/**
219
	 * Try to find the transient for the translation set or retrieve them.
220
	 *
221
	 * @access private
222
	 *
223
	 * @return object|null
224
	 */
225
	private function find_or_initialize_translation_details() {
226
227
		$set = get_transient( 'give_i18n_give_' . $this->locale );
228
229
		if ( ! $set ) {
230
			$set = $this->retrieve_translation_details();
231
			set_transient( 'give_i18n_give_' . $this->locale, $set, DAY_IN_SECONDS );
232
		}
233
234
		return $set;
235
	}
236
237
	/**
238
	 * Try to get translation details from cache, otherwise retrieve them, then parse them.
239
	 *
240
	 * @access private
241
	 */
242
	private function translation_details() {
243
		$set = $this->find_or_initialize_translation_details();
244
245
		$this->translation_exists = ! is_null( $set );
246
		$this->translation_loaded = is_textdomain_loaded( 'give' );
247
248
		$this->parse_translation_set( $set );
249
	}
250
251
	/**
252
	 * Retrieve the translation details from Give Translate.
253
	 *
254
	 * @access private
255
	 *
256
	 * @return object|null
257
	 */
258
	private function retrieve_translation_details() {
259
260
		$api_url = trailingslashit( $this->glotpress_url );
261
262
		$resp = wp_remote_get( $api_url );
263
264
		if ( is_wp_error( $resp ) || wp_remote_retrieve_response_code( $resp ) === '404' ) {
265
			return null;
266
		}
267
268
		$body = wp_remote_retrieve_body( $resp );
269
		unset( $resp );
270
271
		if ( $body ) {
272
			$body = json_decode( $body );
273
274
			foreach ( $body->translation_sets as $set ) {
275
				if ( ! property_exists( $set, 'wp_locale' ) ) {
276
					continue;
277
				}
278
279
				if ( $this->locale == $set->wp_locale ) {
280
					return $set;
281
				}
282
			}
283
		}
284
285
		return null;
286
	}
287
288
	/**
289
	 * Set the needed private variables based on the results from Give Translate.
290
	 *
291
	 * @param object $set The translation set
292
	 *
293
	 * @access private
294
	 */
295
	private function parse_translation_set( $set ) {
296
		if ( $this->translation_exists && is_object( $set ) ) {
297
			$this->locale_name        = $set->name;
298
			$this->percent_translated = $set->percent_translated;
299
		} else {
300
			$this->locale_name        = '';
301
			$this->percent_translated = '';
302
		}
303
	}
304
}
305
306
$give_i18n = new Give_i18n_Banner(
307
	array(
308
		'hook'          => 'give_forms_page_give-settings',
309
		'glotpress_url' => 'https://translate.wordpress.org/api/projects/wp-plugins/give/stable/',
310
	)
311
);