forked from hunk/Magic-Fields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RCCWP_Application.php
412 lines (338 loc) · 12.7 KB
/
RCCWP_Application.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?php
/**
* In this class content all the functions for:
*
* - Install Magic Fields
* - Upgrade Magic Fields
* - Uninstall Magic Fields
* - Check if Magic Fields was correctly installed
*/
class RCCWP_Application {
public static function AddColumnIfNotExist($db, $column, $column_attr = "VARCHAR( 255 ) NULL" ){
global $wpdb;
$exists = false;
if($wpdb->get_var($wpdb->prepare( "SHOW TABLES LIKE %s",$db ) ) == $db){
$sql = "SHOW columns from $db";
$table_colums = $wpdb->get_results($sql);
foreach ($table_colums as $table_colum) {
if($table_colum->Field == $column){
$exists = true;
break;
}
}
if(!$exists){
$sql = $wpdb->prepare( "ALTER TABLE $db ADD $column $column_attr" );
$wpdb->query($sql);
}
}
}
public static function ContinueInstallation(){
RCCWP_Application::SetCaps();
}
public static function SetCaps(){
// Create capabilities if they are not installed
if (!current_user_can(MF_CAPABILITY_PANELS)){
$role = get_role('administrator');
if (!(RCCWP_Application::IsWordpressMu()) || is_site_admin()){
$role->add_cap(MF_CAPABILITY_PANELS);
$role->add_cap(MAGIC_FIELDS_CAPABILITY_MODULES);
}
}
}
/**
* Installing Magic fields
*
* This function create all the Magic Fields default values and
* his tables in the database
*
* @return void
*/
public static function Install(){
include_once('RCCWP_Options.php');
global $wpdb;
// First time installation
if (get_option(RC_CWP_OPTION_KEY) === false){
// Giving full rights to folders. thanks Akis Kesoglou
wp_mkdir_p(MF_UPLOAD_FILES_DIR);
wp_mkdir_p(MF_CACHE_DIR);
wp_mkdir_p(MF_GET_CACHE_DIR);
//Initialize options
$options['condense-menu'] = 0;
$options['hide-non-standart-content'] = 1;
$options['hide-write-post'] = 0;
$options['hide-write-page'] = 0;
$options['hide-visual-editor'] = 0;
$options['prompt-editing-post'] = 0;
$options['assign-to-role'] = 0;
$options['default-custom-write-panel'] = "";
RCCWP_Options::Update($options);
}
// Check blog database
if (get_option("RC_CWP_BLOG_DB_VERSION") == '') update_option("RC_CWP_BLOG_DB_VERSION", 0);
if (get_option("RC_CWP_BLOG_DB_VERSION") < RC_CWP_DB_VERSION)
$BLOG_DBChanged = true;
else
$BLOG_DBChanged = false;
// Install blog tables
if (!$wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", MF_TABLE_POST_META ) ) == MF_TABLE_POST_META ||
$BLOG_DBChanged){
$blog_tables[] = "CREATE TABLE " . MF_TABLE_POST_META . " (
id integer NOT NULL,
group_count integer NOT NULL,
field_count integer NOT NULL,
post_id integer NOT NULL,
field_name text NOT NULL,
order_id integer NOT NULL,
PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci" ;
// try to get around
// these includes like http://trac.mu.wordpress.org/ticket/384
// and http://www.quirm.net/punbb/viewtopic.php?pid=832#p832
if (file_exists(ABSPATH . 'wp-includes/pluggable.php')) {
require_once(ABSPATH . 'wp-includes/pluggable.php');
} else {
require_once(ABSPATH . 'wp-includes/pluggable-functions.php');
}
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
foreach($blog_tables as $blog_table)
dbDelta($blog_table);
}
update_option('RC_CWP_BLOG_DB_VERSION', RC_CWP_DB_VERSION);
// Upgrade Blog
if ($BLOG_DBChanged) RCCWP_Application::UpgradeBlog();
if (RCCWP_Application::IsWordpressMu()){
if (get_site_option("RC_CWP_DB_VERSION") == '') update_site_option("RC_CWP_DB_VERSION", 0);
if (get_site_option("RC_CWP_DB_VERSION") < RC_CWP_DB_VERSION)
$DBChanged = true;
else
$DBChanged = false;
}
else{
if (get_option("RC_CWP_DB_VERSION") == '') update_option("RC_CWP_DB_VERSION", 0);
if (get_option("RC_CWP_DB_VERSION") < RC_CWP_DB_VERSION)
$DBChanged = true;
else
$DBChanged = false;
}
// -- Create Tables if they don't exist or the database changed
$not_installed = false;
if(!$wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s",MF_TABLE_PANELS ) ) == MF_TABLE_PANELS) $not_installed = true;
if( $not_installed ||
$DBChanged){
$qst_tables[] = "CREATE TABLE " . MF_TABLE_PANELS . " (
id int(11) NOT NULL auto_increment,
name varchar(255) NOT NULL,
single tinyint(1) NOT NULL default 0,
description varchar(255),
display_order int(11),
capability_name varchar(255) NOT NULL,
type varchar(255) NOT NULL,
expanded tinyint NOT NULL DEFAULT 1,
PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_GROUP_FIELDS . " (
id int(11) NOT NULL auto_increment,
group_id int(11) NOT NULL,
name varchar(255) NOT NULL,
description varchar(255),
display_order int(11),
display_name enum('true', 'false') NOT NULL,
display_description enum('true', 'false') NOT NULL,
type tinyint NOT NULL,
CSS varchar(100),
required_field tinyint,
duplicate tinyint(1) NOT NULL,
help_text text,
PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_CUSTOM_FIELD_OPTIONS . " (
custom_field_id int(11) NOT NULL,
options text,
default_option text,
PRIMARY KEY (custom_field_id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_PANEL_CATEGORY . " (
panel_id int(11) NOT NULL,
cat_id varchar(100) NOT NULL,
PRIMARY KEY (panel_id, cat_id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_PANEL_STANDARD_FIELD . " (
panel_id int(11) NOT NULL,
standard_field_id int(11) NOT NULL,
PRIMARY KEY (panel_id, standard_field_id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_CUSTOM_FIELD_PROPERTIES . " (
custom_field_id int(11) NOT NULL AUTO_INCREMENT,
properties TEXT,
PRIMARY KEY (custom_field_id)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
$qst_tables[] = "CREATE TABLE " . MF_TABLE_PANEL_GROUPS . " (
id int(11) NOT NULL auto_increment,
panel_id int(11) NOT NULL,
name varchar(255) NOT NULL,
duplicate tinyint(1) NOT NULL,
expanded tinyint,
at_right tinyint(1) NOT NULL,
PRIMARY KEY (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
// try to get around
// these includes like http://trac.mu.wordpress.org/ticket/384
// and http://www.quirm.net/punbb/viewtopic.php?pid=832#p832
if (file_exists(ABSPATH . 'wp-includes/pluggable.php')) {
require_once(ABSPATH . 'wp-includes/pluggable.php');
} else {
require_once(ABSPATH . 'wp-includes/pluggable-functions.php');
};
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
foreach($qst_tables as $qst_table)
dbDelta($qst_table);
if (RCCWP_Application::IsWordpressMu()) {
update_site_option('RC_CWP_DB_VERSION', RC_CWP_DB_VERSION);
} else {
update_option('RC_CWP_DB_VERSION', RC_CWP_DB_VERSION);
}
}
//Import Default modules
if (RCCWP_Application::IsWordpressMu()){
if (get_site_option('MAGIC_FIELDS_fist_time') == ''){
update_site_option('MAGIC_FIELDS_fist_time', '1');
}
} else {
if (get_option('MAGIC_FIELDS_fist_time') == ''){
update_option('MAGIC_FIELDS_fist_time', '1');
}
}
//Post types
if(is_wp30()){
// require_once(MF_PATH.'/MF_PostTypesPage.php');
// MF_PostTypePages::CreatePostTypesTables();
}
RCCWP_Application::UpgradeBlog();
}
/**
* Upgrade Blog
*
* If the Plugin was upgrade from a older version
* this function is executed for check any possible change in the database
*
* @return void
*/
public static function UpgradeBlog(){
global $wpdb;
if (RC_CWP_DB_VERSION <= 2){
$wpdb->query('ALTER TABLE '.MF_TABLE_GROUP_FIELDS.' MODIFY display_order INTEGER');
$wpdb->query('ALTER TABLE '.MF_TABLE_GROUP_FIELDS.' ADD COLUMN help_text text after duplicate');
$wpdb->query('ALTER TABLE '.MF_TABLE_PANELS.' MODIFY display_order INTEGER');
}
if (RC_CWP_DB_VERSION >= 6){
$table_name = $wpdb->prefix . "mf_custom_field_types";
if($wpdb->get_var($wpdb->prepare( "SHOW TABLES LIKE %s",$table_name ) ) == $table_name){
$wpdb->query("DROP TABLE {$wpdb->prefix}mf_custom_field_types");
}
}
if (RC_CWP_DB_VERSION >= 7) {
RCCWP_Application::AddColumnIfNotExist(MF_TABLE_PANEL_GROUPS, "expanded", $column_attr = "tinyint after duplicate" );
RCCWP_Application::AddColumnIfNotExist(MF_TABLE_PANELS, "expanded", $column_attr = "tinyint NOT NULL DEFAULT 1 after type" );
}
if( RC_CWP_DB_VERSION >= 8 ){
if($wpdb->get_var($wpdb->prepare( "SHOW TABLES LIKE %s",MF_TABLE_PANEL_CATEGORY ) ) == MF_TABLE_PANEL_CATEGORY){
$wpdb->query('ALTER TABLE '.MF_TABLE_PANEL_CATEGORY.' MODIFY cat_id VARCHAR(120)');
}
}
}
/**
* Uninstall the Magic Fields Plugin
*
* @return void
*/
public static function Uninstall(){
global $wpdb;
if (get_option("Magic_Fields_notTopAdmin")) return;
// Remove options
delete_option(RC_CWP_OPTION_KEY);
delete_option('MAGIC_FIELDS_fist_time');
delete_option('RC_CWP_DB_VERSION');
delete_option('RC_CWP_BLOG_DB_VERSION');
//delete post_meta WP and WP MF
$sql = "DELETE a.* FROM $wpdb->postmeta AS a, {$wpdb->prefix}mf_post_meta AS b WHERE b.id = a.meta_id";
$wpdb->query($sql);
// Delete meta data
$sql = $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = %s",RC_CWP_POST_WRITE_PANEL_ID_META_KEY );
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_PANELS;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_PANEL_GROUPS;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_GROUP_FIELDS;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_PANEL_CATEGORY;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_PANEL_STANDARD_FIELD;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_PANEL_HIDDEN_EXTERNAL_FIELD;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_CUSTOM_FIELD_OPTIONS;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_CUSTOM_FIELD_PROPERTIES;
$wpdb->query($sql);
$sql = "DROP TABLE IF EXISTS " . MF_TABLE_POST_META;
$wpdb->query($sql);
$current = get_option('active_plugins');
$plugin = plugin_basename(MF_PLUGIN_DIR.'/Main.php');
array_splice($current, array_search( $plugin, $current), 1 );
do_action('deactivate_' . trim( $plugin ));
update_option('active_plugins', $current);
}
/**
* This function returns true if the user be in the Write Post Page
*
* @return Bool
*/
public static function InWritePostPanel()
{
return (strstr($_SERVER['REQUEST_URI'], '/wp-admin/post-new.php') ||
strstr($_SERVER['REQUEST_URI'], '/wp-admin/post.php') ||
strstr($_SERVER['REQUEST_URI'], '/wp-admin/page-new.php') ||
strstr($_SERVER['REQUEST_URI'], '/wp-admin/page.php'));
}
/**
* Return True if Magic Fields was installed in a Wordpress Mu
*
* @return bool
*/
public static function IsWordpressMu(){
global $is_wordpress_mu;
if ($is_wordpress_mu){
return true;
}
return false;
}
/**
* This function check if Magic Fields has all the requirements for his use
*
* @return void
*/
public static function CheckInstallation(){
global $mf_domain;
if (!empty($_GET['page']) && stripos($_GET['page'], "mf") === false && $_GET['page'] != "RCCWP_OptionsPage.php" && !isset($_GET['custom-write-panel-id'])) return;
$dir_list = "";
$dir_list2 = "";
wp_mkdir_p(MF_UPLOAD_FILES_DIR);
wp_mkdir_p(MF_CACHE_DIR);
wp_mkdir_p(MF_GET_CACHE_DIR);
// Giving full rights to folders. thanks Akis Kesoglou
if (!is_dir(MF_CACHE_DIR)){
$dir_list2.= "<li>".MF_CACHE_DIR . "</li>";
}elseif (!is_writable(MF_CACHE_DIR)){
$dir_list.= "<li>".MF_CACHE_DIR . "</li>";
}
if (!is_dir(MF_UPLOAD_FILES_DIR)){
$dir_list2.= "<li>".MF_UPLOAD_FILES_DIR . "</li>";
}elseif (!is_writable(MF_UPLOAD_FILES_DIR)){
$dir_list.= "<li>".MF_UPLOAD_FILES_DIR . "</li>";
}
if ($dir_list2 != ""){
echo "<div id='magic-fields-install-error-message' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('must create the following folders (and must chmod 777):', $mf_domain)."</p><ul>";
echo $dir_list2;
echo "</ul></div>";
}
if ($dir_list != ""){
echo "<div id='magic-fields-install-error-message-2' class='error'><p><strong>".__('Magic Fields is not ready yet.', $mf_domain)."</strong> ".__('The following folders must be writable (usually chmod 777 is neccesary):', $mf_domain)."</p><ul>";
echo $dir_list;
echo "</ul></div>";
}
}
}