PluginProbe
Dokan Invoice / trunk
Dokan Invoice vtrunk
trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8
dokan-invoice / dokan-invoice.php

dokan-invoice.php in Dokan Invoice trunk, at dokan-invoice.php

466 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Dokan - PDF Invoice
4 * Plugin URI: https://dokan.co/wordpress/
5 * Description: A Dokan plugin Add-on to get PDF invoice.
6 * Version: 1.2.8
7 * Author: weDevs
8 * Author URI: https://dokan.co/
9 * License: GPL2
10 * Text Domain: dokan-invoice
11 * WC requires at least: 8.5.0
12 * WC tested up to: 9.8.5
13 * Requires Plugins: woocommerce, dokan-lite, woocommerce-pdf-invoices-packing-slips
14 */
15
16 /**
17 * Copyright (c) 2017 weDevs (email: info@wedevs.com). All rights reserved.
18 *
19 * Released under the GPL license
20 * http://www.opensource.org/licenses/gpl-license.php
21 *
22 * This is an add-on for WordPress
23 * http://wordpress.org/
24 *
25 * **********************************************************************
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 * **********************************************************************
40 */
41 // don't call the file directly
42 if ( ! defined( 'ABSPATH' ) )
43 exit;
44
45 /**
46 * Dokan_Invoice class
47 *
48 * @class Dokan_Invoice The class that holds the entire Dokan_Invoice plugin
49 */
50 class Dokan_Invoice {
51
52 public static $plugin_url;
53 public static $plugin_path;
54 public static $plugin_basename;
55 protected $dokan_invoice_active = 0;
56
57 private $depends_on = array();
58 private $dependency_error = array();
59
60 /**
61 * WC PDF Plugin Class Name.
62 *
63 * @since 1.2.3
64 *
65 * @var string
66 */
67 public $wc_pdf_class = '';
68
69 /**
70 * Constructor for the Dokan_Invoice class
71 *
72 * Sets up all the appropriate hooks and actions
73 * within our plugin.
74 *
75 * @uses register_activation_hook()
76 * @uses register_deactivation_hook()
77 * @uses is_admin()
78 * @uses add_action()
79 */
80 public function __construct() {
81 self::$plugin_basename = plugin_basename( __FILE__ );
82 self::$plugin_url = plugin_dir_url( self::$plugin_basename );
83 self::$plugin_path = trailingslashit( dirname( __FILE__ ) );
84
85 $this->wc_pdf_class = version_compare( get_option( 'wpo_wcpdf_version', false ), '3.9.0', '<' ) ? 'WooCommerce_PDF_Invoices' : 'WPO_WCPDF' ;
86
87 add_action( 'before_woocommerce_init', [ $this, 'add_hpos_support' ] );
88 add_action( 'init', array( $this,'localization_setup_and_is_dependency_available') );
89 add_action( 'plugins_loaded', array( $this, 'init_hooks' ) );
90 }
91
92 /**
93 * Add High Performance Order Storage Support.
94 *
95 * @since 1.2.2
96 *
97 * @return void
98 */
99 public function add_hpos_support() {
100 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
101 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
102 }
103 }
104
105 /**
106 * Initialize plugin for localization
107 * check if dependencies installed or not and add error notice
108 *
109 * @since 1.0.0
110 */
111 public function localization_setup_and_is_dependency_available(){
112 $res = true;
113
114 load_plugin_textdomain( 'dokan-invoice', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
115
116 $this->depends_on['dokan'] = array(
117 'name' => 'WeDevs_Dokan',
118 'notice' => sprintf( esc_html__( '%sDokan PDF Invoice%s requires %sDokan plugin%s to be installed & activated!' , 'dokan-invoice' ), '<b>', '</b>', '<a target="_blank" href="https://dokan.co/wordpress/">', '</a>' ),
119 );
120
121 $this->depends_on['woocommerce_pdf_invoices'] = array(
122 'name' => $this->wc_pdf_class,
123 'notice' => sprintf( esc_html__( '%sDokan PDF Invoice%s requires %sPDF Invoices & packing slips for WooCommerce plugin%s to be installed & activated!' , 'dokan-invoice' ), '<b>', '</b>', '<a target="_blank" href="https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/">', '</a>' ),
124 );
125
126 foreach ( $this->depends_on as $class ){
127 if ( !class_exists( $class['name'] ) ){
128 $this->dependency_error[] = $class['notice'];
129 $res = false;
130 }
131 }
132
133 if ($res == false){
134 add_action( 'admin_notices', array ( $this, 'dependency_notice' ) );
135 }
136
137 return $res;
138 }
139
140 /**
141 * print error notice if dependency not active
142 *
143 * @since 1.0.0
144 */
145 public function dependency_notice(){
146 $errors = '';
147 $error = '';
148 foreach ( $this->dependency_error as $error ) {
149 $errors .= '<p>' . $error . '</p>';
150 }
151 $message = '<div class="error">' . $errors . '</div>';
152
153 echo $message;
154
155 deactivate_plugins( plugin_basename( __FILE__ ) );
156 }
157
158 /**
159 * Initializes the Dokan_Invoice() class
160 *
161 * Checks for an existing Dokan_Invoice() instance
162 * and if it doesn't find one, creates it.
163 */
164 public static function init() {
165 static $instance = false;
166 if ( !$instance ) {
167 $instance = new Dokan_Invoice();
168 }
169
170 return $instance;
171 }
172
173 /**
174 * Init hooks
175 *
176 * @return void
177 */
178 public function init_hooks() {
179 if ( ! class_exists( $this->wc_pdf_class ) ) {
180 return ;
181 }
182
183 add_filter( 'dokan_my_account_my_sub_orders_actions', array( $this, 'dokan_invoice_listing_actions_my_account' ), 50, 2 );
184 add_filter( 'wpo_wcpdf_shop_name', array( $this,'wpo_wcpdf_add_dokan_shop_name'), 10, 2 );
185 add_filter( 'wpo_wcpdf_shop_address', array( $this,'wpo_wcpdf_add_dokan_shop_details'), 10, 2 );
186 add_filter( 'wpo_wcpdf_check_privs', array( $this,'wpo_wcpdf_dokan_privs'), 50, 2 );
187 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
188 add_filter( 'dokan_rest_prepare_shop_order_object', array( $this, 'add_invoice_url_to_rest_response' ), 10, 2 );
189 }
190
191 /**
192 * Add invoice action(s) to the Dokan orders REST response.
193 * @since 1.2.8
194 *
195 * @param WP_REST_Response $response
196 * @param WC_Order $order
197 *
198 * @return WP_REST_Response
199 */
200 public function add_invoice_url_to_rest_response( $response, $order ) {
201 if ( ! is_a( $response, 'WP_REST_Response' ) || ! $order || ! function_exists( 'WPO_WCPDF' ) || ! function_exists( 'wcpdf_get_document' ) ) {
202 return $response;
203 }
204
205 $document_types = array( 'invoice', 'packing-slip' );
206
207 $data = $response->get_data();
208 $actions = isset( $data['actions'] ) && is_array( $data['actions'] ) ? $data['actions'] : array();
209
210 foreach ( $document_types as $document_type ) {
211 $document = wcpdf_get_document( $document_type, $order );
212 if ( ! $document || ! $document->is_enabled() ) {
213 continue;
214 }
215
216 $url = WPO_WCPDF()->endpoint->get_document_link( $order, $document_type );
217 $url = esc_url_raw( wp_specialchars_decode( $url, ENT_QUOTES ) );
218 if ( empty( $url ) ) {
219 continue;
220 }
221
222 $actions[ $document_type ] = array(
223 'url' => $url,
224 );
225 }
226
227 $data['actions'] = $actions;
228 $response->set_data( $data );
229
230 return $response;
231 }
232
233 /**
234 * Enqueue admin scripts
235 *
236 * Allows plugin assets to be loaded.
237 *
238 * @uses wp_enqueue_script()
239 * @uses wp_localize_script()
240 * @uses wp_enqueue_style
241 */
242 public function enqueue_scripts() {
243 wp_enqueue_style( 'dokan-invoice-styles', plugins_url( 'assets/css/style.css', __FILE__ ), false, date( 'Ymd' ) );
244 wp_enqueue_script( 'dokan-invoice-orders', plugins_url( 'assets/js/dokan-orders.js', __FILE__ ), array( 'wp-hooks' ), date( 'Ymd' ), false );
245 }
246
247 /**
248 * Set Dokan_invoice buttons on My Account page
249 *
250 * Hooked with WP_invoice filter
251 *
252 * @param array $actions List of actions
253 * @param \WC_Order $order Order object
254 *
255 * @return array $actions
256 */
257 public function dokan_invoice_listing_actions_my_account( $actions, $order ) {
258 if ( class_exists( '\WPO\WC\PDF_Invoices\Frontend' ) ) {
259 $frontend = new \WPO\WC\PDF_Invoices\Frontend();
260
261 if ( method_exists( $frontend, 'my_account_pdf_link' ) ) {
262 return $frontend->my_account_pdf_link( $actions, $order );
263 }
264
265 if ( method_exists( $frontend, 'my_account_invoice_pdf_link' ) ) {
266 return $frontend->my_account_invoice_pdf_link( $actions, $order );
267 }
268 }
269
270 $frontend = new \WPO\IPS\Frontend();
271
272 /**
273 * Remove there method my_account_invoice_pdf_link and replace with my_account_invoice_actions;
274 *
275 * @see https://github.com/wpovernight/woocommerce-pdf-invoices-packing-slips/pull/1149/files
276 * @see https://github.com/wpovernight/woocommerce-pdf-invoices-packing-slips/commit/f6251e2452e502ca837471fbf1de6eb2a6b638d0
277 */
278
279 if ( method_exists( $frontend, 'my_account_invoice_pdf_link' ) ) {
280 return $frontend->my_account_invoice_pdf_link( $actions, $order );
281 }
282
283 if ( method_exists( $frontend, 'my_account_invoice_actions' ) ) {
284 return $frontend->my_account_invoice_actions( $actions, $order );
285 }
286
287 return $actions;
288 }
289
290 /**
291 * Filter Shop name according to Store name
292 *
293 * @since 1.1
294 *
295 * @global type $wpo_wcpdf
296 *
297 * @param type $shop_name
298 *
299 * @return string $shop_name
300 */
301 public function wpo_wcpdf_add_dokan_shop_name( $shop_name, $document = null ) {
302 extract( $this->get_order_id_parent_id( $document ) );
303
304 $shop_name_label = apply_filters( 'dokan_invoice_shop_name_label', esc_html__( 'Vendor', 'dokan-invoice' ), $document );
305
306 // If parent order keep Original Store name else set seller store name
307 if ( $parent_id == 0 ) {
308 if ( function_exists( 'dokan_get_seller_ids_by' ) ) {
309 $seller_list = dokan_get_seller_ids_by( $order_id );
310 } else {
311 $seller_list = array_unique( array_keys( dokan_get_sellers_by( $order_id ) ) );
312 }
313
314 if ( 1 === count( $seller_list ) ) {
315 $vendor_id = $seller_list[0];
316 $vendor = dokan()->vendor->get( $vendor_id );
317 $store_name = $vendor->get_shop_name();
318 $store_name = $store_name ?: '';
319
320 $shop_name .= sprintf( '<br/><br/>%s: %s', $shop_name_label, $store_name );
321 }
322 } else {
323 $vendor_id = dokan_get_seller_id_by_order( $order_id );
324 $vendor = dokan()->vendor->get( $vendor_id );
325 $store_name = $vendor->get_shop_name();
326 $store_name = $store_name ?: '';
327
328 $shop_name .= sprintf( '<br/><br/>%s: %s', $shop_name_label, $store_name );
329 }
330
331 return apply_filters( 'dokan_invoice_store_name', $shop_name, $document );
332 }
333
334 /**
335 * Filter Shop address
336 *
337 * @since 1.1
338 *
339 * @global type $wpo_wcpdf
340 *
341 * @param type $shop_address
342 *
343 * @return string $shop_address
344 */
345 public function wpo_wcpdf_add_dokan_shop_details( $shop_address, $document = null ) {
346 // get $order_id & $parent_id
347 extract( $this->get_order_id_parent_id( $document ) );
348
349 //If parent order print Store names only after address else Print Seller Store Address
350 if ( $parent_id == 0 ) {
351 if ( function_exists( 'dokan_get_seller_ids_by' ) ) {
352 $seller_list = dokan_get_seller_ids_by( $order_id );
353 } else {
354 $seller_list = array_unique( array_keys( dokan_get_sellers_by( $order_id ) ) );
355 }
356
357 if ( count( $seller_list ) > 1 ) {
358 $shop_address = "<br>" . $shop_address . "<br>" . '<i>' . __( 'From vendors:', 'dokan-invoice' ) . ' </i>';
359 foreach ( $seller_list as $seller ) {
360 $vendor = dokan()->vendor->get( $seller );
361 $shop_name = $vendor->get_shop_name();
362 $shop_name = ! empty( $shop_name ) ? $shop_name : __( 'store_info', 'dokan-invoice' );
363 $shop_address = $shop_address . "<div class='shop-name'><h3>" . $shop_name . "</h3></div>";
364 }
365 } else {
366 $vendor_id = $seller_list[0];
367 /**
368 * @since 1.2.1 added filter hook dokan_invoice_single_seller_address
369 */
370 $shop_address = "<br>" . apply_filters( 'dokan_invoice_single_seller_address', dokan_get_seller_address( $vendor_id ), $vendor_id, $order_id );
371 }
372
373 return $shop_address;
374 } else {
375 $vendor_id = dokan_get_seller_id_by_order( $order_id );
376 /**
377 * @since 1.2.1 added filter hook dokan_invoice_single_seller_address
378 */
379 $shop_address = "<br>" . apply_filters( 'dokan_invoice_single_seller_address', dokan_get_seller_address( $vendor_id ), $vendor_id, $order_id );
380
381 return $shop_address;
382 }
383 }
384
385 /**
386 * Set seller permission true if oreder consists his item
387 *
388 * @param type $allowed
389 * @param type $order_ids
390 *
391 * @return boolean
392 */
393 public function wpo_wcpdf_dokan_privs( $allowed, $order_ids ) {
394 // check if user is seller
395 if ( !$allowed && in_array( 'seller', $GLOBALS['current_user']->roles ) ) {
396
397 if ( count( $order_ids ) == 1 ) {
398
399 $order = wc_get_order( $order_ids );
400 $items = $order->get_items();
401 $seller_id = dokan_get_seller_id_by_order( $order_ids );
402 $current_user = get_current_user_id();
403
404 if ( $current_user == $seller_id ) {
405 return true; // this seller is allowed
406 } else {
407 return false;
408 }
409 }
410
411 foreach ( $order_ids as $order_id ) {
412 // get seller_id
413 $seller_id = dokan_get_seller_id_by_order( $order_id );
414 // loop through items to get list of sellers for this order
415 $order_sellers = array();
416 $order = new WC_Order( $order_id );
417 $items = $order->get_items();
418 foreach ( $items as $item ) {
419 $item_seller = get_post_field( 'post_author', $item['product_id'] );
420 // check if item is from this seller
421 if ( $item_seller != $seller_id ) {
422 return true; // not allowed!
423 }
424 }
425 }
426 // if we got here, that means the user is a seller and all orders and items belong to this seller
427 return true; // allowed!
428 } else {
429 return $allowed; // preserve original check result
430 }
431 }
432
433 /**
434 * Get parent order id
435 *
436 * @param type $document
437 *
438 * @return array
439 */
440 public function get_order_id_parent_id( $document = null ) {
441 if (empty($document) || empty($document->order)) {
442 // PDF Invoice 1.X backwards compatibility
443 global $wpo_wcpdf;
444 /**
445 * @var $order WC_Order
446 */
447 $order = $wpo_wcpdf->export->order;
448 $order_id = $order->get_id();
449 $parent_id = $order->get_parent_id();
450 } else {
451 if ( $document->is_refund( $document->order ) ) {
452 $order_id = $document->get_refund_parent_id( $document->order );
453 } else {
454 $order_id = $document->order_id;
455 }
456 $order = wc_get_order( $order_id );
457 $parent_id = $order->get_parent_id();
458 }
459
460 return compact('order_id','parent_id');
461 }
462
463 }
464
465 $dokan_invoice = Dokan_Invoice::init();
466