/var/www/html_uk/wp-content/plugins/automatewoo/includes/Integrations/Bitly.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace AutomateWoo;

defined'ABSPATH' ) || exit;

/**
 * @class Integration_Bitly
 * @since 3.9
 */
class Integration_Bitly extends Integration {

    
/** @var string */
    
public $integration_id 'bitly';

    
/** @var string */
    
private $api_key;

    
/** @var string */
    
private $api_base_url 'https://api-ssl.bitly.com/v4';

    
/**
     * @param string $api_key
     */
    
public function __construct$api_key ) {
        
$this->api_key $api_key;
    }

    
/**
     * Test the current API key to confirm that AutomateWoo can communicate with the Bitly API
     *
     * @return bool
     */
    
public function test_integration(): bool {
        return 
$this->request'GET''/user' )->is_successful();
    }

    
/**
     * Check if the integration is enabled.
     *
     * @return bool
     */
    
public function is_enabled(): bool {
        return (bool) 
Options::bitly_enabled();
    }

    
/**
     * @param string $long_url
     * @param bool   $ignore_cache
     *
     * @return string|false
     */
    
public function shorten_url$long_url$ignore_cache false ) {
        
$cache_key md5$long_url );
        
$cache     Cache::get$cache_key'bitly' );
        if ( ! 
$ignore_cache && $cache ) {
            return 
$cache;
        }

        
$request $this->request(
            
'POST',
            
'/shorten',
            [
                
'long_url' => esc_url_raw$long_url ),
            ]
        );

        if ( 
false === $request || ! $request->is_successful() ) {
            return 
false;
        }

        
$body      $request->get_body();
        
$short_url esc_url_raw$body['link'] );

        
Cache::set$cache_key$short_url'bitly' );

        return 
apply_filters'automatewoo/bitly/shorten_url'$short_url$long_url );
    }

    
/**
     * @param string $text
     * @return string
     */
    
public function shorten_urls_in_text$text ) {
        
$replacer = new Replace_Helper$text, [ $this'shorten_url' ], 'text_urls' );
        return 
$replacer->process();
    }

    
/**
     * @param string $method
     * @param string $endpoint
     * @param array  $args
     *
     * @return Remote_Request|false
     */
    
public function request$method$endpoint$args = [] ) {
        
$method       strtoupper$method );
        
$request_args = [
            
'timeout'   => 10,
            
'method'    => $method,
            
'sslverify' => false,
            
'headers'   => [
                
'Authorization' => "Bearer {$this->api_key}",
            ],
        ];

        
$url "{$this->api_base_url}{$endpoint}";

        switch ( 
$method ) {
            case 
'GET':
                
// SEMGREP WARNING EXPLANATION
                // This is escaped with esc_url_raw, but semgrep only takes into consideration esc_url.
                
$url esc_url_rawadd_query_argarray_map'urlencode'$args ), $url ) );
                break;

            case 
'POST':
                
$json wp_json_encode$args );
                if ( 
false === $json ) {
                    return 
false;
                } else {
                    
$request_args['body'] = $json;
                }

                
$request_args['headers']['Content-Type'] = 'application/json';
                break;

            default:
                return 
false;
        }

        
$request = new Remote_Request$url$request_args );
        
$this->maybe_log_request_errors$request );

        return 
$request;
    }
}