-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrgChartPlugin.php
More file actions
178 lines (146 loc) · 5.13 KB
/
OrgChartPlugin.php
File metadata and controls
178 lines (146 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace OrgChart;
use OrgChart\PostTypes\Person as PersonCustomPost;
use OrgChart\Shortcodes\Orgchart;
use OrgChart\Shortcodes\PersonList;
class OrgChartPlugin
{
/** @var string The string used to uniquely identify this plugin. */
protected $plugin_name = 'orgchart';
/** @var string The current version of the plugin. */
protected $version;
/** @var string The url of the assets folder. */
protected $asset_url;
/** @var array The actions registered with WordPress to fire when the plugin loads. */
protected $actions = [];
/** @var array The filters registered with WordPress to fire when the plugin loads. */
protected $filters = [];
/** @var array Custom post types */
protected $custom_posts = [];
/** @var array Shortcodes */
protected $shortcodes = [];
/**
* Define the core functionality of the plugin.
*/
public function __construct($version)
{
$this->version = $version;
$this->asset_url = plugin_dir_url(__FILE__) . 'public';
$this->custom_posts[] = new PersonCustomPost();
$this->shortcodes[] = new PersonList();
$this->shortcodes[] = new Orgchart();
$this->load_dependencies();
}
/**
* Load the required dependencies for this plugin.
*/
private function load_dependencies()
{
require_once plugin_dir_path(__FILE__) . 'includes/cmb2/init.php';
}
/**
* Register all of the hooks needed by the plugin.
*/
protected function registerHooks()
{
// Register custom post types
add_action('init', [$this, 'registerCustomPosts']);
// Register custom post types to CPTUI for custom taxonomies
add_filter('cptui_get_post_types_for_taxonomies', [$this, 'getCustomPosts']);
// Register custom fields
add_action('cmb2_admin_init', [$this, 'registerCustomFields']);
// Register the title field placeholder replacement function for custom post types
add_action('enter_title_here', [$this, 'replaceEnterTitleHere']);
// Register CSS and JS
add_action('wp_enqueue_scripts', [$this, 'registerScripts']);
// Register shortcodes
add_action('init', [$this, 'registerShortcodes']);
}
/**
* Register the CSS and JavaScript for the public-facing side of the site.
*/
public function registerScripts()
{
// Note: orgchart and personlist styles are only loaded when the shortcode is used
wp_register_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', [], '4.7.0');
wp_register_style('orgchart_plugin', $this->asset_url . '/css/orgchart.css', ['fontawesome'], $this->version, 'all');
wp_register_style('orgchart_plugin_personlist', $this->asset_url . '/css/personlist.css', ['fontawesome'], $this->version);
// Note: orgchart and personlist scripts are only loaded when the shortcode is used
wp_register_script('es6-promise', $this->asset_url . '/js/es6-promise.auto.min.js', [], '4.1.0');
wp_register_script('html2canvas', $this->asset_url . '/js/html2canvas.min.js', ['es6-promise'], '0.5.0-beta4');
wp_register_script('jquery-orgchart', $this->asset_url . '/js/jquery.orgchart.js', ['jquery', 'html2canvas'], '1.3.6a');
wp_register_script('bootstrap-treeview', $this->asset_url . '/js/bootstrap-treeview.min.js', ['jquery']);
wp_register_script('orgchart_plugin', $this->asset_url . '/js/orgchart.js', ['bootstrap-treeview', 'jquery-orgchart'], $this->version);
}
/**
* Register the filters and actions with WordPress.
*/
public function run()
{
$this->registerHooks();
}
/**
* Register Custom Post Types.
*/
public function registerCustomPosts()
{
foreach ($this->custom_posts as $custom_post) {
$custom_post->register();
}
}
/**
* Get Custom Post Type objects, and (optionally) append to an existing list.
*
* @param array<int,\WP_Post_Type> $post_types existing list to populate
* @return array<int,\WP_Post_Type>
*/
public function getCustomPosts(array $post_types = []): array
{
$post_type_names = wp_list_pluck($post_types, 'name');
foreach ($this->custom_posts as $custom_post) {
if (!in_array($custom_post->name, $post_type_names)) {
$post_types[] = get_post_type_object($custom_post->name);
};
}
return array_filter($post_types);
}
/**
* Register Custom Fields.
*/
public function registerCustomFields()
{
foreach ($this->custom_posts as $custom_post) {
$custom_post->registerFields();
}
}
/**
* Register shortcodes
*/
public function registerShortcodes()
{
foreach ($this->shortcodes as $shortcode) {
$shortcode->register();
}
}
/**
* Replaces the 'Enter Title here' placeholder text in custom post type titles.
*
* To implement on a custom post, add an 'extras' => ['enter_title_here' => 'replacement text']
* key to the register_post_type() options array.
*
* @param string $message : default message
* @return string
*/
public function replaceEnterTitleHere($message)
{
$screen = get_current_screen();
$post_type_object = get_post_type_object($screen->post_type);
if (is_object($post_type_object) && property_exists($post_type_object, 'extras')) {
$extras = $post_type_object->extras;
if (is_array($extras) && array_key_exists('enter_title_here', $extras)) {
return $extras['enter_title_here'];
}
}
return $message;
}
}