PluginProbe
Shariff Wrapper / 4.6.20
Shariff Wrapper v4.6.20
4.6.22 4.6.21 4.6.20 4.6.19 trunk 1.0.0 1.9.0 1.9.9 2.4.3 3.4.2 4.0.8 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.3.0 4.4.2 4.4.3 4.4.4 4.5.0 4.5.1 4.5.2 4.5.3 4.6.0 All 42 releases
shariff / shariff.php

shariff.php in Shariff Wrapper 4.6.20, at shariff.php

1,699 lines 62.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Shariff Wrapper
4 * Plugin URI: https://wordpress.org/plugins-wp/shariff/
5 * Description: Shariff provides share buttons that respect the privacy of your visitors and follow the General Data Protection Regulation (GDPR).
6 * Version: 4.6.20
7 * Author: Jan-Peter Lambeck & 3UU
8 * Author URI: https://wordpress.org/plugins/shariff/
9 * License: MIT
10 * License URI: https://opensource.org/licenses/MIT
11 * Text Domain: shariff
12 *
13 * @package WordPress
14 */
15
16 // Prevent direct calls to shariff.php.
17 if ( ! class_exists( 'WP' ) ) { die(); }
18 // and again the same because the WP Plugin Check does not understand what the line above does ;-)
19 if ( ! defined( 'ABSPATH' ) ) exit;
20
21 require_once plugin_dir_path( __FILE__ ) . 'includes/sanitization.php';
22
23 // Force the creation as a global variable in order to work with WP-CLI.
24 global $shariff3uu_basic, $shariff3uu_design, $shariff3uu_advanced, $shariff3uu_statistic, $shariff3uu;
25
26 // Get options (needed for front- and backend).
27 $shariff3uu_basic = (array) get_option( 'shariff3uu_basic' );
28 $shariff3uu_design = (array) get_option( 'shariff3uu_design' );
29 $shariff3uu_advanced = (array) get_option( 'shariff3uu_advanced' );
30 $shariff3uu_statistic = (array) get_option( 'shariff3uu_statistic' );
31
32 $shariff3uu = array_merge( $shariff3uu_basic, $shariff3uu_design, $shariff3uu_advanced, $shariff3uu_statistic );
33
34 /**
35 * Update function to perform tasks _once_ after an update, based on version number to work for automatic as well as manual updates.
36 */
37 function shariff3uu_update() {
38 // Adjust code version.
39 $code_version = '4.6.20';
40
41 // Get basic options.
42 $shariff3uu_basic = (array) get_option( 'shariff3uu_basic' );
43
44 // Check if the installed version is older than the code version and include updates.php if necessary.
45 if ( empty( $shariff3uu_basic['version'] ) || ( isset( $shariff3uu_basic['version'] ) && version_compare( $shariff3uu_basic['version'], $code_version ) === -1 ) ) {
46 // Get the other options.
47 $shariff3uu_design = (array) get_option( 'shariff3uu_design' );
48 $shariff3uu_advanced = (array) get_option( 'shariff3uu_advanced' );
49 $shariff3uu_statistic = (array) get_option( 'shariff3uu_statistic' );
50 $shariff3uu = array_merge( $shariff3uu_basic, $shariff3uu_design, $shariff3uu_advanced, $shariff3uu_statistic );
51 // Include updates.php.
52 include dirname( __FILE__ ) . '/updates.php';
53 }
54 }
55 add_action( 'admin_init', 'shariff3uu_update' );
56 // In case WP-CLI is used and the backend is never visited, we add it to cli_init as well.
57 add_action( 'cli_init', 'shariff3uu_update' );
58
59 /**
60 * Add privacy policy suggestions to WordPress privacy generator introduced in WordPress 4.9.6.
61 */
62 function shariff3uu_privacy() {
63 if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
64 $content = __(
65 '<h2>Social Media Plugin "Shariff Wrapper"</h2>
66
67 On our website we offer you the possibility to use so called "Social Media Buttons". To protect your data, we use a solution called "Shariff". Hereby the share buttons are implemented as static images, which contain a link to the corresponding social network site. If you click on such a button, you will be redirected to the respective social network site in the same way, as normal links would do as well. Only in that moment of time the provider of the social network site will get information about you, for example your IP address. If you do not click on such a share button, no data will be transmitted. Information about the collection and usage of your date on the social network sites can be found in the corresponding terms of use of the respective provider. More information about the plugin and the Shariff solution can be found here: <a href="https://wordpress.org/plugins/shariff/">https://wordpress.org/plugins/shariff/</a>
68
69 On our website we offer share buttons for the following services / companies: Bluesky, Diaspora, Facebook, Flattr, Flipboard, LinkedIn, Mastodon, Mix, Odnoklassniki, Patreon, PayPal, Pinterest, Pocket, Qzone, Reddit, Telegram, TencentWeibo, Threema, Tumblr, Twitter, VK, Wallabag, Weibo, WhatsApp, Xing.',
70 'shariff'
71 );
72 wp_add_privacy_policy_content( 'Shariff Wrapper', wp_kses_post( wpautop( $content, false ) ) );
73 }
74 }
75 add_action( 'admin_init', 'shariff3uu_privacy' );
76
77 /** Require Shariff Widget. */
78 require dirname( __FILE__ ) . '/includes/class-shariff-widget.php';
79
80 // Allowed tags for headline.
81 $allowed_tags = array(
82 // Direct formatting e.g. <strong>.
83 'strong' => array(),
84 'em' => array(),
85 'b' => array(),
86 'i' => array(),
87 'br' => array(),
88 // Elements that can be formatted via CSS.
89 'span' => array(
90 'class' => array(),
91 'style' => array(),
92 'id' => array(),
93 ),
94 'div' => array(
95 'class' => array(),
96 'style' => array(),
97 'id' => array(),
98 ),
99 'p' => array(
100 'class' => array(),
101 'style' => array(),
102 'id' => array(),
103 ),
104 'h1' => array(
105 'class' => array(),
106 'style' => array(),
107 'id' => array(),
108 ),
109 'h2' => array(
110 'class' => array(),
111 'style' => array(),
112 'id' => array(),
113 ),
114 'h3' => array(
115 'class' => array(),
116 'style' => array(),
117 'id' => array(),
118 ),
119 'h4' => array(
120 'class' => array(),
121 'style' => array(),
122 'id' => array(),
123 ),
124 'h5' => array(
125 'class' => array(),
126 'style' => array(),
127 'id' => array(),
128 ),
129 'h6' => array(
130 'class' => array(),
131 'style' => array(),
132 'id' => array(),
133 ),
134 'hr' => array(
135 'class' => array(),
136 'style' => array(),
137 'id' => array(),
138 ),
139 );
140
141 /** Admin options */
142 if ( is_admin() ) {
143 // Include admin_menu.php.
144 include dirname( __FILE__ ) . '/admin/admin-menu.php';
145 // Include admin_notices.php.
146 # include dirname( __FILE__ ) . '/admin/admin-notices.php';
147 }
148
149 /** Custom meta box */
150 function shariff3uu_include_metabox() {
151 // Check if user is allowed to publish posts.
152 if ( current_user_can( 'publish_posts' ) ) {
153 // Include admin_metabox.php.
154 include dirname( __FILE__ ) . '/admin/admin-metabox.php';
155 }
156 }
157 // Check if meta box has been disabled in the options, if not add_action.
158 if ( ! isset( $shariff3uu['disable_metabox'] ) || isset( $shariff3uu['disable_metabox'] ) && 1 !== $shariff3uu['disable_metabox'] ) {
159 add_action( 'init', 'shariff3uu_include_metabox' );
160 }
161
162 /*
163 * only super admins are allowed to add/change risky attributes like html/styles
164 */
165 function final_shariff_security_check( $data, $postarr ) {
166 // super admins are allowed to edit HTML/CSS. So go ahead.
167 if ( is_super_admin() || $data['post_type'] === 'revision' ) {
168 return $data;
169 }
170
171 $new_content = $data['post_content'];
172 $post_id = isset($postarr['ID']) ? $postarr['ID'] : 0;
173
174 // get the old content with the pre-authorized shorttags
175 // BTW: Here is an problem with shorttags that are inserted while shariff was deactivated.
176 // We can not trust the DB in WP :-( But this will get checks on dangerous content in the render function.
177 # global $wpdb;
178 # $old_content = $post_id ? $wpdb->get_var( $wpdb->prepare( "SELECT post_content FROM $wpdb->posts WHERE ID = %d", $post_id ) ) : '';
179 $old_post = $post_id ? get_post( $post_id ) : null;
180 $old_content = $old_post ? $old_post->post_content : '';
181
182 $allowed_configs = [];
183 if ( ! empty( $old_content ) ) {
184 preg_match_all( '/\[shariff(\s+[^\]]+)?\]/i', $old_content, $old_matches );
185 foreach ( $old_matches[1] as $attr_string ) {
186 $parsed = shortcode_parse_atts( stripslashes( trim( $attr_string ) ) );
187 $parsed_array = is_array( $parsed ) ? $parsed : [];
188 ksort( $parsed_array );
189 $allowed_configs[] = $parsed_array;
190 }
191 }
192
193 // get all new shariff shortcodes
194 preg_match_all( '/\[shariff(\s+[^\]]+)?\]/i', $new_content, $new_matches );
195
196 foreach ( $new_matches[1] as $attr_string ) {
197 $new_parsed = shortcode_parse_atts( stripslashes( trim( $attr_string ) ) );
198 $new_atts = is_array( $new_parsed ) ? $new_parsed : [];
199 ksort( $new_atts );
200
201 if ( isset( $new_atts['headline'] ) || isset( $new_atts['style'] ) ) {
202 $is_authorized = false;
203 foreach ( $allowed_configs as $old_atts ) {
204 if ( $new_atts === $old_atts ) {
205 $is_authorized = true;
206 break;
207 }
208 }
209
210 if ( ! $is_authorized ) {
211 $msg = '<strong>Security Check:</strong> This Shariff configuration is not allowed or has been modified. Please contact an administrator.';
212
213 // REST API (Gutenberg & Co.)
214 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
215 status_header( 403 );
216 wp_send_json_error( array( 'message' => $msg ) );
217 exit;
218 }
219
220 // AJAX (Quick Edit, Page Builder etc.)
221 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
222 wp_send_json_error( $msg );
223 exit;
224 }
225
226 // classic editor / standard HTTP post
227 wp_die( $msg, 'Aktion verweigert', array( 'back_link' => true ) );
228 }
229 }
230 }
231
232 return $data;
233 }
234 add_filter( 'wp_insert_post_data', 'final_shariff_security_check', 10, 2 );
235
236
237 /**
238 * Add meta links (settings and support forum) to our entry on the plugin page.
239 *
240 * @param array $links Array of all current links.
241 * @param string $file Path to plugin of the current element.
242 *
243 * @return array New array including our links to settings and support forum.
244 */
245 function shariff3uu_meta_links( $links, $file ) {
246 $plugin = plugin_basename( __FILE__ );
247 // Create link.
248 if ( $file === $plugin ) {
249 return array_merge(
250 $links,
251 array( '<a href="' . site_url() . '/wp-admin/options-general.php?page=shariff3uu">' . __( 'Settings', 'shariff' ) . '</a>', '<a href="https://wordpress.org/support/plugin/shariff" target="_blank">' . __( 'Support Forum', 'shariff' ) . '</a>' )
252 );
253 }
254 return $links;
255 }
256 add_filter( 'plugin_row_meta', 'shariff3uu_meta_links', 10, 2 );
257
258
259 /** Register the wp rest api route and sanitize the input */
260 function shariff3uu_sanitize_api() {
261 register_rest_route(
262 'shariff/v1',
263 '/share_counts',
264 array(
265 'methods' => 'GET',
266 'callback' => 'shariff3uu_share_counts',
267 #https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/#permissions-callback
268 'permission_callback' => '__return_true',
269 'args' => array(
270 'url' => array(
271 'sanitize_callback' => 'esc_url',
272 'required' => true,
273 'description' => __( 'URL of the post or page to request share counts for.', 'shariff' ),
274 ),
275 'services' => array(
276 'sanitize_callback' => 'sanitize_text_field',
277 'required' => true,
278 'description' => __( 'A list of services separated by |. Example: mastodon|facebook|linkedin', 'shariff' ),
279 ),
280 'timestamp' => array(
281 'sanitize_callback' => 'absint',
282 'description' => __( 'Timestamp of the last update of the post. Used for dynamic cache lifespan.', 'shariff' ),
283 ),
284 ),
285 )
286 );
287 }
288 add_action( 'rest_api_init', 'shariff3uu_sanitize_api' );
289
290 /**
291 * Provide share counts via the wp rest api.
292 *
293 * @param WP_REST_Request $request Incoming request.
294 *
295 * @return string Returns the share counts for all requested services if available or an error message.
296 */
297 function shariff3uu_share_counts( WP_REST_Request $request ) {
298 // Get options.
299 $shariff3uu = $GLOBALS['shariff3uu'];
300
301 // Setup Parameters.
302 $url = urldecode( $request['url'] );
303 $services = $request['services'];
304 $timestamp = $request['timestamp'];
305
306 // Exit if no url is provided and provide an error message.
307 if ( empty( $url ) || 'undefined' === $url ) {
308 return new WP_Error( 'nourl', 'No URL provided!', array( 'status' => 400 ) );
309 }
310
311 // Exit if no services are provided and provide an error message.
312 if ( empty( $services ) || 'undefined' === $services ) {
313 return new WP_Error( 'noservices', 'No services provided!', array( 'status' => 400 ) );
314 }
315
316 // Make sure that the provided url matches the WordPress domain.
317 $get_url = wp_parse_url( $url );
318 $wp_url = wp_parse_url( get_bloginfo( 'url' ) );
319 // On an external backend check allowed hosts, else compare that domain is equal.
320 if ( defined( 'SHARIFF_FRONTENDS' ) ) {
321 $shariff_frontends = array_flip( explode( '|', SHARIFF_FRONTENDS ) );
322 if ( ! isset( $get_url['host'] ) || ! array_key_exists( $get_url['host'], $shariff_frontends ) ) {
323 return new WP_Error( 'externaldomainnotallowed', 'External domain not allowed by this server!', array( 'status' => 400 ) );
324 }
325 } elseif ( ! isset( $get_url['host'] ) || $get_url['host'] !== $wp_url['host'] ) {
326 return new WP_Error( 'domainnotallowed', 'Domain not allowed by this server!', array( 'status' => 400 ) );
327 }
328
329 // Encode the shareurl.
330 $post_url = rawurlencode( $url );
331 $post_url_raw = $url;
332
333 // Set transient name.
334 // Transient names can only contain 40 characters, therefore we use a hash (md5 always creates a 32 character hash).
335 // We need a prefix so we can clean up on uninstall and updates.
336 $post_hash = 'shariff' . hash( 'md5', $post_url );
337
338 // Check for ttl option, must be between 60 and 7200 seconds.
339 if ( isset( $shariff3uu['ttl'] ) ) {
340 $ttl = absint( $shariff3uu['ttl'] );
341 // Make sure ttl is a reasonable number.
342 if ( $ttl < 61 ) {
343 $ttl = 60 ;
344 } elseif ( $ttl > 7200 ) {
345 $ttl = 7200 ;
346 }
347 } else {
348 // Else set it to new default (five minutes).
349 $ttl = 300 ;
350 }
351
352 // Adjust ttl based on the post age.
353 if ( isset( $timestamp ) && ( ! isset( $shariff3uu['disable_dynamic_cache'] ) || ( isset( $shariff3uu['disable_dynamic_cache'] ) && 1 !== $shariff3uu['disable_dynamic_cache'] ) ) ) {
354 // The timestamp represents the last time the post or page was modified.
355 $post_time = intval( $timestamp );
356 $current_time = time(); #rtz current_time( 'timestamp', true );
357 $post_age = round( abs( $current_time - $post_time ) );
358 if ( $post_age > 0 ) {
359 $post_age_days = round( $post_age / 60 / 60 / 24 );
360 // Make sure ttl base is not getting too high.
361 if ( $ttl > 300 ) {
362 $ttl = 300 ;
363 }
364 $ttl = round( ( $ttl + $post_age_days * 3 ) * ( $post_age_days * 2 ) );
365 }
366
367 // Set minimum ttl to 60 seconds and maximum ttl to one week.
368 if ( $ttl < 60 ) {
369 $ttl = 60 ;
370 } elseif ( $ttl > 604800 ) {
371 $ttl = 604800 ;
372 }
373
374 // In case we get a timestamp older than 01.01.2000 or for example a 0, use a reasonable default value of five minutes.
375 if ( $post_time < 946684800 ) {
376 $ttl = 300 ;
377 }
378 }
379
380 // Set the default value.
381 $need_update = false;
382
383 // Remove totalnumber for array.
384 $real_services = str_replace( 'totalnumber|', '', $services );
385 $real_services = str_replace( '|totalnumber', '', $real_services );
386
387 // Explode services.
388 $service_array = explode( '|', $real_services );
389
390 // Remove duplicated entries.
391 $service_array = array_unique( $service_array );
392
393 // Get old share counts.
394 if ( get_transient( $post_hash ) !== false ) {
395 $old_share_counts = get_transient( $post_hash );
396 } else {
397 $old_share_counts = array();
398 }
399
400 // Check if we need to update.
401 if ( get_transient( $post_hash ) !== false ) {
402 // Check timestamp.
403 $diff = time() - $old_share_counts['timestamp'];
404 if ( $diff > $ttl ) {
405 $need_update = true;
406 }
407 // Check if we have a different set of services than stored in the cache.
408 $diff_array = array_diff_key( array_flip( $service_array ), $old_share_counts );
409 if ( ! empty( $diff_array ) ) {
410 $need_update = true;
411 // We only need to update the missing service.
412 $service_array = array_flip( $diff_array );
413 }
414 } else {
415 $need_update = true;
416 }
417
418 // Prevent php notices if debug mode is enabled.
419 $response = '';
420 $share_counts = array();
421
422 // If we do not need an update, use stored data.
423 if ( false === $need_update ) {
424 $share_counts = $old_share_counts;
425 // Provide update info for debugging and support.
426 $share_counts['updated'] = '0';
427 } elseif ( 'totalnumber' === $services ) {
428 // If only totalnumber is requested we only use cached data.
429 $share_counts = $old_share_counts;
430 } elseif ( isset( $shariff3uu['external_host'] ) && ! empty( $shariff3uu['external_host'] ) ) {
431 // Check if we want to use an external API.
432 $response = sanitize_text_field( wp_remote_retrieve_body( wp_remote_get( $shariff3uu['external_host'] . '?url=' . rawurlencode( $url ) . '&services=' . $services . '&timestamp=' . $timestamp . '"' ) ) );
433 // Decode response.
434 $share_counts = json_decode( $response, true );
435 // Save transient.
436 set_transient( $post_hash, $share_counts, 604800 );
437 // Offer a hook to work with the share counts.
438 do_action( 'shariff_share_counts', $share_counts );
439 } else {
440 // Else we fetch new counts ourselves.
441 $share_counts = shariff3uu_fetch_sharecounts( $service_array, $old_share_counts, $post_hash, $post_url_raw );
442 }
443
444 // Return results, if we have some or an error message if not.
445 if ( isset( $share_counts ) && null !== $share_counts ) {
446 return $share_counts;
447 } else {
448 return new WP_Error( 'nodata', 'Could not receive any data.', array( 'status' => 400 ) );
449 }
450 }
451
452 /**
453 * Fetch share counts.
454 *
455 * @param array $service_array Array with the desired services.
456 * @param array $old_share_counts Array of all already stored share counts.
457 * @param string $post_hash MD5-Hash of the current post.
458 * @param string $post_url_raw Raw URL of the current post.
459 *
460 * @return array Array of all fetched share counts.
461 */
462 function shariff3uu_fetch_sharecounts( $service_array, $old_share_counts, $post_hash, $post_url_raw ) {
463 // We only need the backend part from the service phps.
464 $backend = 1;
465
466 // Encode the shareurl.
467 $post_url = rawurlencode( esc_url( $post_url_raw ) );
468
469 // Get options.
470 $shariff3uu = $GLOBALS['shariff3uu'];
471
472 // Prevent php notices.
473 $total_count = 0;
474 $share_counts = array();
475
476 // what are valid service names for the include?
477 $valid_services= explode( '|', $shariff3uu['services'] );
478
479 // Loop through all desired services.
480 foreach ( $service_array as $service ) {
481 // include/execute only valid services
482 if ( !in_array( $service , $valid_services, true )===true ) continue;
483
484 // Only include services that are not disabled.
485 if ( ! empty( $service ) && ( ! isset( $shariff3uu['disable'][ $service ] ) || ( isset( $shariff3uu['disable'][ $service ] ) && 0 === $shariff3uu['disable'][ $service ] ) ) ) {
486 // Determine path.
487 $path_service_file = dirname( __FILE__ ) . '/services/shariff-' . $service . '.php';
488 // Include service files.
489 if ( file_exists( $path_service_file ) ) {
490 include $path_service_file;
491 }
492 // if we have an error (e.g. a timeout) and we have an old share count for this service, keep it!
493 if ( array_key_exists( $service, $old_share_counts ) && ( ! array_key_exists( $service, $share_counts ) || empty( $share_counts[ $service ] ) ) ) {
494 $share_counts[ $service ] = $old_share_counts[ $service ];
495 }
496 }
497 // Calculate total share count.
498 if ( isset( $share_counts[ $service ] ) ) {
499 $total_count = $total_count + $share_counts[ $service ];
500 }
501 }
502
503 // Add total count.
504 if ( 0 !== $total_count ) {
505 $share_counts['total'] = $total_count;
506 }
507
508 // Save transient, if we have counts.
509 if ( isset( $share_counts ) ) {
510 // Add current timestamp and url.
511 $share_counts['timestamp'] = time(); #rtz current_time( 'timestamp', true );
512 $share_counts['url'] = $post_url_raw;
513 // Combine different set of services.
514 if ( get_transient( $post_hash ) !== false ) {
515 $other_request = get_transient( $post_hash );
516 $share_counts = array_merge( $other_request, $share_counts );
517 }
518 // Save transient.
519 set_transient( $post_hash, $share_counts, 604800 );
520 // Offer a hook to work with the share counts.
521 do_action( 'shariff_share_counts', $share_counts );
522 // Update info.
523 $share_counts['updated'] = 1;
524 } elseif ( isset( $old_share_counts ) ) {
525 $share_counts = $old_share_counts;
526 // Update info.
527 $share_counts['updated'] = '0';
528 }
529
530 // Return share counts.
531 return $share_counts;
532 }
533
534 /**
535 * Fills cache automatically.
536 */
537 function shariff3uu_fill_cache() {
538 // Amount of posts - set to 100 if not set.
539 if ( isset( $GLOBALS['shariff3uu']['ranking'] ) && absint( $GLOBALS['shariff3uu']['ranking'] ) > 0 ) {
540 $numberposts = absint( $GLOBALS['shariff3uu']['ranking'] );
541 } else {
542 $numberposts = 100 ;
543 }
544
545 // Avoid errors if no services are given - instead use the default set of services.
546 if ( empty( $GLOBALS['shariff3uu']['services'] ) ) {
547 $services = 'mastodon|facebook|linkedin';
548 } else {
549 $services = $GLOBALS['shariff3uu']['services'];
550 }
551
552 // Explode services.
553 $service_array = explode( '|', $services );
554
555 // Arguments for wp_get_recent_posts().
556 $args = array(
557 'numberposts' => $numberposts,
558 'orderby' => 'post_date',
559 'order' => 'DESC',
560 'post_status' => 'publish',
561 'suppress_filters' => false,
562 );
563
564 // Catch last 100 posts or whatever number is set for it.
565 $recent_posts = wp_get_recent_posts( $args );
566 if ( $recent_posts ) {
567 foreach ( $recent_posts as $recent ) {
568 // Get the url.
569 $url = get_permalink( $recent['ID'] );
570 $post_url = rawurlencode( $url );
571 // Set transient name.
572 $post_hash = 'shariff' . hash( 'md5', $post_url );
573 // Get old share counts.
574 if ( get_transient( $post_hash ) !== false ) {
575 $old_share_counts = get_transient( $post_hash );
576 } else {
577 $old_share_counts = array();
578 }
579 // Fetch share counts and save them.
580 shariff3uu_fetch_sharecounts( $service_array, $old_share_counts, $post_hash, $url );
581 }
582 }
583 }
584 add_action( 'shariff3uu_fill_cache', 'shariff3uu_fill_cache' );
585
586 /**
587 * Adds schedule event in order to fill cache automatically.
588 */
589 function shariff3uu_fill_cache_schedule() {
590 // Get options manually bc of start on activation.
591 $shariff3uu_statistic = (array) get_option( 'shariff3uu_statistic' );
592 // Check if option is set.
593 if ( isset( $shariff3uu_statistic['automaticcache'] ) && 1 === $shariff3uu_statistic['automaticcache'] ) {
594 // Check if job is already scheduled.
595 if ( ! wp_next_scheduled( 'shariff3uu_fill_cache' ) ) {
596 // Add cron job.
597 wp_schedule_event( time(), 'weekly', 'shariff3uu_fill_cache' );
598 }
599 } else {
600 // Else option is not set therefore remove cron job if scheduled.
601 if ( wp_next_scheduled( 'shariff3uu_fill_cache' ) ) {
602 // Remove cron job.
603 wp_clear_scheduled_hook( 'shariff3uu_fill_cache' );
604 }
605 }
606 }
607 add_action( 'shariff3uu_save_statistic_options', 'shariff3uu_fill_cache_schedule' );
608
609 /** Registers activation hook to start cron job after an update. */
610 register_activation_hook( __FILE__ , 'shariff3uu_fill_cache_schedule' );
611
612 /**
613 * Adds custom weekly cron recurrences.
614 *
615 * @param array $schedules Array of existing schedules.
616 *
617 * @return array Updated array including our new schedule.
618 */
619 function shariff3uu_fill_cache_schedule_custom_recurrence( $schedules ) {
620 $schedules['weekly'] = array(
621 'display' => __( 'Once weekly', 'shariff' ),
622 'interval' => 604800,
623 );
624 return $schedules;
625 }
626 add_filter( 'cron_schedules', 'shariff3uu_fill_cache_schedule_custom_recurrence' );
627
628 /**
629 * Adds short tag to posts and pages (including custom post types).
630 *
631 * @param string $content The current post content.
632 *
633 * @return string The post content including our short tag.
634 */
635 function shariff3uu_posts( $content ) {
636 // Get options.
637 $shariff3uu = $GLOBALS['shariff3uu'];
638
639 // Do not add Shariff to excerpts or outside the loop, if option is checked.
640 if ( in_array( 'get_the_excerpt', $GLOBALS['wp_current_filter'], true ) || ( ! in_the_loop() && isset( $shariff3uu['disable_outside_loop'] ) && 1 === $shariff3uu['disable_outside_loop'] ) ) {
641 return $content;
642 }
643
644 // Disable share buttons on password protected posts if configured in the admin menu.
645 if ( ( 1 === post_password_required( get_the_ID() ) || ! empty( $GLOBALS['post']->post_password ) ) && isset( $shariff3uu['disable_on_protected'] ) && 1 === $shariff3uu['disable_on_protected'] ) {
646 $shariff3uu['add_before']['post'] = 0;
647 $shariff3uu['add_before']['blogpage'] = 0;
648 $shariff3uu['add_before']['page'] = 0;
649 $shariff3uu['add_after']['post'] = 0;
650 $shariff3uu['add_after']['blogpage'] = 0;
651 $shariff3uu['add_after']['page'] = 0;
652 $shariff3uu['add_after']['custom_type'] = 0;
653 }
654
655 // If we want to see it as text - replace the slash.
656 if ( false !== strpos( $content, '/hideshariff' ) ) {
657 $content = str_replace( '/hideshariff', 'hideshariff', $content );
658 } elseif ( false !== strpos( $content, 'hideshariff' ) ) {
659 // Remove the sign.
660 $content = str_replace( 'hideshariff', '', $content );
661 // Return the content without adding Shariff.
662 return $content;
663 }
664
665 // Type of current site.
666 $current_post_type = get_post_type();
667 // Prevent php warnings in debug mode.
668 $add_before = 0;
669 $add_after = 0;
670
671 // Check if shariff should be added automatically (plugin options).
672 if ( ! is_singular() ) {
673 // On blog page.
674 if ( isset( $shariff3uu['add_before']['blogpage'] ) && 1 === $shariff3uu['add_before']['blogpage'] ) {
675 $add_before = 1;
676 }
677 if ( isset( $shariff3uu['add_after']['blogpage'] ) && 1 === $shariff3uu['add_after']['blogpage'] ) {
678 $add_after = 1;
679 }
680 } elseif ( is_singular( 'post' ) || is_singular( 'page' ) ) {
681 // On single post or page.
682 if ( isset( $shariff3uu['add_before'][ $current_post_type ] ) && 1 === $shariff3uu['add_before'][ $current_post_type ] ) {
683 $add_before = 1;
684 }
685 if ( isset( $shariff3uu['add_after'][ $current_post_type ] ) && 1 === $shariff3uu['add_after'][ $current_post_type ] ) {
686 $add_after = 1;
687 }
688 } else {
689 // On custom_post_types.
690 $all_custom_post_types = get_post_types( array( '_builtin' => false ) );
691 if ( is_array( $all_custom_post_types ) ) {
692 $custom_types = array_keys( $all_custom_post_types );
693 // Add shariff, if custom type and option checked in the admin menu.
694 if ( isset( $shariff3uu['add_before'][ $current_post_type ] ) && 1 === $shariff3uu['add_before'][ $current_post_type ] ) {
695 $add_before = 1;
696 }
697 if ( isset( $shariff3uu['add_after'][ $current_post_type ] ) && 1 === $shariff3uu['add_after'][ $current_post_type ] ) {
698 $add_after = 1;
699 }
700 }
701 }
702
703 // Check if buttons are enabled on a single post or page via the meta box.
704 if ( get_post_meta( get_the_ID(), 'shariff_metabox_before', true ) ) {
705 $add_before = 1;
706 }
707 if ( get_post_meta( get_the_ID(), 'shariff_metabox_after', true ) ) {
708 $add_after = 1;
709 }
710
711 // Add shariff.
712 if ( 1 === $add_before ) {
713 $content = '[shariff]' . $content;
714 }
715 if ( 1 === $add_after ) {
716 $content .= '[shariff]';
717 }
718
719 // Return content.
720 return $content;
721 }
722 if ( ! isset( $GLOBALS['shariff3uu']['shortcodeprio'] ) ) {
723 $GLOBALS['shariff3uu']['shortcodeprio'] = '10';
724 }
725 add_filter( 'the_content', 'shariff3uu_posts', $GLOBALS['shariff3uu']['shortcodeprio'] );
726
727 /**
728 * Add shorttag to excerpt.
729 *
730 * @param string $content The current content of the excerpt.
731 *
732 * @return string The new content of the post including the shorttag.
733 */
734 function shariff3uu_excerpt( $content ) {
735 // Get options.
736 $shariff3uu = $GLOBALS['shariff3uu'];
737 // Remove headline in post.
738 if ( isset( $shariff3uu['headline'] ) ) {
739 $content = str_replace( wp_strip_all_tags( strip_hackers($shariff3uu['headline']) ), ' ', $content );
740 }
741 // Add shariff before the excerpt, if option checked in the admin menu.
742 if ( isset( $shariff3uu['add_before']['excerpt'] ) && 1 === $shariff3uu['add_before']['excerpt'] ) {
743 $content = do_shortcode( '[shariff]' ) . $content;
744 }
745 // Add shariff after the excerpt, if option checked in the admin menu.
746 if ( isset( $shariff3uu['add_after']['excerpt'] ) && 1 === $shariff3uu['add_after']['excerpt'] ) {
747 $content .= do_shortcode( '[shariff]' );
748 }
749 return $content;
750 }
751 add_filter( 'the_excerpt', 'shariff3uu_excerpt' );
752
753 /**
754 * Removes hideshariff from content in cases of excerpts or other plain text usages.
755 *
756 * @param string $content The current content of the post.
757 *
758 * @return string The modified content without hideshariff.
759 */
760 function shariff3uu_hideshariff( $content ) {
761 if ( true === strpos( $content, 'hideshariff' ) ) {
762 $content = str_replace( 'hideshariff', '', $content );
763 }
764 return $content;
765 }
766 add_filter( 'the_content', 'shariff3uu_hideshariff', 999 );
767
768 /**
769 * Removes shariff from rss feeds.
770 *
771 * @param string $content The current content of the post.
772 *
773 * @return string The modified content without shariff.
774 */
775 function shariff3uu_remove_from_rss( $content ) {
776 $content = preg_replace( '/<div class="shariff\b[^>]*>(.*?)<\/div>/i', '', $content );
777 $content = preg_replace( '/<div class="ShariffSC\b[^>]*>(.*?)<\/div>/i', '', $content );
778 return $content;
779 }
780 add_filter( 'the_content_feed', 'shariff3uu_remove_from_rss', 999 );
781
782 /**
783 * Adds shariff buttons after bbpress forums.
784 */
785 function shariff3uu_bbp_add_shariff_after_forum() {
786 // Get options.
787 $shariff3uu = $GLOBALS['shariff3uu'];
788 if ( isset( $shariff3uu['add_after']['forum'] ) && 1 === $shariff3uu['add_after']['forum'] ) {
789 // phpcs:ignore
790 echo shariff3uu_render( array() );
791 }
792 }
793 add_action( 'bbp_template_after_forums_loop', 'shariff3uu_bbp_add_shariff_after_forum' );
794
795 /**
796 * Adds shariff buttons after bbpress topics.
797 */
798 function shariff3uu_bbp_add_shariff_after_topic() {
799 // Get options.
800 $shariff3uu = $GLOBALS['shariff3uu'];
801 if ( isset( $shariff3uu['add_after']['topic'] ) && 1 === $shariff3uu['add_after']['topic'] ) {
802 // phpcs:ignore
803 echo shariff3uu_render( array() );
804 }
805 }
806 add_action( 'bbp_template_after_topics_loop', 'shariff3uu_bbp_add_shariff_after_topic' );
807
808 /**
809 * Adds shariff buttons after bbpress replies.
810 */
811 function shariff3uu_bbp_add_shariff_after_reply() {
812 // Get options.
813 $shariff3uu = $GLOBALS['shariff3uu'];
814 if ( isset( $shariff3uu['add_after']['reply'] ) && 1 === $shariff3uu['add_after']['reply'] ) {
815 // phpcs:ignore
816 echo shariff3uu_render( array() );
817 }
818 }
819 add_action( 'bbp_theme_after_reply_content', 'shariff3uu_bbp_add_shariff_after_reply' );
820
821 /**
822 * Adds shariff buttons before bbpress forums.
823 */
824 function shariff3uu_bbp_add_shariff_before_forum() {
825 // Get options.
826 $shariff3uu = $GLOBALS['shariff3uu'];
827 if ( isset( $shariff3uu['add_before']['forum'] ) && 1 === $shariff3uu['add_before']['forum'] ) {
828 // phpcs:ignore
829 echo shariff3uu_render( array() );
830 }
831 }
832 add_action( 'bbp_template_before_forums_loop', 'shariff3uu_bbp_add_shariff_before_forum' );
833
834 /**
835 * Adds shariff buttons before bbpress topics.
836 */
837 function shariff3uu_bbp_add_shariff_before_topic() {
838 // Get options.
839 $shariff3uu = $GLOBALS['shariff3uu'];
840 if ( isset( $shariff3uu['add_before']['topic'] ) && 1 === $shariff3uu['add_before']['topic'] ) {
841 // phpcs:ignore
842 echo shariff3uu_render( array() );
843 }
844 }
845 add_action( 'bbp_template_before_topics_loop', 'shariff3uu_bbp_add_shariff_before_topic' );
846
847 /**
848 * Adds shariff buttons before bbpress replies.
849 */
850 function shariff3uu_bbp_add_shariff_before_reply() {
851 // Get options.
852 $shariff3uu = $GLOBALS['shariff3uu'];
853 if ( isset( $shariff3uu['add_before']['reply'] ) && 1 === $shariff3uu['add_before']['reply'] ) {
854 // phpcs:ignore
855 echo shariff3uu_render( array() );
856 }
857 }
858 add_action( 'bbp_theme_before_reply_content', 'shariff3uu_bbp_add_shariff_before_reply' );
859
860 /**
861 * Adds shariff to custom hooks.
862 */
863 function shariff3uu_add_shariff_custom_hooks() {
864 // Get options.
865 $shariff3uu = $GLOBALS['shariff3uu'];
866
867 if ( isset( $shariff3uu['custom_hooks_shortcode'] ) && ! empty( $shariff3uu['custom_hooks_shortcode'] ) ) {
868 // Replaces shariff with shariffmeta.
869 $shariff3uu_custom_hooks_shortcode = str_replace( '[shariff ', '[shariffmeta ', $shariff3uu['custom_hooks_shortcode'] );
870
871 // Extracts attributes.
872 if ( '[shariffmeta]' !== $shariff3uu_custom_hooks_shortcode ) {
873 do_shortcode( $shariff3uu_custom_hooks_shortcode );
874 }
875
876 if ( isset( $GLOBALS['shariff3uu']['metabox'] ) && ! empty( $GLOBALS['shariff3uu']['metabox'] ) ) {
877 $shariff3uu_custom_hooks_shortcode = $GLOBALS['shariff3uu']['metabox'];
878 } else {
879 $shariff3uu_custom_hooks_shortcode = array();
880 }
881
882 // Clears the metabox global.
883 $GLOBALS['shariff3uu']['metabox'] = '';
884
885 // phpcs:ignore
886 echo shariff3uu_render( $shariff3uu_custom_hooks_shortcode );
887 } else {
888 // phpcs:ignore
889 echo shariff3uu_render( array() );
890 }
891 }
892 if ( isset( $shariff3uu['custom_hooks'] ) && ! empty( $shariff3uu['custom_hooks'] ) ) {
893 // Explodes hooks.
894 $hook_array = explode( '|', $shariff3uu['custom_hooks'] );
895
896 // Remove duplicated hooks.
897 $hook_array = array_unique( $hook_array );
898
899 // Loops through all desired hooks.
900 foreach ( $hook_array as $hook ) {
901 add_action( esc_html( $hook ), 'shariff3uu_add_shariff_custom_hooks' );
902 }
903 }
904
905 /**
906 * Function is called to include the shariff.css in the header of AMP pages.
907 * Currently only called by the amp_post_template_css hook.
908 * Supports the AMP plugin by Automatic and the AMP for WP plugin by Ahmed and Mohammed Kaludi.
909 * We need to strip out all !important in order to pass AMP test.
910 */
911 function shariff3uu_amp_css() {
912 // Get options.
913 $shariff3uu = $GLOBALS['shariff3uu'];
914 // Output CSS.
915 ob_start();
916 include dirname( __FILE__ ) . '/css/shariff.min.css';
917 $shariff_css = ob_get_clean();
918 if ( false !== $shariff_css ) {
919 echo esc_html( str_replace( '!important', '', $shariff_css ) );
920 } else {
921 include dirname( __FILE__ ) . '/css/shariff.min.css';
922 }
923 if ( array_key_exists( 'dynamic_css', $shariff3uu ) && ! empty( $shariff3uu['dynamic_css'] ) ) {
924 echo esc_html( $shariff3uu['dynamic_css'] );
925 }
926 }
927 add_action( 'amp_post_template_css', 'shariff3uu_amp_css' );
928
929 // Registers the shortcode.
930 add_shortcode( 'shariff', 'shariff3uu_render' );
931
932 /**
933 * Renders the shorttag to the HTML shorttag of Shariff.
934 *
935 * @param array $atts (optional) Array of shariff options provided in the shorttag.
936 *
937 * @return string|null The rendered HTML shorttag or null if a disable condition is met.
938 */
939 function shariff3uu_render( $atts ) {
940 // Get options as defaults from the DB.
941 $shariff3uu_basic = (array) get_option( 'shariff3uu_basic' );
942 $shariff3uu_design = (array) get_option( 'shariff3uu_design' );
943 $shariff3uu_advanced = (array) get_option( 'shariff3uu_advanced' );
944 $shariff3uu_statistic = (array) get_option( 'shariff3uu_statistic' );
945
946 // merge to a big array
947 $temp_db_options = array_merge( $shariff3uu_basic, $shariff3uu_design, $shariff3uu_advanced, $shariff3uu_statistic );
948
949 // compare shortcode atts with the options from the DB
950 // if not set in shortcode, take the DB as default value
951 $raw_atts = shortcode_atts( $temp_db_options, $atts, 'shariff' );
952
953 // clean all vars. It is a pity but with WP you can not trust DB content
954 $shariff3uu = array_merge(
955 shariff3uu_basic_sanitize( $raw_atts ),
956 shariff3uu_design_sanitize( $raw_atts ),
957 shariff3uu_advanced_sanitize( $raw_atts ),
958 shariff3uu_statistic_sanitize( $raw_atts )
959 );
960
961 if ( array_key_exists( 'dynamic_css', $shariff3uu ) && ! empty( $shariff3uu['dynamic_css'] ) ) {
962 $dynamic_css = $shariff3uu['dynamic_css'];
963 } else {
964 $dynamic_css = '';
965 }
966
967 // Stops all further actions if we are on an admin page.
968 if ( is_admin() && ! wp_doing_ajax() ) {
969 return null;
970 }
971
972 // Avoids errors if no attributes are given - instead uses the old set of services to make it backward compatible.
973 if ( empty( $shariff3uu['services'] ) ) {
974 $shariff3uu['services'] = 'mastodon|facebook|linkedin|info';
975 }
976
977 // Uses the backend option for every option that is not set in the shorttag.
978 $backend_options = $shariff3uu;
979 if ( isset( $shariff3uu['vertical'] ) && 1 === $shariff3uu['vertical'] ) {
980 $backend_options['orientation'] = 'vertical';
981 }
982 if ( isset( $shariff3uu['backend'] ) && 1 === $shariff3uu['backend'] ) {
983 $backend_options['backend'] = 'on';
984 }
985 if ( isset( $shariff3uu['buttonsize'] ) && 1 === $shariff3uu['buttonsize'] ) {
986 $backend_options['buttonsize'] = 'small';
987 }
988 if ( empty( $atts ) ) {
989 $atts = $backend_options;
990 } else {
991 $atts = array_merge( $backend_options, $atts );
992 }
993
994 // Gets the metabox ignore widget value.
995 $shariff_metabox_ignore_widget = get_post_meta( get_the_ID(), 'shariff_metabox_ignore_widget', true );
996
997 // Adds the meta box settings if it is not a widget or if it is a widget and not being set to be ignored.
998 if ( ( ! isset( $atts['widget'] ) || ( isset( $atts['widget'] ) && 1 === $atts['widget'] && 1 !== $shariff_metabox_ignore_widget ) ) && 'total' !== $atts['services'] && 'totalnumber' !== $atts['services'] ) {
999 // Gets the meta box disable value.
1000 $shariff3uu_metabox_disable = get_post_meta( get_the_ID(), 'shariff_metabox_disable', true );
1001
1002 // Stops all further actions if the meta box setting is set to disabled.
1003 if ( '1' === $shariff3uu_metabox_disable ) {
1004 return null;
1005 }
1006
1007 // Gets the meta box shortcode.
1008 $shariff3uu_metabox = get_post_meta( get_the_ID(), 'shariff_metabox', true );
1009
1010 // Replaces shariff with shariffmeta.
1011 $shariff3uu_metabox = str_replace( '[shariff ', '[shariffmeta ', $shariff3uu_metabox );
1012
1013 // Gets the meta box attributes.
1014 if ( '[shariffmeta]' !== $shariff3uu_metabox ) {
1015 do_shortcode( $shariff3uu_metabox );
1016 }
1017
1018 if ( isset( $GLOBALS['shariff3uu']['metabox'] ) && ! empty( $GLOBALS['shariff3uu']['metabox'] ) ) {
1019 $metabox = $GLOBALS['shariff3uu']['metabox'];
1020 } else {
1021 $metabox = array();
1022 }
1023
1024 // Gets the meta box media attribute.
1025 $shariff3uu_metabox_media = get_post_meta( get_the_ID(), 'shariff_metabox_media', true );
1026 if ( ! empty( $shariff3uu_metabox_media ) ) {
1027 $metabox['media'] = $shariff3uu_metabox_media;
1028 }
1029
1030 // Merges the meta box atts array with the atts array (meta box shortcode overrides all others).
1031 if ( ! empty( $metabox ) ) {
1032 $atts = array_merge( $atts, $metabox );
1033 }
1034
1035 // Clears the metabox global.
1036 $GLOBALS['shariff3uu']['metabox'] = '';
1037 } // End meta box if.
1038
1039 // Ov3rfly: Makes the attributes configurable from outside, e.g. for language etc.
1040 $atts = apply_filters( 'shariff3uu_render_atts', $atts );
1041
1042 // Removes empty elements.
1043 $atts = array_filter( $atts );
1044
1045 // Cleans up services (remove leading or trailing |, spaces, etc.).
1046 $atts['services'] = trim( preg_replace( '/[^A-Za-z|]/', '', $atts['services'] ), '|' );
1047
1048 // Cleans up the headline in case it was used in a shorttag.
1049 if ( array_key_exists( 'headline', $atts ) ) {
1050 $atts['headline'] = wp_kses( strip_hackers($atts['headline']), $GLOBALS['allowed_tags'] );
1051 }
1052
1053 // Cleans up the alternative headline in case it was used in a shorttag.
1054 if ( array_key_exists( 'headline_zero', $atts ) ) {
1055 $atts['headline_zero'] = wp_kses( strip_hackers($atts['headline_zero']), $GLOBALS['allowed_tags'] );
1056 }
1057
1058 // Remove previous added inline styles to prevent duplications.
1059 if ( wp_style_is( 'shariffcss', 'enqueued' ) ) {
1060 wp_deregister_style( 'shariffcss' );
1061 }
1062
1063 // Enqueues the stylesheet (loading it here makes sure that it is only loaded on pages that actually contain shariff buttons).
1064 // If SCRIPT_DEBUG is set to true, the non minified version will be loaded.
1065 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ) {
1066 $shariff_css_url = trailingslashit( plugins_url() ) . 'shariff/css/shariff.css';
1067 } else {
1068 $shariff_css_url = trailingslashit( plugins_url() ) . 'shariff/css/shariff.min.css';
1069 }
1070 wp_enqueue_style( 'shariffcss', $shariff_css_url, '', $shariff3uu['version'] );
1071
1072 // Enqueues the share count script (the JS should be loaded at the footer - make sure that wp_footer() is present in your theme!)
1073 // If SCRIPT_DEBUG is set to true, the non minified version will be loaded.
1074 if ( array_key_exists( 'backend', $atts ) && 'on' === $atts['backend'] ) {
1075 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ) {
1076 $shariff_js_url = trailingslashit( plugins_url() ) . 'shariff/js/shariff.js';
1077 } else {
1078 $shariff_js_url = trailingslashit( plugins_url() ) . 'shariff/js/shariff.min.js';
1079 }
1080 wp_enqueue_script( 'shariffjs', $shariff_js_url, '', $shariff3uu['version'], true );
1081 }
1082
1083 // Enqueues the popup script (the JS should be loaded at the footer - make sure that wp_footer() is present in your theme!).
1084 // If SCRIPT_DEBUG is set to true, the non minified version will be loaded.
1085 if ( array_key_exists( 'popup', $atts ) && 1 === $atts['popup'] ) {
1086 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true ) {
1087 $shariff_js_popup_url = trailingslashit( plugins_url() ) . 'shariff/js/shariff-popup.js';
1088 } else {
1089 $shariff_js_popup_url = trailingslashit( plugins_url() ) . 'shariff/js/shariff-popup.min.js';
1090 }
1091 wp_enqueue_script( 'shariff_popup', $shariff_js_popup_url, '', $shariff3uu['version'], true );
1092 }
1093
1094 // Sets the share url.
1095 if ( array_key_exists( 'url', $atts ) ) {
1096 $share_url = rawurlencode( $atts['url'] );
1097 } else {
1098 $share_url = rawurlencode( get_permalink() );
1099 }
1100
1101 // Sets the share title.
1102 if ( array_key_exists( 'title', $atts ) ) {
1103 $share_title = rawurlencode( wp_strip_all_tags( strip_hackers($atts['title']) ) );
1104 } else {
1105 $share_title = rawurlencode( wp_strip_all_tags( html_entity_decode( get_the_title(), ENT_COMPAT, 'UTF-8' ) ) );
1106 }
1107
1108 // Sets the transient name.
1109 $post_hash = 'shariff' . hash( 'md5', $share_url );
1110
1111 // Prevents php notices.
1112 $share_counts = array();
1113 $output = '';
1114
1115 // Gets the cached share counts.
1116 if ( array_key_exists( 'backend', $atts ) && 'on' === $atts['backend'] && get_transient( $post_hash ) !== false ) {
1117 $share_counts = get_transient( $post_hash );
1118 }
1119
1120 // Adds ShariffSC container including custom styles if a custom style attribute or class exists.
1121 if ( array_key_exists( 'style', $atts ) || array_key_exists( 'cssclass', $atts ) ) {
1122 $output .= '<div class="ShariffSC';
1123 if ( array_key_exists( 'cssclass', $atts ) ) {
1124 $output .= ' ' . esc_html( strip_hackers($atts['cssclass']) ) . '"';
1125 } else {
1126 $output .= '"';
1127 }
1128 if ( array_key_exists( 'style', $atts ) ) {
1129 $output .= ' style="' . esc_html( strip_hackers($atts['style']) ) . '"';
1130 }
1131 $output .= '>';
1132 }
1133
1134 // Sets the language using get_locale(), if no language is set or if automatic language selection is set as an option.
1135 if ( ! array_key_exists( 'lang', $atts ) || ( array_key_exists( 'autolang', $atts ) && 1 === $atts['autolang'] ) ) {
1136 $atts['lang'] = substr( get_locale(), 0, 2 );
1137 }
1138
1139 // Sets the default button share text.
1140 $default_button_text_array = array(
1141 'bg' => 'cподеляне',
1142 'cs' => 'sdílet',
1143 'da' => 'del',
1144 'de' => 'teilen',
1145 'en' => 'share',
1146 'es' => 'compartir',
1147 'fi' => 'Jaa',
1148 'fr' => 'partager',
1149 'hr' => 'podijelite',
1150 'hu' => 'megosztás',
1151 'it' => 'condividi',
1152 'ja' => '�
1153 �有',
1154 'ko' => '공유하기',
1155 'nl' => 'delen',
1156 'no' => 'del',
1157 'pl' => 'udostępnij',
1158 'pt' => 'compartilhar',
1159 'ro' => 'partajează',
1160 'ru' => 'поделиться',
1161 'sk' => 'zdieľať',
1162 'sl' => 'deli',
1163 'sr' => 'podeli',
1164 'sv' => 'dela',
1165 'tr' => 'paylaş',
1166 'zh' => '分享',
1167 );
1168
1169 // Adds the timestamp for the cache.
1170 if ( array_key_exists( 'timestamp', $atts ) ) {
1171 $post_timestamp = absint( $atts['timestamp'] );
1172 } else {
1173 $post_timestamp = absint( get_the_modified_date( 'U' ) );
1174 }
1175
1176 // Starts the output of the actual Shariff buttons.
1177 $output .= '<div class="shariff';
1178 // Alignment.
1179 if ( array_key_exists( 'align', $atts ) && 'none' !== $atts['align'] ) {
1180 $output .= ' shariff-align-' . esc_attr( strip_hackers($atts['align']) );
1181 }
1182 // Alignment widget.
1183 if ( array_key_exists( 'align_widget', $atts ) && 'none' !== $atts['align_widget'] ) {
1184 $output .= ' shariff-widget-align-' . esc_attr( strip_hackers($atts['align_widget']) );
1185 }
1186 // Button Stretch.
1187 // phpcs:ignore
1188 if ( array_key_exists( 'buttonstretch', $atts ) && 1 === $atts['buttonstretch'] ) {
1189 $output .= ' shariff-buttonstretch';
1190 }
1191 $output .= '"';
1192
1193 // Hides buttons until css is loaded.
1194 if ( array_key_exists( 'hideuntilcss', $atts ) && 1 === $atts['hideuntilcss'] && ( ! function_exists( 'is_amp_endpoint' ) || ( function_exists( 'is_amp_endpoint' ) && false === is_amp_endpoint() ) ) ) {
1195 $output .= ' style="display:none"';
1196 }
1197 // Adds information for share count request.
1198 if ( array_key_exists( 'backend', $atts ) && 'on' === $atts['backend'] ) {
1199 // Share url.
1200 $output .= ' data-url="' . esc_html( strip_hackers($share_url) ) . '"';
1201 // Timestamp for cache.
1202 $output .= ' data-timestamp="' . esc_attr( $post_timestamp ) . '"';
1203 // Hides share counts when they are zero.
1204 if ( isset( $atts['hidezero'] ) && 1 === $atts['hidezero'] ) {
1205 $output .= ' data-hidezero="1"';
1206 }
1207 // Add external api if entered, elseif test the subapi setting, elseif pretty permalinks are not activated fall back to manual rest route, else use the home url.
1208 if ( isset( $shariff3uu['external_host'] ) && ! empty( $shariff3uu['external_host'] ) && isset( $shariff3uu['external_direct'] ) ) {
1209 $output .= ' data-backendurl="' . esc_attr($shariff3uu['external_host']) . '"';
1210 } elseif ( isset( $shariff3uu['subapi'] ) && 1 === $shariff3uu['subapi'] ) {
1211 $output .= ' data-backendurl="' . strtok( get_bloginfo( 'wpurl' ), '?' ) . '/wp-json/shariff/v1/share_counts?"';
1212 } elseif ( ! get_option( 'permalink_structure' ) ) {
1213 $output .= ' data-backendurl="?rest_route=/shariff/v1/share_counts&"';
1214 } else {
1215 $output .= ' data-backendurl="' . rtrim( strtok( home_url(), '?' ), '/' ) . '/wp-json/shariff/v1/share_counts?"';
1216 }
1217 }
1218 $output .= '>';
1219
1220 // Adds the headline.
1221 if ( array_key_exists( 'headline', $atts ) ) {
1222 if ( ! array_key_exists( 'total', $share_counts ) ) {
1223 $share_counts['total'] = 0;
1224 }
1225 if ( 0 === $share_counts['total'] && array_key_exists( 'headline_zero', $atts ) && ! empty( $atts['headline_zero'] ) ) {
1226 $atts['headline_zero'] = str_replace( '%total', '<span class="shariff-total">' . absint( $share_counts['total'] ) . '</span>', $atts['headline_zero'] );
1227 $output .= '<div class="ShariffHeadline">' . $atts['headline_zero'] . '</div>';
1228 } else {
1229 $atts['headline'] = str_replace( '%total', '<span class="shariff-total">' . absint( $share_counts['total'] ) . '</span>', $atts['headline'] );
1230 $output .= '<div class="ShariffHeadline">' . $atts['headline'] . '</div>';
1231 }
1232 }
1233
1234 // Start the ul list with design classes.
1235 $output .= '<ul class="shariff-buttons ';
1236 // Theme.
1237 if ( array_key_exists( 'theme', $atts ) && 'wcag' !== $atts['theme'] ) {
1238 $output .= 'theme-' . esc_html( $atts['theme'] ) . ' ';
1239 } else {
1240 $output .= 'theme-default ';
1241 if ( array_key_exists( 'theme', $atts ) && 'wcag' === $atts['theme'] ) {
1242 $output .= 'wcag_colors ';
1243 }
1244 }
1245 // Orientation.
1246 if ( array_key_exists( 'orientation', $atts ) ) {
1247 $output .= 'orientation-' . esc_html( $atts['orientation'] ) . ' ';
1248 } else {
1249 $output .= 'orientation-horizontal ';
1250 }
1251 // Size.
1252 if ( array_key_exists( 'buttonsize', $atts ) ) {
1253 $output .= 'buttonsize-' . esc_html( $atts['buttonsize'] );
1254 } else {
1255 $output .= 'buttonsize-medium';
1256 }
1257 $output .= '">';
1258
1259 // Prevents warnings while debug mode is on.
1260 $flattr_error = '';
1261 $paypal_error = '';
1262 $paypalme_error = '';
1263 $bitcoin_error = '';
1264 $patreon_error = '';
1265 $button_text_array = '';
1266 $backend_available = '';
1267 $mobile_only = '';
1268 $button_url = '';
1269 $border_radius = '';
1270
1271 // Explodes services.
1272 $service_array = explode( '|', $atts['services'] );
1273
1274 // Migrates mail to mailto.
1275 $service_array = preg_replace( '/\bmail\b/', 'mailto', $service_array );
1276
1277 // Migrates mailform to mailto.
1278 $service_array = preg_replace( '/\bmailform\b/', 'mailto', $service_array );
1279
1280 // Remove duplicated services.
1281 $service_array = array_unique( $service_array );
1282
1283 // Loops through all desired services.
1284 foreach ( $service_array as $service ) {
1285 // Check if necessary usernames are set and display warning to admins, if needed.
1286 if ( 'paypal' === $service && ! array_key_exists( 'paypalbuttonid', $atts ) ) {
1287 $paypal_error = 1;
1288 } elseif ( 'paypalme' === $service && ! array_key_exists( 'paypalmeid', $atts ) ) {
1289 $paypalme_error = 1;
1290 } elseif ( 'bitcoin' === $service && ! array_key_exists( 'bitcoinaddress', $atts ) ) {
1291 $bitcoin_error = 1;
1292 } elseif ( 'patreon' === $service && ! array_key_exists( 'patreonid', $atts ) ) {
1293 $patreon_error = 1;
1294 } elseif ( 'total' !== $service && 'totalnumber' !== $service ) {
1295
1296 // Only the frontend part is needed.
1297 $frontend = 1;
1298
1299 // Determines the path to the service phps.
1300 $path_service_file = dirname( __FILE__ ) . '/services/shariff-' . $service . '.php';
1301
1302 // Checks if service file exists.
1303 if ( file_exists( $path_service_file ) ) {
1304
1305 // Includes service file.
1306 include $path_service_file;
1307
1308 // Replace $main_color with $wcag_color and $secondary_color with $wcag_secondary, if $wacg_theme is selected.
1309 if ( isset( $atts['theme'] ) && 'wcag' === $atts['theme'] && isset( $wcag_color ) && '' !== $wcag_color ) {
1310 $main_color = esc_attr( $wcag_color );
1311 $secondary_color = '#000';
1312 }
1313
1314 // Info button for default theme.
1315 if ( ! array_key_exists( 'maincolor', $atts ) && 'info' === $service && ( ( array_key_exists( 'theme', $atts ) && 'default' === $atts['theme'] ) || ( array_key_exists( 'theme', $atts ) && 'round' === $atts['theme'] ) || ( array_key_exists( 'theme', $atts ) && 'wcag' === $atts['theme'] ) || ! array_key_exists( 'theme', $atts ) ) ) {
1316 $main_color = '#fff';
1317 $secondary_color = '#eee';
1318 }
1319
1320 // Start <li.
1321 $output .= '<li class="shariff-button ' . esc_attr( $service ) ;
1322
1323 // No custom colors.
1324 if ( ! array_key_exists( 'maincolor', $atts ) ) {
1325 $output .= ' shariff-nocustomcolor';
1326 }
1327
1328 // Mobile only.
1329 if ( 1 === $mobile_only ) {
1330 $output .= ' shariff-mobile';
1331 }
1332
1333 // AMP?
1334 if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
1335 // Custom colors?
1336 if ( array_key_exists( 'secondarycolor', $atts ) ) {
1337 $output .= ' shariff-secondary-color';
1338 $css = '.shariff-secondary-color{background-color:' . esc_attr( $atts['secondarycolor'] ) . '}';
1339 } else {
1340 $output .= ' shariff-' . esc_attr( $service ) . '-secondary-color';
1341 $css = '.shariff-' . esc_attr( $service ) . '-secondary-color{background-color:' . $secondary_color . '}';
1342 }
1343 if ( false === strpos( $dynamic_css, $css ) ) {
1344 $dynamic_css .= $css;
1345 }
1346 // Border radius?
1347 if ( array_key_exists( 'borderradius', $atts ) && array_key_exists( 'theme', $atts ) && 'round' === $atts['theme'] ) {
1348 $output .= ' shariff-borderradius';
1349 $css = '.shariff .shariff-buttons.theme-round .shariff-borderradius{border-radius:' . $atts['borderradius'] . '%}';
1350 if ( false === strpos( $dynamic_css, $css ) ) {
1351 $dynamic_css .= $css;
1352 }
1353 }
1354 } else {
1355 $output .= '" style="';
1356 // Custom colors?
1357 if ( array_key_exists( 'secondarycolor', $atts ) ) {
1358 $output .= 'background-color:' . esc_attr( $atts['secondarycolor'] );
1359 } else {
1360 $output .= 'background-color:' . $secondary_color;
1361 }
1362 // Border radius?
1363 if ( array_key_exists( 'borderradius', $atts ) && array_key_exists( 'theme', $atts ) && 'round' === $atts['theme'] && is_numeric( $atts['borderradius'] ) ) {
1364 $output .= ';border-radius:' . $atts['borderradius'] . '%';
1365 $border_radius = ';border-radius:' . $atts['borderradius'] . '%';
1366 }
1367 }
1368
1369 // End li>.
1370 $output .= '">';
1371
1372 // Uses default button share text, if $button_text_array is empty.
1373 if ( empty( $button_text_array ) ) {
1374 $button_text_array = $default_button_text_array;
1375 }
1376
1377 // Sets button text in desired language, first fallback is default language set in options, second one is English.
1378 if ( array_key_exists( 'lang', $atts ) && array_key_exists( $atts['lang'], $button_text_array ) ) {
1379 $button_text = $button_text_array[ $atts['lang'] ];
1380 } elseif ( isset( $shariff3uu['lang'] ) && ! empty( $shariff3uu['lang'] ) && array_key_exists( $shariff3uu['lang'], $button_text_array ) ) {
1381 $button_text = $button_text_array[ $shariff3uu['lang'] ];
1382 } else {
1383 $button_text = $button_text_array['en'];
1384 }
1385
1386 // Sets the button title / label in desired language; fallback is English.
1387 if ( array_key_exists( 'lang', $atts ) && array_key_exists( $atts['lang'], $button_title_array ) ) {
1388 $button_title = $button_title_array[ $atts['lang'] ];
1389 } else {
1390 $button_title = $button_title_array['en'];
1391 }
1392
1393 // Resets $button_text_array.
1394 $button_text_array = '';
1395
1396 /** Build the actual button. */
1397
1398 // Begin <a.
1399 $output .= '<a ';
1400
1401 // Check if we are on an AMP page and the print button is requested.
1402 if ( 'printer' === $service && function_exists( 'is_amp_endpoint' ) && true === is_amp_endpoint() ) {
1403 $output .= 'on="tap:AMP.print" ';
1404 } else {
1405 $output .= 'href="' . $button_url . '" ';
1406 }
1407
1408 // Output title, label and role.
1409 $output .= 'title="' . $button_title . '" aria-label="' . $button_title . '" role="button"';
1410 if ( 'rss' !== $service ) {
1411 $output .= ' rel="';
1412 if ( 'facebook' !== $service ) {
1413 $output .= 'noopener ';
1414 }
1415 if ( 'info' !== $service ) {
1416 $output .= 'nofollow';
1417 }
1418 $output .= '"';
1419 }
1420 $output .= ' class="shariff-link';
1421
1422 // AMP?
1423 if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
1424 $output .= ' shariff-borderradius';
1425 // Custom color?
1426 if ( array_key_exists( 'maincolor', $atts ) ) {
1427 $output .= ' shariff-main-color';
1428 $css = '.shariff-main-color{background-color:' . esc_attr( $atts['maincolor'] ) . '}';
1429 } else {
1430 $output .= ' shariff-' . $service . '-main-color';
1431 $css = '.shariff-' . $service . '-main-color{background-color:' . $main_color . '}';
1432 }
1433 if ( false === strpos( $dynamic_css, $css ) ) {
1434 $dynamic_css .= $css;
1435 }
1436 // Theme white?
1437 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1438 if ( array_key_exists( 'maincolor', $atts ) ) {
1439 $output .= ' shariff-text-color"';
1440 $css = '.shariff-text-color{color:' . esc_attr( $atts['maincolor'] ) . '}';
1441 } else {
1442 $output .= ' shariff-' . $service . '-text-color"';
1443 $css = '.shariff-' . $service . '-text-color{color:' . $main_color . '}';
1444 }
1445 if ( false === strpos( $dynamic_css, $css ) ) {
1446 $dynamic_css .= $css;
1447 }
1448 } else {
1449 $output .= ' shariff-text-white"';
1450 }
1451 } else {
1452 // Border radius.
1453 $output .= '" style="' . $border_radius;
1454 // Custom color?
1455 if ( array_key_exists( 'maincolor', $atts ) ) {
1456 $output .= '; background-color:' . esc_attr( $atts['maincolor'] );
1457 } else {
1458 $output .= '; background-color:' . $main_color;
1459 }
1460 // Theme white?
1461 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1462 if ( array_key_exists( 'maincolor', $atts ) ) {
1463 $output .= '; color:' . esc_attr( $atts['maincolor'] ) . '"';
1464 } else {
1465 $output .= '; color:' . $main_color . '"';
1466 }
1467 } else {
1468 $output .= '; color:#fff"';
1469 }
1470 }
1471
1472 // Same window?
1473 if ( ! isset( $same_window ) || isset( $same_window ) && 1 !== $same_window ) {
1474 $output .= ' target="_blank"';
1475 }
1476
1477 // End a>.
1478 $output .= '>';
1479
1480 // Shariff icon.
1481 $output .= '<span class="shariff-icon';
1482 // AMP?
1483 if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
1484 // Theme white?
1485 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1486 if ( array_key_exists( 'maincolor', $atts ) ) {
1487 $output .= ' shariff-svg-color';
1488 $css = '.shariff-svg-color{fill:' . esc_attr( $atts['maincolor'] ) . '}';
1489 } else {
1490 $output .= ' shariff-' . $service . '-svg-color';
1491 $css = '.shariff-' . $service . '-svg-color{fill:' . $main_color . '}';
1492 }
1493 if ( false === strpos( $dynamic_css, $css ) ) {
1494 $dynamic_css .= $css;
1495 }
1496 }
1497 } else {
1498 $output .= '" style="';
1499 // Theme white?
1500 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1501 if ( array_key_exists( 'maincolor', $atts ) ) {
1502 $output .= 'fill:' . esc_attr( $atts['maincolor'] );
1503 } else {
1504 $output .= 'fill:' . $main_color;
1505 }
1506 }
1507 }
1508 $output .= '">' . $svg_icon . '</span>';
1509
1510 // Shariff text.
1511 if ( ( ! isset( $atts['theme'] ) || ( isset( $atts['theme'] ) && 'round' !== $atts['theme'] ) ) && 'info' !== $service ) {
1512 $output .= '<span class="shariff-text';
1513 // AMP?
1514 if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
1515 // Theme white?
1516 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1517 if ( array_key_exists( 'maincolor', $atts ) ) {
1518 $output .= ' shariff-text-color';
1519 $css = '.shariff-text-color{color:' . esc_attr( $atts['maincolor'] ) . '}';
1520 } else {
1521 $output .= ' shariff-' . $service . '-text-color';
1522 $css = '.shariff-' . $service . '-text-color{color:' . $main_color . '}';
1523 }
1524 if ( false === strpos( $dynamic_css, $css ) ) {
1525 $dynamic_css .= $css;
1526 }
1527 }
1528 } else {
1529 // Theme white?
1530 if ( isset( $atts['theme'] ) && 'white' === $atts['theme'] ) {
1531 if ( array_key_exists( 'maincolor', $atts ) ) {
1532 $output .= '" style="color:' . esc_attr( $atts['maincolor'] );
1533 } else {
1534 $output .= '" style="color:' . $main_color;
1535 }
1536 }
1537 }
1538 $output .= '">' . $button_text . '</span>&nbsp;';
1539 }
1540
1541 // Share counts?
1542 if ( array_key_exists( 'sharecounts', $atts ) && 1 === $atts['sharecounts'] && 1 === $backend_available && ! isset( $shariff3uu['disable'][ $service ] ) ) {
1543 $output .= '<span data-service="' . $service . '"';
1544 // AMP?
1545 if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
1546 if ( array_key_exists( 'maincolor', $atts ) ) {
1547 $output .= ' class="shariff-count shariff-text-color"';
1548 $css = '.shariff-text-color{color:#fff}';
1549 } else {
1550 $output .= ' class="shariff-count shariff-' . $service . '-text-color';
1551 $css = '.shariff-' . $service . '-text-color{color:' . $main_color . '}';
1552 }
1553 if ( false === strpos( $dynamic_css, $css ) ) {
1554 $dynamic_css .= $css;
1555 }
1556 } else {
1557 if ( array_key_exists( 'maincolor', $atts ) ) {
1558 $output .= ' style="color:#fff" class="shariff-count';
1559
1560 } else {
1561 $output .= ' style="color:' . $main_color . '" class="shariff-count';
1562
1563 }
1564 }
1565 // Hide zero?
1566 if ( true === array_key_exists( $service, $share_counts ) && null !== $share_counts[ $service ] && '-1' !== $share_counts[ $service ] && ( ! isset( $atts['hidezero'] ) || ( isset( $atts['hidezero'] ) && 1 !== $atts['hidezero'] ) || ( isset( $atts['hidezero'] ) && 1 === $atts['hidezero'] && $share_counts[ $service ] > 0 ) ) ) {
1567 $output .= '"> ' . $share_counts[ $service ];
1568 } else {
1569 $output .= ' shariff-hidezero">';
1570 }
1571 $output .= '</span>&nbsp;';
1572 }
1573 $output .= '</a>';
1574 $output .= '</li>';
1575
1576 // Adds service to backend service, if available.
1577 if ( 1 === $backend_available && ! isset( $shariff3uu['disable'][ $service ] ) ) {
1578 $backend_service_array[] = $service;
1579 }
1580
1581 // Reset variables.
1582 $backend_available = '';
1583 $mobile_only = '';
1584 $same_window = '';
1585 $wcag_color = '';
1586 } // End if service file exists.
1587 } // End if is real and fully setup service.
1588 } // End foreach() loop.
1589
1590 // Adds the list of backend services.
1591 if ( ! empty( $backend_service_array ) ) {
1592 $backend_services = implode( '|', $backend_service_array );
1593 $output = str_replace( 'data-url=', 'data-services="' . esc_html( rawurlencode( $backend_services ) ) . '" data-url=', $output );
1594 }
1595
1596 // Closes ul and the main shariff div.
1597 $output .= '</ul></div>';
1598
1599 // Closes the style attribute, if there was one.
1600 if ( array_key_exists( 'style', $atts ) || array_key_exists( 'cssclass', $atts ) ) {
1601 $output .= '</div>';
1602 }
1603
1604 // Update the dynamic css db entry if needed.
1605 if ( ( array_key_exists( 'dynamic_css', $shariff3uu ) && $shariff3uu['dynamic_css'] !== $dynamic_css ) || ! array_key_exists( 'dynamic_css', $shariff3uu ) ) {
1606 $shariff3uu_design = array_filter( $GLOBALS['shariff3uu_design'] );
1607 $shariff3uu_design['dynamic_css'] = $dynamic_css;
1608 $GLOBALS['shariff3uu']['dynamic_css'] = $dynamic_css;
1609 update_option( 'shariff3uu_design', $shariff3uu_design );
1610 }
1611
1612 // Displays a warning to admins if patreon is set, but no patreon username was provided.
1613 if ( 1 === $patreon_error && current_user_can( 'manage_options' ) ) {
1614 $output .= '<div class="shariff-warning">' . __( 'Username for patreon is missing!', 'shariff' ) . '</div>';
1615 }
1616 // Displays a warning to admins if paypal is set, but no paypal button id was provided.
1617 if ( 1 === $paypal_error && current_user_can( 'manage_options' ) ) {
1618 $output .= '<div class="shariff-warning">' . __( 'Button ID for PayPal is missing!', 'shariff' ) . '</div>';
1619 }
1620 // Displays a warning to admins if paypalme is set, but no paypalme id was provided.
1621 if ( 1 === $paypalme_error && current_user_can( 'manage_options' ) ) {
1622 $output .= '<div class="shariff-warning">' . __( 'PayPal.Me ID is missing!', 'shariff' ) . '</div>';
1623 }
1624 // Displays a warning to admins if bitcoin is set, but no bitcoin address was provided.
1625 if ( 1 === $bitcoin_error && current_user_can( 'manage_options' ) ) {
1626 $output .= '<div class="shariff-warning">' . __( 'Address for Bitcoin is missing!', 'shariff' ) . '</div>';
1627 }
1628
1629 // If the service totalnumber is set, just output the total share count.
1630 if ( array_key_exists( '0', $service_array ) && 'totalnumber' === $service_array['0'] ) {
1631 $output = '<span class="shariff" data-services="totalnumber" data-url="' . $share_url . '"';
1632 // Adds the external api.
1633 if ( isset( $shariff3uu['external_host'] ) && ! empty( $shariff3uu['external_host'] ) && isset( $shariff3uu['external_direct'] ) ) {
1634 $output .= ' data-backendurl="' . esc_attr($shariff3uu['external_host']) . '"';
1635 }
1636 $output .= '><span class="shariff-totalnumber">' . absint( $share_counts['total'] ) . '</span></span>';
1637 }
1638
1639 return $output;
1640 }
1641
1642 // Registers the helper shortcode.
1643 add_shortcode( 'shariffmeta', 'shariff3uu_meta' );
1644
1645 /**
1646 * Meta box helper function. Creates a global variable with the current meta box attributes.
1647 *
1648 * @param array $atts List of shariff shortcode attributes.
1649 */
1650 function shariff3uu_meta( $atts ) {
1651 if ( ! empty( $atts ) ) {
1652 $GLOBALS['shariff3uu']['metabox'] = $atts;
1653 } else {
1654 $GLOBALS['shariff3uu']['metabox'] = array();
1655 }
1656 }
1657
1658 /**
1659 * Clears transients and removes cron job upon deactivation.
1660 */
1661 function shariff3uu_deactivate() {
1662 // Checks for multisite.
1663 if ( is_multisite() && function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {
1664 $sites = get_sites();
1665 foreach ( $sites as $site ) {
1666 switch_to_blog( $site->blog_id );
1667 // Purges transients.
1668 shariff3uu_purge_transients_deactivation();
1669 // Removes the cron job.
1670 wp_clear_scheduled_hook( 'shariff3uu_fill_cache' );
1671 // Switches back to main blog.
1672 restore_current_blog();
1673 }
1674 } else {
1675 // Purges transients.
1676 shariff3uu_purge_transients_deactivation();
1677 // Removes cron job.
1678 wp_clear_scheduled_hook( 'shariff3uu_fill_cache' );
1679 }
1680 }
1681 register_deactivation_hook( __FILE__ , 'shariff3uu_deactivate' );
1682
1683 /**
1684 * Purges all the transients associated with our plugin.
1685 */
1686 function shariff3uu_purge_transients_deactivation() {
1687 // Makes sure the $wpdb class is ready.
1688 if ( ! isset( $wpdb ) ) {
1689 global $wpdb;
1690 }
1691 // Deletes transients.
1692 // phpcs:disable
1693 $wpdb->query( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_timeout_shariff%"' );
1694 $wpdb->query( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE "_transient_shariff%"' );
1695 // phpcs:enable
1696 // Clears the object cache.
1697 wp_cache_flush();
1698 }
1699