PluginProbe
Shariff Wrapper / trunk
Shariff Wrapper vtrunk
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 trunk, at shariff.php

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