model.class.php
19.6 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
<?php
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'Please do not load this screen directly. Thanks!' );
}
/**
* WooDojo Model Class
*
* The base Model for WooDojo.
*
* @package WordPress
* @subpackage WooDojo
* @category Administration
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* var $active_components
* var $components
* var $sections
*
* - __construct()
* - is_active_component()
* - is_downloaded_component()
* - has_upgrade()
* - get_status_token()
* - get_status_label()
* - get_component()
* - get_component_slug()
* - get_component_path()
* - clean_component_path()
* - load_components()
* - load_standalone_components()
* - load_downloadable_components()
* - load_bundled_components()
* - activate_component()
* - deactivate_component()
* - download_component()
* - upgrade_component()
* - get_screenshot_url()
* - set_username()
* - get_username()
* - is_logged_in()
* - get_request_error()
*/
class WooDojo_Model {
var $active_components;
public function __construct() {
global $woodojo;
$this->config = $woodojo->base;
$this->active_components = array();
} // End __construct()
/**
* is_active_component function.
*
* @description Check if a specified component is active.
* @access public
* @param string $component
* @param string $type
* @return boolean $is_active
*/
public function is_active_component ( $component, $type ) {
$is_active = false;
if ( $type == 'standalone' ) {
// Treat this as a normal plugin.
$is_active = is_plugin_active( $this->components[$type][$component]->filepath );
} else {
if ( ! isset( $this->active_components[$type] ) ) {
$this->active_components[$type] = get_option( $this->config->token . '_' . $type . '_active', array() );
}
if ( in_array( $component, array_keys( (array)$this->active_components[$type] ) ) ) {
if ( $type == 'downloadable' && file_exists( $this->config->downloads_path . $this->components[$type][$component]->filepath ) ) {
$is_active = true;
} else if ( $type == 'bundled' && file_exists( $this->config->components_path . $this->components[$type][$component]->filepath ) ) {
$is_active = true;
} else {
$this->deactivate_component( $component, $type, false );
}
}
}
return $is_active;
} // End is_active_component()
/**
* is_downloaded_component function.
*
* @description Check if a specified component is downloaded.
* @access public
* @param string $component
* @param string $type
* @return boolean $is_downloaded
*/
public function is_downloaded_component ( $component, $type ) {
$is_downloaded = false;
if ( $type == 'downloadable' && file_exists( $this->config->downloads_path . $this->components[$type][$component]->filepath ) ) {
$is_downloaded = true;
}
if ( $type == 'standalone' && file_exists( trailingslashit( WP_PLUGIN_DIR ). $this->components[$type][$component]->filepath ) ) {
$is_downloaded = true;
}
return $is_downloaded;
} // End is_downloaded_component()
/**
* has_upgrade function.
*
* @description Check if an upgrade is available for a component.
* @access public
* @param string $component
* @param string $type
* @return boolean/string $has_upgrade
*/
public function has_upgrade ( $component, $type ) {
$has_upgrade = false;
if ( ( $this->components[$type][$component]->is_free == true ) ) {
$latest = $this->components[$type][$component]->version;
$active = $this->get_component( $this->config->downloads_path . $this->components[$type][$component]->filepath )->version;
}
if ( version_compare( $active, $latest, '<' ) ) {
$has_upgrade = $latest;
}
return $has_upgrade;
} // End has_upgrade()
/**
* get_status_token function.
*
* @access public
* @param string $component
* @param string $type
* @return string $label
*/
public function get_status_token ( $component, $type ) {
$label = 'disabled';
if ( $this->is_active_component( $component, $type ) ) {
$label = 'enabled';
}
if ( ( $type == 'downloadable' || $type == 'standalone' ) && ! $this->is_downloaded_component( $component, $type ) ) {
$label = 'download';
}
return $label;
} // End get_status_token()
/**
* get_status_label function.
*
* @access public
* @param string $component
* @param string $type
* @return string $label
*/
public function get_status_label ( $component, $type ) {
$label = __( 'Disabled', 'woodojo' );
if ( $this->is_active_component( $component, $type ) ) {
$label = __( 'Enabled', 'woodojo' );
}
if ( ( $type == 'downloadable' || $type == 'standalone' ) && ! $this->is_downloaded_component( $component, $type ) ) {
$label = __( 'Not Installed', 'woodojo' );
}
return $label;
} // End get_status_label()
/**
* get_component function.
*
* @access public
* @since 1.0.0
* @param mixed $component
* @return void
*/
public function get_component ( $component ) {
$headers = array(
'title' => 'Module Name',
'short_description' => 'Module Description',
'version' => 'Module Version',
'sort' => 'Sort Order',
'settings' => 'Module Settings',
'deps' => 'Dependencies'
);
$mod = get_file_data( $component, $headers );
if ( empty( $mod['sort'] ) )
$mod['sort'] = 10;
if ( ! empty( $mod['title'] ) ) {
$obj = new StdClass();
foreach ( $mod as $k => $v ) {
$obj->$k = $v;
}
if ( ! isset( $obj->product_id ) ) {
$obj->product_id = 0;
}
return $obj;
}
return false;
} // End get_component()
/**
* get_component_slug function.
*
* @access public
* @since 1.0.0
* @param mixed $file
* @return void
*/
public function get_component_slug ( $file ) {
return str_replace( '.php', '', basename( $file ) );
} // End get_component_slug()
/**
* get_component_path function.
*
* @access public
* @since 1.0.0
* @param mixed $slug
* @return void
*/
public function get_component_path ( $slug ) {
return $this->config->components_path . $slug . '.php';
} // End get_component_path()
/**
* clean_component_path function.
*
* @description Return the component path, relative to the bundled components directory.
* @access public
* @since 1.0.0
* @param string $path
* @return string $path
*/
public function clean_component_path ( $path ) {
return str_replace( $this->config->components_path, '', $path );
} // End clean_component_path()
/**
* load_components function.
*
* @access public
* @since 1.0.0
* @return void
*/
public function load_components () {
$this->components['bundled'] = $this->load_bundled_components();
$this->components['downloadable'] = $this->load_downloadable_components();
$this->components['standalone'] = $this->load_standalone_components();
} // End load_components()
/**
* load_standalone_components function.
*
* @description Load the standalone components.
* @access public
* @since 1.0.0
* @uses global $woodojo->api->get_products_by_type()
* @return array $components
*/
public function load_standalone_components () {
global $woodojo;
$response = $woodojo->api->get_products_by_type( 'standalone' );
// Check the current version.
foreach ( $response as $k => $v ) {
if ( file_exists( trailingslashit( WP_PLUGIN_DIR ) . $v->filepath ) ) {
$data = get_plugin_data( trailingslashit( WP_PLUGIN_DIR ) . $v->filepath );
if ( isset( $data['Version'] ) ) {
$response[$k]->current_version = $data['Version'];
}
} else {
$response[$k]->current_version = $v->version_number;
}
}
return $response;
} // End load_standalone_components()
/**
* load_downloadable_components function.
*
* @description Load the downloadable components.
* @access public
* @since 1.0.0
* @uses global $woodojo->api->get_products_by_type()
* @return array $components
*/
public function load_downloadable_components () {
global $woodojo;
$response = $woodojo->api->get_products_by_type( 'downloadable' );
// Check the current version.
foreach ( $response as $k => $v ) {
$response[$k]->version = $v->version_number;
if ( file_exists( $this->config->downloads_path . $v->filepath ) ) {
$data = $this->get_component( $this->config->downloads_path . $v->filepath );
if ( isset( $data->version ) ) {
$response[$k]->current_version = $data->version;
if ( version_compare( $data->version, $v->version_number, '<' ) ) {
$response[$k]->has_upgrade = true;
}
}
}
}
return $response;
} // End load_downloadable_components()
/**
* load_bundled_components function.
*
* @description Load the components that come bundled.
* @access public
* @since 1.0.0
* @return array $components
*/
public function load_bundled_components () {
$files = WooDojo_Utils::glob_php( '*.php', GLOB_MARK, $this->config->components_path );
foreach ( $files as $file ) {
if ( $headers = $this->get_component( $file ) ) {
$slug = $this->get_component_slug( $file );
$components[$slug] = $headers;
$components[$slug]->filepath = $this->clean_component_path( $file );
$components[$slug]->current_version = $components[$slug]->version;
}
}
return $components;
} // End load_bundled_components()
/**
* activate_component function.
*
* @access public
* @since 1.0.0
* @param string $component
* @param string $type
* @param boolean $redirect
* @return boolean $activated
*/
public function activate_component ( $component, $type = 'bundled', $redirect = true ) {
$activated = false;
if ( $type == 'standalone' ) {
activate_plugin( $this->components[$type][$component]->filepath );
$activated = true;
} else {
$filepath = $this->components[$type][$component]->filepath;
$directory = $this->config->get_directory_by_type( $type );
if ( $filepath != '' && file_exists( $directory . $filepath ) ) {
$components = get_option( $this->config->token . '_' . $type . '_active', array() );
if ( ! in_array( $filepath, $components ) ) {
$components[$component] = $filepath;
$activated = update_option( $this->config->token . '_' . $type . '_active', $components );
}
}
}
if ( $redirect == true ) {
if ( $activated == true ) {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&activated-component=' . $component . '&type=' . $type ) );
exit;
} else {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&activation-error=' . $component . '&type=' . $type ) );
exit;
}
} else {
return $activated;
}
} // End activate_component()
/**
* deactivate_component function.
*
* @access public
* @since 1.0.0
* @param string $component
* @param string $type
* @param boolean $redirect
* @return boolean $deactivated
*/
public function deactivate_component ( $component, $type = 'bundled', $redirect = true ) {
$deactivated = false;
if ( $type == 'standalone' ) {
deactivate_plugins( array( $this->components[$type][$component]->filepath ) );
$deactivated = true;
} else {
$components = get_option( $this->config->token . '_' . $type . '_active', array() );
if ( in_array( $component, array_keys( $components ) ) ) {
unset( $components[$component] );
$deactivated = update_option( $this->config->token . '_' . $type . '_active', $components );
}
}
if ( $redirect == true ) {
if ( $deactivated == true ) {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&deactivated-component=' . $component . '&type=' . $type ) );
exit;
} else {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&deactivation-error=' . $component . '&type=' . $type ) );
exit;
}
} else {
return $deactivated;
}
} // End deactivate_component()
/**
* download_component function.
*
* @access public
* @since 1.0.0
* @param string $component
* @param string $type
* @param boolean $redirect
* @param boolean $activate
* @uses $woodojo->api->request_remote_file
* @return boolean $downloaded
*/
public function download_component ( $component, $type = 'bundled', $redirect = true, $activate = true ) {
global $woodojo;
$is_downloaded = false;
$redirect_to = admin_url( 'admin.php?page=' . $this->config->token . '&component=' . $component . '&component-type=' . $type . '&process-action=' . 'download' . '&download-component=' . $component );
// check_admin_referer( $component );
// okay, let's see about getting credentials
// $url = wp_nonce_url( 'admin.php?page=' . $this->config->token );
if ( false === ( $creds = request_filesystem_credentials( $redirect_to, '', false, false ) ) ) {
// if we get here, then we don't have credentials yet,
// but have just produced a form for the user to fill in,
// so stop processing for now
return 'cred'; // stop the normal page form from displaying
}
// now we have some credentials, try to get the wp_filesystem running
if ( ! WP_Filesystem( $creds ) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials( $url, $method, true, false, $form_fields );
return 'cred';
}
// by this point, the $wp_filesystem global should be working, so let's use it to create a file
global $wp_filesystem;
if ( $type == 'downloadable' ) {
// Create the components directory if it doesn't exist.
$components_dir = $this->config->downloads_path;
if ( ! $wp_filesystem->is_dir( $components_dir ) ) {
$wp_filesystem->mkdir( $components_dir );
}
// Create the backups directory if it doesn't exist.
$backups_dir = $this->config->backups_path;
if ( ! $wp_filesystem->is_dir( $backups_dir ) ) {
$wp_filesystem->mkdir( $backups_dir );
}
}
if ( $type == 'standalone' ) {
$components_dir = WP_PLUGIN_DIR;
}
$id = $this->components[$type][$component]->product_id;
// Download remote file.
$file_url = $woodojo->api->request_remote_file( $id );
if ( $file_url != '' ) {
$remote_file = download_url( $file_url );
// Make sure to remove the existing copy, before loading in the latest version.
if ( ! is_wp_error( $remote_file ) ) {
if ( $wp_filesystem->is_dir( $components_dir . $component ) ) {
$wp_filesystem->rmdir( $components_dir . $component, true );
}
}
$downloaded = unzip_file( $remote_file, $components_dir );
if ( is_wp_error( $downloaded ) ) {
$errors = array();
foreach ( $downloaded->errors as $k => $v ) {
$errors[] = '<strong>' . $v[0] . '</strong> ' . $downloaded->error_data[$k];
}
set_transient( $this->config->token . '-request-error', $errors );
$is_downloaded = false;
} else {
$is_downloaded = true;
if ( $activate == true ) {
unlink( $remote_file );
$this->activate_component( $component, $type );
}
}
unlink( $remote_file );
}
if ( $redirect == true ) {
if ( $is_downloaded == true ) {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&downloaded-component=' . $component . '&type=' . $type ) );
exit;
} else {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&download-error=' . $component . '&type=' . $type ) );
exit;
}
} else {
return $downloaded;
}
} // End download_component()
/**
* upgrade_component function.
*
* @access public
* @since 1.0.0
* @param string $component
* @param string $type
* @param boolean $redirect
* @return void
*/
public function upgrade_component ( $component, $type = 'downloadable', $redirect = true ) {
global $woodojo;
$redirect_to = admin_url( 'admin.php?page=' . $this->config->token . '&component=' . $component . '&component-type=' . $type . '&process-action=' . 'upgrade' . '&upgrade-component=' . $component . '&activate=false' );
// Backup the current version.
$dir = $this->config->downloads_path;
if ( $type == 'bundled' ) {
$dir = $this->config->components_path;
}
$component_dir = $dir . dirname( $this->components[$type][$component]->filepath );
$files = WooDojo_Utils::glob_php( '*', 0, $component_dir );
if ( count( $files ) > 0 ) {
foreach ( $files as $k => $v ) {
if ( $v == $dir || is_dir( $v ) ) {
unset( $files[$k] );
} else {
$files[$k] = str_replace( $this->config->downloads_path, '', $v );
}
}
}
$has_zip = WooDojo_Utils::create_zip( $files, $dir, $this->config->backups_path . sanitize_title_with_dashes( $component ) . '.zip', true );
if ( $has_zip == true ) {
// Download the latest version.
$upgraded = $this->download_component( $component, $type, false, false );
}
if ( $upgraded == true ) {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&upgraded-component=' . $component . '&type=' . $type ) );
exit;
} else {
wp_redirect( admin_url( 'admin.php?page=' . $this->config->token . '&upgrade-error=' . $component . '&type=' . $type ) );
exit;
}
} // End upgrade_component()
/**
* get_screenshot_url function.
*
* @access public
* @since 1.0.0
* @param string $component
* @param string $type
* @return string $html
*/
public function get_screenshot_url ( $component, $type ) {
global $woodojo;
$html = '';
// Custom screenshot URL specified.
if ( isset( $this->components[$type][$component]->thumbnail ) && ( $this->components[$type][$component]->thumbnail != '' ) ) {
if ( stristr( $this->components[$type][$component]->thumbnail, 'http://' ) ) {
$html = esc_url( $this->components[$type][$component]->thumbnail );
} else {
$html = esc_url( $woodojo->settings->screenshot_url . $this->components[$type][$component]->thumbnail );
}
}
// Try and find the screenshot if no URL is specified.
if ( $html == '' ) {
switch ( $type ) {
case 'standalone':
$path = trailingslashit( WP_PLUGIN_DIR );
$url = trailingslashit( WP_PLUGIN_URL );
break;
case 'downloadable':
$path = $this->config->downloads_path;
$url = $this->config->downloads_url;
break;
case 'bundled':
default:
$path = $this->config->components_path;
$url = $this->config->components_url;
break;
}
$screenshot_path = trailingslashit( $path . dirname( $this->components[$type][$component]->filepath ) );
$screenshot_url = trailingslashit( $url . dirname( $this->components[$type][$component]->filepath ) );
foreach ( array( 'png', 'jpg', 'jpeg', 'gif' ) as $k => $v ) {
if ( file_exists( $screenshot_path . 'dojo-screenshot.' . $v ) ) {
$html = $screenshot_url . 'dojo-screenshot.' . $v;
break;
}
}
}
// If no screenshot, look in the "assets/screenshots" folder for component-screenshot.ext.
if ( $html == '' ) {
foreach ( array( 'png', 'jpg', 'jpeg', 'gif' ) as $k => $v ) {
if ( file_exists( $this->config->assets_path . 'screenshots/' . esc_attr( $component ) . '-screenshot.' . $v ) ) {
$html = $this->config->assets_url . 'screenshots/' . esc_attr( $component ) . '-screenshot.' . $v;
break;
}
}
}
// If no screenshot, replace with a placeholder image.
if ( $html == '' ) {
$html = esc_url( 'http://placehold.it/100x100' );
}
return $html;
} // End get_screenshot_url()
/**
* get_request_error function.
*
* @access protected
* @since 1.0.0
* @return string $message
*/
protected function get_request_error () {
$notice = get_transient( $this->config->token . '-request-error' );
$message = '';
if ( $notice != '' && ! is_array( $notice ) ) { $message = wpautop( '<strong>' . __( 'Message:', 'woodojo' ) . '</strong> ' . $notice ); }
if ( is_array( $notice ) && count( $notice ) > 0 ) {
foreach ( $notice as $k => $v ) {
$message .= wpautop( $v );
}
}
return $message;
} // End get_request_error()
} // End Class
?>