PluginProbe
Shariff Wrapper / 4.6.7
Shariff Wrapper v4.6.7
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.7, at shariff.php

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