1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace lsx_health_plan\classes\frontend; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Contains the endpoints |
6
|
|
|
* |
7
|
|
|
* @package lsx-health-plan |
8
|
|
|
*/ |
9
|
|
|
class Endpoints { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Holds class instance |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
* |
16
|
|
|
* @var object \lsx_health_plan\classes\frontend\Endpoints() |
17
|
|
|
*/ |
18
|
|
|
protected static $instance = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
*/ |
23
|
|
|
public function __construct() { |
|
|
|
|
24
|
|
|
add_action( 'init', array( $this, 'setup' ) ); |
25
|
|
|
} |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return an instance of this class. |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* |
32
|
|
|
* @return object \lsx_health_plan\classes\frontend\Endpoints() A single instance of this class. |
33
|
|
|
*/ |
34
|
|
|
public static function get_instance() { |
35
|
|
|
// If the single instance hasn't been set, set it now. |
36
|
|
|
if ( null === self::$instance ) { |
|
|
|
|
37
|
|
|
self::$instance = new self(); |
38
|
|
|
} |
|
|
|
|
39
|
|
|
return self::$instance; |
40
|
|
|
} |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Runs on init |
44
|
|
|
*/ |
45
|
|
|
public function setup() { |
46
|
|
|
$this->add_rewrite_rules(); |
47
|
|
|
} |
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Registers the rewrites. |
51
|
|
|
*/ |
52
|
|
|
public function add_rewrite_rules() { |
53
|
|
|
// Here is where we add in the rewrite rules above the normal WP ones. |
54
|
|
|
add_rewrite_tag( '%endpoint%', '([^&]+)' ); |
55
|
|
|
add_rewrite_tag( '%section%', '([^&]+)' ); |
56
|
|
|
|
57
|
|
|
// Plan Sections. |
58
|
|
|
add_rewrite_rule( 'plan/([^/]+)/([^/]+)/?$', 'index.php?plan=$matches[1]§ion=$matches[2]', 'top' ); |
59
|
|
|
|
60
|
|
|
// Warm up. |
61
|
|
|
$warm_up = \lsx_health_plan\functions\get_option( 'endpoint_warm_up', false ); |
62
|
|
|
if ( false === $warm_up ) { |
|
|
|
|
63
|
|
|
$warm_up = 'warm-up'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
add_rewrite_rule( 'plan/([^/]+)/([^/]+)/' . $warm_up . '/?$', 'index.php?plan=$matches[1]§ion=$matches[2]&endpoint=warm-up', 'top' ); |
67
|
|
|
|
68
|
|
|
// Workout. |
69
|
|
|
if ( post_type_exists( 'workout' ) ) { |
|
|
|
|
70
|
|
|
$workout = \lsx_health_plan\functions\get_option( 'endpoint_workout', false ); |
71
|
|
|
if ( false === $workout ) { |
|
|
|
|
72
|
|
|
$workout = 'workout'; |
73
|
|
|
} |
74
|
|
|
} |
|
|
|
|
75
|
|
|
add_rewrite_rule( 'plan/([^/]+)/([^/]+)/' . $workout . '/?$', 'index.php?plan=$matches[1]§ion=$matches[2]&endpoint=workout', 'top' ); |
76
|
|
|
|
77
|
|
|
// Meal. |
78
|
|
|
if ( post_type_exists( 'meal' ) ) { |
|
|
|
|
79
|
|
|
$meal = \lsx_health_plan\functions\get_option( 'endpoint_meal', false ); |
80
|
|
|
if ( false === $meal ) { |
|
|
|
|
81
|
|
|
$meal = 'meal'; |
82
|
|
|
} |
83
|
|
|
} |
|
|
|
|
84
|
|
|
add_rewrite_rule( 'plan/([^/]+)/([^/]+)/' . $meal . '/?$', 'index.php?plan=$matches[1]§ion=$matches[2]&endpoint=meal', 'top' ); |
85
|
|
|
|
86
|
|
|
// Recipe. |
87
|
|
|
if ( post_type_exists( 'recipe' ) ) { |
|
|
|
|
88
|
|
|
$recipe = \lsx_health_plan\functions\get_option( 'endpoint_recipe', false ); |
89
|
|
|
if ( false === $recipe ) { |
|
|
|
|
90
|
|
|
$recipe = 'recipes'; |
91
|
|
|
} |
92
|
|
|
} |
|
|
|
|
93
|
|
|
add_rewrite_rule( 'plan/([^/]+)/([^/]+)/' . $recipe . '/?$', 'index.php?plan=$matches[1]§ion=$matches[2]&endpoint=recipes', 'top' ); |
94
|
|
|
} |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|