$two_months_period ) { // Post older than 60 days - expire cache after 12 hours. $seconds = apply_filters( 'powerkit_share_buttons_refresh_60_days', 43200 ); } elseif ( isset( $post_age ) && $post_age > $three_weeks_period ) { // Post older than 21 days - expire cache after 4 hours. $seconds = apply_filters( 'powerkit_share_buttons_refresh_21_days', 14400 ); } else { // Expire cache after one hour. $seconds = apply_filters( 'powerkit_share_buttons_refresh_1_hour', 3600 ); } return $seconds; } /** * Get Current Post ID * * @since 1.0.0 * @param string $url Custom URL. */ function powerkit_share_buttons_get_current_post_id( $url ) { // Custom URL. if ( $url ) { $postid = url_to_postid( $url ); if ( $postid ) { return $postid; } return 'options'; } // Auto. global $post; if ( isset( $post->ID ) ) { return $post->ID; } else { return 'options'; } } /** * Get Share URL * * @param string|int $post_id Post ID. * @param string $url Custom URL. */ function powerkit_share_buttons_get_url( $post_id = false, $url = null ) { // Custom URL. if ( $url ) { return $url; } // Auto URL. if ( ! $post_id ) { $post_id = powerkit_share_buttons_get_current_post_id( $url ); } if ( 'options' === $post_id ) { return preg_replace( '/\?.*/', '', home_url( add_query_arg( null, null ) ) ); } else { return get_permalink( intval( $post_id ) ); } } /** * Create a new Bitly short URL * * This is the method used to interface with the Bitly API with regard to creating * new shortened URL's via their service. * * @param string $api_token The bitly api token. * @param string $url The URL to be shortened. * @param string $title The post title. */ function powerkit_share_buttons_make_bitly_url( $api_token, $url, $title = null ) { $data = wp_remote_post( 'https://api-ssl.bitly.com/v4/bitlinks', array( 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Authorization' => 'Bearer ' . $api_token, ), 'body' => wp_json_encode( array( 'title' => $title, 'long_url' => $url, ) ), 'timeout' => 60, ) ); if ( is_wp_error( $data ) ) { return false; } // Retrieve data. $data = wp_remote_retrieve_body( $data ); // Parse the JSON formated response into an array. $data = json_decode( $data, true ); // If the shortening succeeded.... if ( isset( $data['link'] ) ) : // Store the short URL to return to the plugin. $short_url = $data['link']; // If the shortening failed.... else : // Return a status of false. $short_url = false; endif; return $short_url; } /** * Process URL * * @param string $account Account Name. * @param string|int $post_id Post ID. * @param string $url Custom URL. */ function powerkit_share_buttons_process_url( $account, $post_id = false, $url = null ) { // Check post id. if ( ! (int) $post_id ) { return $url; } // Check url. if ( ! $url ) { $url = get_permalink( (int) $post_id ); } $title = get_the_title( $post_id ); // Bitly Link Shortening. $bitly_api_token = get_option( 'powerkit_share_buttons_bitly_api_token' ); if ( $bitly_api_token ) { // These can not have bitly urls created. if ( 'pinterest' === $account ) { return $url; } $bitly_cache_name = sprintf( '_powerkit_bitly_url_%s', md5( $url ) ); // Get bitly from DB. $bitly_url = get_post_meta( $post_id, $bitly_cache_name, true ); // Get bitly url. if ( ! $bitly_url ) { $bitly_url = powerkit_share_buttons_make_bitly_url( $bitly_api_token, $url, $title ); if ( $bitly_url ) { update_post_meta( $post_id, $bitly_cache_name, $bitly_url ); } } // Return bitly url. if ( $bitly_url ) { $url = $bitly_url; } } return $url; } /** * Get Alternate Share URL * * @param string $url Custom URL. */ function powerkit_share_buttons_get_alt_url( $url = null ) { if ( false === strpos( $url, 'https' ) ) { return str_replace( 'http', 'https', $url ); } else { return str_replace( 'https', 'http', $url ); } } /** * Get Cached Counts * * @param string $account Account Name. * @param string|int $post_id Post ID. * @param string $url Custom URL. * @param bool $ignore_time Ignore Cache Time. * @param bool $suffix Unique suffix. */ function powerkit_share_buttons_get_cached_account( $account, $post_id, $url = null, $ignore_time = false, $suffix = false ) { // Add suffix. if ( $suffix ) { $account .= '_' . $suffix; } if ( 'options' === $post_id ) { // Get Url Shares. $share_url = powerkit_share_buttons_get_url( $post_id, $url ); $shares = get_transient( substr( 'powerkit_share_buttons_count_' . $account . '_' . md5( $share_url ), 0, 170 ) ); $shares = apply_filters( 'powerkit_share_buttons_count', $shares, $account, $post_id ); } else { // Get Post Shares. $shares = false; if ( $ignore_time ) { $shares = get_post_meta( intval( $post_id ), 'powerkit_share_buttons_count_' . $account, true ); } else { $share_transient = get_post_meta( intval( $post_id ), 'powerkit_share_buttons_transient_' . $account, true ); if ( intval( date( 'U' ) ) < intval( $share_transient ) ) { $shares = get_post_meta( intval( $post_id ), 'powerkit_share_buttons_count_' . $account, true ); } } $shares = apply_filters( 'powerkit_share_buttons_count', $shares, $account, $post_id ); } return $shares; } /** * Set Cache Counts * * @since 1.0.0 * @param string $account Account Name. * @param string|int $post_id Post ID. * @param int $count Shares Count. * @param string $url Custom URL. * @param bool $suffix Unique suffix. */ function powerkit_share_buttons_set_cache_account( $account, $post_id, $count, $url = null, $suffix = false ) { // Get Cache Time. $cache_time = powerkit_share_buttons_get_cache_time( $post_id ); // Add suffix. if ( $suffix ) { $account .= '_' . $suffix; } // Set Cache. if ( 'options' === $post_id ) { // Set Url Shares Count. $share_url = powerkit_share_buttons_get_url( $post_id, $url ); set_transient( substr( 'powerkit_share_buttons_count_' . $account . '_' . md5( $share_url ), 0, 170 ), $count, $cache_time ); } else { // Set Post Shares Count. $cache_time = powerkit_share_buttons_get_cache_time( $post_id ) + intval( date( 'U' ) ); update_post_meta( intval( $post_id ), 'powerkit_share_buttons_transient_' . $account, $cache_time ); if ( $count ) { update_post_meta( intval( $post_id ), 'powerkit_share_buttons_count_' . $account, $count ); } } return false; } /** * Get Accounts Data * * @since 1.0.0 * @param string $account Account Name. * @param string|int $post_id Post ID. * @param string $url Custom URL. */ function powerkit_share_buttons_get_accounts( $account = false, $post_id = false, $url = null ) { $post_id = (int) $post_id; if ( $post_id <= 0 ) { $post_id = false; } // Get Current Share URL. $share_url = powerkit_share_buttons_get_url( $post_id, $url ); // Get All Accounts. $all_accounts = apply_filters( 'powerkit_share_buttons_accounts', array(), $share_url, $post_id ); if ( ! $account ) { return $all_accounts; } elseif ( isset( $all_accounts[ $account ] ) ) { return $all_accounts[ $account ]; } return false; } /** * Get Cached Counts * * @param string $account Account Name. * @param string|int $post_id Post ID. * @param string $url Custom URL. * @param bool $ignore_time Ignore Cache Time. */ function powerkit_share_buttons_get_cached_count( $account, $post_id, $url = null, $ignore_time = false ) { $count = powerkit_share_buttons_get_cached_account( $account, $post_id, $url, $ignore_time ); // Recover shares. if ( get_option( 'powerkit_share_buttons_recover', false ) ) { $alt_count = powerkit_share_buttons_get_cached_account( $account, $post_id, $url, $ignore_time, 'recover' ); if ( $alt_count ) { $count = intval( $count ) + intval( $alt_count ); } } return $count; } /** * Get Share Count * * @since 1.0.0 * @param string $account Account Name. * @param string|int $post_id Post ID. * @param string $url Custom URL. */ function powerkit_share_buttons_get_count( $account, $post_id = false, $url = null ) { $count = false; // Get Account Data. $account_data = powerkit_share_buttons_get_accounts( $account, $post_id, $url ); if ( isset( $account_data['count_walker'] ) ) { $func = $account_data['count_walker']; // Get Share URL. $url = powerkit_share_buttons_get_url( $post_id, $url ); if ( function_exists( $func ) ) { $count = call_user_func_array( $func, array( $account, $post_id, $url ) ); // Recover shares. if ( get_option( 'powerkit_share_buttons_recover', false ) ) { $url = powerkit_share_buttons_get_alt_url( $url ); $count_recover = call_user_func_array( $func, array( $account, $post_id, $url, 'recover' ) ); if ( is_numeric( $count_recover ) ) { $count += $count_recover; } } } } $count = apply_filters( 'powerkit_share_buttons_count', $count, $account, $post_id ); return $count; } /** * Get Total Count * * @since 1.0.0 * @param array $accounts Accounts List. * @param string|int $post_id Post ID. * @param string $url Custom URL. * @param bool $cached Only cached. */ function powerkit_share_buttons_get_total_count( $accounts, $post_id = false, $url = null, $cached = false ) { $total_count = 0; foreach ( $accounts as $account ) { if ( $cached ) { $total_count += intval( powerkit_share_buttons_get_cached_count( $account, $post_id, $url ) ); } else { $total_count += intval( powerkit_share_buttons_get_count( $account, $post_id, $url ) ); } } return $total_count; } /** * Get Shares * * @since 1.0.0 * @param array $accounts Accounts List. * @param bool $total Total Count. * @param bool $icons Show Icons. * @param bool $titles Show Title. * @param bool $labels Show labels. * @param bool $counts Show Counts. * @param bool $title_location Title location. * @param bool $label_location Label location. * @param bool $count_location Count location. * @param string $mode Counter mode. * @param string $layout Share layout. * @param string $scheme Color Sheme. * @param string $class Additional class. * @param string $url Custom URL. * @param string $attrs Attrs. */ function powerkit_share_buttons( $accounts = array( 'facebook', 'twitter', 'pinterest' ), $total = true, $icons = true, $titles = false, $labels = true, $counts = true, $title_location = 'inside', $label_location = 'inside', $count_location = 'inside', $mode = 'mixed', $layout = 'default', $scheme = 'default', $class = null, $url = null, $attrs = '' ) { // Check accounts list. if ( empty( $accounts ) ) { return false; } // Post ID. $post_id = powerkit_share_buttons_get_current_post_id( $url ); // Title location. if ( ! in_array( $title_location, array( 'inside', 'outside' ), true ) ) { $title_location = 'inside'; } // Label location. if ( ! in_array( $label_location, array( 'inside', 'outside' ), true ) ) { $label_location = 'inside'; } // Count location. if ( ! in_array( $count_location, array( 'inside', 'outside' ), true ) ) { $count_location = 'inside'; } // Wrap Classes. $classes = array( 'pk-share-buttons-wrap' ); if ( $layout ) { $classes[] = 'pk-share-buttons-layout-' . $layout; } if ( $scheme ) { $classes[] = 'pk-share-buttons-scheme-' . $scheme; } if ( $counts ) { $classes[] = 'pk-share-buttons-has-counts'; } if ( $total ) { $classes[] = 'pk-share-buttons-has-total-counts'; } if ( $class ) { $classes[] = $class; } $mode = apply_filters( 'powerkit_share_buttons_mode', $mode, $post_id ); switch ( $mode ) { case 'none': $classes[] = 'pk-share-buttons-mode-none'; break; case 'php': $classes[] = 'pk-share-buttons-mode-php'; break; case 'cached': $classes[] = 'pk-share-buttons-mode-cached'; break; case 'rest': $classes[] = 'pk-share-buttons-mode-rest'; // Smart Load restapi scripts. add_action( 'wp_footer', 'powerkit_share_buttons_rest_api_scripts', 99 ); break; case 'mixed': $classes[] = 'pk-share-buttons-mode-php'; $classes[] = 'pk-share-buttons-mode-rest'; // Smart Load restapi scripts. add_action( 'wp_footer', 'powerkit_share_buttons_rest_api_scripts', 99 ); break; } // Icon prefix. $powerkit_share_buttons_icon_prefix = apply_filters( 'powerkit_share_buttons_icon_prefix', 'pk-icon' ); ?>
> 0 ) { $total_count = powerkit_share_buttons_count_format( $total_count ); } else { $total_class = 'pk-share-buttons-total pk-share-buttons-total-no-count'; } // Total Title. $total_title = apply_filters( 'powerkit_share_buttons_total_title', esc_html__( 'Total', 'powerkit' ), $class, $total_count ); // Total Label. $total_label = apply_filters( 'powerkit_share_buttons_total_label', esc_html__( 'Shares', 'powerkit' ), $class, $total_count ); // Total Output. ?>
$val ) { if ( in_array( $key, $accounts_check, true ) ) { $accounts[] = $key; } } } $title_location = get_option( "powerkit_share_buttons_{$location}_title_location" ); $label_location = get_option( "powerkit_share_buttons_{$location}_label_location" ); $count_location = get_option( "powerkit_share_buttons_{$location}_count_location" ); $labels = get_option( "powerkit_share_buttons_{$location}_display_labels" ); $total = get_option( "powerkit_share_buttons_{$location}_display_total_share_count" ); $counts = get_option( "powerkit_share_buttons_{$location}_display_count" ); $scheme = get_option( "powerkit_share_buttons_{$location}_scheme" ); $class = null; // Layouts. $layout = powerkit_share_buttons_handler_layout( $location ); // Scheme. $scheme = powerkit_share_buttons_handler_color_scheme( $location ); // Add location to the wrapper class. $class = trim( 'pk-share-buttons-' . $location . ' ' . $class ); // Before | After Content. $locations = apply_filters( 'powerkit_share_buttons_locations', array() ); $mode = 'smart'; $before = ''; $after = ''; $attrs = ''; foreach ( $locations as $location_data ) { if ( $location_data['location'] === $location ) { $before = isset( $location_data['before'] ) ? $location_data['before'] : $before; $after = isset( $location_data['after'] ) ? $location_data['after'] : $after; $mode = isset( $location_data['mode'] ) ? $location_data['mode'] : $mode; $icons = isset( $location_data['meta']['icons'] ) ? $location_data['meta']['icons'] : true; $titles = isset( $location_data['meta']['titles'] ) ? $location_data['meta']['titles'] : false; $attrs = isset( $location_data['attrs'] ) ? $location_data['attrs'] : $attrs; } } // Before Shares. echo wp_kses_post( $before ); // Get Shares. powerkit_share_buttons( $accounts, $total, $icons, $titles, $labels, $counts, $title_location, $label_location, $count_location, $mode, $layout, $scheme, $class, $url, $attrs ); // After Shares. echo wp_kses_post( $after ); } } /** * Count format * * @since 1.0.0 * @param int $value Count number. */ function powerkit_share_buttons_count_format( $value = 0 ) { $result = ''; $value = (int) $value; $count_format = apply_filters( 'powerkit_share_buttons_count_format', true ); if ( $count_format ) { if ( $value > 999 && $value <= 999999 ) { $result = floor( $value / 1000 ) . esc_html( 'K', 'powerkit' ); } elseif ( $value > 999999 ) { $result = floor( $value / 1000000 ) . esc_html( 'M', 'powerkit' ); } else { $result = $value; } } else { $result = $value; } return $result; } /** * Add Social Share REST API Scripts */ function powerkit_share_buttons_rest_api_scripts() { ?> 'accounts_not_found', 'message' => esc_html__( 'Social Accounts not found.', 'powerkit' ), ) ); } // Post ID. if ( isset( $request['post_id'] ) ) { $post_id = (int) $request['post_id']; if ( $post_id <= 0 ) { $post_id = false; } } else { $post_id = false; } // URL. if ( isset( $request['url'] ) ) { $url = $request['url']; } else { $url = null; } // Get Counts. $share_counts = array(); if ( isset( $request['ids'] ) ) { $ids = explode( ',', $request['ids'] ); $ids = array_map( 'trim', $ids ); } else { $ids = $social_accounts; } $total_count = 0; foreach ( $ids as $account ) { if ( in_array( $account, $social_accounts, true ) ) { $account_count = powerkit_share_buttons_get_count( $account, $post_id, $url ); $class = $account_count ? 'pk-share-buttons-item-count' : 'pk-share-buttons-no-count'; $total_count += (int) $account_count; $share_counts[ $account ] = array( 'count' => powerkit_share_buttons_count_format( $account_count ), 'class' => $class, ); } } $share_counts['total_count'] = powerkit_share_buttons_count_format( $total_count ); // Return Succes Result. return rest_ensure_response( $share_counts ); } /** * Register Share REST API Route */ function powerkit_share_buttons_register_api_routes() { register_rest_route( 'social-share/v1', '/get-shares', array( 'methods' => WP_REST_Server::READABLE, 'callback' => 'powerkit_share_buttons_get_accounts_restapi', 'permission_callback' => function() { return true; }, ) ); } add_action( 'rest_api_init', 'powerkit_share_buttons_register_api_routes' );