controller
7 years ago
core
9 years ago
Ga_Admin.php
7 years ago
Ga_Autoloader.php
9 years ago
Ga_Frontend.php
9 years ago
Ga_Helper.php
8 years ago
Ga_Hook.php
9 years ago
Ga_Notice.php
9 years ago
Ga_Sharethis.php
8 years ago
Ga_Stats.php
9 years ago
Ga_Sharethis.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Ga_Sharethis class |
| 5 | * |
| 6 | * Preparing request and parsing response from Sharethis Platform Api |
| 7 | * |
| 8 | * @author wle@adips.com |
| 9 | * @version 1.0 |
| 10 | */ |
| 11 | class Ga_Sharethis { |
| 12 | |
| 13 | const GA_SHARETHIS_ALERTS_ERROR = 'Trending content alerts are temporarily unavailable, please try again later or contact support@sharethis.com'; |
| 14 | |
| 15 | public static function get_body( $data ) { |
| 16 | $body = $data->getBody(); |
| 17 | return json_decode( $body ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Create sharethis options |
| 22 | */ |
| 23 | public static function create_sharethis_options( $api_client ) { |
| 24 | $data = array(); |
| 25 | $parsed_url = parse_url( get_option( 'siteurl' ) ); |
| 26 | if ( Ga_Helper::should_create_sharethis_property() ) { |
| 27 | $domain = $parsed_url['host'] . ( !empty( $parsed_url['path'] ) ? $parsed_url['path'] : '' ); |
| 28 | $query_params = array( |
| 29 | 'domain' => $domain, |
| 30 | 'is_wordpress' => true, |
| 31 | ); |
| 32 | $response = $api_client->call( 'ga_api_create_sharethis_property', array( |
| 33 | $query_params |
| 34 | ) ); |
| 35 | $sharethis_options = self::get_sharethis_options( $response ); |
| 36 | if ( !empty( $sharethis_options[ 'id' ] ) ) { |
| 37 | add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID, $sharethis_options[ 'id' ] ); |
| 38 | } |
| 39 | if ( !empty( $sharethis_options[ 'secret' ] ) ) { |
| 40 | add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET, $sharethis_options[ 'secret' ] ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $data; |
| 45 | } |
| 46 | |
| 47 | public static function get_sharethis_options( $response ) { |
| 48 | $body = self::get_body( $response ); |
| 49 | $options = array(); |
| 50 | if ( !empty( $body ) ) { |
| 51 | foreach ( $body as $key => $value ) { |
| 52 | if ( $key == '_id' ) { |
| 53 | $options[ 'id' ] = $value; |
| 54 | } else if ( $key == 'secret' ) { |
| 55 | $options[ 'secret' ] = $value; |
| 56 | } else if ( $key == 'error' ) { |
| 57 | $options[ 'error' ] = $value; |
| 58 | } |
| 59 | } |
| 60 | } else { |
| 61 | $options[ 'error' ] = self::GA_SHARETHIS_ALERTS_ERROR; |
| 62 | } |
| 63 | return $options; |
| 64 | } |
| 65 | |
| 66 | public static function sharethis_installation_verification( $api_client ) { |
| 67 | if ( Ga_Helper::should_verify_sharethis_installation() ) { |
| 68 | $query_params = array( |
| 69 | 'id' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ), |
| 70 | 'secret' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ) |
| 71 | ); |
| 72 | $response = $api_client->call( 'ga_api_sharethis_installation_verification', array( |
| 73 | $query_params |
| 74 | ) ); |
| 75 | $result = self::get_verification_result( $response ); |
| 76 | if ( !empty( $result ) ) { |
| 77 | add_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT, true ); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | public static function get_verification_result( $response ) { |
| 83 | $body = self::get_body( $response ); |
| 84 | if ( !empty( $body->{"status"} ) ) { |
| 85 | return true; |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | public static function load_sharethis_trending_alerts( $api_client ) { |
| 91 | if ( Ga_Helper::should_load_trending_alerts() ) { |
| 92 | $query_params = array( |
| 93 | 'id' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ), |
| 94 | 'secret' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ) |
| 95 | ); |
| 96 | $response = $api_client->call( 'ga_api_sharethis_get_trending_alerts', array( |
| 97 | $query_params |
| 98 | ) ); |
| 99 | return self::get_alerts( $response ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public static function get_alerts( $response ) { |
| 104 | $body = self::get_body( $response ); |
| 105 | if ( !empty( $body ) ) { |
| 106 | if ( !empty( $body[ 'error' ] ) ) { |
| 107 | return (object) array( 'error' => self::GA_SHARETHIS_ALERTS_ERROR ); |
| 108 | } |
| 109 | |
| 110 | return $body; |
| 111 | } else { |
| 112 | return array(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |
| 117 |