PluginProbe
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP / 2.4.13
BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP v2.4.13
3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 3.0.0 2.4.13 2.4.12 2.4.11 2.4.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 110 releases
betterlinks / includes / API / Geolocation.php

Geolocation.php in BetterLinks – Link Shortener, Link Cloaking, Redirects, Affiliate Link Manager & MCP 2.4.13, at includes/API/Geolocation.php

230 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BetterLinks\API;
4 if ( ! defined( 'ABSPATH' ) ) { exit; }
5
6 use BetterLinks\Services\CountryDetectionService;
7
8 /**
9 * Geolocation REST API
10 *
11 * Provides backend fallback for frontend geolocation detection
12 */
13 class Geolocation {
14
15 private $namespace = BETTERLINKS_PLUGIN_SLUG . '/v1';
16
17 /**
18 * Initialize hooks
19 */
20 public function __construct() {
21 add_action( 'rest_api_init', [ $this, 'register_routes' ] );
22 }
23
24 /**
25 * Register the routes for geolocation detection
26 */
27 public function register_routes() {
28 register_rest_route(
29 $this->namespace,
30 '/geolocation/detect',
31 array(
32 array(
33 'methods' => \WP_REST_Server::READABLE,
34 'callback' => array( $this, 'detect_country' ),
35 'permission_callback' => '__return_true',
36 ),
37 )
38 );
39
40 // Endpoint to fetch country for a specific IP (for backward compatibility)
41 register_rest_route(
42 $this->namespace,
43 '/geolocation/fetch-by-ip',
44 array(
45 array(
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => array( $this, 'fetch_country_by_ip' ),
48 'permission_callback' => '__return_true',
49 'args' => array(
50 'ip' => array(
51 'required' => true,
52 'type' => 'string',
53 'sanitize_callback' => 'sanitize_text_field',
54 ),
55 ),
56 ),
57 )
58 );
59 }
60
61 /**
62 * Detect country for current user's IP
63 *
64 * This is a fallback endpoint when frontend geolocation fails
65 *
66 * @param \WP_REST_Request $request Full data about the request.
67 * @return \WP_REST_Response
68 */
69 public function detect_country( $request ) {
70 // Check if BetterLinks Pro v2.5.0 or newer is installed
71 if ( ! defined( 'BETTERLINKS_PRO_VERSION' ) || version_compare( BETTERLINKS_PRO_VERSION, '2.5.0', '<' ) ) {
72 return new \WP_REST_Response(
73 array(
74 'success' => false,
75 'message' => 'Country detection requires BetterLinks Pro v2.5.0 or newer',
76 'code' => 'pro_version_required',
77 'data' => null,
78 ),
79 403
80 );
81 }
82
83 // Additional check: Verify Pro plugin has the country tracking function (prevents bypass with old Pro files)
84 if ( ! class_exists( 'BetterLinksPro\\Helper' ) ||
85 ! method_exists( 'BetterLinksPro\\Helper', 'is_country_tracking_enabled' ) ||
86 ! \BetterLinksPro\Helper::is_country_tracking_enabled() ) {
87 return new \WP_REST_Response(
88 array(
89 'success' => false,
90 'message' => 'Please update BetterLinks Pro to v2.5.0 or newer to use this feature',
91 'code' => 'pro_update_required',
92 'data' => null,
93 ),
94 403
95 );
96 }
97
98 $ip = CountryDetectionService::get_current_client_ip();
99
100 if ( ! $ip ) {
101 return new \WP_REST_Response(
102 array(
103 'success' => false,
104 'message' => 'Could not determine client IP',
105 'data' => null,
106 ),
107 400
108 );
109 }
110
111 $country_data = CountryDetectionService::get_country_by_ip( $ip );
112
113 if ( $country_data ) {
114 // Get or create country record and include country_id in response
115 $country_id = CountryDetectionService::get_or_create_country_id(
116 $country_data['country_code'],
117 $country_data['country_name']
118 );
119
120 $response_data = $country_data;
121 if ( $country_id ) {
122 $response_data['country_id'] = $country_id;
123 }
124
125 return new \WP_REST_Response(
126 array(
127 'success' => true,
128 'message' => 'Country detected successfully',
129 'data' => $response_data,
130 ),
131 200
132 );
133 }
134
135 return new \WP_REST_Response(
136 array(
137 'success' => false,
138 'message' => 'Could not detect country for this IP',
139 'data' => null,
140 ),
141 404
142 );
143 }
144
145 /**
146 * Fetch country data for a specific IP address
147 *
148 * Used for backward compatibility to fetch country for existing clicks
149 *
150 * @param \WP_REST_Request $request Full data about the request.
151 * @return \WP_REST_Response
152 */
153 public function fetch_country_by_ip( $request ) {
154 // Check if BetterLinks Pro v2.5.0 or newer is installed
155 if ( ! defined( 'BETTERLINKS_PRO_VERSION' ) || version_compare( BETTERLINKS_PRO_VERSION, '2.5.0', '<' ) ) {
156 return new \WP_REST_Response(
157 array(
158 'success' => false,
159 'message' => 'Country detection requires BetterLinks Pro v2.5.0 or newer',
160 'code' => 'pro_version_required',
161 'data' => null,
162 ),
163 403
164 );
165 }
166
167 // Additional check: Verify Pro plugin has the country tracking function (prevents bypass with old Pro files)
168 if ( ! class_exists( 'BetterLinksPro\\Helper' ) ||
169 ! method_exists( 'BetterLinksPro\\Helper', 'is_country_tracking_enabled' ) ||
170 ! \BetterLinksPro\Helper::is_country_tracking_enabled() ) {
171 return new \WP_REST_Response(
172 array(
173 'success' => false,
174 'message' => 'Please update BetterLinks Pro to v2.5.0 or newer to use this feature',
175 'code' => 'pro_update_required',
176 'data' => null,
177 ),
178 403
179 );
180 }
181
182 $ip = $request->get_param( 'ip' );
183
184 if ( ! $ip || ! filter_var( $ip, FILTER_VALIDATE_IP ) ) {
185 return new \WP_REST_Response(
186 array(
187 'success' => false,
188 'message' => 'Invalid IP address',
189 'data' => null,
190 ),
191 400
192 );
193 }
194
195 $country_data = CountryDetectionService::get_country_by_ip( $ip );
196
197 if ( $country_data ) {
198 // Get or create country record and include country_id in response
199 $country_id = CountryDetectionService::get_or_create_country_id(
200 $country_data['country_code'],
201 $country_data['country_name']
202 );
203
204 $response_data = $country_data;
205 if ( $country_id ) {
206 $response_data['country_id'] = $country_id;
207 }
208
209 return new \WP_REST_Response(
210 array(
211 'success' => true,
212 'message' => 'Country detected successfully',
213 'data' => $response_data,
214 ),
215 200
216 );
217 }
218
219 return new \WP_REST_Response(
220 array(
221 'success' => false,
222 'message' => 'Could not detect country for this IP',
223 'data' => null,
224 ),
225 404
226 );
227 }
228 }
229
230