PluginProbe
Shariff Wrapper / 4.6.19
Shariff Wrapper v4.6.19
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.19, at shariff.php

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