Helpers.php
2.49 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
<?php
namespace Favorites;
/**
* Static Helper Methods
*/
class Helpers
{
/**
* Plugin Root Directory
*/
public static function plugin_url()
{
return plugins_url() . '/' . dirname( plugin_basename( FAVORITES_PLUGIN_FILE ) );
}
/**
* Views
*/
public static function view($file)
{
return dirname(__FILE__) . '/Views/' . $file . '.php';
}
/**
* Plugin Version
*/
public static function version()
{
global $favorites_version;
return $favorites_version;
}
/**
* Get File Contents
*/
public static function getFileContents($file)
{
return file_get_contents( dirname( dirname(__FILE__) ) . '/' . $file);
}
/**
* Multidemensional array key search
* @since 1.1
* @return boolean
*/
public static function keyExists($needle, $haystack)
{
if ( array_key_exists($needle, $haystack) || in_array($needle, $haystack) ){
return true;
} else {
$return = false;
foreach ( array_values($haystack) as $value ){
if ( is_array($value) && !$return ) $return = self::keyExists($needle, $value);
}
return $return;
}
}
/**
* Site ID Exists
* checks if site id is in favorites array yet
* @since 1.1
* @return boolean
*/
public static function siteExists($site_id, $meta)
{
foreach ( $meta as $key => $site ){
if ( $site['site_id'] == $site_id ) return true;
}
return false;
}
/**
* Groups Exists
* checks if groups array is in favorites array yet
* @since 2.2
* @return boolean
*/
public static function groupsExist($site_favorites)
{
if ( isset($site_favorites['groups']) && !empty($site_favorites['groups']) ) return true;
return false;
}
/**
* Pluck the site favorites from saved meta array
* @since 1.1
* @param int $site_id
* @param array $favorites (user meta)
* @return array
*/
public static function pluckSiteFavorites($site_id, $all_favorites)
{
foreach($all_favorites as $site_favorites){
if ( $site_favorites['site_id'] == $site_id && isset($site_favorites['posts']) ) return $site_favorites['posts'];
}
return array();
}
/**
* Pluck the site favorites from saved meta array
* @since 1.1
* @param int $site_id
* @param array $favorites (user meta)
* @return array
*/
public static function pluckGroupFavorites($group_id, $site_id, $all_favorites)
{
foreach($all_favorites as $key => $site_favorites){
if ( $site_favorites['site_id'] !== $site_id ) continue;
foreach ( $all_favorites[$key]['groups'] as $group ){
if ( $group['group_id'] == $group_id ){
return $group['posts'];
}
}
}
return array();
}
}