| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register Custom Endpoint |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Base; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
use Timetics\Utils\Singleton; |
| 12 |
|
| 13 |
class Custom_Endpoint { |
| 14 |
use Singleton; |
| 15 |
|
| 16 |
/** |
| 17 |
* Initialize |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public function init() { |
| 22 |
add_action( 'init', [$this, 'register'] ); |
| 23 |
add_action( 'init', [$this, 'maybe_flush_rules'], 99 ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Register all custom endpoints |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function register() { |
| 32 |
$endpoints = $this->get_endpoints(); |
| 33 |
|
| 34 |
foreach ( $endpoints as $endpoint ) { |
| 35 |
add_rewrite_endpoint( $endpoint, EP_ALL ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Flush rewrite rules once per plugin version (after upgrade or fresh install). |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function maybe_flush_rules() { |
| 45 |
$stored = get_option( 'timetics_rewrite_version' ); |
| 46 |
|
| 47 |
if ( defined( 'TIMETICS_VERSION' ) && $stored !== TIMETICS_VERSION ) { |
| 48 |
flush_rewrite_rules( false ); |
| 49 |
update_option( 'timetics_rewrite_version', TIMETICS_VERSION, false ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get all endpoints |
| 55 |
* |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function get_endpoints() { |
| 59 |
/** |
| 60 |
* All endpoints that have to be register |
| 61 |
*/ |
| 62 |
return [ |
| 63 |
'timetics-integration', |
| 64 |
]; |
| 65 |
} |
| 66 |
}; |
| 67 |
|