| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Helper. |
| 5 |
* |
| 6 |
* @since 3.3.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace wpCloud\StatelessMedia { |
| 10 |
|
| 11 |
class Helper { |
| 12 |
|
| 13 |
/** |
| 14 |
* Checks theme name against the current theme or it's parent. |
| 15 |
*/ |
| 16 |
public static function is_theme_name($theme_name) { |
| 17 |
$theme = wp_get_theme(); |
| 18 |
|
| 19 |
if ($theme->Name == $theme_name) { |
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
$parent_theme = $theme->parent(); |
| 24 |
if ( is_a($parent_theme, 'WP_Theme') && $parent_theme->Name == $theme_name ) { |
| 25 |
return true; |
| 26 |
} |
| 27 |
|
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Checks if plugin is active. |
| 33 |
*/ |
| 34 |
public static function get_active_plugins() { |
| 35 |
$active_plugins = []; |
| 36 |
|
| 37 |
// If multisite then check if plugin is network active |
| 38 |
if ( is_multisite() ) { |
| 39 |
$active_plugins = (array)get_site_option('active_sitewide_plugins', []); |
| 40 |
$active_plugins = array_keys($active_plugins); |
| 41 |
|
| 42 |
// If we are in network admin then return, unless it will get data from main site. |
| 43 |
if ( is_network_admin() ) { |
| 44 |
return $active_plugins; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return array_merge( |
| 49 |
$active_plugins, |
| 50 |
(array)get_option('active_plugins', []) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Convert array to objects. |
| 56 |
* |
| 57 |
* @param array $array |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public static function array_of_objects($array) { |
| 62 |
return array_map(function($item) { |
| 63 |
return (object)$item; |
| 64 |
}, $array); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Writes to error log. |
| 69 |
* |
| 70 |
* @param mixed $data |
| 71 |
*/ |
| 72 |
public static function log($data, $json = false) { |
| 73 |
if (!WP_DEBUG) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ( is_array($data) || is_object($data) || !is_string($data) ) { |
| 78 |
if ( $json ) { |
| 79 |
error_log( json_encode($data) ); |
| 80 |
} else { |
| 81 |
error_log( print_r($data, true) ); |
| 82 |
} |
| 83 |
|
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
error_log($data); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Writes debug to error log. |
| 92 |
* |
| 93 |
* @param mixed $data |
| 94 |
*/ |
| 95 |
public static function debug($data, $json = false) { |
| 96 |
self::log($data, $json); |
| 97 |
} |
| 98 |
} |
| 99 |
} |