1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tips Class |
4
|
|
|
* |
5
|
|
|
* @package lsx-health-plan |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace lsx_health_plan\classes; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Contains the tip post type |
12
|
|
|
* |
13
|
|
|
* @package lsx-health-plan |
14
|
|
|
*/ |
15
|
|
|
class Tip { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Holds class instance |
19
|
|
|
* |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
* |
22
|
|
|
* @var object \lsx_health_plan\classes\Tip() |
23
|
|
|
*/ |
24
|
|
|
protected static $instance = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Holds post_type slug used as an index |
28
|
|
|
* |
29
|
|
|
* @since 1.0.0 |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $slug = 'tip'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
*/ |
38
|
|
|
public function __construct() { |
|
|
|
|
39
|
|
|
$this->default_types = array( |
|
|
|
|
40
|
|
|
\lsx_health_plan\functions\get_option( 'endpoint_meal', 'meal' ), |
41
|
|
|
\lsx_health_plan\functions\get_option( 'endpoint_exercise_single', 'exercise' ), |
42
|
|
|
\lsx_health_plan\functions\get_option( 'endpoint_recipe_single', 'recipe' ), |
43
|
|
|
\lsx_health_plan\functions\get_option( 'endpoint_workout', 'workout' ), |
44
|
|
|
\lsx_health_plan\functions\get_option( 'endpoint_plan', 'plan' ), |
45
|
|
|
); |
46
|
|
|
add_action( 'init', array( $this, 'register_post_type' ) ); |
47
|
|
|
add_action( 'admin_menu', array( $this, 'register_menus' ) ); |
48
|
|
|
add_filter( 'lsx_health_plan_connections', array( $this, 'enable_connections' ), 10, 1 ); |
49
|
|
|
add_action( 'cmb2_admin_init', array( $this, 'tips_connections' ), 15 ); |
50
|
|
|
} |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return an instance of this class. |
54
|
|
|
* |
55
|
|
|
* @since 1.0.0 |
56
|
|
|
* |
57
|
|
|
* @return object \lsx_health_plan\classes\Tip() A single instance of this class. |
58
|
|
|
*/ |
59
|
|
|
public static function get_instance() { |
60
|
|
|
// If the single instance hasn't been set, set it now. |
61
|
|
|
if ( null === self::$instance ) { |
|
|
|
|
62
|
|
|
self::$instance = new self(); |
63
|
|
|
} |
|
|
|
|
64
|
|
|
return self::$instance; |
65
|
|
|
} |
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Register the post type. |
68
|
|
|
*/ |
69
|
|
|
public function register_post_type() { |
70
|
|
|
$labels = array( |
71
|
|
|
'name' => esc_html__( 'Tips', 'lsx-health-plan' ), |
72
|
|
|
'singular_name' => esc_html__( 'Tip', 'lsx-health-plan' ), |
73
|
|
|
'add_new' => esc_html_x( 'Add New', 'post type general name', 'lsx-health-plan' ), |
74
|
|
|
'add_new_item' => esc_html__( 'Add New', 'lsx-health-plan' ), |
75
|
|
|
'edit_item' => esc_html__( 'Edit', 'lsx-health-plan' ), |
76
|
|
|
'new_item' => esc_html__( 'New', 'lsx-health-plan' ), |
77
|
|
|
'all_items' => esc_html__( 'All Tips', 'lsx-health-plan' ), |
78
|
|
|
'view_item' => esc_html__( 'View', 'lsx-health-plan' ), |
79
|
|
|
'search_items' => esc_html__( 'Search', 'lsx-health-plan' ), |
80
|
|
|
'not_found' => esc_html__( 'None found', 'lsx-health-plan' ), |
81
|
|
|
'not_found_in_trash' => esc_html__( 'None found in Trash', 'lsx-health-plan' ), |
82
|
|
|
'parent_item_colon' => '', |
83
|
|
|
'menu_name' => esc_html__( 'Tips', 'lsx-health-plan' ), |
84
|
|
|
); |
85
|
|
|
$args = array( |
86
|
|
|
'labels' => $labels, |
87
|
|
|
'public' => true, |
88
|
|
|
'publicly_queryable' => false, |
89
|
|
|
'show_ui' => true, |
90
|
|
|
'show_in_menu' => false, |
91
|
|
|
'show_in_rest' => true, |
92
|
|
|
'menu_icon' => 'dashicons-admin-post', |
93
|
|
|
'query_var' => true, |
94
|
|
|
'rewrite' => false, |
95
|
|
|
'capability_type' => 'post', |
96
|
|
|
'has_archive' => false, |
97
|
|
|
'hierarchical' => false, |
98
|
|
|
'menu_position' => null, |
99
|
|
|
'supports' => array( |
100
|
|
|
'title', |
101
|
|
|
'editor', |
102
|
|
|
'custom-fields', |
103
|
|
|
), |
104
|
|
|
); |
105
|
|
|
register_post_type( 'tip', $args ); |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Registers the Recipes under the Meals Post type menu. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function register_menus() { |
114
|
|
|
add_submenu_page( 'edit.php?post_type=plan', esc_html__( 'Tips', 'lsx-health-plan' ), esc_html__( 'Tips', 'lsx-health-plan' ), 'edit_posts', 'edit.php?post_type=tip' ); |
115
|
|
|
} |
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Enables the Bi Directional relationships |
119
|
|
|
* |
120
|
|
|
* @param array $connections |
|
|
|
|
121
|
|
|
* @return void |
|
|
|
|
122
|
|
|
*/ |
123
|
|
|
public function enable_connections( $connections = array() ) { |
124
|
|
|
$connections['tip']['connected_plans'] = 'plan_connected_tips'; |
125
|
|
|
$connections['plan']['plan_connected_tips'] = 'connected_plans'; |
126
|
|
|
return $connections; |
127
|
|
|
} |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Registers the workout connections on the plan post type. |
131
|
|
|
* |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function tips_connections() { |
135
|
|
|
foreach ( $this->default_types as $type => $default_type ) { |
|
|
|
|
136
|
|
|
$cmb = new_cmb2_box( |
137
|
|
|
array( |
138
|
|
|
'id' => $default_type . '_tips_connections_metabox', |
|
|
|
|
139
|
|
|
'title' => __( 'Related Tips', 'lsx-health-plan' ), |
140
|
|
|
'object_types' => array( $default_type ), // Post types. |
141
|
|
|
'context' => 'normal', |
142
|
|
|
'priority' => 'high', |
143
|
|
|
'show_names' => false, |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
$cmb->add_field( |
147
|
|
|
array( |
148
|
|
|
'name' => __( 'Tips', 'lsx-health-plan' ), |
149
|
|
|
'id' => $default_type . '_connected_tips', |
150
|
|
|
'desc' => __( 'Connect the tips that apply to this ', 'lsx-health-plan' ) . $default_type, |
151
|
|
|
'type' => 'post_search_ajax', |
152
|
|
|
'limit' => 15, |
153
|
|
|
'sortable' => true, |
154
|
|
|
'query_args' => array( |
155
|
|
|
'post_type' => array( 'tip' ), |
156
|
|
|
'post_status' => array( 'publish' ), |
157
|
|
|
'posts_per_page' => -1, |
158
|
|
|
), |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
} |
|
|
|
|
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|