display-conditions
3 years ago
front
9 months ago
helpers
9 months ago
metas
3 years ago
multisite
3 years ago
palettes
3 years ago
provider
3 years ago
providers
9 months ago
templates
3 years ago
update
3 years ago
class-hustle-admin-page-abstract.php
9 months ago
class-hustle-black-friday-campaign.php
7 months ago
class-hustle-condition-factory.php
6 years ago
class-hustle-cross-sell.php
9 months ago
class-hustle-dashboard-admin.php
3 years ago
class-hustle-data.php
3 years ago
class-hustle-db.php
2 years ago
class-hustle-installer.php
4 years ago
class-hustle-meta.php
3 years ago
class-hustle-module-admin.php
9 months ago
class-hustle-module-collection.php
3 years ago
class-hustle-module-page-abstract.php
2 years ago
class-hustle-notifications.php
3 years ago
class-hustle-settings-admin.php
3 years ago
class-hustle-tutorials-page.php
4 years ago
class-hustle-wp-dashboard-page.php
3 years ago
hustle-deletion.php
3 years ago
hustle-embedded-admin.php
3 years ago
hustle-entries-admin.php
3 years ago
hustle-entry-model.php
3 years ago
hustle-general-data-protection.php
3 years ago
hustle-init.php
7 months ago
hustle-mail.php
3 years ago
hustle-migration.php
3 years ago
hustle-model.php
3 years ago
hustle-module-model.php
9 months ago
hustle-module-widget-legacy.php
3 years ago
hustle-module-widget.php
3 years ago
hustle-modules-common-admin-ajax.php
9 months ago
hustle-popup-admin.php
3 years ago
hustle-providers-admin.php
3 years ago
hustle-providers.php
3 years ago
hustle-settings-admin-ajax.php
3 years ago
hustle-settings-page.php
3 years ago
hustle-slidein-admin.php
3 years ago
hustle-sshare-admin.php
3 years ago
hustle-sshare-model.php
3 years ago
hustle-tracking-model.php
3 years ago
opt-in-geo.php
9 months ago
opt-in-utils.php
3 years ago
opt-in-wpmudev-api.php
3 years ago
hustle-sshare-model.php
739 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Test CPT conditions |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Hustle_SShare_Model class |
| 10 | */ |
| 11 | class Hustle_SShare_Model extends Hustle_Model { |
| 12 | |
| 13 | const SETTINGS_KEY = 'sshare_counters'; |
| 14 | const COUNTER_META_KEY = 'hustle_shares'; |
| 15 | const TIMESTAMP_META_KEY = 'hustle_timestamp'; |
| 16 | const REFRESH_OPTION_KEY = 'hustle_ss_refresh_counters'; |
| 17 | const FLOAT_DESKTOP = 'float_desktop'; |
| 18 | const FLOAT_MOBILE = 'float_mobile'; |
| 19 | const FLOAT_MODULE = 'floating'; |
| 20 | |
| 21 | /** |
| 22 | * Display settings |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | public $display; |
| 27 | |
| 28 | /** |
| 29 | * Get the sub-types for this module. |
| 30 | * |
| 31 | * @since 4.2.0 |
| 32 | * @param bool $with_titles With titles. |
| 33 | * |
| 34 | * @return array |
| 35 | */ |
| 36 | public static function get_sshare_types( $with_titles = false ) { |
| 37 | if ( ! $with_titles ) { |
| 38 | return array( self::FLOAT_MODULE, 'inline', 'widget', 'shortcode' ); |
| 39 | } else { |
| 40 | return array( |
| 41 | self::FLOAT_MODULE => __( 'Floating', 'hustle' ), |
| 42 | 'inline' => __( 'Inline', 'hustle' ), |
| 43 | 'widget' => __( 'Widget', 'hustle' ), |
| 44 | 'shortcode' => __( 'Shortcode', 'hustle' ), |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get content |
| 51 | * |
| 52 | * @return \Hustle_SShare_Content |
| 53 | */ |
| 54 | public function get_content() { |
| 55 | return new Hustle_SShare_Content( $this->get_settings_meta( self::KEY_CONTENT ), $this ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Get design |
| 60 | * |
| 61 | * @return \Hustle_SShare_Design |
| 62 | */ |
| 63 | public function get_design() { |
| 64 | return new Hustle_SShare_Design( $this->get_settings_meta( self::KEY_DESIGN ), $this ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the stored settings for the "Display" tab. |
| 69 | * |
| 70 | * @since 4.0 |
| 71 | * |
| 72 | * @return Hustle_SShare_Display |
| 73 | */ |
| 74 | public function get_display() { |
| 75 | return new Hustle_SShare_Display( $this->get_settings_meta( self::KEY_DISPLAY_OPTIONS ), $this ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the instance of the decorator class for this module type. |
| 80 | * |
| 81 | * @since 4.3.0 |
| 82 | * |
| 83 | * @return Hustle_Decorator_Sshare |
| 84 | */ |
| 85 | public function get_decorator_instance() { |
| 86 | return new Hustle_Decorator_Sshare( $this ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a new module of the provided mode and type. |
| 91 | * |
| 92 | * @since 4.0 |
| 93 | * |
| 94 | * @param array $data Must contain the Module's 'mode', 'name' and 'type. |
| 95 | * @return int|false Module ID if successfully saved. False otherwise. |
| 96 | */ |
| 97 | public function create_new( $data ) { |
| 98 | // Verify it's a valid module type. |
| 99 | if ( self::SOCIAL_SHARING_MODULE !== $data['module_type'] ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Save to modules table. |
| 104 | $this->module_name = sanitize_text_field( $data['module_name'] ); |
| 105 | $this->module_type = $data['module_type']; |
| 106 | $this->active = 0; |
| 107 | $this->module_mode = ''; |
| 108 | $this->save(); |
| 109 | |
| 110 | // Save the new module's meta. |
| 111 | $this->store_new_module_meta( $data ); |
| 112 | |
| 113 | return $this->id; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Store the defaults meta when creating a new module. |
| 118 | * |
| 119 | * @since 4.0.0 |
| 120 | * |
| 121 | * @param array $data Data to store. |
| 122 | */ |
| 123 | private function store_new_module_meta( $data ) { |
| 124 | $def_content = apply_filters( 'hustle_module_get_' . self::KEY_CONTENT . '_defaults', $this->get_content()->to_array(), $this, $data ); |
| 125 | $content_data = empty( $data['content'] ) ? $def_content : array_merge( $def_content, $data['content'] ); |
| 126 | |
| 127 | $def_design = apply_filters( 'hustle_module_get_' . self::KEY_DESIGN . '_defaults', $this->get_design()->to_array(), $this, $data ); |
| 128 | $design_data = empty( $data['design'] ) ? $def_design : array_merge( $def_design, $data['design'] ); |
| 129 | |
| 130 | // Visibility settings. |
| 131 | $def_visibility = apply_filters( 'hustle_module_get_' . self::KEY_VISIBILITY . '_defaults', $this->get_visibility()->to_array(), $this, $data ); |
| 132 | $visibility_data = empty( $data['visibility'] ) ? $def_visibility : array_merge( $def_visibility, $data['visibility'] ); |
| 133 | |
| 134 | // Display options. |
| 135 | $def_display = apply_filters( 'hustle_module_get_' . self::KEY_DISPLAY_OPTIONS . '_defaults', $this->get_display()->to_array(), $this, $data ); |
| 136 | $display_data = empty( $data['display'] ) ? $def_display : array_merge( $def_display, $data['display'] ); |
| 137 | |
| 138 | // Save to meta table. |
| 139 | $this->update_meta( self::KEY_CONTENT, $content_data ); |
| 140 | $this->update_meta( self::KEY_DESIGN, $design_data ); |
| 141 | $this->update_meta( self::KEY_VISIBILITY, $visibility_data ); |
| 142 | $this->update_meta( self::KEY_DISPLAY_OPTIONS, $display_data ); |
| 143 | |
| 144 | $this->enable_type_track_mode( $this->module_type, true ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sanitize/Replace the module's data. |
| 149 | * |
| 150 | * @param array $data Data to sanitize. |
| 151 | * @return array Sanitized data. |
| 152 | */ |
| 153 | public function sanitize_module( $data ) { |
| 154 | $data = Opt_In_Utils::validate_and_sanitize_fields( $data ); |
| 155 | return $data; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Validates the module's data. |
| 160 | * |
| 161 | * @since 4.0.3 |
| 162 | * |
| 163 | * @param array $data Data to validate. |
| 164 | * @return array |
| 165 | */ |
| 166 | public function validate_module( $data ) { |
| 167 | |
| 168 | $icons = isset( $data['content']['social_icons'] ) ? $data['content']['social_icons'] : array(); |
| 169 | $display = $data['display']; |
| 170 | $selector = array( |
| 171 | 'desktop' => isset( $display['float_desktop_offset'] ) ? $display['float_desktop_offset'] : '', |
| 172 | 'mobile' => isset( $display['float_mobile_offset'] ) ? $display['float_mobile_offset'] : '', |
| 173 | ); |
| 174 | |
| 175 | $errors = array(); |
| 176 | |
| 177 | // Name validation. |
| 178 | if ( empty( sanitize_text_field( $data['module']['module_name'] ) ) ) { |
| 179 | $errors['error']['name_error'] = __( 'This field is required', 'hustle' ); |
| 180 | } |
| 181 | |
| 182 | // Social platform url check. |
| 183 | if ( ! empty( $icons ) ) { |
| 184 | foreach ( $icons as $key => $icon ) { |
| 185 | $icon_with_enpoints = self::get_sharing_endpoints(); |
| 186 | if ( ! in_array( $icon['platform'], $icon_with_enpoints, true ) && empty( $icon['link'] ) ) { |
| 187 | $errors['error']['icon_error'][] = $icon['platform']; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // css selector check. |
| 193 | if ( ! empty( $selector ) ) { |
| 194 | if ( 'css_selector' === $selector['desktop'] && empty( $display['float_desktop_css_selector'] ) |
| 195 | && ! empty( $display['float_desktop_enabled'] ) ) { |
| 196 | $errors['error']['selector_error'][] = 'float_desktop'; |
| 197 | } |
| 198 | if ( 'css_selector' === $selector['mobile'] && empty( $display['float_mobile_css_selector'] ) && ! empty( $display['float_mobile_enabled'] ) ) { |
| 199 | $errors['error']['selector_error'][] = 'float_mobile'; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // return errors if any. |
| 204 | if ( ! empty( $errors ) ) { |
| 205 | $errors['success'] = false; |
| 206 | return $errors; |
| 207 | } |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Updates the metas specific for Social Sharing modules. |
| 214 | * |
| 215 | * @since 4.3.0 |
| 216 | * @param array $data Data to save. |
| 217 | * @return void |
| 218 | */ |
| 219 | protected function update_module_metas( $data ) { |
| 220 | // Meta used in all module types. |
| 221 | if ( isset( $data['content'] ) ) { |
| 222 | $this->update_meta( self::KEY_CONTENT, $data['content'] ); |
| 223 | } |
| 224 | // Meta used in all module types. |
| 225 | if ( isset( $data['visibility'] ) ) { |
| 226 | $this->update_meta( self::KEY_VISIBILITY, $data['visibility'] ); |
| 227 | } |
| 228 | |
| 229 | if ( isset( $data['design'] ) ) { |
| 230 | $this->update_meta( self::KEY_DESIGN, $data['design'] ); |
| 231 | } |
| 232 | |
| 233 | if ( isset( $data['display'] ) ) { |
| 234 | $this->update_meta( self::KEY_DISPLAY_OPTIONS, $data['display'] ); |
| 235 | } |
| 236 | |
| 237 | // Force all counters to retrieve the data from the APIs. |
| 238 | self::refresh_all_counters(); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Get the module's data. Used to display it. |
| 243 | * |
| 244 | * @since 4.3.0 |
| 245 | * |
| 246 | * @return array |
| 247 | */ |
| 248 | public function get_module_data_to_display() { |
| 249 | return $this->get_data(); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Get the sub-types for this module. |
| 254 | * |
| 255 | * @since 4.3.1 |
| 256 | * @param bool $with_titles With titles. |
| 257 | * |
| 258 | * @return array |
| 259 | */ |
| 260 | public function get_sub_types( $with_titles = false ) { |
| 261 | return self::get_sshare_types( $with_titles ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Return whether or not the requested counter type is enabled |
| 266 | * |
| 267 | * @since 3.0.3 |
| 268 | * |
| 269 | * @param string $type Type. |
| 270 | * @return boolean |
| 271 | */ |
| 272 | public function is_click_counter_type_enabled( $type ) { |
| 273 | $content = $this->get_content()->to_array(); |
| 274 | if ( isset( $content['click_counter'] ) && $content['click_counter'] === $type ) { |
| 275 | return true; |
| 276 | } else { |
| 277 | return false; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get social patform names |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public static function get_social_platform_names() { |
| 287 | $social_platform_names = array( |
| 288 | 'facebook' => esc_html__( 'Facebook', 'hustle' ), |
| 289 | 'twitter' => esc_html__( 'Twitter', 'hustle' ), |
| 290 | 'pinterest' => esc_html__( 'Pinterest', 'hustle' ), |
| 291 | 'reddit' => esc_html__( 'Reddit', 'hustle' ), |
| 292 | 'linkedin' => esc_html__( 'LinkedIn', 'hustle' ), |
| 293 | 'vkontakte' => esc_html__( 'Vkontakte', 'hustle' ), |
| 294 | 'fivehundredpx' => esc_html__( '500px', 'hustle' ), |
| 295 | 'houzz' => esc_html__( 'Houzz', 'hustle' ), |
| 296 | 'instagram' => esc_html__( 'Instagram', 'hustle' ), |
| 297 | 'twitch' => esc_html__( 'Twitch', 'hustle' ), |
| 298 | 'youtube' => esc_html__( 'YouTube', 'hustle' ), |
| 299 | 'telegram' => esc_html__( 'Telegram', 'hustle' ), |
| 300 | 'whatsapp' => esc_html__( 'WhatsApp', 'hustle' ), |
| 301 | 'email' => esc_html__( 'Email', 'hustle' ), |
| 302 | ); |
| 303 | |
| 304 | /** |
| 305 | * Social networks list |
| 306 | * |
| 307 | * @since 4.0.4 |
| 308 | * |
| 309 | * @param array $social_platform_names {slug} => {name} |
| 310 | */ |
| 311 | return apply_filters( 'hustle_social_platform_names', $social_platform_names ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Get the stored counter of each network. |
| 316 | * |
| 317 | * @since 3.0.3 |
| 318 | * |
| 319 | * @param integer $post_id Post ID. |
| 320 | * @return array |
| 321 | */ |
| 322 | public function get_stored_network_shares( $post_id ) { |
| 323 | |
| 324 | if ( 0 !== $post_id ) { |
| 325 | $stored_counters = get_post_meta( $post_id, self::COUNTER_META_KEY, true ); |
| 326 | |
| 327 | } else { |
| 328 | $sshare_networks_option = Hustle_Settings_Admin::get_hustle_settings( self::SETTINGS_KEY ); |
| 329 | |
| 330 | $stored_counters = ! empty( $sshare_networks_option[ self::COUNTER_META_KEY ] ) ? $sshare_networks_option[ self::COUNTER_META_KEY ] : array(); |
| 331 | } |
| 332 | |
| 333 | return $stored_counters; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Check if stored values should be used. |
| 338 | * |
| 339 | * @since 3.0.3 |
| 340 | * |
| 341 | * @param integer $post_id Post ID. |
| 342 | * @param bool $check_expiration_time Optional. Check expiration time or not. |
| 343 | * @return bool |
| 344 | */ |
| 345 | public function should_use_stored( $post_id, $check_expiration_time = false ) { |
| 346 | |
| 347 | // If we're in a page/post... |
| 348 | if ( 0 !== $post_id ) { |
| 349 | |
| 350 | // Don't use stored if we don't have anything stored in this post. |
| 351 | if ( ! get_post_meta( $post_id, self::COUNTER_META_KEY ) ) { |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | // If we're in somewhere that's not a page/post... |
| 356 | } else { |
| 357 | |
| 358 | // Don't use stored if we don't have anything stored in the options. |
| 359 | $sshare_networks_option = Hustle_Settings_Admin::get_hustle_settings( self::SETTINGS_KEY ); |
| 360 | if ( empty( $sshare_networks_option ) || empty( $sshare_networks_option[ self::COUNTER_META_KEY ] ) ) { |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Do use stored values if traffic is a crawler/bot. |
| 366 | $user_agent = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_SPECIAL_CHARS ); |
| 367 | if ( $user_agent && preg_match( '/bot|crawler|ia_archiver|mediapartners-google|80legs|wget|voyager|baiduspider|curl|yahoo!|slurp/i', $user_agent ) ) { |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | if ( $check_expiration_time ) { |
| 372 | |
| 373 | if ( 0 !== $post_id ) { |
| 374 | $timestamp = intval( get_post_meta( $post_id, self::TIMESTAMP_META_KEY, true ) ); |
| 375 | } else { |
| 376 | $timestamp = isset( $sshare_networks_option[ self::TIMESTAMP_META_KEY ] ) ? $sshare_networks_option[ self::TIMESTAMP_META_KEY ] : 'true'; |
| 377 | } |
| 378 | |
| 379 | // Don't use stored if the expiration time of the counter already passed. |
| 380 | if ( 'true' === $timestamp || time() > ( $timestamp + ( 6 * 60 * 60 ) ) ) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | // Don't use stored if the counter hasn't beeen updated after the last time that all counters were cleared. |
| 385 | $clear_counters_time = get_option( self::REFRESH_OPTION_KEY, false ); |
| 386 | if ( $clear_counters_time && $timestamp < intval( $clear_counters_time ) ) { |
| 387 | return false; |
| 388 | } |
| 389 | } |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Set the number of shares of each network in an array. Like $result[facebook] = 44 |
| 395 | * |
| 396 | * @since 3.0.3 |
| 397 | * @since 4.0 |
| 398 | * |
| 399 | * @param array $networks Networks. |
| 400 | * @param integer $post_id Post ID. |
| 401 | * @return array |
| 402 | */ |
| 403 | public function retrieve_networks_shares( $networks, $post_id ) { |
| 404 | |
| 405 | // TODO: handle homepage when post_id is not a specific page. |
| 406 | $post_id = apply_filters( 'hustle_network_shares_post_id', $post_id ); |
| 407 | |
| 408 | $current_link = ( 0 !== $post_id && get_permalink( $post_id ) ) ? get_permalink( $post_id ) : home_url(); |
| 409 | // We share URL without trailing slash, So we need to remove slash when we fetch from API. |
| 410 | $current_link = untrailingslashit( $current_link ); |
| 411 | $current_link = apply_filters( 'hustle_network_shares_from_url', $current_link ); |
| 412 | |
| 413 | // If we should use the stored values instead of retrieving them from the API. |
| 414 | $should_use_stored = apply_filters( 'hustle_sshare_should_use_stored_counters', $this->should_use_stored( $post_id, true ), $post_id, $current_link ); |
| 415 | if ( $should_use_stored ) { |
| 416 | |
| 417 | $networks_info = $this->get_stored_network_shares( $post_id ); |
| 418 | |
| 419 | // Call the API for the networks we don't have the value stored. |
| 420 | $missing_networks = array_diff( $networks, array_keys( $networks_info ) ); |
| 421 | if ( ! empty( $missing_networks ) ) { |
| 422 | $missing_info = $this->get_refreshed_counters( $current_link, $missing_networks, $post_id ); |
| 423 | |
| 424 | $networks_info = array_merge( $networks_info, $missing_info ); |
| 425 | } |
| 426 | } else { |
| 427 | $networks_info = $this->get_refreshed_counters( $current_link, $networks, $post_id ); |
| 428 | } |
| 429 | |
| 430 | return $networks_info; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Get the counter from the APIs, store it in the post_meta, and store the current time for expiration. |
| 435 | * |
| 436 | * @since 4.0 |
| 437 | * |
| 438 | * @param string $current_link Current link. |
| 439 | * @param array $networks Networks. |
| 440 | * @param id $post_id Post ID. |
| 441 | * @return array |
| 442 | */ |
| 443 | public function get_refreshed_counters( $current_link, $networks, $post_id ) { |
| 444 | |
| 445 | // Get array with json formatted data for each active network. |
| 446 | $networks_info = $this->get_networks_data_from_api( $current_link, $networks ); |
| 447 | $networks_info = $this->format_networks_api_raw_data( $networks_info, false ); |
| 448 | |
| 449 | // Store the values for "caching". |
| 450 | if ( 0 !== $post_id ) { |
| 451 | |
| 452 | // Store the values of all new networks. |
| 453 | $stored_info = get_post_meta( $post_id, self::COUNTER_META_KEY, true ); |
| 454 | if ( ! empty( $stored_info ) ) { |
| 455 | $networks_info = array_merge( $stored_info, $networks_info ); |
| 456 | } |
| 457 | |
| 458 | // Set the counters values for current post. |
| 459 | update_post_meta( $post_id, self::COUNTER_META_KEY, $networks_info ); |
| 460 | |
| 461 | // Set the counter update time. |
| 462 | update_post_meta( $post_id, self::TIMESTAMP_META_KEY, time() ); |
| 463 | |
| 464 | } else { |
| 465 | |
| 466 | // Store the values of all new networks. |
| 467 | $stored_info = Hustle_Settings_Admin::get_hustle_settings( self::SETTINGS_KEY ); |
| 468 | if ( ! empty( $stored_info[ self::COUNTER_META_KEY ] ) ) { |
| 469 | $networks_info = array_merge( $stored_info[ self::COUNTER_META_KEY ], $networks_info ); |
| 470 | } |
| 471 | |
| 472 | // Set global option for pages that are not a post. |
| 473 | $networks_data = array( |
| 474 | self::COUNTER_META_KEY => $networks_info, |
| 475 | self::TIMESTAMP_META_KEY => time(), |
| 476 | ); |
| 477 | |
| 478 | Hustle_Settings_Admin::update_hustle_settings( $networks_data, self::SETTINGS_KEY ); |
| 479 | } |
| 480 | |
| 481 | return $networks_info; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Get the data from each network's API |
| 486 | * |
| 487 | * @since 3.0.3 |
| 488 | * |
| 489 | * @param string $current_link Current Link. |
| 490 | * @param array $social_networks Social networks. |
| 491 | * @param array $options Options. |
| 492 | * @return array |
| 493 | */ |
| 494 | private function get_networks_data_from_api( $current_link, $social_networks = array(), $options = array() ) { |
| 495 | $result = array(); |
| 496 | |
| 497 | $networks_endpoint = self::get_networks_counter_endpoint( false, $current_link ); |
| 498 | |
| 499 | foreach ( $social_networks as $network ) { |
| 500 | |
| 501 | if ( isset( $networks_endpoint[ $network ] ) ) { |
| 502 | $url = $networks_endpoint[ $network ]; |
| 503 | } else { |
| 504 | continue; |
| 505 | } |
| 506 | |
| 507 | $response = wp_remote_get( $url ); |
| 508 | $response_body = wp_remote_retrieve_body( $response ); |
| 509 | |
| 510 | if ( $response_body ) { |
| 511 | $result[ $network ] = $response_body; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return $result; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Get the API URL for each network, or the name of the networks with a counter endpoint. |
| 520 | * |
| 521 | * @since 4.0 |
| 522 | * |
| 523 | * @param string $networks_only Networks only. |
| 524 | * @param string $current_link Current link. |
| 525 | * @return array |
| 526 | */ |
| 527 | public static function get_networks_counter_endpoint( $networks_only = true, $current_link = '' ) { |
| 528 | |
| 529 | if ( $networks_only ) { |
| 530 | return apply_filters( |
| 531 | 'hustle_networks_with_counter_endpoint', |
| 532 | array( 'twitter', 'reddit', 'vkontakte' ) |
| 533 | ); |
| 534 | } |
| 535 | |
| 536 | $current_link = rawurlencode( $current_link ); |
| 537 | |
| 538 | return apply_filters( |
| 539 | 'hustle_native_share_counter_enpoints', |
| 540 | array( |
| 541 | // FB stopped working without an app token. |
| 542 | // 'facebook' => 'https://graph.facebook.com/?fields=og_object{engagement{count}}&id=' . $current_link, |
| 543 | // There's no official twitter api for doing this. This alternative requires signing in. |
| 544 | 'twitter' => 'https://counts.twitcount.com/counts.php?url=' . $current_link, |
| 545 | 'reddit' => 'https://www.reddit.com/api/info.json?url=' . $current_link, |
| 546 | 'vkontakte' => 'https://vk.com/share.php?act=count&url=' . untrailingslashit( $current_link ), |
| 547 | ) |
| 548 | ); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Format the raw response from the networks so it can be displayed in front. |
| 553 | * |
| 554 | * @since 4.0 |
| 555 | * |
| 556 | * @param array $networks_data Networks data. |
| 557 | * @param boolean $shorten_count If true, 1000 shares would be formatted to 1K. |
| 558 | * @return array |
| 559 | */ |
| 560 | private function format_networks_api_raw_data( $networks_data, $shorten_count = true ) { |
| 561 | |
| 562 | $formatted = array(); |
| 563 | |
| 564 | foreach ( $networks_data as $network => $response ) { |
| 565 | |
| 566 | // Get "count" from each network's response and add the "count" number to $formatted array. |
| 567 | $get_formatted_response = 'format_' . $network . '_api_response'; |
| 568 | if ( ! is_callable( array( $this, $get_formatted_response ) ) ) { |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | $formatted[ $network ] = $this->{$get_formatted_response}( $networks_data[ $network ] ); |
| 573 | |
| 574 | if ( $shorten_count ) { |
| 575 | $formatted[ $network ] = $this->shorten_count( $formatted[ $network ] ); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | return $formatted; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Format a given number to display it nicely. 10K instead of 10093 |
| 584 | * |
| 585 | * @since 3.0.3 |
| 586 | * |
| 587 | * @param integer $count Count. |
| 588 | * @return string |
| 589 | */ |
| 590 | private function shorten_count( $count ) { |
| 591 | $count = intval( $count ); |
| 592 | if ( $count < 1000 ) { |
| 593 | return $count; |
| 594 | } elseif ( $count < 1000000 ) { |
| 595 | return round( $count / 1000, 1, PHP_ROUND_HALF_DOWN ) . __( ' K', 'hustle' ); |
| 596 | } else { |
| 597 | return round( $count / 1000000, 1, PHP_ROUND_HALF_DOWN ) . __( ' M', 'hustle' ); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Set option to trigger the refresh of the counters |
| 603 | * |
| 604 | * @since 3.0.3 |
| 605 | */ |
| 606 | public static function refresh_all_counters() { |
| 607 | update_option( self::REFRESH_OPTION_KEY, time() ); |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Format the response of each API to get the counter |
| 612 | * |
| 613 | * @since 3.0.3 |
| 614 | * |
| 615 | * @param string $response Response. |
| 616 | * @return integer |
| 617 | */ |
| 618 | private function format_facebook_api_response( $response ) { |
| 619 | $response = json_decode( $response, true ); |
| 620 | $engagement = ! empty( $response['og_object'] ) && ! empty( $response['og_object']['engagement']['count'] ) ? intval( $response['og_object']['engagement']['count'] ) : 0; |
| 621 | |
| 622 | return $engagement; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Format twitter api response |
| 627 | * |
| 628 | * @param string $response Response. |
| 629 | * @return int |
| 630 | */ |
| 631 | private function format_twitter_api_response( $response ) { |
| 632 | $response = json_decode( $response, true ); |
| 633 | return isset( $response['count'] ) ? intval( $response['count'] ) : 0; |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Format pinterest api response |
| 638 | * |
| 639 | * @param string $response Response. |
| 640 | * @return int |
| 641 | */ |
| 642 | private function format_pinterest_api_response( $response ) { |
| 643 | preg_match( '/^receiveCount\((.*)\)$/', $response, $match ); |
| 644 | if ( ! isset( $match[1] ) ) { |
| 645 | return 0; |
| 646 | } |
| 647 | $response = json_decode( $match[1], true ); |
| 648 | return isset( $response['count'] ) ? intval( $response['count'] ) : 0; |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Format reddit api response |
| 653 | * |
| 654 | * @param string $response Response. |
| 655 | * @return int |
| 656 | */ |
| 657 | private function format_reddit_api_response( $response ) { |
| 658 | $response = json_decode( $response, true ); |
| 659 | if ( ! isset( $response['data']['children'] ) ) { |
| 660 | return 0; |
| 661 | } |
| 662 | $data = $response['data']['children']; |
| 663 | $counter = 0; |
| 664 | foreach ( $data as $sub ) { |
| 665 | if ( ! isset( $sub['data']['subreddit_subscribers'] ) ) { |
| 666 | continue; |
| 667 | } |
| 668 | $counter = $counter + intval( $sub['data']['subreddit_subscribers'] ); |
| 669 | } |
| 670 | return $counter; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Format vkontakte api response |
| 675 | * |
| 676 | * @param string $response Response. |
| 677 | * @return int |
| 678 | */ |
| 679 | private function format_vkontakte_api_response( $response ) { |
| 680 | preg_match( '/^VK\.Share\.count\(.{1,3}(.*)\)/', $response, $match ); |
| 681 | if ( ! isset( $match[1] ) ) { |
| 682 | return 0; |
| 683 | } |
| 684 | return intval( $match[1] ); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Get the network's sharing endpoints. |
| 689 | * |
| 690 | * @since 4.0 |
| 691 | * |
| 692 | * @param bool $networks_only Networks only. |
| 693 | * @return array |
| 694 | */ |
| 695 | public static function get_sharing_endpoints( $networks_only = true ) { |
| 696 | |
| 697 | if ( $networks_only ) { |
| 698 | return apply_filters( |
| 699 | 'hustle_networks_with_share_enpoints', |
| 700 | array( 'facebook', 'twitter', 'pinterest', 'reddit', 'linkedin', 'vkontakte', 'whatsapp', 'email' ) |
| 701 | ); |
| 702 | |
| 703 | } |
| 704 | |
| 705 | global $wp; |
| 706 | $current_url = rawurlencode( home_url( $wp->request ) ); |
| 707 | |
| 708 | // let users filter the title. |
| 709 | $title = apply_filters( 'hustle_social_share_platform_title', rawurlencode( html_entity_decode( esc_html( get_the_title() ) ) ) ); |
| 710 | |
| 711 | return apply_filters( |
| 712 | 'hustle_native_share_enpoints', |
| 713 | array( |
| 714 | |
| 715 | 'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . $current_url, |
| 716 | 'twitter' => 'https://twitter.com/intent/tweet?url=' . $current_url . '&text=' . $title, |
| 717 | 'pinterest' => 'https://www.pinterest.com/pin/create/button/?url=' . $current_url, |
| 718 | 'reddit' => 'https://www.reddit.com/submit?url=' . $current_url, |
| 719 | 'linkedin' => 'https://www.linkedin.com/shareArticle?mini=true&url=' . $current_url, |
| 720 | 'vkontakte' => 'https://vk.com/share.php?url=' . $current_url, |
| 721 | 'whatsapp' => 'https://api.whatsapp.com/send?text=' . $current_url, |
| 722 | 'email' => 'mailto:?subject=' . $title . '&body=' . $current_url, |
| 723 | |
| 724 | ), |
| 725 | $current_url |
| 726 | ); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Get renderer |
| 731 | * |
| 732 | * @return \Hustle_Renderer_Sshare |
| 733 | */ |
| 734 | public function get_renderer() { |
| 735 | return new Hustle_Renderer_Sshare(); |
| 736 | } |
| 737 | |
| 738 | } |
| 739 |