This repository has been archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.php
357 lines (331 loc) · 13.1 KB
/
backup.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Plugin Name: Product Showcase
* Description: Showcase your products with a WhatsApp redirect feature.
* Version: 1.0.0
* Author: NityamAS
*/
// Register custom post type for products
function pws_register_product_post_type() {
$labels = array(
'name' => 'Products',
'singular_name' => 'Product',
'menu_name' => 'Products',
'add_new' => 'Add New',
'add_new_item' => 'Add New Product',
'edit_item' => 'Edit Product',
'new_item' => 'New Product',
'view_item' => 'View Product',
'view_items' => 'View Products',
'search_items' => 'Search Products',
'not_found' => 'No products found',
'not_found_in_trash' => 'No products found in trash',
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'menu_icon' => 'dashicons-products',
'supports' => array('title', 'thumbnail'),
'taxonomies' => array('category'),
);
register_post_type('product', $args);
}
add_action('init', 'pws_register_product_post_type');
// Add custom meta box for product price
function pws_add_price_meta_box() {
add_meta_box(
'pws_price_meta_box',
'Product Price',
'pws_render_price_meta_box',
'product',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'pws_add_price_meta_box');
// Render the product price meta box
function pws_render_price_meta_box($post) {
$price = get_post_meta($post->ID, 'price', true);
?>
<label for="pws_product_price">Price:</label>
<input type="text" id="pws_product_price" name="pws_product_price" value="<?php echo esc_attr($price); ?>">
<?php
}
// Save product price
function pws_save_product_price($post_id) {
if (array_key_exists('pws_product_price', $_POST)) {
update_post_meta(
$post_id,
'price',
sanitize_text_field($_POST['pws_product_price'])
);
}
}
add_action('save_post_product', 'pws_save_product_price');
// Generate WhatsApp link with product details and your phone number
function pws_generate_whatsapp_link($post_id) {
$product_title = get_the_title($post_id);
$product_image = get_the_post_thumbnail_url($post_id, 'medium');
$product_price = get_post_meta($post_id, 'price', true);
$whatsapp_number = get_option('pws_whatsapp_number', '919664833459'); // Replace with your phone number
$message = "Product: $product_title\n";
$message .= "Price: ₹$product_price\n";
$message .= "Image: $product_image\n";
$message .= "Product Link: " . get_permalink($post_id);
$whatsapp_message = rawurlencode($message);
$whatsapp_link = "https://wa.me/$whatsapp_number?text=$whatsapp_message";
return $whatsapp_link;
}
// Shortcode for displaying products with WhatsApp redirect
function pws_product_display_shortcode($atts) {
ob_start();
$products = new WP_Query(array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
));
if ($products->have_posts()) {
?>
<div class="product-list">
<div class="product-search">
<div class="search-input">
<input type="text" id="product-search" placeholder="Search for a product">
<button id="product-search-btn">Search</button>
</div>
<?php if (get_option('pws_enable_sort', true)) : ?>
<div class="sort-select">
<label for="product-sort-select">Sort by:</label>
<select id="product-sort-select">
<option value="title_asc">Title (A-Z)</option>
<option value="title_desc">Title (Z-A)</option>
<option value="date_asc">Date (Oldest to Newest)</option>
<option value="date_desc">Date (Newest to Oldest)</option>
</select>
</div>
<?php endif; ?>
</div>
<div class="products">
<?php
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
$whatsapp_link = pws_generate_whatsapp_link($product_id);
?>
<div class="product">
<a href="<?php echo esc_url($whatsapp_link); ?>">
<div class="product-image">
<?php
if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} else {
echo '<img src="' . plugins_url('product-showcase/images/default-product-image.jpg') . '" alt="Product Image">';
}
?>
</div>
<div class="product-details">
<h3><?php the_title(); ?></h3>
<p class="product-price">₹<?php echo get_post_meta($product_id, 'price', true); ?></p>
</div>
</a>
</div>
<?php
}
wp_reset_postdata();
?>
</div>
</div>
<style>
.product-list {
margin: 20px;
}
.product-search {
margin-bottom: 10px;
display: flex;
align-items: center;
}
.search-input {
display: flex;
margin-right: 10px;
}
.search-input input {
width: 300px;
padding: 10px;
font-size: 16px;
}
.search-input button {
padding: 10px 15px;
font-size: 16px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
.sort-select {
display: flex;
align-items: center;
}
.sort-select label {
margin-right: 5px;
}
.sort-select select {
padding: 5px;
font-size: 14px;
}
.products {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: 20px;
}
.product {
border: 1px solid #ddd;
padding: 10px;
text-align: center;
position: relative;
overflow: hidden;
}
.product a {
display: block;
position: relative;
}
.product .product-image {
transition: transform 0.3s ease;
}
.product:hover .product-image {
transform: scale(1.1);
}
.product-image img {
max-width: 100%;
height: auto;
}
.product .product-details {
margin-top: 10px;
}
.product h3 {
margin: 0;
font-size: 16px;
font-weight: bold;
}
.product .product-price {
margin: 5px 0;
font-size: 14px;
}
</style>
<script>
jQuery(document).ready(function($) {
// Search products
$("#product-search-btn").on("click", function() {
var searchTerm = $("#product-search").val().toLowerCase();
$(".product").each(function() {
var productTitle = $(this).find("h3").text().toLowerCase();
if (productTitle.indexOf(searchTerm) !== -1) {
$(this).show();
} else {
$(this).hide();
}
});
});
// Sort products
$("#product-sort-select").on("change", function() {
var sortValue = $(this).val();
var productsContainer = $(".products");
var products = productsContainer.children(".product");
products.sort(function(a, b) {
var aValue = $(a).find("h3").text().toLowerCase();
var bValue = $(b).find("h3").text().toLowerCase();
if (sortValue === "title_asc") {
return aValue.localeCompare(bValue);
} else if (sortValue === "title_desc") {
return bValue.localeCompare(aValue);
} else if (sortValue === "date_asc") {
var aDate = new Date($(a).data("date"));
var bDate = new Date($(b).data("date"));
return aDate - bDate;
} else if (sortValue === "date_desc") {
var aDate = new Date($(a).data("date"));
var bDate = new Date($(b).data("date"));
return bDate - aDate;
}
});
products.detach().appendTo(productsContainer);
});
});
</script>
<?php
} else {
echo 'No products found.';
}
return ob_get_clean();
}
add_shortcode('product_showcase', 'pws_product_display_shortcode');
// Add settings submenu under the Products menu
function pws_add_settings_submenu() {
add_submenu_page(
'edit.php?post_type=product',
'Product Showcase Settings',
'Settings',
'manage_options',
'pws-settings',
'pws_render_settings_page'
);
}
add_action('admin_menu', 'pws_add_settings_submenu');
// Render the settings page
function pws_render_settings_page() {
if (!current_user_can('manage_options')) {
return;
}
// Save settings
if (isset($_POST['pws_save_settings'])) {
$whatsapp_number = sanitize_text_field($_POST['pws_whatsapp_number']);
$enable_search = isset($_POST['pws_enable_search']) ? true : false;
$enable_sort = isset($_POST['pws_enable_sort']) ? true : false;
update_option('pws_whatsapp_number', $whatsapp_number);
update_option('pws_enable_search', $enable_search);
update_option('pws_enable_sort', $enable_sort);
echo '<div class="notice notice-success"><p>Settings saved successfully.</p></div>';
}
// Get current settings
$whatsapp_number = get_option('pws_whatsapp_number', '919664833459'); // Replace with your phone number
$enable_search = get_option('pws_enable_search', true);
$enable_sort = get_option('pws_enable_sort', true);
?>
<div class="wrap">
<h1>Product Showcase Settings</h1>
<form method="post" action="">
<table class="form-table">
<tr>
<th scope="row"><label for="pws_whatsapp_number">WhatsApp Number</label></th>
<td>
<input type="text" id="pws_whatsapp_number" name="pws_whatsapp_number" value="<?php echo esc_attr($whatsapp_number); ?>" class="regular-text">
<p class="description">Enter your WhatsApp number in international format without any spaces or special characters (e.g., 919664833459).</p>
</td>
</tr>
<tr>
<th scope="row">Enable Search</th>
<td>
<label for="pws_enable_search">
<input type="checkbox" id="pws_enable_search" name="pws_enable_search" value="1" <?php checked($enable_search, true); ?>>
Enable search functionality for products
</label>
</td>
</tr>
<tr>
<th scope="row">Enable Sort</th>
<td>
<label for="pws_enable_sort">
<input type="checkbox" id="pws_enable_sort" name="pws_enable_sort" value="1" <?php checked($enable_sort, true); ?>>
Enable sorting options for products
</label>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="pws_save_settings" class="button-primary" value="Save Settings">
</p>
</form>
</div>
<?php
}
?>