-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrgChartPlugin.php
More file actions
156 lines (128 loc) · 4.45 KB
/
OrgChartPlugin.php
File metadata and controls
156 lines (128 loc) · 4.45 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
<?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 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();
}
}
/**
* 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;
}
}