-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax-functions.php
executable file
·139 lines (103 loc) · 4.25 KB
/
ajax-functions.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
<?php
/*
===========================================================
ACTION HOOKS
=========================================================== */
add_action('wp_ajax_ssp_auto_process_product', 'ssp_auto_process_product');
add_action('wp_ajax_ssp_auto_get_all_products', 'ssp_auto_get_all_products');
add_action('wp_ajax_ssp_auto_remove_products', 'ssp_auto_remove_products');
/*
===========================================================
AUTO PROCESSING AND ADD ALL SHOPIFY PRODUCTS TO WORDPRESS SITE
=========================================================== */
function ssp_auto_process_product()
{
// Get the ID of the current Product
$id = sanitize_key($_POST['product_id']);
// Get the title of the current Product
$title = sanitize_title($_POST['product_title'], strtolower($_POST['product_title']));
// Get refresh options
$auto_publish = rest_sanitize_boolean($_POST['auto_publish']) === true ? 'publish' : 'pending';
$output = 'Error processing Product ' . $title . '!';
// Find any existing Products that match the desired ID
$args = array(
'posts_per_page' => -1,
'post_type' => sanitize_option('ssp_product_post_type_slug', get_option('ssp_product_post_type_slug')),
'meta_key' => sanitize_option('ssp_product_id_meta_slug', get_option('ssp_product_id_meta_slug')),
'meta_value' => $id,
'post_status' => 'publish,private,draft,future,pending'
);
$posts = get_posts($args);
if (count($posts) > 0) {
// We have a matching Product, so let's update it
$target_post = $posts[0];
$args = array(
'ID' => $target_post->ID,
'post_title' => $title,
'post_name' => sanitize_title($title, strtolower($title))
);
wp_update_post($args);
$output = 'Updated existing Product ' . $title . '.';
} else {
// No matching Product, so let's create one
$args = array(
'post_title' => $title,
'post_type' => sanitize_option('ssp_product_post_type_slug', get_option('ssp_product_post_type_slug')),
'post_status' => $auto_publish,
'meta_input' => array(sanitize_option('ssp_product_id_meta_slug', get_option('ssp_product_id_meta_slug')) => $id)
);
$post_id = wp_insert_post($args);
global $wp_version;
if ($wp_version < 4.4 and $post_id != 0) {
// Update meta field for ID (for older WP installs)
update_post_meta($post_id, sanitize_option('ssp_product_id_meta_slug', get_option('ssp_product_id_meta_slug')), $id);
}
$output = 'Added new Product ' . $title . ' (ID: ' . $id . ').';
}
$output = array(
'message' => $output,
'id' => $id
);
echo json_encode($output);
die();
}
/*
===========================================================
AUTO GETTING ALL EXITING SHOPIFY PRODUCTS FROM WORDPRESS SITE
=========================================================== */
function ssp_auto_get_all_products()
{
// Get all Products
$args = array(
'posts_per_page' => -1,
'post_type' => sanitize_option('ssp_product_post_type_slug', get_option('ssp_product_post_type_slug')),
'post_status' => 'publish'
);
$posts = get_posts($args);
$output = array();
// Create an array of WP post IDs and their attached product IDs
foreach ($posts as $target_post) {
$output[] = array(
'wp_id' => $target_post->ID,
sanitize_option('ssp_product_id_meta_slug', get_option('ssp_product_id_meta_slug')) =>
get_post_meta($target_post->ID, sanitize_option('ssp_product_id_meta_slug', get_option('ssp_product_id_meta_slug')), true)
);
}
echo json_encode($output);
die();
}
/*
===========================================================
AUTO REMOVING ALL OLD SHOPIFY PRODUCTS FROM WORDPRESS SITE
=========================================================== */
function ssp_auto_remove_products()
{
$posts_to_remove = explode(',', sanitize_post($_POST['to_remove']));
// Remove a list of Products
foreach ($posts_to_remove as $id_to_remove) {
echo $id_to_remove;
wp_delete_post($id_to_remove);
}
die();
}
/* ======================================================== */