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