Upgrade
1 month ago
CLIService.php
1 month ago
FeedCacheUpdateService.php
1 month ago
MigrationReactivationNotice.php
1 month ago
SBR_Upgrader.php
1 month ago
SettingsManagerService.php
1 month ago
ShortcodeService.php
1 month ago
SiteUrlWatcher.php
1 month ago
UsageTrackingService.php
1 month ago
UsageTrackingService.php
260 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Tracking functions for reporting plugin usage to the Smash Balloon site for users that have opted in |
| 5 | * |
| 6 | * @copyright Copyright (c) 2018, Chris Christoff |
| 7 | * @since 5.6 |
| 8 | */ |
| 9 | |
| 10 | // Exit if accessed directly |
| 11 | namespace SmashBalloon\Reviews\Common\Services; |
| 12 | |
| 13 | use SmashBalloon\Reviews\Vendor\Smashballoon\Framework\Utilities\UsageTracking; |
| 14 | use SmashBalloon\Reviews\Common\Builder\SBR_Feed_Saver; |
| 15 | use SmashBalloon\Reviews\Common\Customizer\DB; |
| 16 | use SmashBalloon\Reviews\Common\Util; |
| 17 | use SmashBalloon\Reviews\Pro\FeedCache; |
| 18 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 19 | |
| 20 | if (! defined('ABSPATH')) { |
| 21 | exit; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Usage tracking |
| 26 | * |
| 27 | * @access public |
| 28 | * @since 5.6 |
| 29 | * @return void |
| 30 | */ |
| 31 | class UsageTrackingService extends ServiceProvider { |
| 32 | /** |
| 33 | * @var DB |
| 34 | */ |
| 35 | private $DB; |
| 36 | |
| 37 | public function __construct(DB $DB) |
| 38 | { |
| 39 | $this->DB = $DB; |
| 40 | } |
| 41 | public function register() |
| 42 | { |
| 43 | add_action('init', array( $this, 'schedule_send' )); |
| 44 | add_filter('cron_schedules', array( $this, 'add_schedules' )); |
| 45 | add_filter('sb_usage_tracking_data', array( $this, 'filter_usage_tracking_data' ), 10, 2); |
| 46 | add_action('sbr_usage_tracking_cron', array( $this, 'send_checkin' )); |
| 47 | } |
| 48 | |
| 49 | public function get_data() |
| 50 | { |
| 51 | $data = array(); |
| 52 | |
| 53 | // Retrieve current theme info |
| 54 | $theme_data = wp_get_theme(); |
| 55 | |
| 56 | $count_b = 1; |
| 57 | if (is_multisite()) { |
| 58 | if (function_exists('get_blog_count')) { |
| 59 | $count_b = get_blog_count(); |
| 60 | } else { |
| 61 | $count_b = 'Not Set'; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | $php_version = rtrim(ltrim(sanitize_text_field(phpversion()))); |
| 66 | $php_version = ! empty($php_version) ? substr($php_version, 0, strpos($php_version, '.', strpos($php_version, '.') + 1)) : phpversion(); |
| 67 | |
| 68 | global $wp_version; |
| 69 | $data['this_plugin'] = 'sbr'; |
| 70 | $data['php_version'] = $php_version; |
| 71 | $data['mi_version'] = SBRVER; |
| 72 | $data['wp_version'] = $wp_version; |
| 73 | $data['server'] = isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : ''; |
| 74 | $data['multisite'] = is_multisite(); |
| 75 | $data['url'] = home_url(); |
| 76 | $data['themename'] = $theme_data->Name; |
| 77 | $data['themeversion'] = $theme_data->Version; |
| 78 | $data['settings'] = array(); |
| 79 | $data['pro'] = (int) Util::sbr_is_pro(); |
| 80 | $data['sites'] = $count_b; |
| 81 | $data['usagetracking'] = get_option('sbr_usage_tracking_config', false); |
| 82 | $num_users = function_exists('count_users') ? count_users() : 'Not Set'; |
| 83 | $data['usercount'] = is_array($num_users) ? $num_users['total_users'] : 1; |
| 84 | $data['timezoneoffset'] = date('P'); |
| 85 | |
| 86 | $settings_to_send = array(); |
| 87 | $raw_settings = get_option('sbr_settings', array()); |
| 88 | if (! is_array($raw_settings)) { |
| 89 | $raw_settings = array(); |
| 90 | } |
| 91 | $feeds = DB::get_feeds_list(); |
| 92 | $feed_settings = []; |
| 93 | |
| 94 | $settings_to_send = array_merge($settings_to_send, $feed_settings); |
| 95 | $con_bus_accounts = 0; |
| 96 | $recently_searched_hashtags = 0; |
| 97 | $access_tokens_tried = array(); |
| 98 | |
| 99 | |
| 100 | $sbr_cache = new FeedCache(''); |
| 101 | |
| 102 | $settings_to_send['num_found_feed_caches'] = (int)$sbr_cache->get_cache_count(); |
| 103 | $settings_to_send['recently_requested_caches'] = (int)$sbr_cache->get_cache_count(true); |
| 104 | |
| 105 | $settings_to_send['num_found_feeds'] = count($feeds); |
| 106 | |
| 107 | $data['settings'] = $settings_to_send; |
| 108 | |
| 109 | // Retrieve current plugin information |
| 110 | if (! function_exists('get_plugins')) { |
| 111 | include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 112 | } |
| 113 | |
| 114 | $plugins = get_plugins(); |
| 115 | $active_plugins = get_option('active_plugins', array()); |
| 116 | $plugins_to_send = array(); |
| 117 | |
| 118 | foreach ($plugins as $plugin_path => $plugin) { |
| 119 | // If the plugin isn't active, don't show it. |
| 120 | if (! in_array($plugin_path, $active_plugins)) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | $plugins_to_send[] = $plugin['Name']; |
| 125 | } |
| 126 | |
| 127 | $data['active_plugins'] = $plugins_to_send; |
| 128 | $data['locale'] = get_locale(); |
| 129 | |
| 130 | return $data; |
| 131 | } |
| 132 | |
| 133 | public function send_checkin($override = false) |
| 134 | { |
| 135 | |
| 136 | $home_url = trailingslashit(home_url()); |
| 137 | if (strpos($home_url, 'smashballoon.com') !== false) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (! $this->tracking_allowed() && ! $override) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return UsageTracking::send_usage_update($this->get_data(), 'sbr'); |
| 146 | } |
| 147 | |
| 148 | private function tracking_allowed() |
| 149 | { |
| 150 | $settings = sbr_get_database_settings(); |
| 151 | |
| 152 | return ( isset($settings['usagetracking']) && true === $settings['usagetracking'] ); |
| 153 | } |
| 154 | |
| 155 | public function schedule_send() |
| 156 | { |
| 157 | if (! wp_next_scheduled('sbr_usage_tracking_cron')) { |
| 158 | $tracking = array(); |
| 159 | $tracking['day'] = rand(0, 6); |
| 160 | $tracking['hour'] = rand(0, 23); |
| 161 | $tracking['minute'] = rand(0, 59); |
| 162 | $tracking['second'] = rand(0, 59); |
| 163 | $tracking['offset'] = ( $tracking['day'] * DAY_IN_SECONDS ) + |
| 164 | ( $tracking['hour'] * HOUR_IN_SECONDS ) + |
| 165 | ( $tracking['minute'] * MINUTE_IN_SECONDS ) + |
| 166 | $tracking['second']; |
| 167 | $last_sunday = strtotime('next sunday') - ( 7 * DAY_IN_SECONDS ); |
| 168 | if (( $last_sunday + $tracking['offset'] ) > time() + 6 * HOUR_IN_SECONDS) { |
| 169 | $tracking['initsend'] = $last_sunday + $tracking['offset']; |
| 170 | } else { |
| 171 | $tracking['initsend'] = strtotime('next sunday') + $tracking['offset']; |
| 172 | } |
| 173 | |
| 174 | wp_schedule_event($tracking['initsend'], 'weekly', 'sbr_usage_tracking_cron'); |
| 175 | update_option('sbr_usage_tracking_config', $tracking); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | public function add_schedules($schedules = array()) |
| 180 | { |
| 181 | // Adds once weekly to the existing schedules. |
| 182 | $schedules['weekly'] = array( |
| 183 | 'interval' => 604800, |
| 184 | 'display' => __('Once Weekly', 'reviews-feed'), |
| 185 | ); |
| 186 | return $schedules; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Filter the usage tracking data |
| 191 | * |
| 192 | * @param array $data |
| 193 | * @param string $plugin_slug |
| 194 | * |
| 195 | * @handles sb_usage_tracking_data |
| 196 | * |
| 197 | * @return array|mixed |
| 198 | */ |
| 199 | public function filter_usage_tracking_data($data, $plugin_slug) |
| 200 | { |
| 201 | if ('sbr' !== $plugin_slug) { |
| 202 | return $data; |
| 203 | } |
| 204 | |
| 205 | if (! is_array($data)) { |
| 206 | return $data; |
| 207 | } |
| 208 | |
| 209 | if (! isset($data['settings'])) { |
| 210 | $data['settings'] = []; |
| 211 | } |
| 212 | |
| 213 | $tracked_boolean_settings = explode( |
| 214 | ',', |
| 215 | 'contentLength,carouselIntervalTime,carouselShowArrows,carouselShowPagination,carouselEnableAutoplay,showHeader, |
| 216 | headerContent,headerPadding,headerMargin,headerHeadingContent,headingFont,headingColor,headerHeadingPadding, |
| 217 | headerHeadingMargin,headerButtonLinkTo,headerButtonIcon,headerButtonExternalLink,headerButtonFont,headerButtonColor, |
| 218 | headerButtonBg,headerButtonHoverColor,headerButtonHoverBg,headerButtonPadding,headerButtonMargin,headerAvReviewFont, |
| 219 | headerAvSubtextReviewFont,headerAvReviewIconColor,headerAvReviewColor,headerAvReviewSubtextColor,headerAvReviewMargin, |
| 220 | headerAvReviewPadding,boxedBackgroundColor,boxedBoxShadow,boxedBorderRadius,postStroke,postPadding,postElements,ratingIconSize, |
| 221 | ratingIconColor,ratingIconPadding,ratingIconMargin,paragraphFont,paragraphColor,paragraphPadding,paragraphMargin,authorContent, |
| 222 | authorPadding,authorMargin,authorNameFont,authorNameColor,authorNamePadding,authorNameMargin,dateFont,dateColor,dateFormat, |
| 223 | dateCustomFormat,dateBeforeText,dateAfterText,datePadding,dateMargin,authorImageBorderRadius,authorImageMargin,showLoadButton, |
| 224 | loadButtonText,loadButtonFont,loadButtonColor,loadButtonHoverColor,loadButtonBg,loadButtonHoverBg,loadButtonPadding,loadButtonMargin, |
| 225 | includedStarFilters,includeWords,excludeWords,filterByImage,filterByVideos,sortByDateEnabled,sortByRatingEnabled,sortByRating, |
| 226 | sortRandomEnabled,colorScheme,moderationEnabled,moderationType,moderationAllowList,moderationBlockList,localization' |
| 227 | ); |
| 228 | |
| 229 | $tracked_string_settings = explode( |
| 230 | ',', |
| 231 | 'feedTemplate,layout,verticalSpacing,horizontalSpacing,contentAlignment,numPostDesktop,numPostTablet, |
| 232 | numPostMobile,gridDesktopColumns,gridTabletColumns,gridMobileColumns,masonryDesktopColumns,masonryTabletColumns, |
| 233 | masonryMobileColumns,carouselDesktopColumns,carouselTabletColumns,carouselMobileColumns,carouselDesktopRows, |
| 234 | carouselTabletRows,carouselMobileRows,carouselLoopType,sortByDate,filterCharCountMin,filterCharCountMax' |
| 235 | ); |
| 236 | |
| 237 | $feeds = DB::get_feeds_list(); |
| 238 | $settings_defaults = sbr_settings_defaults(); |
| 239 | |
| 240 | // Track settings of the first feed |
| 241 | if (! empty($feeds)) { |
| 242 | $feed = $feeds[0]; |
| 243 | $feed_settings = ( new SBR_Feed_Saver($feed['id']) )->get_feed_settings(); |
| 244 | |
| 245 | if (!is_array($feed_settings)) { |
| 246 | return $data; |
| 247 | } |
| 248 | |
| 249 | $booleans = UsageTracking::tracked_settings_to_booleans($tracked_boolean_settings, $settings_defaults, $feed_settings); |
| 250 | $strings = UsageTracking::tracked_settings_to_strings($tracked_string_settings, $feed_settings); |
| 251 | |
| 252 | if (is_array($booleans) && is_array($strings)) { |
| 253 | $data['settings'] = array_merge($data['settings'], $booleans, $strings); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return $data; |
| 258 | } |
| 259 | } |
| 260 |