Passed
Push — add/multiplan ( fc334e...983152 )
by Virginia
05:44 queued 02:19
created

Tip::taxonomy_setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.568

1 Method

Rating   Name   Duplication   Size   Complexity  
A Tip::enable_connections() 0 4 1
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(
0 ignored issues
show
Bug Best Practice introduced by
The property default_types does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
				'thumbnail',
103
				'custom-fields',
104
			),
105
		);
106
		register_post_type( 'tip', $args );
107
	}
108
109
	/**
110
	 * Registers the Recipes under the Meals Post type menu.
111
	 *
112
	 * @return void
113
	 */
114
	public function register_menus() {
115
		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' );
116
	}
117
118
	/**
119
	 * Enables the Bi Directional relationships
120
	 *
121
	 * @param array $connections
122
	 * @return void
123
	 */
124
	public function enable_connections( $connections = array() ) {
125
		$connections['tip']['connected_plans']      = 'plan_connected_tips';
126
		$connections['plan']['plan_connected_tips'] = 'connected_plans';
127
		return $connections;
128
	}
129
130
	/**
131
	 * Registers the workout connections on the plan post type.
132
	 *
133
	 * @return void
134
	 */
135
	public function tips_connections() {
136
		foreach ( $this->default_types as $type => $default_type ) {
137
			$cmb = new_cmb2_box(
138
				array(
139
					'id'           => $default_type . '_tips_connections_metabox',
0 ignored issues
show
Bug introduced by
Are you sure $default_type of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
					'id'           => /** @scrutinizer ignore-type */ $default_type . '_tips_connections_metabox',
Loading history...
140
					'title'        => __( 'Related Tips', 'lsx-health-plan' ),
141
					'object_types' => array( $default_type ), // Post types.
142
					'context'      => 'normal',
143
					'priority'     => 'high',
144
					'show_names'   => false,
145
				)
146
			);
147
			$cmb->add_field(
148
				array(
149
					'name'       => __( 'Tips', 'lsx-health-plan' ),
150
					'id'         => $default_type . '_connected_tips',
151
					'desc'       => __( 'Connect the tips that apply to this', 'lsx-health-plan' ) . $default_type,
152
					'type'       => 'post_search_ajax',
153
					'limit'      => 15,
154
					'sortable'   => true,
155
					'query_args' => array(
156
						'post_type'      => array( 'tip' ),
157
						'post_status'    => array( 'publish' ),
158
						'posts_per_page' => -1,
159
					),
160
				)
161
			);
162
		}
163
	}
164
165
}
166