Completed
Push — master ( 9e3902...3c0417 )
by Devin
39:54 queued 19:56
created

Give_i18n_Banner   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 317
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A init() 0 6 2
A hide_promo() 0 12 3
C promo_message() 0 22 7
B promo() 0 80 2
A find_or_initialize_translation_details() 0 11 2
A translation_details() 0 8 1
C retrieve_translation_details() 0 29 7
A parse_translation_set() 0 9 3
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 324.

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
	public function __construct( $args ) {
69
70
		// Only for admins.
71
		if ( ! is_admin() ) {
72
			return;
73
		}
74
75
		// This plugin is en_US native.
76
		$this->locale = get_locale();
77
		if ( 'en_US' === $this->locale ) {
78
			return;
79
		}
80
81
		$this->init( $args );
82
83
		if ( ! $this->hide_promo() ) {
84
			add_action( $this->hook, array( $this, 'promo' ) );
85
		}
86
	}
87
88
	/**
89
	 * This is where you decide where to display the messages and where you set the plugin specific variables.
90
	 *
91
	 * @access private
92
	 *
93
	 * @param array $args
94
	 */
95
	private function init( $args ) {
96
		foreach ( $args as $key => $arg ) {
97
			$this->$key = $arg;
98
		}
99
100
	}
101
102
	/**
103
	 * Check whether the promo should be hidden or not.
104
	 *
105
	 * @access private
106
	 *
107
	 * @return bool
108
	 */
109
	private function hide_promo() {
110
		$hide_promo = get_transient( 'give_i18n_give_promo_hide' );
111
		if ( ! $hide_promo ) {
112
			if ( filter_input( INPUT_GET, 'remove_i18n_promo', FILTER_VALIDATE_INT ) === 1 ) {
113
				// No expiration time, so this would normally not expire, but it wouldn't be copied to other sites etc.
114
				set_transient( 'give_i18n_give_promo_hide', true );
115
				$hide_promo = true;
116
			}
117
		}
118
119
		return $hide_promo;
120
	}
121
122
	/**
123
	 * Generates a promo message.
124
	 *
125
	 * @access private
126
	 *
127
	 * @return bool|string $message
128
	 */
129
	private function promo_message() {
130
		$message = false;
131
132
		// Using a translation less than 90% complete.
133
		if ( $this->translation_exists && $this->translation_loaded && $this->percent_translated < 90 ) {
134
			$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 %5$s to %1$s!', 'give' );
135
		} elseif ( ! $this->translation_loaded && $this->translation_exists ) {
136
			$message = __( 'You\'re using WordPress in %1$s. While %2$s has been %3$d%% translated to %1$s, it has not been shipped with the plugin yet. You can help! Register at %4$s to help complete the translation to %1$s!', 'give' );
137
		} elseif ( ! $this->translation_exists ) {
138
			$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' );
139
		}
140
141
		// Links.
142
		$registration_link = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://wordpress.org/support/register.php', esc_html__( 'WordPress.org', 'give' ) );
143
		$translations_link = sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://translate.wordpress.org/projects/wp-plugins/give', esc_html__( 'complete the translation', 'give' ) );
144
145
		// Message.
146
		$message = sprintf( $message, $this->locale_name, 'Give', $this->percent_translated, $registration_link, $translations_link );
147
148
		return $message;
149
150
	}
151
152
	/**
153
	 * Outputs a promo box
154
	 */
155
	public function promo() {
156
157
		$this->translation_details();
158
		$message = $this->promo_message();
159
160
		if ( $message ) { ?>
161
162
			<style>
163
				/* Banner specific styles */
164
				div.give-addon-alert.updated {
165
					padding: 10px 20px;
166
					position: relative;
167
					border-color: #66BB6A;
168
					overflow: hidden;
169
				}
170
171
				div.give-addon-alert a {
172
					color: #66BB6A;
173
				}
174
175
				#give-i18n-notice > .give-i18n-icon {
176
					overflow: hidden;
177
				}
178
179
				#give-i18n-notice > .give-i18n-icon .dashicons {
180
					width: 110px;
181
					height: 110px;
182
				}
183
184
				#give-i18n-notice > .give-i18n-icon:focus {
185
					box-shadow: none;
186
				}
187
188
				.give-i18n-notice-content {
189
					margin: 0 30px 0 125px;
190
				}
191
192
				div.give-addon-alert .dismiss {
193
					position: absolute;
194
					right: 20px;
195
					height: 100%;
196
					top: 50%;
197
					margin-top: -10px;
198
					outline: none;
199
					box-shadow: none;
200
					text-decoration: none;
201
					color: #AAA;
202
				}
203
204
				div.give-addon-alert .dismiss:hover {
205
					color: #333;
206
				}
207
208
				/* RTL Styles for banner */
209
				body.rtl .give-i18n-notice-content {
210
					margin: 0 125px 0 30px;
211
				}
212
				body.rtl div.give-addon-alert .dismiss {
213
					left:20px;
214
					right:auto;
215
				}
216
217
			</style>
218
			<div id="give-i18n-notice" class="give-addon-alert updated" style="">
219
220
				<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>
221
222
				<div class="give-i18n-notice-content">
223
					<a href="<?php echo esc_url( add_query_arg( array( 'remove_i18n_promo' => '1' ) ) ); ?>" class="dismiss"><span class="dashicons dashicons-dismiss"></span></a>
224
225
					<h2 style="margin: 10px 0;"><?php printf( esc_html__( 'Help Translate Give to %s', 'give' ), $this->locale_name ); ?></h2>
226
					<p><?php echo $message; ?></p>
227
					<p>
228
						<a href="https://wordpress.org/support/register.php" target="_blank"><?php _e( 'Register now &raquo;', 'give' ); ?></a>
229
					</p>
230
				</div>
231
			</div>
232
			<?php
233
		}
234
	}
235
236
	/**
237
	 * Try to find the transient for the translation set or retrieve them.
238
	 *
239
	 * @access private
240
	 *
241
	 * @return object|null
242
	 */
243
	private function find_or_initialize_translation_details() {
244
245
		$set = get_transient( 'give_i18n_give_' . $this->locale );
246
247
		if ( ! $set ) {
248
			$set = $this->retrieve_translation_details();
249
			set_transient( 'give_i18n_give_' . $this->locale, $set, DAY_IN_SECONDS );
250
		}
251
252
		return $set;
253
	}
254
255
	/**
256
	 * Try to get translation details from cache, otherwise retrieve them, then parse them.
257
	 *
258
	 * @access private
259
	 */
260
	private function translation_details() {
261
		$set = $this->find_or_initialize_translation_details();
262
263
		$this->translation_exists = ! is_null( $set );
264
		$this->translation_loaded = is_textdomain_loaded( 'give' );
265
266
		$this->parse_translation_set( $set );
267
	}
268
269
	/**
270
	 * Retrieve the translation details from Give Translate.
271
	 *
272
	 * @access private
273
	 *
274
	 * @return object|null
275
	 */
276
	private function retrieve_translation_details() {
277
278
		$api_url = trailingslashit( $this->glotpress_url );
279
280
		$resp = wp_remote_get( $api_url );
281
282
		if ( is_wp_error( $resp ) || wp_remote_retrieve_response_code( $resp ) === '404' ) {
283
			return null;
284
		}
285
286
		$body = wp_remote_retrieve_body( $resp );
287
		unset( $resp );
288
289
		if ( $body ) {
290
			$body = json_decode( $body );
291
292
			foreach ( $body->translation_sets as $set ) {
293
				if ( ! property_exists( $set, 'wp_locale' ) ) {
294
					continue;
295
				}
296
297
				if ( $this->locale == $set->wp_locale ) {
298
					return $set;
299
				}
300
			}
301
		}
302
303
		return null;
304
	}
305
306
	/**
307
	 * Set the needed private variables based on the results from Give Translate.
308
	 *
309
	 * @param object $set The translation set
310
	 *
311
	 * @access private
312
	 */
313
	private function parse_translation_set( $set ) {
314
		if ( $this->translation_exists && is_object( $set ) ) {
315
			$this->locale_name        = $set->name;
316
			$this->percent_translated = $set->percent_translated;
317
		} else {
318
			$this->locale_name        = '';
319
			$this->percent_translated = '';
320
		}
321
	}
322
}
323
324
$give_i18n = new Give_i18n_Banner(
325
	array(
326
		'hook'          => 'give_forms_page_give-settings',
327
		'glotpress_url' => 'https://translate.wordpress.org/api/projects/wp-plugins/give/stable/',
328
	)
329
);
330