-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwp_all_export_csv_rows.php
130 lines (108 loc) · 3.63 KB
/
wp_all_export_csv_rows.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
<?php
/**
* ==================================
* Filter: wp_all_export_csv_rows
* ==================================
*
* Filters the records to export.
* This is for CSV formatted exports only. See 'wp_all_export_xml_rows' for filtering XML exports.
*
* @param $articles - An array of records for exporting. Each article is keyed by the column header name for that field.
* @param $options
* @param $export_id int - The export in progress
*
* @return array - the records to export
*/
function wp_all_export_csv_rows($articles, $options, $export_id)
{
// Unless you want this code to execute for every export, be sure to check the export id
// if ($export_id == 5) { ...
// Loop through the array and unset() any entries you don't want exported
// foreach ($articles as $key => $article) {
// if ($article["Title"] == "Something") {
// unset($articles[$key]);
// }
// }
return $articles; // Return the array of records to export
}
add_filter('wp_all_export_csv_rows', 'wp_all_export_csv_rows', 10, 3);
// ----------------------------
// Example uses below
// ----------------------------
/**
* Export based on some criteria. In this case pricing.
*
*/
function my_export_csv_rows($articles, $options, $export_id)
{
foreach ($articles as $key => $article) {
if (!empty($article['Regular Price'] && !empty($article['Sale Price']))) {
if ($article['Sale Price'] < ($article['Regular Price'] / 2)) {
unset($articles[$key]);
}
}
}
return $articles;
}
add_filter('wp_all_export_csv_rows', 'my_export_csv_rows', 10, 3);
/**
* Export based on date in the "_my_time" field
*
*/
function wp_all_export_csv_rows($articles, $options, $export_id)
{
// Loop through the array and unset() any entries you don't want exported
foreach ($articles as $key => $article) {
$date = date(strtotime("tomorrow"));
$my_time = strtotime($article["_my_time"]);
if ($my_time < $date) {
unset($articles[$key]);
}
}
return $articles; // Return the array of records to export
}
add_filter('wp_all_export_csv_rows', 'wp_all_export_csv_rows', 10, 3);
/*
* Export all posts from the previous calendar month
*
*/
function wp_all_export_csv_rows($articles, $options, $export_id) {
// Get start date of previous month
$start_date = date("Y-m-d", strtotime("first day of previous month"));
// Get end date of previous month
$end_date = date("Y-m-d", strtotime("last day of previous month"));
// Loop through the array and unset() any entries you don't want exported
foreach ($articles as $key => $article) {
// Get post date from export data
$post_date = $article["_my_post_date"];
// Compare the dates
if ( ( $post_date >= $start_date ) && ( $post_date <= $end_date ) ) {
// Don't unset the $articles
} else {
// Unset the $articles
unset($articles[$key]);
}
}
// Return the array of records to export
return $articles;
}
add_filter('wp_all_export_csv_rows', 'wp_all_export_csv_rows', 10, 3);
/*
* Exclude variations from Google Merchant Center export when the parent product is in "Draft" status
*
*/
function my_exclude_drafts_from_gmc_export($articles, $options, $export_id) {
if ($options["xml_template_type"] == "XmlGoogleMerchants") {
foreach ($articles as $key => $article) {
if ( ! empty($article['id']) ) {
$post_id = $article['id'];
$parent_id = wp_get_post_parent_id($post_id);
if ( get_post_status($parent_id) == "draft" ) {
unset($articles[$key]);
}
}
}
}
return $articles;
}
add_filter('wp_all_export_csv_rows', 'my_exclude_drafts_from_gmc_export', 10, 3);