controller
4 years ago
core
4 years ago
class-ga-admin.php
3 years ago
class-ga-autoloader.php
4 years ago
class-ga-frontend.php
3 years ago
class-ga-helper.php
3 years ago
class-ga-hook.php
4 years ago
class-ga-notice.php
4 years ago
class-ga-sharethis.php
4 years ago
class-ga-stats.php
3 years ago
class-ga-template.php
4 years ago
class-ga-sharethis.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Ga_Sharethis class |
| 4 | * |
| 5 | * Preparing request and parsing response from Sharethis Platform Api. |
| 6 | * |
| 7 | * @package GoogleAnalytics |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Sharethis class. |
| 12 | */ |
| 13 | class Ga_Sharethis { |
| 14 | |
| 15 | /** |
| 16 | * Get body decoded from JSON. |
| 17 | * |
| 18 | * @param string $data Data string. |
| 19 | * |
| 20 | * @return mixed |
| 21 | */ |
| 22 | public static function get_body( $data ) { |
| 23 | $body = $data->getBody(); |
| 24 | return json_decode( $body ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Create sharethis options. |
| 29 | * |
| 30 | * @param object $api_client API client. |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | public static function create_sharethis_options( $api_client ) { |
| 35 | $data = array(); |
| 36 | $parsed_url = wp_parse_url( get_option( 'siteurl' ) ); |
| 37 | $domain = $parsed_url['host'] . ( ! empty( $parsed_url['path'] ) ? $parsed_url['path'] : '' ); |
| 38 | $query_params = array( |
| 39 | 'domain' => $domain, |
| 40 | 'is_wordpress' => true, |
| 41 | 'onboarding_product' => 'ga', |
| 42 | ); |
| 43 | $response = $api_client->call( |
| 44 | 'ga_api_create_sharethis_property', |
| 45 | array( |
| 46 | $query_params, |
| 47 | ) |
| 48 | ); |
| 49 | $sharethis_options = self::get_sharethis_options( $response ); |
| 50 | if ( ! empty( $sharethis_options['id'] ) ) { |
| 51 | add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID, $sharethis_options['id'] ); |
| 52 | } |
| 53 | if ( ! empty( $sharethis_options['secret'] ) ) { |
| 54 | add_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET, $sharethis_options['secret'] ); |
| 55 | } |
| 56 | |
| 57 | return $data; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get ShareThis options array. |
| 62 | * |
| 63 | * @param object $response Response object. |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | public static function get_sharethis_options( $response ) { |
| 68 | $body = self::get_body( $response ); |
| 69 | $options = array(); |
| 70 | if ( ! empty( $body ) ) { |
| 71 | foreach ( $body as $key => $value ) { |
| 72 | if ( '_id' === $key ) { |
| 73 | $options['id'] = $value; |
| 74 | } elseif ( 'secret' === $key ) { |
| 75 | $options['secret'] = $value; |
| 76 | } elseif ( 'error' === $key ) { |
| 77 | $options['error'] = $value; |
| 78 | } |
| 79 | } |
| 80 | } else { |
| 81 | $options['error'] = 'error'; |
| 82 | } |
| 83 | return $options; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Installation verification. |
| 88 | * |
| 89 | * @param object $api_client API Client object. |
| 90 | * |
| 91 | * @return void |
| 92 | */ |
| 93 | public static function sharethis_installation_verification( $api_client ) { |
| 94 | if ( Ga_Helper::should_verify_sharethis_installation() ) { |
| 95 | $query_params = array( |
| 96 | 'id' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ), |
| 97 | 'secret' => get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET ), |
| 98 | ); |
| 99 | $response = $api_client->call( |
| 100 | 'ga_api_sharethis_installation_verification', |
| 101 | array( |
| 102 | $query_params, |
| 103 | ) |
| 104 | ); |
| 105 | $result = self::get_verification_result( $response ); |
| 106 | if ( ! empty( $result ) ) { |
| 107 | add_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT, true ); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get verification result. |
| 114 | * |
| 115 | * @param object $response Response object. |
| 116 | * |
| 117 | * @return bool |
| 118 | */ |
| 119 | public static function get_verification_result( $response ) { |
| 120 | $body = self::get_body( $response ); |
| 121 | if ( ! empty( $body->{'status'} ) ) { |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get alerts. |
| 129 | * |
| 130 | * @param object $response Response object. |
| 131 | * |
| 132 | * @return array|mixed|object |
| 133 | */ |
| 134 | public static function get_alerts( $response ) { |
| 135 | $body = self::get_body( $response ); |
| 136 | if ( false === empty( $body ) ) { |
| 137 | if ( false === empty( $body['error'] ) ) { |
| 138 | return (object) array( 'error' => $body['error'] ); |
| 139 | } |
| 140 | |
| 141 | return $body; |
| 142 | } else { |
| 143 | return array(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 |