PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-twitter.php

class-mainwp-twitter.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.1, at class/class-mainwp-twitter.php

420 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Twitter Bragger
4 *
5 * Each time a Child Site is updated, build a Tweet to be sent out to brag
6 * that MainWP was used and how fast it was.
7 *
8 * @package MainWP/Twitter
9 */
10
11 namespace MainWP\Dashboard;
12
13 /**
14 * Class MainWP_Twitter
15 *
16 * @package MainWP\Dashboard
17 */
18 class MainWP_Twitter {
19
20 /**
21 * Method get_class_name()
22 *
23 * Get Class Name.
24 *
25 * @return string CLASS Class Name.
26 */
27 public static function get_class_name() {
28 return __CLASS__;
29 }
30
31
32 /**
33 * Method get_filter()
34 *
35 * @return array Filter List.
36 */
37 public static function get_filter() {
38 return array(
39 'upgrade_everything',
40 'upgrade_all_wp_core',
41 'upgrade_all_plugins',
42 'upgrade_all_themes',
43 'new_post',
44 'new_page',
45 'installing_new_plugin',
46 'installing_new_theme',
47 'create_new_user',
48 );
49 }
50
51 /**
52 * Method enabled_twitter_messages()
53 *
54 * Check if Twitter Bragger should be hidden or not.
55 *
56 * @return boolean True|False.
57 */
58 public static function enabled_twitter_messages() {
59 if ( ! get_option( 'mainwp_hide_twitters_message', 0 ) ) {
60 return true;
61 }
62
63 return false;
64 }
65
66 /**
67 * Method clear_all_twitter_messages()
68 *
69 * Clear the twitter messages for current user.
70 */
71 public static function clear_all_twitter_messages() {
72 $filters = self::get_filter();
73 $user_id = get_current_user_id();
74
75 foreach ( $filters as $what ) {
76 $opt_name = 'mainwp_tt_message_' . $what;
77 delete_user_option( $user_id, $opt_name );
78 }
79 }
80
81 /**
82 * Method random_word()
83 *
84 * Array of random words to use in each tweet.
85 *
86 * @return string $words
87 */
88 public static function random_word() {
89 $words = array(
90 __( 'Awesome', 'mainwp' ),
91 __( 'Fabulous', 'mainwp' ),
92 __( 'Impressive', 'mainwp' ),
93 __( 'Incredible', 'mainwp' ),
94 __( 'Super', 'mainwp' ),
95 __( 'Wonderful', 'mainwp' ),
96 __( 'Wow', 'mainwp' ),
97 );
98
99 return $words[ wp_rand( 0, 6 ) ];
100 }
101
102 /**
103 * Method get_notice()
104 *
105 * Build Twitter message based on what action was performed.
106 *
107 * @param mixed $what Performed action.
108 * @param mixed $value Performed action details.
109 *
110 * @return mixed $message Twit content.
111 */
112 public static function get_notice( $what, $value ) {
113
114 if ( ! is_array( $value ) || empty( $value['sites'] ) || ! isset( $value['seconds'] ) ) {
115 return '';
116 }
117
118 $message = '';
119 $first_word = self::random_word();
120 switch ( $what ) {
121 case 'upgrade_everything':
122 $message = _n( sprintf( 'you just updated <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just updated <strong>%d</strong> sites', $value['sites'] ), $value['sites'], 'mainwp' );
123 $message = $first_word . ', ' . $message;
124 break;
125 case 'upgrade_all_wp_core':
126 $message = _n( sprintf( 'you just updated <strong>%d</strong> WordPress site', $value['sites'] ), sprintf( 'you just updated <strong>%d</strong> WordPress sites', $value['sites'] ), $value['sites'], 'mainwp' );
127 $message = $first_word . ', ' . $message;
128 break;
129 case 'upgrade_all_plugins':
130 $message = _n( sprintf( 'you just updated <strong>%d</strong> plugin', $value['sites'] ), sprintf( 'you just updated <strong>%d</strong> plugins', $value['sites'] ), $value['items'], 'mainwp' ) . ' ' . _n( sprintf( 'on <strong>%d</strong> site', $value['sites'] ), sprintf( 'on <strong>%d</strong> sites', $value['sites'] ), $value['items'], 'mainwp' );
131 $message = $first_word . ', ' . $message;
132 break;
133 case 'upgrade_all_themes':
134 $message = _n( sprintf( 'you just updated <strong>%d</strong> theme', $value['sites'] ), sprintf( 'you just updated <strong>%d</strong> themes', $value['sites'] ), $value['items'], 'mainwp' ) . ' ' . _n( sprintf( 'on <strong>%d</strong> site', 'on <strong>%d</strong> sites', $value['sites'], 'mainwp' ), $value['items'], $value['sites'] );
135 $message = $first_word . ', ' . $message;
136 break;
137 case 'new_post':
138 $message = _n( sprintf( 'you just published a new post on <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just published a new post on <strong>%d</strong> sites', $value['sites'] ), $value['sites'], 'mainwp' );
139 $message = $first_word . ', ' . $message;
140 break;
141 case 'new_page':
142 $message = _n( sprintf( 'you just published a new page on <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just published a new page on <strong>%d</strong> sites', $value['sites'] ), $value['sites'], 'mainwp' );
143 $message = $first_word . ', ' . $message;
144 break;
145 case 'installing_new_plugin':
146 $message = _n( sprintf( 'you just installed a new plugin on <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just installed a new plugin on <strong>%d</strong> sites', $value['sites'] ), $value['sites'], 'mainwp' );
147 $message = $first_word . ', ' . $message;
148 break;
149 case 'installing_new_theme':
150 $message = _n( sprintf( 'you just installed a new theme on <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just installed a new theme on <strong>%d</strong> sites', $value['sites'] ), $value['sites'], 'mainwp' );
151 $message = $first_word . ', ' . $message;
152 break;
153 case 'create_new_user':
154 $message = _n( sprintf( 'you just created a new user on <strong>%d</strong> site', $value['sites'] ), sprintf( 'you just created a new user on <strong>%d</strong> site', $value['sites'] ), $value['sites'], 'mainwp' );
155 $message = $first_word . ', ' . $message;
156 break;
157 }
158
159 if ( ! empty( $message ) ) {
160 $in_sec = $value['seconds'];
161 if ( $in_sec <= 60 ) {
162 if ( 'upgrade_all_plugins' == $what || 'upgrade_all_themes' == $what || 'upgrade_everything' == $what ) {
163 $real_updated = $value['real_items'];
164 $message .= ', ' . _n( sprintf( '<strong>%d</strong> total update', $real_updated ), sprintf( '<strong>%d</strong> total updates', $real_updated ), $real_updated, 'mainwp' );
165 }
166 $message .= ' ' . _n( sprintf( 'in <strong>%d</strong> second', $real_updated ), sprintf( 'in <strong>%d</strong> seconds', $in_sec ), $in_sec, 'mainwp' );
167 }
168 $message .= '!';
169 }
170
171 return $message;
172 }
173
174 /**
175 * Method gen_twitter_button()
176 *
177 * Build Twitter Brag button.
178 *
179 * @param mixed $content Twit content.
180 * @param bool $echo Echo or return content.
181 *
182 * @return mixed $return Button HTML
183 */
184 public static function gen_twitter_button( $content, $echo = true ) {
185 ob_start();
186 $content
187 ?>
188 <button class="ui mini twitter button mainwp_tweet_this" msg="<?php echo rawurlencode( $content ); ?>">
189 <i class="twitter icon"></i>
190 <?php esc_html_e( 'Brag on Twitter', 'mainwp' ); ?>
191 </button>
192 <?php
193 $return = ob_get_clean();
194
195 if ( $echo ) {
196 echo $return;
197 } else {
198 return $return;
199 }
200 }
201
202 /**
203 * Method update_twitter_info()
204 *
205 * Build Twitter message to be sent.
206 *
207 * @param mixed $what What task was performed.
208 * @param integer $countSites Number of Sites updated.
209 * @param integer $countSec Second it took to update.
210 * @param integer $coutRealItems Number of items updated.
211 * @param integer $twId Twitter ID.
212 * @param integer $countItems Total number of Items together.
213 *
214 * @return boolean True|False.
215 */
216 public static function update_twitter_info( $what, $countSites = 0, $countSec = 0, $coutRealItems = 0, $twId = 0, $countItems = 1 ) {
217 if ( empty( $twId ) ) {
218 return false;
219 }
220
221 $filters = self::get_filter();
222
223 if ( ! in_array( $what, $filters ) ) {
224 return false;
225 }
226
227 $clear_twit = false;
228 if ( empty( $coutRealItems ) || 1 == $coutRealItems ) {
229 $clear_twit = true;
230 }
231
232 $opt_name = 'mainwp_tt_message_' . $what;
233 $user_id = get_current_user_id();
234
235 if ( $clear_twit ) {
236 delete_user_option( $user_id, $opt_name );
237 } else {
238 if ( empty( $countSec ) ) {
239 $countSec = 1;
240 }
241 // store one twitt info only.
242 $data = array(
243 $twId => array(
244 'sites' => $countSites,
245 'seconds' => $countSec,
246 'items' => $countItems,
247 'real_items' => $coutRealItems,
248 ),
249 );
250 if ( update_user_option( $user_id, $opt_name, $data ) ) {
251 return true;
252 }
253 }
254 return false;
255 }
256
257 /**
258 * Method clear_twitter_info
259 *
260 * @param mixed $what What task was performed.
261 * @param integer $twId Twitter ID.
262 *
263 * @return boolean True|False.
264 */
265 public static function clear_twitter_info( $what, $twId = 0 ) {
266 if ( empty( $twId ) ) {
267 return false;
268 }
269
270 $filters = self::get_filter();
271
272 if ( ! in_array( $what, $filters ) ) {
273 return false;
274 }
275
276 $opt_name = 'mainwp_tt_message_' . $what;
277
278 $data = get_user_option( $opt_name );
279
280 if ( ! is_array( $data ) ) {
281 $data = array();
282 }
283
284 if ( isset( $data[ $twId ] ) ) {
285 unset( $data[ $twId ] );
286 $user_id = get_current_user_id();
287 update_user_option( $user_id, $opt_name, $data );
288 }
289
290 return true;
291 }
292
293 /**
294 * Method get_twitter_notice()
295 *
296 * Grab Twitter message that was built.
297 *
298 * @param mixed $what What task was performed.
299 * @param integer $twId Twitter ID.
300 *
301 * @return mixed $return[ $time ] = $mess.
302 */
303 public static function get_twitter_notice( $what, $twId = 0 ) {
304
305 $filters = self::get_filter();
306
307 if ( ! in_array( $what, $filters ) ) {
308 return false;
309 }
310
311 $opt_name = 'mainwp_tt_message_' . $what;
312 $twitter_messages = get_user_option( $opt_name );
313
314 $return = array();
315
316 if ( is_array( $twitter_messages ) ) {
317 if ( ! empty( $twId ) ) {
318 if ( isset( $twitter_messages[ $twId ] ) ) {
319 $value = $twitter_messages[ $twId ];
320 $mess = self::get_notice( $what, $value );
321 if ( ! empty( $mess ) ) {
322 $return[ $twId ] = $mess;
323 }
324 }
325 } else {
326 foreach ( $twitter_messages as $time => $value ) {
327 $mess = self::get_notice( $what, $value );
328 if ( ! empty( $mess ) ) {
329 $return[ $time ] = $mess;
330 }
331 }
332 }
333 }
334
335 return $return;
336 }
337
338 /**
339 * Method get_twit_to_send()
340 *
341 * Example @MyMainWP I just quickly updated 3 plugins on 3 #WordPress sites, 5 total updates in 12 seconds.
342 *
343 * @param mixed $what What task was performed.
344 * @param integer $twId Twitter ID.
345 *
346 * @return string Tweet to send.
347 */
348 public static function get_twit_to_send( $what, $twId = 0 ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
349
350 $filters = self::get_filter();
351
352 if ( ! in_array( $what, $filters ) ) {
353 return '';
354 }
355
356 $opt_name = 'mainwp_tt_message_' . $what;
357 $twitter_messages = get_user_option( $opt_name );
358 if ( is_array( $twitter_messages[ $twId ] ) && isset( $twitter_messages[ $twId ] ) ) {
359 $value = $twitter_messages[ $twId ];
360 if ( is_array( $value ) && ! empty( $value['sites'] ) && ! empty( $value['seconds'] ) ) {
361 $twit = '';
362 switch ( $what ) {
363 case 'upgrade_everything':
364 $twit = _n( 'Thanks to @MyMainWP I just quickly updated %d #WordPress site', 'Thanks to @MyMainWP I just quickly updated %d #WordPress sites', $value['sites'], 'mainwp' );
365 $twit = sprintf( $twit, $value['sites'] );
366 break;
367 case 'upgrade_all_wp_core':
368 $twit = _n( 'Thanks to @MyMainWP I just quickly updated %d #WordPress site', 'Thanks to @MyMainWP I just quickly updated %d #WordPress sites', $value['sites'], 'mainwp' );
369 $twit = sprintf( $twit, $value['sites'] );
370 break;
371 case 'upgrade_all_plugins':
372 $twit = _n( 'Thanks to @MyMainWP I just quickly updated %d plugin', 'Thanks to @MyMainWP I just quickly updated %d plugins', $value['items'], 'mainwp' ) . ' ' . _n( 'on %d #WordPress site', 'on %d #WordPress sites', $value['sites'], 'mainwp' );
373 $twit = sprintf( $twit, $value['items'], $value['sites'] );
374 break;
375 case 'upgrade_all_themes':
376 $twit = _n( 'Thanks to @MyMainWP I just quickly updated %d theme', 'Thanks to @MyMainWP I just quickly updated %d themes', $value['items'], 'mainwp' ) . ' ' . _n( 'on %d #WordPress site', 'on %d #WordPress sites', $value['sites'], 'mainwp' );
377 $twit = sprintf( $twit, $value['items'], $value['sites'] );
378 break;
379 case 'new_post':
380 $twit = _n( 'Thanks to @MyMainWP I just quickly published a new post on %d #WordPress site', 'Thanks to @MyMainWP I just quickly published a new post on %d #WordPress sites', $value['sites'], 'mainwp' );
381 $twit = sprintf( $twit, $value['sites'] );
382 break;
383 case 'new_page':
384 $twit = _n( 'Thanks to @MyMainWP I just quickly published a new page on %d #WordPress site', 'Thanks to @MyMainWP I just quickly published a new page on %d #WordPress sites', $value['sites'], 'mainwp' );
385 $twit = sprintf( $twit, $value['sites'] );
386 break;
387 case 'installing_new_plugin':
388 $twit = _n( 'Thanks to @MyMainWP I just quickly installed a new plugin on %d #WordPress site', 'Thanks to @MyMainWP I just quickly installed a new plugin on %d #WordPress sites', $value['sites'], 'mainwp' );
389 $twit = sprintf( $twit, $value['sites'] );
390 break;
391 case 'installing_new_theme':
392 $twit = _n( 'Thanks to @MyMainWP I just quickly installed a new theme on %d #WordPress site', 'Thanks to @MyMainWP I just quickly installed a new theme on %d #WordPress sites', $value['sites'], 'mainwp' );
393 $twit = sprintf( $twit, $value['sites'] );
394 break;
395 case 'create_new_user':
396 $twit = _n( 'Thanks to @MyMainWP I just quickly created a new user on %d #WordPress site', 'Thanks to @MyMainWP I just quickly created a new user on %d #WordPress sites', $value['sites'], 'mainwp' );
397 $twit = sprintf( $twit, $value['sites'] );
398 break;
399 }
400 if ( ! empty( $twit ) ) {
401 $in_sec = $value['seconds'];
402 if ( $in_sec <= 60 ) {
403 if ( 'upgrade_all_plugins' == $what || 'upgrade_all_themes' == $what || 'upgrade_everything' == $what ) {
404 $real_updated = $value['real_items'];
405 $twit .= ', ' . sprintf( _n( '%d total update', '%d total updates', $real_updated, 'mainwp' ), $real_updated );
406 }
407 $twit .= ' ' . sprintf( _n( 'in %d second', 'in %d seconds', $in_sec, 'mainwp' ), $in_sec );
408 }
409 $twit .= '! https://mainwp.com';
410
411 return $twit;
412 }
413 }
414 }
415
416 return '';
417 }
418
419 }
420