utils.class.php
4.51 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
<?php
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'Please do not load this screen directly. Thanks!' );
}
/**
* WooDojo Utils Class
*
* A class containing various WooDojo utilities.
*
* @package WordPress
* @subpackage WooDojo
* @category Core
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* - load_common_l10n()
* - glob_php()
* - create_zip()
* - get_or_post()
*/
class WooDojo_Utils {
/**
* load_common_l10n function.
*
* @description Load common translatable strings, commonly made available to JavaScript files.
* @access public
* @since 1.0.0
* @return void
*/
public function load_common_l10n () {
$strings = array(
'disable' => __( 'Deactivate', 'woodojo' ),
'enable' => __( 'Activate', 'woodojo' ),
'disabled' => __( 'Deactivated', 'woodojo' ),
'enabled' => __( 'Activated', 'woodojo' ),
'enabled_successfully' => __( '%s Activated Successfully', 'woodojo' ),
'disabled_successfully' => __( '%s Deactivated Successfully', 'woodojo' ),
'enabled_error' => __( 'There was an error activating %s', 'woodojo' ),
'disabled_error' => __( 'There was an error deactivating %s', 'woodojo' ),
'upgrade_confirmation' => __( 'Are you sure you\'d like to update %s?', 'woodojo' )
);
return $strings;
} // End load_common_l10n()
/**
* glob_php function.
*
* @description Returns an array of all PHP files in the specified absolute path.
* Equivalent to glob( "$absolute_path/*.php" ).
* @since 1.0.0
* @access public
* @static
* @param string $absolute_path The absolute path of the directory to search.
* @return array Array of absolute paths to the PHP files.
*/
public static function glob_php ( $pattern, $flags = 0, $path = '' ) {
if ( ! $path && ( $dir = dirname( $pattern ) ) != '.' ) {
if ($dir == '\\' || $dir == '/') { $dir = ''; } // End IF Statement
return self::glob_php(basename( $pattern ), $flags, $dir . '/' );
} // End IF Statement
$paths = glob( $path . '*', GLOB_ONLYDIR | GLOB_NOSORT );
$files = glob( $path . $pattern, $flags );
if ( $files == '' ) {
$files = array();
}
if ( is_array( $paths ) && count( $paths ) > 0 ) {
foreach ( $paths as $p ) {
if ( is_array( $files ) ) {
$files = array_merge( $files, self::glob_php( $pattern, $flags, $p . '/' ) );
}
} // End FOREACH Loop
}
return $files;
} // End glob_php()
/**
* create_zip function.
*
* @description Create a ZIP file from a given array of files.
* @since 1.0.0
* @access public
* @static
* @param array $files
* @param string $root_path
* @param string $destination
* @param boolean $overwrite
* @return boolean
*/
public static function create_zip ( $files = array(), $root_path, $destination = '', $overwrite = false ) {
//if the zip file already exists and overwrite is false, return false
if( file_exists( $destination) && ! $overwrite ) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($root_path . $file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if( count( $valid_files ) ) {
//create the archive
$zip = new ZipArchive();
if( $zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true ) {
return false;
}
//add the files
foreach( $valid_files as $file ) {
$zip->addFile( $root_path . $file, $file );
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists( $destination );
} else {
return false;
}
} // End create_zip()
/**
* Determine whether a value should be called from $_GET or from $_POST.
* @access public
* @since 1.1.3
* @param string $field The field to look for.
* @return string The response.
*/
public static function get_or_post ( $field ) {
$response = '';
$field = trim( strip_tags( $field ) );
if ( isset( $_GET[$field] ) && $_GET[$field] != '' ) {
$response = esc_attr( $_GET[$field] );
} else if ( isset( $_POST[$field] ) && $_POST[$field] != '' ) {
$response = esc_attr( $_POST[$field] );
}
return $response;
} // End get_or_post()
}
?>