Completed
Pull Request — master (#1085)
by Devin
19:12
created

Give_i18n_Banner::promo_message()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 4
nop 0
dl 0
loc 23
rs 6.7272
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 315.

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