-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathextractor.php
71 lines (52 loc) · 1.44 KB
/
extractor.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
<?php defined( 'ABSPATH' ) or die();
class Brizy_Import_Extractor {
private $url;
/**
* @var WP_Filesystem_Direct
*/
private $fileSystem;
public function __construct( $url ) {
global $wp_filesystem;
if ( ! $wp_filesystem ) {
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' );
}
WP_Filesystem();
}
$this->fileSystem = $wp_filesystem;
$this->url = $url;
}
/**
* @throws Exception
*/
public function getFiles() {
if ( ! function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$tmpfname = download_url( $this->url );
if ( is_wp_error( $tmpfname ) ) {
throw new Exception( $tmpfname->get_error_message() );
}
$to = $this->getPath();
if ( $this->fileSystem->is_dir( $to ) ) {
$this->fileSystem->delete( $to, true );
}
if ( ! $this->fileSystem->mkdir( $to ) ) {
unlink( $tmpfname );
throw new Exception( 'Could not create the path: ' . $to );
}
$unzip = unzip_file( $tmpfname, $to );
unlink( $tmpfname );
if ( is_wp_error( $unzip ) ) {
$this->fileSystem->delete( $to, true );
throw new Exception( $unzip->get_error_message() );
}
return true;
}
public function getPath( $path = '' ) {
return $this->fileSystem->wp_content_dir() . 'demo' . DIRECTORY_SEPARATOR . $path;
}
public function cleanup() {
$this->fileSystem->delete( $this->getPath(), true );
}
}