-
Notifications
You must be signed in to change notification settings - Fork 4
/
class-pods-visualize.php
277 lines (217 loc) · 6.76 KB
/
class-pods-visualize.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* Class Pods_Visualize
*/
class Pods_Visualize {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @var string
*/
const VERSION = '1.0.0';
/**
*
*/
const PODS_ADMIN_MENU_SLUG_PREFIX = 'pods-admin_page_';
/**
* Unique identifier
*
* The variable name is used as the text domain when internationalizing strings
* of text. Its value should match the Text Domain file header in the main
* plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = 'pods-visualize';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_screen_hook_suffix = null;
/**
* Initialize the plugin by setting localization and loading scripts
* and styles.
*
* @since 1.0.0
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Activate plugin when new blog is added
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
// Load admin style sheet and JavaScript.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
// Add the menu item to Pods Admin menu
add_filter( 'pods_admin_menu', array( $this, 'add_plugin_admin_menu' ) );
}
/**
* Return the plugin slug.
*
* @since 1.0.0
*
* @return string Plugin slug variable.
*/
public function get_plugin_slug() {
return $this->plugin_slug;
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Register the menu
*
* @uses pods_admin_menu filter.
*
* @since 1.0.0
*/
public function add_plugin_admin_menu( $admin_menus ) {
$this->plugin_screen_hook_suffix = self::PODS_ADMIN_MENU_SLUG_PREFIX . $this->plugin_slug;
$admin_menus[ $this->plugin_slug ] = array(
'label' => __( 'Pods Visualize', $this->plugin_slug ),
'function' => array( $this, 'display_plugin_admin_page' ),
'access' => 'manage_options'
);
return $admin_menus;
}
/**
* Register and enqueue admin-specific style sheet.
*/
public function enqueue_admin_styles() {
if ( !isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $this->plugin_screen_hook_suffix == $screen->id ) {
wp_enqueue_style( $this->plugin_slug .'-admin-styles', PODS_VISUALIZE_URL . 'includes/css/pods-visualize.css', array(), Pods_Visualize::VERSION );
wp_enqueue_style( 'jointjs-css', PODS_VISUALIZE_URL . 'includes/jointjs/joint.min.css' );
}
}
/**
* Register and enqueue admin-specific JavaScript.
*/
public function enqueue_admin_scripts() {
if ( !isset( $this->plugin_screen_hook_suffix ) ) {
return;
}
$screen = get_current_screen();
if ( $this->plugin_screen_hook_suffix == $screen->id && function_exists( 'pods' ) ) {
wp_enqueue_script( 'jointjs', PODS_VISUALIZE_URL . 'includes/jointjs/joint.min.js', array( 'jquery', 'backbone', 'underscore' ) );
wp_enqueue_script( 'jointjs-pods', PODS_VISUALIZE_URL . 'includes/js/jointjs-pods.js', array( 'jquery', 'jointjs' ), Pods_Visualize::VERSION );
wp_enqueue_script( $this->plugin_slug . '-admin-script', PODS_VISUALIZE_URL . 'includes/js/pods-visualize.js', array( 'jquery', 'jointjs' ), Pods_Visualize::VERSION );
// Pass pod and field info to the js
$visualize_data = $this->get_data();
wp_localize_script( $this->plugin_slug . '-admin-script', 'pods_visualization_data', $visualize_data );
}
}
/**
* Render the settings page for this plugin.
*
* @since 1.0.0
*/
public function display_plugin_admin_page() {
// ToDo: This test is likely unnecessary; this function should now only be triggered by the pods_admin_menu filter hook
if ( !function_exists( 'pods' ) ) {
include PODS_VISUALIZE_DIR . 'views/no-pods.php';
}
else {
include PODS_VISUALIZE_DIR . 'views/admin.php';
}
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, trailingslashit( WP_LANG_DIR ) . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
}
/**
* @return array
*/
private function get_data() {
$api = pods_api();
$all_pods = $api->load_pods();
$return_data = array();
foreach ( $all_pods as $this_pod_id => $this_pod ) {
$relationships = array();
foreach ( $this_pod[ 'fields' ] as $this_field_name => $this_field ) {
// Ignore everything except pick fields
if ( 'pick' != $this_field['type'] ) {
continue;
}
// Related element's name
if ( 'pod' == $this_field[ 'pick_object' ] ) {
$related_pod_name = $this_field[ 'pick_val' ];
}
else {
$check = $api->get_table_info( $this_field[ 'pick_object' ], $this_field[ 'pick_val' ] );
if ( !empty( $check ) && !empty( $check[ 'pod' ] ) ) {
$related_pod_name = $check[ 'pod' ][ 'name' ];
}
else {
$related_pod_name = $check[ 'object_name' ];
}
}
// Indicate single/multi
$is_multi = ( 'multi' == $this_field[ 'options' ][ 'pick_format_type' ] );
// Collect info if it's a bi-directional field
$bi_directional = array();
if ( !empty( $this_field[ 'sister_id' ] ) ) {
$related_pod = $api->load_pod( array( 'name' => $related_pod_name, 'table_info' => false ) );
foreach ( $related_pod[ 'fields' ] as $this_related_field ) {
if ( $this_related_field[ 'id' ] == $this_field[ 'sister_id' ] ) {
$bi_directional = array(
'sister_field_name' => $this_related_field[ 'name' ],
'is_multi' => ( 'multi' == $this_related_field[ 'options' ][ 'pick_format_type' ] )
);
break;
}
}
}
$relationships[ $this_field_name ] = array(
'related_pod_name' => $related_pod_name,
'type' => $this_field[ 'pick_object' ],
'is_multi' => $is_multi,
'bidirectional' => $bi_directional
);
}
$return_data[ $this_pod[ 'name' ] ] = array(
'name' => $this_pod[ 'name' ],
'type' => $this_pod[ 'type' ],
'relationships' => $relationships
);
}
ksort( $return_data );
return $return_data;
}
}