/var/www/html_uk/wp-content/plugins/automatewoo/includes/Active_Triggers_Cache.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
<?php

namespace AutomateWoo;

/**
 * Class Active_Triggers_Cache
 *
 * @since 4.8.0
 *
 * @package AutomateWoo
 */
final class Active_Triggers_Cache {

    
/**
     * Active triggers.
     *
     * @var array
     */
    
protected static $active_triggers;

    
/**
     * Init Active_Triggers_Cache
     */
    
public static function init() {
        
// fires when a workflow is saved, trashed or deleted
        
add_action'automatewoo/workflow/updated', [ __CLASS__'clear_active_triggers_cache' ] );
        
add_action'automatewoo/workflow/deleted', [ __CLASS__'clear_active_triggers_cache' ] );

        
// refresh cache when version changes
        
add_action'automatewoo_version_changed', [ __CLASS__'clear_active_triggers_cache' ] );
    }

    
/**
     * Get an array of trigger names of triggers that are in use on the store.
     *
     * @return array
     */
    
public static function get_active_triggers() {
        if ( isset( 
self::$active_triggers ) ) {
            
// Cache the active triggers in memory
            
return self::$active_triggers;
        }

        
$triggers get_option'automatewoo_active_triggers' );

        if ( 
is_array$triggers ) ) {
            
$triggers Clean::recursive$triggers );
        } else {
            
// Cache is missing
            
$triggers self::query_active_triggers();
            
// Update option cache
            
update_option'automatewoo_active_triggers'$triggerstrue );
        }

        
// Apply filter after getting from DB but before setting in memory.
        
self::$active_triggers apply_filters'automatewoo/active_triggers'$triggers );

        return 
self::$active_triggers;
    }

    
/**
     * Is a trigger actively in use?
     *
     * @param string $trigger_name
     *
     * @return bool
     */
    
public static function is_trigger_active$trigger_name ) {
        return 
in_array$trigger_nameself::get_active_triggers(), true );
    }

    
/**
     * Query active triggers.
     *
     * @return array
     */
    
protected static function query_active_triggers() {
        global 
$wpdb;

        
$sql "SELECT DISTINCT(meta.meta_value) FROM {$wpdb->posts} as posts
            LEFT JOIN 
{$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
            WHERE post_type = 'aw_workflow'
            AND post_status = 'publish'
            AND meta.meta_key = 'trigger_name'"
;

        
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
        
$results Clean::recursive$wpdb->get_col$sql ) );
        
// phpcs:enable

        
return $results;
    }

    
/**
     * Clear active triggers cached values.
     */
    
public static function clear_active_triggers_cache() {
        
// Note: Option will only update if value has changed
        
update_option'automatewoo_active_triggers'''true );
        
self::$active_triggers null;
    }
}