-
Notifications
You must be signed in to change notification settings - Fork 4
/
dokan-duplicate-product.php
executable file
·258 lines (221 loc) · 9.47 KB
/
dokan-duplicate-product.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
<?php
/*
Plugin Name: Dokan - Duplicate Product
Plugin URI: https://wedevs.com/products/dokan/product-duplicator/
Description: Product Duplicate add-on for Dokan
Version: 0.3
Author: weDevs
Author URI: http://wedevs.com
License: GPL2
*/
/**
* Copyright (c) 2015 weDevs (email: info@wedevs.com). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* **********************************************************************
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Dokan_Duplicate_Product class
*
* @class Dokan_Duplicate_Product The class that holds the entire Dokan_Duplicate_Product plugin
*/
class Dokan_Duplicate_Product {
/**
* Constructor for the Dokan_Duplicate_Product class
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*
* @uses register_activation_hook()
* @uses register_deactivation_hook()
* @uses is_admin()
* @uses add_action()
*/
public function __construct() {
// Localize our plugin
add_action( 'init', array( $this, 'is_dependency_installed' ) );
add_action( 'init', array( $this, 'localization_setup' ) );
add_filter( 'dokan_settings_fields', array( $this, 'dokan_duplicate_product_button_text' ) );
add_action( 'woocommerce_single_product_summary', array( $this, 'add_to_my_product_button' ), 100 );
add_filter( 'woocommerce_duplicate_product_capability', array( $this, 'add_duplicate_capability' ) );
add_action( 'template_redirect', array( $this, 'product_clone_redirect' ) );
}
/**
* Check if dependency is available
* @since 1.0.0
*/
function is_dependency_installed(){
if ( !class_exists( 'WeDevs_Dokan' )){
add_action( 'admin_notices', array ( $this, 'need_dependency_class' ) );
}
}
/*
* print error notice if dependency not active
* @since 1.0.0
*/
function need_dependency_class(){
$error = sprintf( __( '<b>Dokan Product Duplicator </b> requires %sDokan plugin%s to be installed & activated!' , 'dokan-product-duplicator' ), '<a target="_blank" href="https://wedevs.com/products/plugins/dokan/">', '</a>' );
$message = '<div class="error"><p>' . $error . '</p></div>';
echo $message;
}
/**
* Initializes the Dokan_Duplicate_Product() class
*
* Checks for an existing Dokan_Duplicate_Product() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new Dokan_Duplicate_Product();
}
return $instance;
}
/**
* Initialize plugin for localization
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'dokan-product-duplicator', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Check if a user is seller
*
* @param array $settings_fields
* @return array
*/
public function dokan_duplicate_product_button_text( $settings_fields ) {
$settings_fields['dokan_selling']['product_duplicate_check'] = array(
'name' => 'product_duplicate_check',
'label' => __( 'Allow Product duplicate', 'dokan-product-duplicator' ),
'desc' => __( 'Allow vendors to copy other vendors product to their store', 'dokan-product-duplicator' ),
'type' => 'checkbox',
'default' => 'on',
);
$settings_fields['dokan_selling']['duplicate_button_txt'] = array(
'name' => 'duplicate_button_txt',
'label' => __( 'Duplicate Button Text', 'dokan-product-duplicator' ),
'desc' => __( 'Product duplicate button text on single product page', 'dokan-product-duplicator' ),
'default' => 'Add To My Store',
'type' => 'text',
);
return $settings_fields;
}
/**
* Set Product Duplication Button
*/
public function add_to_my_product_button() {
global $post;
if ( current_user_can( 'dokandar' ) && ( $post->post_author != get_current_user_id() ) && dokan_is_seller_enabled( get_current_user_id() ) && dokan_get_option( 'product_duplicate_check', 'dokan_selling', 'on' ) == 'on' ) {
if ( class_exists( 'Dokan_Product_Subscription' ) ) {
$remaining_product = dps_user_remaining_product( get_current_user_id() );
if ( $remaining_product == 0 ) {
if( Dokan_Product_Subscription::is_dokan_plugin() ) {
$permalink = dokan_get_navigation_url('subscription');
} else {
$page_id = dokan_get_option( 'subscription_pack', 'dokan_product_subscription' );
$permalink = get_permalink( $page_id );
}
// $page_id = dokan_get_option( 'subscription_pack', 'dokan_product_subscription' );
$info = sprintf( __( 'Sorry! You can not add any product. Please <a href="%s">update your package</a>.', 'dokan-product-duplicator' ), $permalink );
echo "<p class='dokan-info'>" . $info . "</p>";
} else {
?>
<form method="post">
<?php echo "<p class='dokan-info'>". sprintf( __( 'You can add %d more product(s).', 'dokan-product-duplicator' ), $remaining_product ); ?>
<?php wp_nonce_field( 'dokan_duplicate_product', 'dokan_duplicate_product_nonce' ); ?>
<input type="submit" name="add_to_my_store" id="add_to_my_store" class="single_add_to_cart_button button alt" value="<?php echo dokan_get_option( 'duplicate_button_txt', 'dokan_selling', 'Add To My Store' ); ?>"/>
<style type="text/css">
#add_to_my_store { margin-top:10px; }
</style>
</p>
</form>
<?php
}
} else {
?>
<form method="post">
<div class="dokan-form-group">
<?php wp_nonce_field( 'dokan_duplicate_product', 'dokan_duplicate_product_nonce' ); ?>
<input type="submit" name="add_to_my_store" id="add_to_my_store" class="single_add_to_cart_button button alt" value="<?php echo dokan_get_option( 'duplicate_button_txt', 'dokan_selling', 'Add To My Store' ); ?>"/>
</div>
<style type="text/css">
#add_to_my_store { margin-top:10px; }
</style>
</form>
<?php
}
}
}
/**
* Manage Product Duplication Capability
*
* @param string
*/
public function add_duplicate_capability( $role ) {
$role = 'dokandar';
return $role;
}
/**
* Product Duplicate and Redirect to Edit Page
*/
public function product_clone_redirect() {
if ( ! is_user_logged_in() ) {
return;
}
if ( ! dokan_is_user_seller( get_current_user_id() ) ) {
return;
}
if ( class_exists( 'Dokan_Product_Subscription' ) ) {
if ( ! Dokan_Product_Subscription::can_post_product() ) {
return;
}
}
$errors = array();
if ( isset( $_POST['add_to_my_store'] ) && wp_verify_nonce( $_POST['dokan_duplicate_product_nonce'], 'dokan_duplicate_product' ) ) {
if ( apply_filters( 'dokan_can_add_product', $errors ) ) {
return;
}
global $post;
if ( ! $post ) {
return;
}
$wo_dup = new WC_Admin_Duplicate_Product();
// Compatibility for WC 3.0.0+
if ( version_compare( WC_VERSION, '2.7', '>' ) ) {
$product = wc_get_product( $post->ID );
$clone_product = $wo_dup->product_duplicate( $product );
$clone_product_id = $clone_product->get_id();
} else {
$clone_product_id = $wo_dup->duplicate_product( $post );
}
$product_status = dokan_get_new_post_status();
wp_update_post( array( 'ID' => intval( $clone_product_id ), 'post_status' => $product_status ) );
wp_redirect( dokan_edit_product_url( $clone_product_id ) );
exit;
}
}
} // Dokan_Duplicate_Product
$dokan_duplicate_product = Dokan_Duplicate_Product::init();