| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_API_Base |
| 5 |
* |
| 6 |
* Base class for api |
| 7 |
* |
| 8 |
* @since 3.2.6 |
| 9 |
*/ |
| 10 |
abstract class LP_Abstract_API { |
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
public $version = 'v1'; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public $endpoint = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var WC_REST_Controller[]|string[] |
| 23 |
*/ |
| 24 |
public $controllers = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* LP_API_Base constructor. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->rest_api_init(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Init REST. |
| 35 |
* |
| 36 |
* @since 3.2.6 |
| 37 |
*/ |
| 38 |
public function rest_api_init() { |
| 39 |
if ( ! class_exists( 'WP_REST_Server' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$this->rest_api_includes(); |
| 44 |
|
| 45 |
add_action( 'rest_api_init', array( $this, 'rest_api_register_routes' ), 10 ); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
public function rest_api_includes() { |
| 50 |
// include_once realpath( LP_PLUGIN_PATH . 'inc/rest-api/class-lp-rest-authentication.php' ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Register routes |
| 55 |
* |
| 56 |
* @since 3.2.6 |
| 57 |
*/ |
| 58 |
public function rest_api_register_routes() { |
| 59 |
if ( ! $this->controllers ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$controllers = array(); |
| 64 |
|
| 65 |
foreach ( $this->controllers as $name => $controller ) { |
| 66 |
|
| 67 |
if ( is_string( $controller ) ) { |
| 68 |
$name = $controller; |
| 69 |
$controllers[ $name ] = new $controller(); |
| 70 |
} else { |
| 71 |
$controllers[ $name ] = $controller; |
| 72 |
} |
| 73 |
|
| 74 |
$controllers[ $name ]->register_routes(); |
| 75 |
} |
| 76 |
|
| 77 |
$this->controllers = $controllers; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Check permission if user is administrator. |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public static function check_admin_permission(): bool { |
| 86 |
return current_user_can( ADMIN_ROLE ); |
| 87 |
} |
| 88 |
} |
| 89 |
|