| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\API; |
| 4 |
|
| 5 |
class Tour extends API { |
| 6 |
|
| 7 |
private static $tour_meta_key = 'tour_status'; |
| 8 |
|
| 9 |
public function register_routes() { |
| 10 |
$this->get( 'tour/status', [ $this, 'get_status' ] ); |
| 11 |
$this->post( 'tour/complete', [ $this, 'mark_complete' ] ); |
| 12 |
$this->post( 'tour/reset', [ $this, 'reset' ] ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Get the current tour completion status. |
| 17 |
* |
| 18 |
* @return \WP_REST_Response |
| 19 |
*/ |
| 20 |
public function get_status() { |
| 21 |
$status = $this->utils( 'options' )->get( self::$tour_meta_key, [] ); |
| 22 |
|
| 23 |
if ( ! is_array( $status ) ) { |
| 24 |
$status = []; |
| 25 |
} |
| 26 |
|
| 27 |
return $this->success( $status ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Mark a tour as complete. |
| 32 |
* |
| 33 |
* Expects a `tour_key` parameter (e.g. "templately_library_tour_v1"). |
| 34 |
* |
| 35 |
* @return \WP_REST_Response|\WP_Error |
| 36 |
*/ |
| 37 |
public function mark_complete() { |
| 38 |
$tour_key = $this->get_param( 'tour_key', '' ); |
| 39 |
|
| 40 |
if ( empty( $tour_key ) ) { |
| 41 |
return $this->error( 'missing_tour_key', __( 'tour_key is required.', 'templately' ), 'tour/complete', 400 ); |
| 42 |
} |
| 43 |
|
| 44 |
$status = $this->utils( 'options' )->get( self::$tour_meta_key, [] ); |
| 45 |
|
| 46 |
if ( ! is_array( $status ) ) { |
| 47 |
$status = []; |
| 48 |
} |
| 49 |
|
| 50 |
$status[ $tour_key ] = 'done'; |
| 51 |
|
| 52 |
$this->utils( 'options' )->set( self::$tour_meta_key, $status ); |
| 53 |
|
| 54 |
return $this->success( $status ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Reset one or all tours. |
| 59 |
* |
| 60 |
* Optional `tour_key` parameter — if omitted, resets all tours. |
| 61 |
* |
| 62 |
* @return \WP_REST_Response |
| 63 |
*/ |
| 64 |
public function reset() { |
| 65 |
$tour_key = $this->get_param( 'tour_key', '' ); |
| 66 |
|
| 67 |
if ( ! empty( $tour_key ) ) { |
| 68 |
$status = $this->utils( 'options' )->get( self::$tour_meta_key, [] ); |
| 69 |
|
| 70 |
if ( ! is_array( $status ) ) { |
| 71 |
$status = []; |
| 72 |
} |
| 73 |
|
| 74 |
unset( $status[ $tour_key ] ); |
| 75 |
|
| 76 |
$this->utils( 'options' )->set( self::$tour_meta_key, $status ); |
| 77 |
|
| 78 |
return $this->success( $status ); |
| 79 |
} |
| 80 |
|
| 81 |
// Reset all |
| 82 |
$this->utils( 'options' )->set( self::$tour_meta_key, [] ); |
| 83 |
|
| 84 |
return $this->success( [] ); |
| 85 |
} |
| 86 |
} |
| 87 |
|