PluginProbe
Shariff Wrapper / 4.6.9
Shariff Wrapper v4.6.9
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.9, at shariff.php

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