WC_Zapier_Feed_Factory.php
4.21 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* This class is responsible for retrieving WC_Zapier_Feed objects.
*
* Class WC_Zapier_Feed_Factory
*/
class WC_Zapier_Feed_Factory {
/**
* Obtain the (active and valid) Zapier Feeds that are configured for the specified trigger.
* The oldest feeds are first.
*
* Note: multiple calls to this function will simply return the cached result.
*
* @param WC_Zapier_Trigger $trigger
* @return WC_Zapier_Feed[] Array of WC_Zapier_Feed objects
*/
public static function get_feeds_for_trigger( WC_Zapier_Trigger $trigger ) {
$feeds = array();
// Strangely, WP_Query doesn't let us search by post_content so we need to do it manually
$enabled_feeds = self::get_enabled_feeds();
foreach ( $enabled_feeds as $feed ) {
if ( get_class( $feed->trigger() ) === get_class( $trigger ) ) {
$feeds[] = $feed;
}
}
return $feeds;
}
/***
* Obtain the number of existing Zapier Feeds that have the specified webhook URL and trigger.
* This is used to help ensure that two Zapier Feeds can't exist with the same webhook URL and trigger combination.
*
* @param string $webhook_url Zapier Webhook URL
* @param WC_Zapier_Trigger $trigger Trigger
* @param int|WC_Zapier_Feed $feed_to_exclude Optional feed not to include in the search
*
* @return int
*/
public static function get_number_of_feeds_with_webhook_url_and_trigger( $webhook_url, WC_Zapier_Trigger $trigger, $feed_to_exclude = null ) {
$post_id_to_exclude = is_null( $feed_to_exclude ) ? 0 : $feed_to_exclude->id();
$query = array(
'post_type' => 'wc_zapier_feed'
, 'nopaging' => true
, 'post_status' => 'publish'
, 'post__not_in' => array( $post_id_to_exclude )
);
$feeds_query = new WP_Query($query);
$posts = $feeds_query->get_posts();
// Strangely, WP_Query doesn't let us search by post_content or post_excerpt so we need to do it manually
foreach ( $posts as $index => $post ) {
$feed = new WC_Zapier_Feed( $post );
if ( get_class( $feed->trigger() ) != get_class( $trigger ) || $feed->webhook_url() !== $webhook_url ) {
unset ($posts[$index]);
}
}
wp_reset_postdata();
return sizeof( $posts );
}
/***
* Obtain the number of existing Zapier Feeds that have the specified title.
* This is used to help ensure that two Zapier Feeds can't exist with the same title.
*
* @param string $title Zapier Feed Title
* @param int|WC_Zapier_Feed $feed_to_exclude Optional feed not to include in the search
*
* @return int
*/
public static function get_number_of_feeds_with_title( $title, $feed_to_exclude = null ) {
$post_id_to_exclude = is_null( $feed_to_exclude ) ? 0 : $feed_to_exclude->id();
$query = array(
'post_type' => 'wc_zapier_feed'
, 'nopaging' => true
, 'post_status' => 'publish'
, 'post__not_in' => array( $post_id_to_exclude )
);
$feeds_query = new WP_Query($query);
$posts = $feeds_query->get_posts();
// Strangely, WP_Query doesn't let us search by post_title so we need to do it manually
foreach ( $posts as $index => $post ) {
$feed = new WC_Zapier_Feed( $post );
if ( $feed->title() !== $title ) {
unset ($posts[$index]);
}
}
wp_reset_postdata();
return sizeof( $posts );
}
/**
* Obtain the number of configured Zapier feeds.
* This only includes published (active) ones.
*
* @return int
*/
public static function get_number_of_enabled_feeds() {
return sizeof( self::get_enabled_feeds() );
}
/**
* Obtain all of the configured active and valid Zapier feeds. This only includes published (active) ones.
*
* @return WC_Zapier_Feed[]
*/
public static function get_enabled_feeds() {
$query = array(
'post_type' => 'wc_zapier_feed'
, 'nopaging' => true
, 'post_status' => 'publish'
, 'orderby' => 'date'
, 'order' => 'ASC'
);
$feeds_query = new WP_Query($query);
$feeds = array();
$posts = $feeds_query->get_posts();
foreach ( $posts as $post ) {
$feed = new WC_Zapier_Feed( $post );
// Ensure the active feed's trigger is valid.
if ( $feed->is_valid_trigger() ) {
$feeds[] = $feed;
}
}
wp_reset_postdata();
return $feeds;
}
}