| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
class LocatoraidRest extends hcWpBase6 |
| 3 |
{ |
| 4 |
public $authCode; |
| 5 |
|
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
register_activation_hook( __FILE__, array($this, 'onActivate') ); |
| 9 |
|
| 10 |
$parent = $this->findParent(); |
| 11 |
if( ! $parent ){ |
| 12 |
$error = 'Locatorad REST requires either Locatoraid or Locatoraid Pro active'; |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
parent::__construct( |
| 17 |
array($parent, 'lc'), // app |
| 18 |
__FILE__, // path, |
| 19 |
'', // hc product, |
| 20 |
'locatoraid', // slug |
| 21 |
'lctr2' // db prefix |
| 22 |
); |
| 23 |
|
| 24 |
add_action( 'init', array($this, '_this_init') ); |
| 25 |
} |
| 26 |
|
| 27 |
public function onActivate() |
| 28 |
{ |
| 29 |
$parent = $this->findParent(); |
| 30 |
if( ! $parent ){ |
| 31 |
deactivate_plugins( __FILE__ ); |
| 32 |
$error = 'Locatorad REST requires either Locatoraid or Locatoraid Pro active'; |
| 33 |
die( $error ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public function findParent() |
| 38 |
{ |
| 39 |
$ret = NULL; |
| 40 |
|
| 41 |
$parents = array( 'locatoraid-pro', 'locatoraid' ); |
| 42 |
foreach( $parents as $thisParent ){ |
| 43 |
$confFile = dirname( __FILE__ ) . '/config/' . $thisParent . '.php'; |
| 44 |
$className = $thisParent; |
| 45 |
// if( class_exists($className) && file_exists($confFile) ){ |
| 46 |
if( file_exists($confFile) ){ |
| 47 |
$ret = $thisParent; |
| 48 |
break; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $ret; |
| 53 |
} |
| 54 |
|
| 55 |
public function _this_init() |
| 56 |
{ |
| 57 |
$this->hcapp_start(); |
| 58 |
add_action( 'rest_api_init', array($this, 'routes') ); |
| 59 |
|
| 60 |
$authOptionName = 'locatoraid-rest_auth_code'; |
| 61 |
register_setting( 'locatoraid-rest', $authOptionName ); |
| 62 |
$this->initOption(); |
| 63 |
} |
| 64 |
|
| 65 |
public function initOption() |
| 66 |
{ |
| 67 |
$authOptionName = 'locatoraid-rest_auth_code'; |
| 68 |
$v = get_option( $authOptionName, '' ); |
| 69 |
|
| 70 |
if( ! strlen($v) ){ |
| 71 |
// $salt .= 'abcdefghijklmnopqrstuvxyz'; |
| 72 |
// $salt = '123456789abcdef'; |
| 73 |
$salt = '123456789'; |
| 74 |
$len = 12; |
| 75 |
|
| 76 |
$v = array(); |
| 77 |
$i = 1; |
| 78 |
while ( $i <= $len ){ |
| 79 |
$num = rand() % strlen($salt); |
| 80 |
$tmp = substr($salt, $num, 1); |
| 81 |
$v[] = $tmp; |
| 82 |
$i++; |
| 83 |
} |
| 84 |
shuffle( $v ); |
| 85 |
$v = join( '', $v ); |
| 86 |
|
| 87 |
update_option( $authOptionName, $v ); |
| 88 |
} |
| 89 |
|
| 90 |
$this->authCode = $v; |
| 91 |
} |
| 92 |
|
| 93 |
public function admin_menu() |
| 94 |
{ |
| 95 |
$menuSlug = 'locatoraid-rest'; |
| 96 |
$menuTitle = 'Locatoraid REST'; |
| 97 |
|
| 98 |
$page = add_menu_page( |
| 99 |
$menuTitle, |
| 100 |
$menuTitle, |
| 101 |
'read', |
| 102 |
$menuSlug, |
| 103 |
array($this, 'adminView'), |
| 104 |
'', |
| 105 |
31 |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
public function routes() |
| 110 |
{ |
| 111 |
register_rest_route( 'locatoraid/v3', '/locations', |
| 112 |
array( |
| 113 |
array( |
| 114 |
'methods' => WP_REST_Server::READABLE, |
| 115 |
'callback' => array($this, 'locationsGet') |
| 116 |
), |
| 117 |
array( |
| 118 |
'methods' => WP_REST_Server::CREATABLE, |
| 119 |
'callback' => array($this, 'locationsCreate'), |
| 120 |
'permission_callback' => array( $this, 'checkAdmin' ), |
| 121 |
), |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
register_rest_route( 'locatoraid/v3', '/locations/(?P<id>\d+)', |
| 126 |
array( |
| 127 |
array( |
| 128 |
'methods' => WP_REST_Server::READABLE, |
| 129 |
'callback' => array( $this, 'locationsIdGet' ) |
| 130 |
), |
| 131 |
array( |
| 132 |
'methods' => WP_REST_Server::DELETABLE, |
| 133 |
'callback' => array( $this, 'locationsIdDelete' ), |
| 134 |
'permission_callback' => array( $this, 'checkAdmin' ), |
| 135 |
), |
| 136 |
) |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
public function checkAdmin( $request ) |
| 141 |
{ |
| 142 |
$authCode = $request->get_header( 'X-WP-Locatoraid-AuthCode' ); |
| 143 |
|
| 144 |
if( $authCode && ($authCode == $this->authCode) ){ |
| 145 |
$ret = TRUE; |
| 146 |
return $ret; |
| 147 |
} |
| 148 |
|
| 149 |
$errors = 'not allowed'; |
| 150 |
$ret = new WP_Error( 'error', $errors, array('status' => 500) ); |
| 151 |
sleep( 1 ); |
| 152 |
|
| 153 |
return $ret; |
| 154 |
} |
| 155 |
|
| 156 |
public function locationsGet( $request ) |
| 157 |
{ |
| 158 |
$command = $this->hcapp->make('/locations/commands/read'); |
| 159 |
|
| 160 |
$queryParams = $request->get_query_params(); |
| 161 |
$page = isset($queryParams['page']) ? $queryParams['page'] : 1; |
| 162 |
$perPage = isset($queryParams['per_page']) ? $queryParams['per_page'] : 20; |
| 163 |
$search = isset($queryParams['search']) ? $queryParams['search'] : NULL; |
| 164 |
|
| 165 |
$countArgs = array(); |
| 166 |
$countArgs[] = 'count'; |
| 167 |
if( $search ){ |
| 168 |
$countArgs[] = array('search', $search); |
| 169 |
} |
| 170 |
|
| 171 |
$totalCount = $command |
| 172 |
->execute( $countArgs ) |
| 173 |
; |
| 174 |
|
| 175 |
$limit = $perPage; |
| 176 |
$numberOfPages = 1; |
| 177 |
|
| 178 |
if( $totalCount > $perPage ){ |
| 179 |
$pager = $this->hcapp->make('/html/pager') |
| 180 |
->set_total_count( $totalCount ) |
| 181 |
->set_per_page( $perPage ) |
| 182 |
; |
| 183 |
|
| 184 |
$numberOfPages = $pager->number_of_pages(); |
| 185 |
|
| 186 |
if( $page > $numberOfPages ){ |
| 187 |
$page = $numberOfPages; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
$commandArgs = array(); |
| 192 |
$commandArgs[] = array('with', '-all-'); |
| 193 |
|
| 194 |
if( $page && $page > 1 ){ |
| 195 |
$commandArgs[] = array('limit', $perPage, ($page - 1) * $perPage); |
| 196 |
} |
| 197 |
else { |
| 198 |
$commandArgs[] = array('limit', $perPage); |
| 199 |
} |
| 200 |
|
| 201 |
if( $search ){ |
| 202 |
$commandArgs[] = array('search', $search); |
| 203 |
} |
| 204 |
|
| 205 |
$entries = $command |
| 206 |
->execute( $commandArgs ) |
| 207 |
; |
| 208 |
|
| 209 |
$response = new WP_REST_Response( $entries ); |
| 210 |
$response->header( 'X-WP-Total', (int) $totalCount ); |
| 211 |
$response->header( 'X-WP-TotalPages', (int) $numberOfPages ); |
| 212 |
return $response; |
| 213 |
} |
| 214 |
|
| 215 |
public function locationsCreate( $request ) |
| 216 |
{ |
| 217 |
$values = $request->get_body_params(); |
| 218 |
|
| 219 |
$keys = array_keys($values); |
| 220 |
foreach( $keys as $k ){ |
| 221 |
if( 'product:' == substr($k, 0, strlen('product:')) ){ |
| 222 |
$newK = str_replace( '_', ' ', $k ); |
| 223 |
$values[ $newK ] = $values[ $k ]; |
| 224 |
unset( $values[$k] ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
$cm = $this->hcapp->make('/commands/manager'); |
| 229 |
|
| 230 |
$command = $this->hcapp->make('/locations/commands/create'); |
| 231 |
$command |
| 232 |
->execute( $values ) |
| 233 |
; |
| 234 |
|
| 235 |
$errors = $cm->errors( $command ); |
| 236 |
if( $errors ){ |
| 237 |
return new WP_Error( 'error', $errors, array('status' => 500) ); |
| 238 |
} |
| 239 |
|
| 240 |
$results = $cm->results( $command ); |
| 241 |
return $results['id']; |
| 242 |
} |
| 243 |
|
| 244 |
public function locationsIdGet( $request ) |
| 245 |
{ |
| 246 |
$command = $this->hcapp->make('/locations/commands/read'); |
| 247 |
|
| 248 |
$id = $request['id']; |
| 249 |
$args[] = $id; |
| 250 |
$args[] = array('with', '-all-', 'flat'); |
| 251 |
|
| 252 |
$model = $command |
| 253 |
->execute( $args ) |
| 254 |
; |
| 255 |
|
| 256 |
if( ! $model ){ |
| 257 |
return new WP_Error( 'no_location', 'Invalid Location', array('status' => 404) ); |
| 258 |
} |
| 259 |
|
| 260 |
return $model; |
| 261 |
} |
| 262 |
|
| 263 |
public function locationsIdDelete( $request ) |
| 264 |
{ |
| 265 |
$command = $this->hcapp->make('/locations/commands/read'); |
| 266 |
|
| 267 |
$id = $request['id']; |
| 268 |
$args[] = $id; |
| 269 |
|
| 270 |
$model = $command |
| 271 |
->execute( $args ) |
| 272 |
; |
| 273 |
|
| 274 |
if( ! $model ){ |
| 275 |
return new WP_Error( 'no_location', 'Invalid Location', array('status' => 404) ); |
| 276 |
} |
| 277 |
|
| 278 |
$command = $this->hcapp->make('/locations/commands/delete'); |
| 279 |
$response = $command |
| 280 |
->execute( $id ) |
| 281 |
; |
| 282 |
|
| 283 |
if( isset($response['errors']) ){ |
| 284 |
return new WP_Error( 'error', $response['errors'], array('status' => 500) ); |
| 285 |
} |
| 286 |
|
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
public function adminView() |
| 291 |
{ |
| 292 |
$app = 'locatoraid-rest'; |
| 293 |
|
| 294 |
if( isset($_POST[$app . '_submit']) ){ |
| 295 |
if( isset($_POST[$app]) ){ |
| 296 |
foreach( (array)$_POST[$app] as $key => $value ){ |
| 297 |
$option_name = $app . '_' . $key; |
| 298 |
$value = sanitize_text_field( $value ); |
| 299 |
update_option( $option_name, $value ); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// $this->initOption(); |
| 305 |
|
| 306 |
$current = array(); |
| 307 |
$current['auth_code'] = get_option( $app . '_auth_code', '' ); |
| 308 |
|
| 309 |
// spaghetti starts here |
| 310 |
?> |
| 311 |
|
| 312 |
<div class="wrap"> |
| 313 |
<h2>Locatoraid REST Interface</h2> |
| 314 |
|
| 315 |
<?php if( isset($_POST[$app . '_submit']) ) : ?> |
| 316 |
<div id="message" class="updated fade"> |
| 317 |
<p> |
| 318 |
<?php _e( 'Settings Saved', 'locatoraid' ) ?> |
| 319 |
</p> |
| 320 |
</div> |
| 321 |
<?php endif; ?> |
| 322 |
|
| 323 |
<form method="post" action=""> |
| 324 |
<?php settings_fields( $app ); ?> |
| 325 |
<?php //do_settings_sections( $this->app ); ?> |
| 326 |
<table class="form-table"> |
| 327 |
<tr valign="top"> |
| 328 |
<th scope="row">X-WP-Locatoraid-AuthCode</th> |
| 329 |
<td> |
| 330 |
<input type="text" name="<?php echo $app; ?>[auth_code]" value="<?php echo esc_attr( $current['auth_code'] ); ?>" /> |
| 331 |
<input name="<?php echo $app; ?>_submit" type="submit" class="button-primary" value="Save" /> |
| 332 |
|
| 333 |
<div> |
| 334 |
For create, update and delete operations. |
| 335 |
</div> |
| 336 |
</td> |
| 337 |
<td> |
| 338 |
</td> |
| 339 |
</tr> |
| 340 |
|
| 341 |
</table> |
| 342 |
</form> |
| 343 |
|
| 344 |
<h3 style="text-decoration: underline;">List locations</h3> |
| 345 |
<?php |
| 346 |
$url = '/locatoraid/v3/locations'; |
| 347 |
$fullUrl = get_rest_url( NULL, $url ); |
| 348 |
?> |
| 349 |
|
| 350 |
<p> |
| 351 |
<strong> |
| 352 |
GET <?php echo $url; ?> |
| 353 |
</strong> |
| 354 |
</p> |
| 355 |
|
| 356 |
<p> |
| 357 |
<strong>Arguments</strong> |
| 358 |
</p> |
| 359 |
|
| 360 |
<p> |
| 361 |
page<br/> |
| 362 |
per_page<br/> |
| 363 |
search<br/> |
| 364 |
</p> |
| 365 |
|
| 366 |
<p> |
| 367 |
<strong>Examples</strong> |
| 368 |
</p> |
| 369 |
|
| 370 |
<p> |
| 371 |
<code> |
| 372 |
GET <?php echo $fullUrl; ?> |
| 373 |
</code> |
| 374 |
</p> |
| 375 |
|
| 376 |
<p> |
| 377 |
<code> |
| 378 |
GET <?php echo $fullUrl; ?>?page=2&per_page=100 |
| 379 |
</code> |
| 380 |
</p> |
| 381 |
|
| 382 |
<p> |
| 383 |
<code> |
| 384 |
GET <?php echo $fullUrl; ?>?search=helsinki |
| 385 |
</code> |
| 386 |
</p> |
| 387 |
|
| 388 |
<h3 style="text-decoration: underline;">Retrieve a location</h3> |
| 389 |
<?php |
| 390 |
$url = '/locatoraid/v3/locations/<id>'; |
| 391 |
$fullUrl = get_rest_url( NULL, $url ); |
| 392 |
?> |
| 393 |
|
| 394 |
<p> |
| 395 |
<strong> |
| 396 |
GET <?php echo $url; ?> |
| 397 |
</strong> |
| 398 |
</p> |
| 399 |
|
| 400 |
<p> |
| 401 |
<strong>Examples</strong> |
| 402 |
</p> |
| 403 |
|
| 404 |
<p> |
| 405 |
<code> |
| 406 |
GET <?php echo $fullUrl; ?> |
| 407 |
</code> |
| 408 |
</p> |
| 409 |
|
| 410 |
|
| 411 |
<h3 style="text-decoration: underline;">Delete a location</h3> |
| 412 |
<?php |
| 413 |
$url = '/locatoraid/v3/locations/<id>'; |
| 414 |
$fullUrl = get_rest_url( NULL, $url ); |
| 415 |
?> |
| 416 |
|
| 417 |
<p> |
| 418 |
<strong> |
| 419 |
DELETE <?php echo $url; ?> |
| 420 |
</strong> |
| 421 |
</p> |
| 422 |
|
| 423 |
<p> |
| 424 |
<strong>Headers</strong> |
| 425 |
</p> |
| 426 |
|
| 427 |
<p> |
| 428 |
X-WP-Locatoraid-AuthCode [required] |
| 429 |
</p> |
| 430 |
|
| 431 |
<p> |
| 432 |
<strong>Examples</strong> |
| 433 |
</p> |
| 434 |
|
| 435 |
<p> |
| 436 |
<code> |
| 437 |
DELETE <?php echo $fullUrl; ?> |
| 438 |
</code> |
| 439 |
</p> |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
<h3 style="text-decoration: underline;">Create a location</h3> |
| 444 |
<?php |
| 445 |
$url = '/locatoraid/v3/locations'; |
| 446 |
$fullUrl = get_rest_url( NULL, $url ); |
| 447 |
?> |
| 448 |
|
| 449 |
<p> |
| 450 |
<strong> |
| 451 |
POST <?php echo $url; ?> |
| 452 |
</strong> |
| 453 |
</p> |
| 454 |
|
| 455 |
<p> |
| 456 |
<strong>Headers</strong> |
| 457 |
</p> |
| 458 |
|
| 459 |
<p> |
| 460 |
X-WP-Locatoraid-AuthCode [required] |
| 461 |
</p> |
| 462 |
|
| 463 |
<p> |
| 464 |
<strong>Arguments</strong> |
| 465 |
</p> |
| 466 |
|
| 467 |
<?php |
| 468 |
$p = $this->hcapp->make('/locations/presenter'); |
| 469 |
$fields = $p->database_fields(); |
| 470 |
?> |
| 471 |
|
| 472 |
<p> |
| 473 |
<?php foreach( $fields as $f ) : ?> |
| 474 |
<?php echo $f; ?><br/> |
| 475 |
<?php endforeach; ?> |
| 476 |
</p> |
| 477 |
|
| 478 |
<p> |
| 479 |
<strong>Examples</strong> |
| 480 |
</p> |
| 481 |
|
| 482 |
<p> |
| 483 |
<code> |
| 484 |
POST <?php echo $fullUrl; ?> |
| 485 |
</code> |
| 486 |
</p> |
| 487 |
|
| 488 |
</div> |
| 489 |
|
| 490 |
<?php |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
$locatoraidRest = new LocatoraidRest(); |
| 495 |
|