| 1 |
<?php |
| 2 |
|
| 3 |
class Mmb_Helper |
| 4 |
{ |
| 5 |
/** |
| 6 |
* Creates an ajax response |
| 7 |
* |
| 8 |
* @param mixed $status |
| 9 |
* @param mixed $msg |
| 10 |
*/ |
| 11 |
function _create_ajax_response($msg, $success = TRUE, $other_html = '') |
| 12 |
{ |
| 13 |
$msg = base64_encode($msg); |
| 14 |
$success = $success ? 'true' : 'false'; |
| 15 |
echo <<<EOT |
| 16 |
<script type="text/javascript"> |
| 17 |
mmbAjaxSuccess = $success; |
| 18 |
mmbAjaxMessage = "$msg"; |
| 19 |
</script> |
| 20 |
$other_html |
| 21 |
EOT; |
| 22 |
die(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* A helper function to log data |
| 27 |
* |
| 28 |
* @param mixed $mixed |
| 29 |
*/ |
| 30 |
function _log($mixed) |
| 31 |
{ |
| 32 |
if (is_array($mixed)) |
| 33 |
{ |
| 34 |
$mixed = print_r($mixed, 1); |
| 35 |
} |
| 36 |
else if (is_object($mixed)) |
| 37 |
{ |
| 38 |
ob_start(); |
| 39 |
var_dump($mixed); |
| 40 |
$mixed = ob_get_clean(); |
| 41 |
} |
| 42 |
|
| 43 |
$handle = fopen(dirname(__FILE__) . '/log', 'a'); |
| 44 |
fwrite($handle, $mixed . PHP_EOL); |
| 45 |
fclose($handle); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Strips WP disallowed HTML and PHP tags from a string |
| 50 |
* |
| 51 |
* @param mixed $str |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
function _strip_tags($str) |
| 55 |
{ |
| 56 |
return strip_tags($str, '<address><a><abbr><acronym><area><b><big><blockquote><br><caption><cite><class><code><col><del><dd><div><dl><dt><em><font><h1><h2><h3><h4><h5><h6><hr><i><img><ins><kbd><li><map><ol><p><pre><q><s><span><strike><strong><sub><sup><table><tbody><td><tfoot><tr><tt><ul><var>'); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Filters a WordPress content (being comments, pages, posts etc) |
| 61 |
* |
| 62 |
* @param mixed $str |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
function _filter_content($str) |
| 66 |
{ |
| 67 |
return nl2br($this->_strip_tags($str)); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function _escape(&$array) { |
| 73 |
global $wpdb; |
| 74 |
|
| 75 |
if(!is_array($array)) { |
| 76 |
return($wpdb->escape($array)); |
| 77 |
} |
| 78 |
else { |
| 79 |
foreach ( (array) $array as $k => $v ) { |
| 80 |
if (is_array($v)) { |
| 81 |
$this->_escape($array[$k]); |
| 82 |
} else if (is_object($v)) { |
| 83 |
//skip |
| 84 |
} else { |
| 85 |
$array[$k] = $wpdb->escape($v); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
function _base64_encode($str) |
| 92 |
{ |
| 93 |
// a plus sign can break the encoded string |
| 94 |
// if sent via URL |
| 95 |
return str_replace('+', '|', base64_encode($str)); |
| 96 |
} |
| 97 |
|
| 98 |
function _base64_decode($str) |
| 99 |
{ |
| 100 |
return base64_decode(str_replace('|', '+', $str)); |
| 101 |
} |
| 102 |
|
| 103 |
function _print_r($arr) |
| 104 |
{ |
| 105 |
if (is_string($arr)) $arr = array($arr); |
| 106 |
echo '<pre>'; |
| 107 |
print_r($arr); |
| 108 |
echo '</pre>'; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Initializes the file system |
| 113 |
* |
| 114 |
*/ |
| 115 |
function _init_filesystem() |
| 116 |
{ |
| 117 |
global $wp_filesystem; |
| 118 |
|
| 119 |
if (!$wp_filesystem || !is_object($wp_filesystem)) |
| 120 |
{ |
| 121 |
WP_Filesystem(); |
| 122 |
} |
| 123 |
|
| 124 |
if (!is_object($wp_filesystem)) |
| 125 |
return FALSE; |
| 126 |
|
| 127 |
return TRUE; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Gets transient based on WP version |
| 132 |
* |
| 133 |
* @global string $wp_version |
| 134 |
* @param string $option_name |
| 135 |
* @return mixed |
| 136 |
*/ |
| 137 |
function mmb_get_transient($option_name) |
| 138 |
{ |
| 139 |
|
| 140 |
if(trim($option_name) == ''){ |
| 141 |
return FALSE; |
| 142 |
} |
| 143 |
|
| 144 |
global $wp_version; |
| 145 |
|
| 146 |
if (version_compare($wp_version, '2.8', '<')) |
| 147 |
return get_option($option_name); |
| 148 |
|
| 149 |
else if (version_compare($wp_version, '3.0', '<')) |
| 150 |
return get_transient($option_name); |
| 151 |
|
| 152 |
else |
| 153 |
return get_site_transient($option_name); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
function mmb_null_op_buffer($buffer) { |
| 158 |
//do nothing |
| 159 |
if(!ob_get_level()) |
| 160 |
ob_start(array($this, 'mmb_null_op_buffer')); |
| 161 |
return ''; |
| 162 |
} |
| 163 |
|
| 164 |
function _deleteTempDir($directory) { |
| 165 |
if(substr($directory,-1) == "/") { |
| 166 |
$directory = substr($directory,0,-1); |
| 167 |
} |
| 168 |
// $this->_log($directory); |
| 169 |
if(!file_exists($directory) || !is_dir($directory)) { |
| 170 |
return false; |
| 171 |
} elseif(!is_readable($directory)) { |
| 172 |
return false; |
| 173 |
} else { |
| 174 |
$directoryHandle = opendir($directory); |
| 175 |
|
| 176 |
while ($contents = readdir($directoryHandle)) { |
| 177 |
if($contents != '.' && $contents != '..') { |
| 178 |
$path = $directory . "/" . $contents; |
| 179 |
|
| 180 |
if(is_dir($path)) { |
| 181 |
$this->_deleteTempDir($path); |
| 182 |
} else { |
| 183 |
unlink($path); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
closedir($directoryHandle); |
| 188 |
rmdir($directory); |
| 189 |
return true; |
| 190 |
} |
| 191 |
} |
| 192 |
function _last_worker_message($message){ |
| 193 |
add_option('_worker_last_massage', serialize($message)) or update_option('_worker_last_massage', serialize($message)); |
| 194 |
} |
| 195 |
function _get_last_worker_message(){ |
| 196 |
$message = get_option('_worker_last_massage', array()); |
| 197 |
delete_option('_worker_last_massage'); |
| 198 |
return (array)maybe_unserialize($message); |
| 199 |
} |
| 200 |
function _is_ftp_writable_mmb(){ |
| 201 |
if(defined('FTP_PASS')){ |
| 202 |
return true; |
| 203 |
} |
| 204 |
else return false; |
| 205 |
} |
| 206 |
} |
| 207 |
|