Plugin Directory

source: lifeline-donation/trunk/includes/class-lifeline-donation.php @ 2575844

Last change on this file since 2575844 was 2575844, checked in by webinnane, 4 years ago

version 1.2.6

File size: 18.3 KB
Line 
1<?php
2/**
3 * Lifeline Donation
4 *
5 * @package WordPress
6 */
7
8use LifelineDoantion\Classes\GeneralDonation;
9use LifelineDoantion\Classes\SingleDonation;
10use LifelineDonation\Classes\DbUpgradeFromThree;
11use LifelineDonation\Classes\LifelineDonation;
12use WebinaneCommerce\Classes\Customers;
13use WebinaneCommerce\Classes\Orders;
14use WebinaneCommerce\Fields\Select;
15use WebinaneCommerce\Models\OrderItems;
16
17/**
18 * Webinane Donation main class.
19 */
20class Lifeline_Donation {
21
22
23        /**
24         * Init method.
25         *
26         * @return void [description]
27         */
28        public static function init() {
29                add_action( 'get_footer', array( __CLASS__, 'donation_modal' ), 4 );
30                add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ), 1000 );
31
32                add_filter( 'wpcommerce_admin_orders_metabox_title', array( __CLASS__, 'donation_metabox_title' ) );
33                add_filter( 'wpcommerce_settings', array( __CLASS__, 'settings' ) );
34
35                add_filter( 'manage_cause_posts_columns', array( __CLASS__, 'cpt_columns' ) );
36                add_filter( 'manage_project_posts_columns', array( __CLASS__, 'cpt_columns' ) );
37                add_action( 'manage_posts_custom_column', array( __CLASS__, 'custom_columns' ) );
38
39                add_filter( 'wpcommerce_metabox_fields_supported_post_types', array( __CLASS__, 'get_supported_post_types' ) );
40
41                add_shortcode( 'webinane_donation_page', array( __CLASS__, 'shortcode_donation_page' ) );
42
43                add_action( 'wp_ajax_webinane_donation_admin_order_data', array( __CLASS__, 'admin_order_data' ) );
44                self::image_sizes();
45
46                add_filter(
47                        'wpcm_orders_admin_menu_label',
48                        function( $label ) {
49                                return esc_html__( 'Donations', 'lifeline-donation' );
50                        }
51                );
52                add_filter(
53                        'wpcm_order_admin_menu_label',
54                        function( $label ) {
55                                return esc_html__( 'Donation', 'lifeline-donation' );
56                        }
57                );
58                add_filter(
59                        'wpcm_price_label',
60                        function( $label ) {
61                                return esc_html__( 'Donation', 'lifeline-donation' );
62                        }
63                );
64
65
66                add_action( 'wpcm_new_order_process_meta', array( __CLASS__, 'update_donation_meta' ), 10, 2 );
67                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue' ) );
68
69                add_action('wp_ajax_lifelie_donation_load_modal_html', array(__CLASS__, 'donation_modal_component'));
70                add_action('wp_ajax_nopriv_lifelie_donation_load_modal_html', array(__CLASS__, 'donation_modal_component'));
71
72                add_action('wp_body_open', array(__CLASS__, 'preloader'));
73
74                add_filter('webinane_commerce/settings/display_settings', array(__CLASS__, 'display_settings'));
75                // add_filter('wpcm_new_order_customer_email_template', [__CLASS__, 'new_donation_email_template'], 30, 3);
76                // add_filter('wpcm_new_order_owner_email_template', [__CLASS__, 'new_donation_admin_email_template'], 30, 3);
77
78                add_filter('lifeline_donation/settings', function($settings) {
79                        $settings[] = require LIFELINE_DONATION_PATH . 'config/donation_settings.php';
80                        // $settings[] = require LIFELINE_DONATION_PATH . 'config/donation_button.php';
81                        $settings[] = require LIFELINE_DONATION_PATH . 'config/general_popup.php';
82                        $settings[] = require LIFELINE_DONATION_PATH . 'config/posttype_popup.php';
83                        $settings[] = require LIFELINE_DONATION_PATH . 'config/menu_button.php';
84                        $settings[] = require LIFELINE_DONATION_PATH . 'config/archive_setting.php';
85
86                        return $settings;
87                });
88
89        }
90
91
92        /**
93         * [donation_modal_component description]
94         *
95         * @return [type] [description]
96         */
97        public static function donation_modal_component() {
98                $settings = wpcm_get_settings();
99                if ( ( $settings->get( 'donation_Cpost_type' ) == 'donation_popup_box' ) || ( $settings->get( 'donation_general_type' ) == 'donation_popup_box' ) ) {
100
101                        $modal_style = $settings->get( 'donation_popup_style' );
102                        if ( 'style2' == $modal_style ) {
103                                $file = get_theme_file_path( 'lifeline-donation/modal/modal2.php' );
104                        } elseif ( 'style3' == $modal_style ) {
105                                $file = get_theme_file_path( 'lifeline-donation/modal/modal3.php' );
106
107                        } else {
108                                $file = get_theme_file_path( 'lifeline-donation/modal/modal.php' );
109                        }
110
111                        if ( file_exists( $file ) ) {
112                                ob_start();
113                                include $file;
114                                $content = ob_get_clean();
115                                wp_send_json(array('data' => $content));
116                        }
117
118
119                        ob_start();
120
121                        if ( 'style2' == $modal_style ) {
122                                include LIFELINE_DONATION_PATH . 'templates/modal/modal2.php';
123                        } elseif ( 'style3' == $modal_style ) {
124                                include LIFELINE_DONATION_PATH . 'templates/modal/modal3.php';
125                        } else {
126                                include LIFELINE_DONATION_PATH . 'templates/modal/modal.php';
127                        }
128
129                        $content = ob_get_clean();
130
131                        wp_send_json(array('data' => $content));
132                }
133        }
134        /**
135         * Donation modal.
136         *
137         * @return [type] [description]
138         */
139        public static function donation_modal() {
140
141                $settings = wpcm_get_settings();
142                // printr($settings);
143                if ( ( $settings->get( 'donation_Cpost_type' ) == 'donation_popup_box' ) || ( $settings->get( 'donation_general_type' ) == 'donation_popup_box' ) ) {
144
145                        $modal_style = $settings->get( 'donation_popup_style' );
146                        if ( 'style2' == $modal_style ) {
147                                $file = get_theme_file_path( 'lifeline-donation/modal2.php' );
148                        } elseif ( 'style3' == $modal_style ) {
149                                $file = get_theme_file_path( 'lifeline-donation/modal2.php' );
150
151                        } else {
152                                $file = get_theme_file_path( 'lifeline-donation/modal.php' );
153                        }
154
155                        if ( file_exists( $file ) ) {
156                                include $file;
157                                return;
158                        }
159
160                        $js = 'jQuery(document).ready(function($){
161                                $(document).on(\'webinane_donation_modal_opened\', function(app){
162                                        $(\'.donation-wrapper-content.lifeline-donation-modal\').addClass(\'active\');
163                                        if($.fn.perfectScrollbar !== undefined) {
164                                        $(\'.donation-wrapper-content.lifeline-donation-modal\').perfectScrollbar();
165                                        }
166                                });
167
168                                $(document).on(\'webinane_donation_modal_closed\', function(app){
169                                        $(\'.donation-wrapper-content.lifeline-donation-modal\').removeClass(\'active\');
170                                })
171                        });';
172
173                        wp_add_inline_script( 'lifeline-donation-modal', $js );
174
175                        if ( 'style2' == $modal_style ) {
176                                include LIFELINE_DONATION_PATH . 'templates/modal2.php';
177                        } elseif ( 'style3' == $modal_style ) {
178                                include LIFELINE_DONATION_PATH . 'templates/modal3.php';
179                        } else {
180                                include LIFELINE_DONATION_PATH . 'templates/modal.php';
181                        }
182                }
183        }
184
185        /**
186         * Add image sizes.
187         *
188         * @return void [description]
189         */
190        public static function image_sizes() {
191                add_image_size( 'wi_donation_460x490', 460, 490, true );
192                add_image_size( 'wi_donation_350x270', 350, 270, true );
193                add_image_size( 'wi_donation_370x475', 370, 475, true );
194                add_image_size( 'wi_donation_540x380', 540, 380, true );
195        }
196
197        /**
198         * Donation modal enquque scripts and styles.
199         *
200         * @return void [description]
201         */
202        public static function enqueue() {
203
204                wp_enqueue_style( array( 'wpcommerce_main' ) );
205                global $post;
206
207                $settings = wpcm_get_settings();
208
209                $min = (WP_DEBUG) ? '.min' : '';
210                $show_style = array_get($settings, 'donation_enable_plugin_css', true);
211                $show_style = ( 'false' === $show_style || ! $show_style ) ? false : $show_style;
212                $show_style = apply_filters( 'webinane_donation_load_plugin_style', $show_style, $settings );
213
214
215                if ( $show_style ) {
216                        wp_enqueue_style( 'lifeline-donation-syle', LIFELINE_DONATION_URL . 'assets/css/style'.$min.'.css', array(), LifelineDonation::$version );
217                }
218
219
220                wp_register_style( 'webinane-shortcodes', LIFELINE_DONATION_URL . 'assets/css/webinane-shortcodes'.$min.'.css', array(), LifelineDonation::$version );
221                wp_register_style( 'webinane-flat-icon', LIFELINE_DONATION_URL . 'assets/css/flaticon.css', array(), LifelineDonation::$version );
222                wp_register_script( 'knob', LIFELINE_DONATION_URL . 'assets/js/jquery.knob.js', array( 'jquery' ), LifelineDonation::$version, true );
223
224                wp_register_script( 'select2', LIFELINE_DONATION_URL . 'assets/js/select2.min.js', array( 'jquery' ), LifelineDonation::$version, true );
225
226                wp_register_script( 'lifeline-donation-modal', LIFELINE_DONATION_URL . 'assets/js/index.js', array( 'element-ui-en', 'knob', 'select2' ), 1.0, true );
227
228                wp_enqueue_script( array( 'wp-i18n', 'underscore', 'lifeline-donation-modal' ) );
229                wp_set_script_translations( 'lifeline-donation-modal', 'lifeline-donation', LIFELINE_DONATION_PATH . 'languages' );
230        }
231
232        /**
233         * [fontend_donation_form_data description]
234         *
235         * @return void [description]
236         */
237        public static function fontend_donation_form_data() {
238                $_post = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
239
240                $nonce = sanitize_text_field( webinane_set( $_post, 'nonce' ) );
241
242                if ( ! wp_verify_nonce( $nonce, WPCM_GLOBAL_KEY ) ) {
243                        wp_send_json(
244                                array(
245                                        'success' => false,
246                                        'message' => esc_html__(
247                                                'Security verification failed',
248                                                'lifeline-donation'
249                                        ),
250                                ),
251                                403
252                        );
253                }
254
255                if( array_get($_post, 'type') == 'general') {
256                        GeneralDonation::instance()->getData($_post);
257                } else {
258                        SingleDonation::instance()->getData($_post);
259                }
260
261
262        }
263
264        /**
265         * Get currency symbol.
266         *
267         * @return void [description]
268         */
269        public static function currency_symbol() {
270                $_post = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
271                if ( ! sanitize_text_field( webinane_set( $_post, 'currency' ) ) ) {
272                        wp_send_json_error( array( 'message' => esc_html__( 'No currency data provided', 'lifeline-donation' ) ) );
273                }
274
275                $currency = sanitize_text_field( webinane_set( $_post, 'currency' ) );
276                $currencies = webinane_array( webinane_currencies() );
277
278                $symbol = $currencies->filter(
279                        function( $value ) use ( $currency ) {
280                                return $value['iso']['code'] == $currency;
281                        }
282                )->first();
283
284                $symbol = array_get( $symbol, 'units.major.symbol', '$' );
285                wp_send_json_success( $symbol );
286        }
287        /**
288         * Donate now.
289         *
290         * @return void [description]
291         */
292        public static function donate_now() {
293                $_post = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
294
295                $nonce = sanitize_text_field( webinane_set( $_post, 'nonce' ) );
296
297                if ( ! wp_verify_nonce( $nonce, WPCM_GLOBAL_KEY ) ) {
298                        wp_send_json(
299                                array(
300                                        'success' => false,
301                                        'message' => esc_html__(
302                                                'Security verification failed',
303                                                'lifeline-donation'
304                                        ),
305                                ),
306                                403
307                        );
308                }
309
310                $type = array_get($_post, 'type');
311                if($type == 'general') {
312                        GeneralDonation::instance()->donateNow($_post);
313                } else {
314                        SingleDonation::instance()->donateNow($_post);
315                }
316        }
317
318        /**
319         * Metabox title.
320         *
321         * @return [type] [description]
322         */
323        public static function donation_metabox_title() {
324                return esc_html__( 'Donation Setting', 'lifeline-donation' );
325        }
326
327        /**
328         * Load settings.
329         *
330         * @param  [type] $settings [description].
331         *
332         * @return [type]           [description]
333         */
334        public static function settings( $settings ) {
335                $donation_settings = require LIFELINE_DONATION_PATH . 'config/settings.php';
336                // $donation_button = require LIFELINE_DONATION_PATH . 'config/donation_button.php';
337                // $archive_setting = require LIFELINE_DONATION_PATH . 'config/archive_setting.php';
338                /*if ( $donation_settings ) {
339                        $settings = array_merge( (array) $settings, $donation_settings );
340                }
341                if ( $donation_button ) {
342                        $settings = array_merge( (array) $settings, $donation_button );
343                }
344                if ( $archive_setting ) {
345                        $settings = array_merge( (array) $settings, $archive_setting );
346                }*/
347                $settings = array_merge($settings, $donation_settings);
348                return $settings;
349        }
350
351        /**
352         * Display settings.
353         *
354         * @param  [type] $settings [description].
355         *
356         * @return [type]           [description]
357         */
358        public static function display_settings( $settings ) {
359                $_setting = array(
360                        Select::make(
361                                esc_html__( 'Donation History Page', 'lifeline-donation' ),
362                                'donors_donation_listing_page'
363                        )->setOptions(wpcm_posts_data(array('post_type' => 'page', 'posts_per_page' => 100)))
364                        ->setHelp(
365                                esc_html__( 'This page shows a complete donation history for the specific user. The donation history "shortcode should be on this page."', 'lifeline-donation' )
366                        ),
367                );
368                $settings = array_merge( (array) $settings, $_setting );
369
370                foreach($settings as $key => $value ) {
371                        if(in_array($value->attribute, array('checkout_page', 'redirect_to_checkout'))) {
372                                unset( $settings[ $key ] );
373                        }
374                }
375
376                return array_values($settings);
377        }
378
379        /**
380         * Custom post types columns.
381         *
382         * @param  [type] $columns [description].
383         *
384         * @return [type]          [description]
385         */
386        public static function cpt_columns( $columns ) {
387                $mycol = array(
388                        'cb' => '<input type="checkbox" />',
389                        'thumbnail' => '<span><span title="Thumbnail" class="dashicons dashicons-format-image"><span class="screen-reader-text">' . esc_html__( 'Thumbnail', 'lifeline-donation' ) . '</span></span></span>',
390                );
391
392                $columns = array_merge( $mycol, $columns );
393
394                $columns['collected']= esc_html__('Collected', 'lifeline-donation');
395                $columns['target']= esc_html__('Target', 'lifeline-donation');
396
397                return $columns;
398        }
399
400        /**
401         * Custom column
402         *
403         * @param  [type] $column [description].
404         *
405         * @return [type]         [description]
406         */
407        public static function custom_columns( $column ) {
408                global $post, $wpdb;
409
410                if ( ! in_array( $post->post_type, array( 'cause', 'project', 'product' ) ) ) {
411                        return;
412                }
413
414                switch ( $column ) {
415                        case 'thumbnail':
416                                echo get_the_post_thumbnail( $post, array( 50, 50 ) );
417                                break;
418                        case 'collected':
419                                $ids = $post->ID;
420                                $orders = OrderItems::whereHas(
421                                        'order',
422                                        function( $query ) use ( $ids ) {
423                                                return $query->where( 'post_id', $ids )->status( 'completed' );
424                                        }
425                                )->sum( 'price' );
426                                echo '<strong>'.webinane_currency_symbol().'</strong>' . $orders;
427                                break;
428                        case 'target':
429                                $key_set = '';
430                                if($post->post_type == 'cause'){
431                                        $key_set = 'causes_settings';
432                                }
433                                if($post->post_type == 'project'){
434                                        $key_set = 'project_settings';
435                                }
436                                $meta = get_post_meta($post->ID, $key_set, true);
437                                if(webinane_set($meta,'donation')){
438                                        echo '<strong>'.webinane_currency_symbol().'</strong>' . webinane_set($meta,'donation');
439                                }else{
440                                        echo '<strong>'.webinane_currency_symbol().'</strong> 0';
441                                }
442                                break;
443
444                }
445        }
446
447        /**
448         * Hookup enabled post types
449         *
450         * @param  [type] $post_types [description].
451         *
452         * @return [type]             [description]
453         */
454        public static function get_supported_post_types( $post_types ) {
455
456                if ( wpcm_get_settings()->get( 'donation_causes_status' ) ) {
457                        $post_types[] = 'cause';
458                }
459
460                if ( wpcm_get_settings()->get( 'donation_projects_status' ) ) {
461                        $post_types[] = 'project';
462                }
463
464                return $post_types;
465        }
466
467        /**
468         * Change the upgrade status after completion.
469         *
470         * @param  [type] $status [description].
471         *
472         * @return [type]         [description]
473         */
474        public static function db_upgrade_status( $status ) {
475                $query = new WP_Query( array( 'post_type' => 'lif_causes' ) );
476
477                if ( $query->have_posts() ) {
478                        $res = 'YES';
479                } else {
480                        $res = 'NO';
481                }
482                wp_reset_postdata();
483
484                return $res;
485        }
486
487        /**
488         * Finally run db update.
489         *
490         * @return void [description]
491         */
492        public static function run_database_upgrade() {
493                DbUpgradeFromThree::init();
494        }
495
496
497        /**
498         * Shortcode Donation page.
499         *
500         * @param  array  $atts     Array of attributes.
501         * @param  string $content HTML content.
502         * @param  string $tag     shortcode name.
503         * @return string          Returns the output of shortcode.
504         */
505        public static function shortcode_donation_page( $atts, $content = null, $tag ) {
506
507                $id = webinane_set( $atts, 'id' );
508                $style = webinane_set( $atts, 'style' );
509                $title = webinane_set( $atts, 'title' );
510                if ( isset( $_GET['post_id'] ) ) {
511                        $id = sanitize_text_field( webinane_set( $_GET, 'post_id' ) );
512                }
513
514                /*if ( ! absint( $id ) ) {
515                        return '<div class="alert alert-warning">' . esc_html__( 'Donation ID is not provided, please visit homepage', 'lifeline-donation' ) . '</div>';
516                }*/
517
518                wp_enqueue_script( array( 'lifeline-donation-modal' ) );
519
520                $file = '';
521                if ( 'style1' == $style ) {
522                        $file = get_theme_file_path( 'lifeline-donation/donation-templates/donation-page.php' );
523                }
524                if ( 'style2' == $style ) {
525                        $file = get_theme_file_path( 'lifeline-donation/donation-templates/donation-page-2.php' );
526                }
527
528                ob_start();
529
530                if ( file_exists( $file ) ) {
531                        include $file;
532                        return ob_get_clean();
533                }
534                if ( 'style2' == $style ) {
535
536                        include LIFELINE_DONATION_PATH . 'templates/donation-templates/donation-page-2.php';
537                } else if ( 'style3' == $style ) {
538                        include LIFELINE_DONATION_PATH . 'templates/donation-templates/donation-page-3.php';
539                } else {
540                        include LIFELINE_DONATION_PATH . 'templates/donation-templates/donation-page.php';
541                }
542
543                return ob_get_clean();
544        }
545
546        /**
547         * [new_donation_email_template description]
548         *
549         * @param  [type] $template    [description].
550         * @param  [type] $order       [description].
551         * @param  [type] $customer_id [description].
552         *
553         * @return [type]              [description]
554         */
555        public static function new_donation_email_template( $template, $order, $customer_id ) {
556                ob_start();
557                include LIFELINE_DONATION_PATH . 'templates/emails/customer-new-donation.php';
558                return ob_get_clean();
559        }
560        /**
561         * [new_donation_email_template description]
562         *
563         * @param  [type] $template    [description].
564         * @param  [type] $order       [description].
565         * @param  [type] $customer_id [description].
566         *
567         * @return [type]              [description]
568         */
569        public static function new_donation_admin_email_template( $template, $order, $customer_id ) {
570
571                ob_start();
572                include LIFELINE_DONATION_PATH . 'templates/emails/owner-new-donation.php';
573                return ob_get_clean();
574        }
575
576        /**
577         * [update_donation_meta description]
578         *
579         * @param  [type] $data  [description].
580         * @param  [type] $order [description].
581         *
582         * @return void        [description]
583         */
584        public static function update_donation_meta( $data, $order ) {
585
586                $dropdown = array_get( $data, 'post_data.extras.dropdown' );
587                if ( $dropdown ) {
588                        update_post_meta( $order->ID, '_donation_extra_dropdown', $dropdown );
589                }
590        }
591
592        /**
593         * [admin_enqueue description]
594         *
595         * @return void [description]
596         */
597        public static function admin_enqueue() {
598                global $post_type;
599
600                if ( 'orders' === $post_type ) {
601                        wp_enqueue_script( 'lifeline-donation', LIFELINE_DONATION_URL . 'assets/js/admin.js', array( 'vuejs' ), '1.0.3', true );
602                }
603        }
604
605        /**
606         * [admin_order_data description]
607         *
608         * @return void [description]
609         */
610        public static function admin_order_data() {
611                $_post = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );
612                $id = absint( sanitize_text_field( array_get( $_post, 'id' ) ) );
613
614                if ( $id ) {
615                        $meta = get_post_meta( $id, '_donation_extra_dropdown', true );
616
617                        wp_send_json_success( $meta );
618                }
619                wp_send_json_success( '' );
620        }
621
622
623        public static function preloader() {
624                ?>
625                <div class="donation-modal-wraper"></div>
626                <div class="donation-modal-preloader"><div class="my_loader"></div></div>
627                <div class="donation-modal-box"></div>
628                <?php
629        }
630}
631
632Lifeline_Donation::init();
Note: See TracBrowser for help on using the repository browser.