/var/www/html_us/wp-content/plugins/elementor/modules/library/user-favorites.php


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
<?php
namespace Elementor\Modules\Library;

if ( ! 
defined'ABSPATH' ) ) {
    exit; 
// Exit if accessed directly.
}

class 
User_Favorites {
    const 
USER_META_KEY 'elementor_library_favorites';

    
/**
     * @var int
     */
    
private $user_id;

    
/**
     * @var array|null
     */
    
private $cache;

    
/**
     * User_Favorites constructor.
     *
     * @param $user_id
     */
    
public function __construct$user_id ) {
        
$this->user_id $user_id;
    }

    
/**
     * @param null  $vendor
     * @param null  $resource
     * @param false $ignore_cache
     *
     * @return array
     */
    
public function get$vendor null$resource null$ignore_cache false ) {
        if ( 
$ignore_cache || empty( $this->cache ) ) {
            
$this->cache get_user_meta$this->user_idself::USER_META_KEYtrue );
        }

        if ( ! 
$this->cache || ! is_array$this->cache ) ) {
            return [];
        }

        if ( 
$vendor && $resource ) {
            
$key $this->get_key$vendor$resource );

            return isset( 
$this->cache$key ] ) ? $this->cache$key ] : [];
        }

        return 
$this->cache;
    }

    
/**
     * @param $vendor
     * @param $resource
     * @param $id
     *
     * @return bool
     */
    
public function exists$vendor$resource$id ) {
        return 
in_array$id$this->get$vendor$resource ), true );
    }

    
/**
     * @param       $vendor
     * @param       $resource
     * @param array $value
     *
     * @return $this
     * @throws \Exception
     */
    
public function save$vendor$resource$value = [] ) {
        
$all_favorites $this->get();

        
$all_favorites$this->get_key$vendor$resource ) ] = $value;

        
$result update_user_meta$this->user_idself::USER_META_KEY$all_favorites );

        if ( 
false === $result ) {
            throw new 
\Exception'Failed to save user favorites.' );
        }

        
$this->cache $all_favorites;

        return 
$this;
    }

    
/**
     * @param $vendor
     * @param $resource
     * @param $id
     *
     * @return $this
     * @throws \Exception
     */
    
public function add$vendor$resource$id ) {
        
$favorites $this->get$vendor$resource );

        if ( 
in_array$id$favoritestrue ) ) {
            return 
$this;
        }

        
$favorites[] = $id;

        
$this->save$vendor$resource$favorites );

        return 
$this;
    }

    
/**
     * @param $vendor
     * @param $resource
     * @param $id
     *
     * @return $this
     * @throws \Exception
     */
    
public function remove$vendor$resource$id ) {
        
$favorites $this->get$vendor$resource );

        if ( ! 
in_array$id$favoritestrue ) ) {
            return 
$this;
        }

        
$favorites array_filter$favorites, function ( $item ) use ( $id ) {
            return 
$item !== $id;
        } );

        
$this->save$vendor$resource$favorites );

        return 
$this;
    }

    
/**
     * @param $vendor
     * @param $resource
     *
     * @return string
     */
    
private function get_key$vendor$resource ) {
        return 
"{$vendor}/{$resource}";
    }
}