| 1 |
<?php |
| 2 |
namespace FileBird\Classes; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Helpers { |
| 7 |
|
| 8 |
protected static $instance = null; |
| 9 |
|
| 10 |
public static function getInstance() { |
| 11 |
if ( null == self::$instance ) { |
| 12 |
self::$instance = new self(); |
| 13 |
} |
| 14 |
return self::$instance; |
| 15 |
} |
| 16 |
|
| 17 |
public static function sanitize_array( $var ) { |
| 18 |
if ( is_array( $var ) ) { |
| 19 |
return array_map( 'self::sanitize_array', $var ); |
| 20 |
} else { |
| 21 |
return is_scalar( $var ) ? sanitize_text_field( $var ) : $var; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public static function sanitize_for_excel( $input ) { |
| 26 |
$dangerousCharacters = array( '=', '+', '-', '@', '|' ); |
| 27 |
|
| 28 |
while ( in_array( $input[0], $dangerousCharacters ) ) { |
| 29 |
$input = substr( $input, 1 ); |
| 30 |
} |
| 31 |
|
| 32 |
return $input; |
| 33 |
} |
| 34 |
|
| 35 |
public static function sanitize_intval_array( $var ) { |
| 36 |
if ( is_array( $var ) ) { |
| 37 |
return array_map( 'intval', $var ); |
| 38 |
} else { |
| 39 |
return intval( $var ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public static function getAttachmentIdsByFolderId( $folder_id ) { |
| 44 |
global $wpdb; |
| 45 |
return $wpdb->get_col( 'SELECT `attachment_id` FROM ' . $wpdb->prefix . 'fbv_attachment_folder WHERE `folder_id` = ' . (int) $folder_id ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function getAttachmentCountByFolderId( $folder_id ) { |
| 49 |
return Tree::getCount( $folder_id ); |
| 50 |
} |
| 51 |
|
| 52 |
public static function view( $path, $data = array() ) { |
| 53 |
extract( $data ); |
| 54 |
ob_start(); |
| 55 |
include_once NJFB_PLUGIN_PATH . 'views/' . $path . '.php'; |
| 56 |
return ob_get_clean(); |
| 57 |
} |
| 58 |
|
| 59 |
public static function isListMode() { |
| 60 |
if ( function_exists( 'get_current_screen' ) ) { |
| 61 |
$screen = get_current_screen(); |
| 62 |
return ( isset( $screen->id ) && 'upload' == $screen->id ); |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
public static function wp_kses_i18n( $string ) { |
| 68 |
return wp_kses( |
| 69 |
$string, |
| 70 |
array( |
| 71 |
'strong' => array(), |
| 72 |
'a' => array( |
| 73 |
'target' => array(), |
| 74 |
'href' => array(), |
| 75 |
), |
| 76 |
) |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
public static function findFolder( $folder_id, $tree ) { |
| 81 |
$folder = null; |
| 82 |
foreach ( $tree as $k => $v ) { |
| 83 |
if ( $v['id'] == $folder_id ) { |
| 84 |
$folder = $v; |
| 85 |
break; |
| 86 |
} else { |
| 87 |
$folder = self::findFolder( $folder_id, $v['children'] ); |
| 88 |
if ( ! is_null( $folder ) ) { |
| 89 |
break; |
| 90 |
} else { |
| 91 |
continue; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
return $folder; |
| 96 |
} |
| 97 |
|
| 98 |
public static function get_bytes( $post_id ) { |
| 99 |
$bytes = ''; |
| 100 |
$meta = wp_get_attachment_metadata( $post_id ); |
| 101 |
if ( isset( $meta['filesize'] ) ) { |
| 102 |
$bytes = $meta['filesize']; |
| 103 |
} else { |
| 104 |
$attached_file = get_attached_file( $post_id ); |
| 105 |
if ( file_exists( $attached_file ) ) { |
| 106 |
$bytes = \wp_filesize( $attached_file ); |
| 107 |
} |
| 108 |
} |
| 109 |
return $bytes; |
| 110 |
} |
| 111 |
|
| 112 |
public static function loadView( $view, $data = array(), $return_html = false ) { |
| 113 |
$viewPath = NJFB_PLUGIN_PATH . 'views/' . $view . '.php'; |
| 114 |
if ( ! file_exists( $viewPath ) ) { |
| 115 |
die( 'View <strong>' . esc_html( $viewPath ) . '</strong> not found!' ); |
| 116 |
} |
| 117 |
extract( $data ); |
| 118 |
if ( $return_html === true ) { |
| 119 |
ob_start(); |
| 120 |
include_once $viewPath; |
| 121 |
return ob_get_clean(); |
| 122 |
} |
| 123 |
include_once $viewPath; |
| 124 |
} |
| 125 |
public static function getDomain() { |
| 126 |
$url = get_site_url(); |
| 127 |
if ( $url == '' || $url == null ) { |
| 128 |
$url = home_url(); |
| 129 |
} |
| 130 |
$url = preg_replace( '#https?:\/\/#', '', $url ); |
| 131 |
return apply_filters( 'fbv_domain_for_activate', $url ); |
| 132 |
} |
| 133 |
public static function getIp() { |
| 134 |
return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : ''; |
| 135 |
} |
| 136 |
public static function removeEmojis( $text ) { |
| 137 |
// Emoji Unicode ranges |
| 138 |
$emojiRegex = '/[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F700}-\x{1F77F}]|[\x{1F780}-\x{1F7FF}]|[\x{1F800}-\x{1F8FF}]|[\x{1F900}-\x{1F9FF}]|[\x{1FA00}-\x{1FA6F}]|[\x{2600}-\x{26FF}]|[\x{2700}-\x{27BF}]|\x{2B50}/u'; |
| 139 |
|
| 140 |
// Remove emojis from the text |
| 141 |
$textWithoutEmojis = preg_replace( $emojiRegex, '', $text ); |
| 142 |
|
| 143 |
return $textWithoutEmojis; |
| 144 |
} |
| 145 |
public static function isOptionCollationMb4() { |
| 146 |
global $wpdb; |
| 147 |
$query = $wpdb->get_row( "SHOW FULL COLUMNS FROM $wpdb->options LIKE 'option_value'" ); |
| 148 |
if ( is_object( $query ) && isset( $query->Collation ) ) { |
| 149 |
return strpos( $query->Collation, 'mb4' ) !== false; |
| 150 |
} else { |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public static function array_to_in_clause( $array ) { |
| 156 |
// Escape each value and wrap it in single quotes |
| 157 |
$escaped_values = array_map( |
| 158 |
function( $value ) { |
| 159 |
// Escape the value to prevent SQL injection |
| 160 |
return "'" . esc_sql( $value ) . "'"; |
| 161 |
}, |
| 162 |
$array |
| 163 |
); |
| 164 |
|
| 165 |
// Join the escaped values into a comma-separated string |
| 166 |
$values_string = implode( ', ', $escaped_values ); |
| 167 |
|
| 168 |
// Return the formatted string |
| 169 |
return $values_string; |
| 170 |
} |
| 171 |
} |
| 172 |
|